Code refactoring for bpa operator
[icn.git] / cmd / bpa-operator / vendor / k8s.io / apiextensions-apiserver / pkg / apis / apiextensions / v1beta1 / defaults.go
1 /*
2 Copyright 2017 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8     http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package v1beta1
18
19 import (
20         "strings"
21
22         "k8s.io/apimachinery/pkg/runtime"
23 )
24
25 func addDefaultingFuncs(scheme *runtime.Scheme) error {
26         scheme.AddTypeDefaultingFunc(&CustomResourceDefinition{}, func(obj interface{}) { SetDefaults_CustomResourceDefinition(obj.(*CustomResourceDefinition)) })
27         // TODO figure out why I can't seem to get my defaulter generated
28         // return RegisterDefaults(scheme)
29         return nil
30 }
31
32 func SetDefaults_CustomResourceDefinition(obj *CustomResourceDefinition) {
33         SetDefaults_CustomResourceDefinitionSpec(&obj.Spec)
34         if len(obj.Status.StoredVersions) == 0 {
35                 for _, v := range obj.Spec.Versions {
36                         if v.Storage {
37                                 obj.Status.StoredVersions = append(obj.Status.StoredVersions, v.Name)
38                                 break
39                         }
40                 }
41         }
42 }
43
44 func SetDefaults_CustomResourceDefinitionSpec(obj *CustomResourceDefinitionSpec) {
45         if len(obj.Scope) == 0 {
46                 obj.Scope = NamespaceScoped
47         }
48         if len(obj.Names.Singular) == 0 {
49                 obj.Names.Singular = strings.ToLower(obj.Names.Kind)
50         }
51         if len(obj.Names.ListKind) == 0 && len(obj.Names.Kind) > 0 {
52                 obj.Names.ListKind = obj.Names.Kind + "List"
53         }
54         // If there is no list of versions, create on using deprecated Version field.
55         if len(obj.Versions) == 0 && len(obj.Version) != 0 {
56                 obj.Versions = []CustomResourceDefinitionVersion{{
57                         Name:    obj.Version,
58                         Storage: true,
59                         Served:  true,
60                 }}
61         }
62         // For backward compatibility set the version field to the first item in versions list.
63         if len(obj.Version) == 0 && len(obj.Versions) != 0 {
64                 obj.Version = obj.Versions[0].Name
65         }
66         if obj.Conversion == nil {
67                 obj.Conversion = &CustomResourceConversion{
68                         Strategy: NoneConverter,
69                 }
70         }
71 }
72
73 // hasPerVersionColumns returns true if a CRD uses per-version columns.
74 func hasPerVersionColumns(versions []CustomResourceDefinitionVersion) bool {
75         for _, v := range versions {
76                 if len(v.AdditionalPrinterColumns) > 0 {
77                         return true
78                 }
79         }
80         return false
81 }