2 Copyright 2017 The Kubernetes Authors.
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
22 "k8s.io/apimachinery/pkg/runtime"
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)
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 {
37 obj.Status.StoredVersions = append(obj.Status.StoredVersions, v.Name)
44 func SetDefaults_CustomResourceDefinitionSpec(obj *CustomResourceDefinitionSpec) {
45 if len(obj.Scope) == 0 {
46 obj.Scope = NamespaceScoped
48 if len(obj.Names.Singular) == 0 {
49 obj.Names.Singular = strings.ToLower(obj.Names.Kind)
51 if len(obj.Names.ListKind) == 0 && len(obj.Names.Kind) > 0 {
52 obj.Names.ListKind = obj.Names.Kind + "List"
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{{
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
66 if obj.Conversion == nil {
67 obj.Conversion = &CustomResourceConversion{
68 Strategy: NoneConverter,
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 {