Documentation ¶
Overview ¶
Package crd contains utilities for generating CustomResourceDefinitions and their corresponding OpenAPI validation schemata.
Markers ¶
Markers live under the markers subpackage. Two types of markers exist: those that modify schema generation (for validation), and those that modify the rest of the CRD. See the subpackage for more information and all supported markers.
Collecting Types and Generating CRDs ¶
The Parser is the entrypoint for collecting the information required to generate CRDs. Like loader and collector, its methods are idemptotent, not doing extra work if called multiple times.
Parser's method start with Need. Calling NeedXYZ indicates that XYZ should be made present in the eqivalent field in the Parser, where it can then be loaded from. Each Need method will in turn call Need on anything it needs.
In general, root packages should first be loaded into the Parser with NeedPackage. Then, CRDs can be generated with NeedCRDFor.
Errors are generally attached directly to the relevant Package with AddError.
Known Packages ¶
There are a few types from Kubernetes that have special meaning, but don't have validation markers attached. Those specific types have overrides listed in KnownPackages that can be added as overrides to any parser.
Flattening ¶
Once schemata are generated, they can be used directly by external tooling (like JSONSchema validators), but must first be "flattened" to not contain references before use in a CRD (Kubernetes doesn't allow references in the CRD's validation schema).
The Flattener built in to the Parser takes care of flattening out references when requesting the CRDs, but can be invoked manually. It will not modify the input schemata.
Flattened schemata may further be passed to FlattenEmbedded to remove the use of AllOf (which is used to describe embedded struct fields when references are in use). This done automatically when fetching CRDs.
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Index ¶
- Variables
- func AddKnownTypes(parser *Parser)
- func EditSchema(schema *apiext.JSONSchemaProps, visitor SchemaVisitor)
- func FlattenEmbedded(schema *apiext.JSONSchemaProps) *apiext.JSONSchemaProps
- type Flattener
- type Generator
- type PackageOverride
- type Parser
- type SchemaMarker
- type SchemaVisitor
- type SpecMarker
- type TypeIdent
Constants ¶
This section is empty.
Variables ¶
var KnownPackages = map[string]PackageOverride{ "k8s.io/apimachinery/pkg/apis/meta/v1": func(p *Parser, pkg *loader.Package) { p.Schemata[TypeIdent{Name: "Time", Package: pkg}] = apiext.JSONSchemaProps{ Type: "string", Format: "date-time", } p.Schemata[TypeIdent{Name: "Duration", Package: pkg}] = apiext.JSONSchemaProps{ Type: "string", } p.AddPackage(pkg) }, "k8s.io/apimachinery/pkg/api/resource": func(p *Parser, pkg *loader.Package) { p.Schemata[TypeIdent{Name: "Quantity", Package: pkg}] = apiext.JSONSchemaProps{ Type: "string", } }, "k8s.io/apimachinery/pkg/runtime": func(p *Parser, pkg *loader.Package) { p.Schemata[TypeIdent{Name: "RawExtension", Package: pkg}] = apiext.JSONSchemaProps{ Type: "object", } p.AddPackage(pkg) }, "k8s.io/apimachinery/pkg/util/intstr": func(p *Parser, pkg *loader.Package) { p.Schemata[TypeIdent{Name: "IntOrString", Package: pkg}] = apiext.JSONSchemaProps{ AnyOf: []apiext.JSONSchemaProps{ {Type: "string"}, {Type: "integer"}, }, } }, }
KnownPackages overrides types in some comment packages that have custom validation but don't have validation markers on them (since they're from core Kubernetes).
Functions ¶
func AddKnownTypes ¶
func AddKnownTypes(parser *Parser)
AddKnownTypes registers the packages overrides in KnownPackages with the given parser.
func EditSchema ¶
func EditSchema(schema *apiext.JSONSchemaProps, visitor SchemaVisitor)
EditSchema walks the given schema using the given visitor. Actual pointers to each schema node are passed to the visitor, so any changes made by the visitor will be reflected to the passed-in schema.
func FlattenEmbedded ¶
func FlattenEmbedded(schema *apiext.JSONSchemaProps) *apiext.JSONSchemaProps
FlattenEmbedded flattens embedded fields (represented via AllOf) which have already had their references resolved into simple properties in the containing schema.
Types ¶
type Flattener ¶
type Flattener struct { // Parser is used to lookup package and type details, and parse in new packages. Parser *Parser // contains filtered or unexported fields }
Flattener knows how to take a root type, and flatten all references in it into a single, flat type. Flattened types are cached, so it's relatively cheap to make repeated calls with the same type.
func (*Flattener) FlattenSchema ¶
func (f *Flattener) FlattenSchema(baseSchema apiext.JSONSchemaProps, currentPackage *loader.Package) *apiext.JSONSchemaProps
FlattenSchema flattens the given schema, removing any references. It deep-copies the schema first, so the input schema won't be affected.
func (*Flattener) FlattenType ¶
func (f *Flattener) FlattenType(typ TypeIdent) *apiext.JSONSchemaProps
FlattenType flattens the given pre-loaded type, removing any references from it. It deep-copies the schema first, so it won't affect the parser's version of the schema.
type Generator ¶
type Generator struct { // TrivialVersions indicates that we should produce a legacy // "trival-version" CRD compatible with older (pre 1.13) Kubernetes API // servers. The storage version's schema will be used as the CRD's schema. TrivialVersions bool `marker:",optional"` }
Generator is a genall.Generator that generates CRDs.
type PackageOverride ¶
PackageOverride overrides the loading of some package (potentially setting custom schemata, etc). It must call AddPackage if it wants to continue with the default loading behavior.
type Parser ¶
type Parser struct { Collector *markers.Collector // Types contains the known TypeInfo for this parser. Types map[TypeIdent]*markers.TypeInfo // Schemata contains the known OpenAPI JSONSchemata for this parser. Schemata map[TypeIdent]apiext.JSONSchemaProps // GroupVersions contains the known group-versions of each package in this parser. GroupVersions map[*loader.Package]schema.GroupVersion // CustomResourceDefinitions contains the known CustomResourceDefinitions for types in this parser. CustomResourceDefinitions map[schema.GroupKind]apiext.CustomResourceDefinition // PackageOverrides indicates that the loading of any package with // the given path should be handled by the given overrider. PackageOverrides map[string]PackageOverride // checker stores persistent partial type-checking/reference-traversal information. Checker *loader.TypeChecker // contains filtered or unexported fields }
Parser knows how to parse out CRD information and generate OpenAPI schemata from some collection of types and markers. Most methods on Parser cache their results automatically, and thus may be called any number of times.
func (*Parser) AddPackage ¶
AddPackage indicates that types and type-checking information is needed for the the given package, *ignoring* overrides. Generally, consumers should call NeedPackage, while PackageOverrides should call AddPackage to continue with the normal loading procedure.
func (*Parser) LookupType ¶
LookupType fetches type info from Types.
func (*Parser) NeedCRDFor ¶
NeedCRDFor requests the full CRD for the given group-kind. It requires that the packages containing the Go structs for that CRD have already been loaded with NeedPackage.
func (*Parser) NeedPackage ¶
NeedPackage indicates that types and type-checking information is needed for the given package.
func (*Parser) NeedSchemaFor ¶
NeedSchemaFor indicates that a schema should be generated for the given type.
type SchemaMarker ¶
type SchemaMarker interface { // ApplyToSchema is called after the rest of the schema for a given type // or field is generated, to modify the schema appropriately. ApplyToSchema(*v1beta1.JSONSchemaProps) error }
SchemaMarker is any marker that needs to modify the schema of the underlying type or field.
type SchemaVisitor ¶
type SchemaVisitor interface { // Visit is called for each schema node. If it returns a visitor, // the visitor will be called on each direct child node, and then // this visitor will be called again with `nil` to indicate that // all children have been visited. If a nil visitor is returned, // children are not visited. Visit(schema *apiext.JSONSchemaProps) SchemaVisitor }
SchemaVisitor walks the nodes of a schema.
type SpecMarker ¶
type SpecMarker interface { // ApplyToCRD applies this marker to the given CRD, in the given version // within that CRD. It's called after everything else in the CRD is populated. ApplyToCRD(crd *apiext.CustomResourceDefinitionSpec, version string) error }
SpecMarker is a marker that knows how to apply itself to a particular version in a CRD.