github.com/waynz0r/controller-tools@v0.4.1-0.20200916220028-16254aeef2d7/pkg/crd/known_types.go (about)

     1  /*
     2  Copyright 2019 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  package crd
    17  
    18  import (
    19  	apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
    20  
    21  	"sigs.k8s.io/controller-tools/pkg/loader"
    22  )
    23  
    24  // KnownPackages overrides types in some comment packages that have custom validation
    25  // but don't have validation markers on them (since they're from core Kubernetes).
    26  var KnownPackages = map[string]PackageOverride{
    27  	"k8s.io/api/core/v1": func(p *Parser, pkg *loader.Package) {
    28  		// Explicit defaulting for the corev1.Protocol type in lieu of https://github.com/kubernetes/enhancements/pull/1928
    29  		p.Schemata[TypeIdent{Name: "Protocol", Package: pkg}] = apiext.JSONSchemaProps{
    30  			Type:    "string",
    31  			Default: &apiext.JSON{Raw: []byte(`"TCP"`)},
    32  		}
    33  		p.AddPackage(pkg)
    34  	},
    35  
    36  	"k8s.io/apimachinery/pkg/apis/meta/v1": func(p *Parser, pkg *loader.Package) {
    37  		// ObjectMeta is managed by the Kubernetes API server, so no need to
    38  		// generate validation for it.
    39  		p.Schemata[TypeIdent{Name: "ObjectMeta", Package: pkg}] = apiext.JSONSchemaProps{
    40  			Type: "object",
    41  		}
    42  		p.Schemata[TypeIdent{Name: "Time", Package: pkg}] = apiext.JSONSchemaProps{
    43  			Type:   "string",
    44  			Format: "date-time",
    45  		}
    46  		p.Schemata[TypeIdent{Name: "MicroTime", Package: pkg}] = apiext.JSONSchemaProps{
    47  			Type:   "string",
    48  			Format: "date-time",
    49  		}
    50  		p.Schemata[TypeIdent{Name: "Duration", Package: pkg}] = apiext.JSONSchemaProps{
    51  			// TODO(directxman12): regexp validation for this (or get kube to support it as a format value)
    52  			Type: "string",
    53  		}
    54  		p.Schemata[TypeIdent{Name: "Fields", Package: pkg}] = apiext.JSONSchemaProps{
    55  			// this is a recursive structure that can't be flattened or, for that matter, properly generated.
    56  			// so just treat it as an arbitrary map
    57  			Type:                 "object",
    58  			AdditionalProperties: &apiext.JSONSchemaPropsOrBool{Allows: true},
    59  		}
    60  		p.AddPackage(pkg) // get the rest of the types
    61  	},
    62  
    63  	"k8s.io/apimachinery/pkg/api/resource": func(p *Parser, pkg *loader.Package) {
    64  		p.Schemata[TypeIdent{Name: "Quantity", Package: pkg}] = apiext.JSONSchemaProps{
    65  			// TODO(directxman12): regexp validation for this (or get kube to support it as a format value)
    66  			XIntOrString: true,
    67  			AnyOf: []apiext.JSONSchemaProps{
    68  				{Type: "integer"},
    69  				{Type: "string"},
    70  			},
    71  			Pattern: "^(\\+|-)?(([0-9]+(\\.[0-9]*)?)|(\\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\\+|-)?(([0-9]+(\\.[0-9]*)?)|(\\.[0-9]+))))?$",
    72  		}
    73  		// No point in calling AddPackage, this is the sole inhabitant
    74  	},
    75  
    76  	"k8s.io/apimachinery/pkg/runtime": func(p *Parser, pkg *loader.Package) {
    77  		p.Schemata[TypeIdent{Name: "RawExtension", Package: pkg}] = apiext.JSONSchemaProps{
    78  			// TODO(directxman12): regexp validation for this (or get kube to support it as a format value)
    79  			Type: "object",
    80  		}
    81  		p.AddPackage(pkg) // get the rest of the types
    82  	},
    83  
    84  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured": func(p *Parser, pkg *loader.Package) {
    85  		p.Schemata[TypeIdent{Name: "Unstructured", Package: pkg}] = apiext.JSONSchemaProps{
    86  			Type: "object",
    87  		}
    88  		p.AddPackage(pkg) // get the rest of the types
    89  	},
    90  
    91  	"k8s.io/apimachinery/pkg/util/intstr": func(p *Parser, pkg *loader.Package) {
    92  		p.Schemata[TypeIdent{Name: "IntOrString", Package: pkg}] = apiext.JSONSchemaProps{
    93  			XIntOrString: true,
    94  			AnyOf: []apiext.JSONSchemaProps{
    95  				{Type: "integer"},
    96  				{Type: "string"},
    97  			},
    98  		}
    99  		// No point in calling AddPackage, this is the sole inhabitant
   100  	},
   101  
   102  	"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1": func(p *Parser, pkg *loader.Package) {
   103  		p.Schemata[TypeIdent{Name: "JSON", Package: pkg}] = apiext.JSONSchemaProps{
   104  			XPreserveUnknownFields: boolPtr(true),
   105  		}
   106  		p.AddPackage(pkg) // get the rest of the types
   107  	},
   108  	"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1": func(p *Parser, pkg *loader.Package) {
   109  		p.Schemata[TypeIdent{Name: "JSON", Package: pkg}] = apiext.JSONSchemaProps{
   110  			XPreserveUnknownFields: boolPtr(true),
   111  		}
   112  		p.AddPackage(pkg) // get the rest of the types
   113  	},
   114  }
   115  
   116  func boolPtr(b bool) *bool {
   117  	return &b
   118  }
   119  
   120  // AddKnownTypes registers the packages overrides in KnownPackages with the given parser.
   121  func AddKnownTypes(parser *Parser) {
   122  	// ensure everything is there before adding to PackageOverrides
   123  	// TODO(directxman12): this is a bit of a hack, maybe just use constructors?
   124  	parser.init()
   125  	for pkgName, override := range KnownPackages {
   126  		parser.PackageOverrides[pkgName] = override
   127  	}
   128  }