2
vendor/sigs.k8s.io/controller-tools/pkg/crd/conv.go
generated
vendored
2
vendor/sigs.k8s.io/controller-tools/pkg/crd/conv.go
generated
vendored
@@ -35,7 +35,7 @@ func AsVersion(original apiext.CustomResourceDefinition, gv schema.GroupVersion)
|
||||
// questionable decision.
|
||||
intVer, err := conversionScheme.ConvertToVersion(&original, apiextinternal.SchemeGroupVersion)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to convert to internal CRD version: %v", err)
|
||||
return nil, fmt.Errorf("unable to convert to internal CRD version: %w", err)
|
||||
}
|
||||
|
||||
return conversionScheme.ConvertToVersion(intVer, gv)
|
||||
|
||||
27
vendor/sigs.k8s.io/controller-tools/pkg/crd/gen.go
generated
vendored
27
vendor/sigs.k8s.io/controller-tools/pkg/crd/gen.go
generated
vendored
@@ -31,6 +31,9 @@ import (
|
||||
"sigs.k8s.io/controller-tools/pkg/version"
|
||||
)
|
||||
|
||||
// The default CustomResourceDefinition version to generate.
|
||||
const defaultVersion = "v1"
|
||||
|
||||
// +controllertools:marker:generateHelp
|
||||
|
||||
// Generator generates CustomResourceDefinition objects.
|
||||
@@ -53,6 +56,16 @@ type Generator struct {
|
||||
// It's required to be false for v1 CRDs.
|
||||
PreserveUnknownFields *bool `marker:",optional"`
|
||||
|
||||
// AllowDangerousTypes allows types which are usually omitted from CRD generation
|
||||
// because they are not recommended.
|
||||
//
|
||||
// Currently the following additional types are allowed when this is true:
|
||||
// float32
|
||||
// float64
|
||||
//
|
||||
// Left unspecified, the default is false
|
||||
AllowDangerousTypes *bool `marker:",optional"`
|
||||
|
||||
// MaxDescLen specifies the maximum description length for fields in CRD's OpenAPI schema.
|
||||
//
|
||||
// 0 indicates drop the description for all fields completely.
|
||||
@@ -61,7 +74,7 @@ type Generator struct {
|
||||
MaxDescLen *int `marker:",optional"`
|
||||
|
||||
// CRDVersions specifies the target API versions of the CRD type itself to
|
||||
// generate. Defaults to v1beta1.
|
||||
// generate. Defaults to v1.
|
||||
//
|
||||
// The first version listed will be assumed to be the "default" version and
|
||||
// will not get a version suffix in the output filename.
|
||||
@@ -78,6 +91,8 @@ func (g Generator) Generate(ctx *genall.GenerationContext) error {
|
||||
parser := &Parser{
|
||||
Collector: ctx.Collector,
|
||||
Checker: ctx.Checker,
|
||||
// Perform defaulting here to avoid ambiguity later
|
||||
AllowDangerousTypes: g.AllowDangerousTypes != nil && *g.AllowDangerousTypes == true,
|
||||
}
|
||||
|
||||
AddKnownTypes(parser)
|
||||
@@ -101,10 +116,10 @@ func (g Generator) Generate(ctx *genall.GenerationContext) error {
|
||||
crdVersions := g.CRDVersions
|
||||
|
||||
if len(crdVersions) == 0 {
|
||||
crdVersions = []string{"v1beta1"}
|
||||
crdVersions = []string{defaultVersion}
|
||||
}
|
||||
|
||||
for _, groupKind := range kubeKinds {
|
||||
for groupKind := range kubeKinds {
|
||||
parser.NeedCRDFor(groupKind, g.MaxDescLen)
|
||||
crdRaw := parser.CustomResourceDefinitions[groupKind]
|
||||
addAttribution(&crdRaw)
|
||||
@@ -206,9 +221,9 @@ func FindMetav1(roots []*loader.Package) *loader.Package {
|
||||
// FindKubeKinds locates all types that contain TypeMeta and ObjectMeta
|
||||
// (and thus may be a Kubernetes object), and returns the corresponding
|
||||
// group-kinds.
|
||||
func FindKubeKinds(parser *Parser, metav1Pkg *loader.Package) []schema.GroupKind {
|
||||
func FindKubeKinds(parser *Parser, metav1Pkg *loader.Package) map[schema.GroupKind]struct{} {
|
||||
// TODO(directxman12): technically, we should be finding metav1 per-package
|
||||
var kubeKinds []schema.GroupKind
|
||||
kubeKinds := map[schema.GroupKind]struct{}{}
|
||||
for typeIdent, info := range parser.Types {
|
||||
hasObjectMeta := false
|
||||
hasTypeMeta := false
|
||||
@@ -257,7 +272,7 @@ func FindKubeKinds(parser *Parser, metav1Pkg *loader.Package) []schema.GroupKind
|
||||
Group: parser.GroupVersions[pkg].Group,
|
||||
Kind: typeIdent.Name,
|
||||
}
|
||||
kubeKinds = append(kubeKinds, groupKind)
|
||||
kubeKinds[groupKind] = struct{}{}
|
||||
}
|
||||
|
||||
return kubeKinds
|
||||
|
||||
7
vendor/sigs.k8s.io/controller-tools/pkg/crd/known_types.go
generated
vendored
7
vendor/sigs.k8s.io/controller-tools/pkg/crd/known_types.go
generated
vendored
@@ -55,7 +55,12 @@ var KnownPackages = map[string]PackageOverride{
|
||||
"k8s.io/apimachinery/pkg/api/resource": func(p *Parser, pkg *loader.Package) {
|
||||
p.Schemata[TypeIdent{Name: "Quantity", Package: pkg}] = apiext.JSONSchemaProps{
|
||||
// TODO(directxman12): regexp validation for this (or get kube to support it as a format value)
|
||||
Type: "string",
|
||||
XIntOrString: true,
|
||||
AnyOf: []apiext.JSONSchemaProps{
|
||||
{Type: "integer"},
|
||||
{Type: "string"},
|
||||
},
|
||||
Pattern: "^(\\+|-)?(([0-9]+(\\.[0-9]*)?)|(\\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\\+|-)?(([0-9]+(\\.[0-9]*)?)|(\\.[0-9]+))))?$",
|
||||
}
|
||||
// No point in calling AddPackage, this is the sole inhabitant
|
||||
},
|
||||
|
||||
25
vendor/sigs.k8s.io/controller-tools/pkg/crd/markers/crd.go
generated
vendored
25
vendor/sigs.k8s.io/controller-tools/pkg/crd/markers/crd.go
generated
vendored
@@ -45,6 +45,9 @@ var CRDMarkers = []*definitionWithHelp{
|
||||
|
||||
must(markers.MakeDefinition("kubebuilder:skipversion", markers.DescribesType, SkipVersion{})).
|
||||
WithHelp(SkipVersion{}.Help()),
|
||||
|
||||
must(markers.MakeDefinition("kubebuilder:unservedversion", markers.DescribesType, UnservedVersion{})).
|
||||
WithHelp(UnservedVersion{}.Help()),
|
||||
}
|
||||
|
||||
// TODO: categories and singular used to be annotations types
|
||||
@@ -277,6 +280,9 @@ func (s Resource) ApplyToCRD(crd *apiext.CustomResourceDefinitionSpec, version s
|
||||
if s.Path != "" {
|
||||
crd.Names.Plural = s.Path
|
||||
}
|
||||
if s.Singular != "" {
|
||||
crd.Names.Singular = s.Singular
|
||||
}
|
||||
crd.Names.ShortNames = s.ShortName
|
||||
crd.Names.Categories = s.Categories
|
||||
|
||||
@@ -290,4 +296,23 @@ func (s Resource) ApplyToCRD(crd *apiext.CustomResourceDefinitionSpec, version s
|
||||
return nil
|
||||
}
|
||||
|
||||
// +controllertools:marker:generateHelp:category=CRD
|
||||
|
||||
// UnservedVersion does not serve this version.
|
||||
//
|
||||
// This is useful if you need to drop support for a version in favor of a newer version.
|
||||
type UnservedVersion struct{}
|
||||
|
||||
func (s UnservedVersion) ApplyToCRD(crd *apiext.CustomResourceDefinitionSpec, version string) error {
|
||||
for i := range crd.Versions {
|
||||
ver := &crd.Versions[i]
|
||||
if ver.Name != version {
|
||||
continue
|
||||
}
|
||||
ver.Served = false
|
||||
break
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// NB(directxman12): singular was historically distinct, so we keep it here for backwards compat
|
||||
|
||||
155
vendor/sigs.k8s.io/controller-tools/pkg/crd/markers/topology.go
generated
vendored
Normal file
155
vendor/sigs.k8s.io/controller-tools/pkg/crd/markers/topology.go
generated
vendored
Normal file
@@ -0,0 +1,155 @@
|
||||
/*
|
||||
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.
|
||||
*/
|
||||
|
||||
package markers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
|
||||
"sigs.k8s.io/controller-tools/pkg/markers"
|
||||
)
|
||||
|
||||
// TopologyMarkers specify topology markers (i.e. markers that describe if a
|
||||
// list behaves as an associative-list or a set, if a map is atomic or not).
|
||||
var TopologyMarkers = []*definitionWithHelp{
|
||||
must(markers.MakeDefinition("listMapKey", markers.DescribesField, ListMapKey(""))).
|
||||
WithHelp(ListMapKey("").Help()),
|
||||
must(markers.MakeDefinition("listType", markers.DescribesField, ListType(""))).
|
||||
WithHelp(ListType("").Help()),
|
||||
must(markers.MakeDefinition("mapType", markers.DescribesField, MapType(""))).
|
||||
WithHelp(MapType("").Help()),
|
||||
must(markers.MakeDefinition("structType", markers.DescribesField, StructType(""))).
|
||||
WithHelp(StructType("").Help()),
|
||||
}
|
||||
|
||||
func init() {
|
||||
AllDefinitions = append(AllDefinitions, TopologyMarkers...)
|
||||
}
|
||||
|
||||
// +controllertools:marker:generateHelp:category="CRD processing"
|
||||
|
||||
// ListType specifies the type of data-structure that the list
|
||||
// represents (map, set, atomic).
|
||||
//
|
||||
// Possible data-structure types of a list are:
|
||||
//
|
||||
// - "map": it needs to have a key field, which will be used to build an
|
||||
// associative list. A typical example is a the pod container list,
|
||||
// which is indexed by the container name.
|
||||
//
|
||||
// - "set": Fields need to be "scalar", and there can be only one
|
||||
// occurrence of each.
|
||||
//
|
||||
// - "atomic": All the fields in the list are treated as a single value,
|
||||
// are typically manipulated together by the same actor.
|
||||
type ListType string
|
||||
|
||||
// +controllertools:marker:generateHelp:category="CRD processing"
|
||||
|
||||
// ListMapKey specifies the keys to map listTypes.
|
||||
//
|
||||
// It indicates the index of a map list. They can be repeated if multiple keys
|
||||
// must be used. It can only be used when ListType is set to map, and the keys
|
||||
// should be scalar types.
|
||||
type ListMapKey string
|
||||
|
||||
// +controllertools:marker:generateHelp:category="CRD processing"
|
||||
|
||||
// MapType specifies the level of atomicity of the map;
|
||||
// i.e. whether each item in the map is independent of the others,
|
||||
// or all fields are treated as a single unit.
|
||||
//
|
||||
// Possible values:
|
||||
//
|
||||
// - "granular": items in the map are independent of each other,
|
||||
// and can be manipulated by different actors.
|
||||
// This is the default behavior.
|
||||
//
|
||||
// - "atomic": all fields are treated as one unit.
|
||||
// Any changes have to replace the entire map.
|
||||
type MapType string
|
||||
|
||||
// +controllertools:marker:generateHelp:category="CRD processing"
|
||||
|
||||
// StructType specifies the level of atomicity of the struct;
|
||||
// i.e. whether each field in the struct is independent of the others,
|
||||
// or all fields are treated as a single unit.
|
||||
//
|
||||
// Possible values:
|
||||
//
|
||||
// - "granular": fields in the struct are independent of each other,
|
||||
// and can be manipulated by different actors.
|
||||
// This is the default behavior.
|
||||
//
|
||||
// - "atomic": all fields are treated as one unit.
|
||||
// Any changes have to replace the entire struct.
|
||||
type StructType string
|
||||
|
||||
func (l ListType) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
|
||||
if schema.Type != "array" {
|
||||
return fmt.Errorf("must apply listType to an array")
|
||||
}
|
||||
if l != "map" && l != "atomic" && l != "set" {
|
||||
return fmt.Errorf(`ListType must be either "map", "set" or "atomic"`)
|
||||
}
|
||||
p := string(l)
|
||||
schema.XListType = &p
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l ListType) ApplyFirst() {}
|
||||
|
||||
func (l ListMapKey) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
|
||||
if schema.Type != "array" {
|
||||
return fmt.Errorf("must apply listMapKey to an array")
|
||||
}
|
||||
if schema.XListType == nil || *schema.XListType != "map" {
|
||||
return fmt.Errorf("must apply listMapKey to an associative-list")
|
||||
}
|
||||
schema.XListMapKeys = append(schema.XListMapKeys, string(l))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m MapType) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
|
||||
if schema.Type != "object" {
|
||||
return fmt.Errorf("must apply mapType to an object")
|
||||
}
|
||||
|
||||
if m != "atomic" && m != "granular" {
|
||||
return fmt.Errorf(`MapType must be either "granular" or "atomic"`)
|
||||
}
|
||||
|
||||
p := string(m)
|
||||
schema.XMapType = &p
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s StructType) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
|
||||
if schema.Type != "object" && schema.Type != "" {
|
||||
return fmt.Errorf("must apply structType to an object; either explicitly set or defaulted through an empty schema type")
|
||||
}
|
||||
|
||||
if s != "atomic" && s != "granular" {
|
||||
return fmt.Errorf(`StructType must be either "granular" or "atomic"`)
|
||||
}
|
||||
|
||||
p := string(s)
|
||||
schema.XMapType = &p
|
||||
|
||||
return nil
|
||||
}
|
||||
59
vendor/sigs.k8s.io/controller-tools/pkg/crd/markers/zz_generated.markerhelp.go
generated
vendored
59
vendor/sigs.k8s.io/controller-tools/pkg/crd/markers/zz_generated.markerhelp.go
generated
vendored
@@ -84,6 +84,39 @@ func (Format) Help() *markers.DefinitionHelp {
|
||||
}
|
||||
}
|
||||
|
||||
func (ListMapKey) Help() *markers.DefinitionHelp {
|
||||
return &markers.DefinitionHelp{
|
||||
Category: "CRD processing",
|
||||
DetailedHelp: markers.DetailedHelp{
|
||||
Summary: "specifies the keys to map listTypes. ",
|
||||
Details: "It indicates the index of a map list. They can be repeated if multiple keys must be used. It can only be used when ListType is set to map, and the keys should be scalar types.",
|
||||
},
|
||||
FieldHelp: map[string]markers.DetailedHelp{},
|
||||
}
|
||||
}
|
||||
|
||||
func (ListType) Help() *markers.DefinitionHelp {
|
||||
return &markers.DefinitionHelp{
|
||||
Category: "CRD processing",
|
||||
DetailedHelp: markers.DetailedHelp{
|
||||
Summary: "specifies the type of data-structure that the list represents (map, set, atomic). ",
|
||||
Details: "Possible data-structure types of a list are: \n - \"map\": it needs to have a key field, which will be used to build an associative list. A typical example is a the pod container list, which is indexed by the container name. \n - \"set\": Fields need to be \"scalar\", and there can be only one occurrence of each. \n - \"atomic\": All the fields in the list are treated as a single value, are typically manipulated together by the same actor.",
|
||||
},
|
||||
FieldHelp: map[string]markers.DetailedHelp{},
|
||||
}
|
||||
}
|
||||
|
||||
func (MapType) Help() *markers.DefinitionHelp {
|
||||
return &markers.DefinitionHelp{
|
||||
Category: "CRD processing",
|
||||
DetailedHelp: markers.DetailedHelp{
|
||||
Summary: "specifies the level of atomicity of the map; i.e. whether each item in the map is independent of the others, or all fields are treated as a single unit. ",
|
||||
Details: "Possible values: \n - \"granular\": items in the map are independent of each other, and can be manipulated by different actors. This is the default behavior. \n - \"atomic\": all fields are treated as one unit. Any changes have to replace the entire map.",
|
||||
},
|
||||
FieldHelp: map[string]markers.DetailedHelp{},
|
||||
}
|
||||
}
|
||||
|
||||
func (MaxItems) Help() *markers.DefinitionHelp {
|
||||
return &markers.DefinitionHelp{
|
||||
Category: "CRD validation",
|
||||
@@ -244,8 +277,8 @@ func (Resource) Help() *markers.DefinitionHelp {
|
||||
Details: "The singular form is otherwise defaulted off the plural (path).",
|
||||
},
|
||||
"Scope": markers.DetailedHelp{
|
||||
Summary: "overrides the scope of the CRD (cluster vs namespaced). ",
|
||||
Details: "Scope defaults to \"namespaced\". Cluster-scoped (\"cluster\") resources don't exist in namespaces.",
|
||||
Summary: "overrides the scope of the CRD (Cluster vs Namespaced). ",
|
||||
Details: "Scope defaults to \"Namespaced\". Cluster-scoped (\"Cluster\") resources don't exist in namespaces.",
|
||||
},
|
||||
},
|
||||
}
|
||||
@@ -273,6 +306,17 @@ func (StorageVersion) Help() *markers.DefinitionHelp {
|
||||
}
|
||||
}
|
||||
|
||||
func (StructType) Help() *markers.DefinitionHelp {
|
||||
return &markers.DefinitionHelp{
|
||||
Category: "CRD processing",
|
||||
DetailedHelp: markers.DetailedHelp{
|
||||
Summary: "specifies the level of atomicity of the struct; i.e. whether each field in the struct is independent of the others, or all fields are treated as a single unit. ",
|
||||
Details: "Possible values: \n - \"granular\": fields in the struct are independent of each other, and can be manipulated by different actors. This is the default behavior. \n - \"atomic\": all fields are treated as one unit. Any changes have to replace the entire struct.",
|
||||
},
|
||||
FieldHelp: map[string]markers.DetailedHelp{},
|
||||
}
|
||||
}
|
||||
|
||||
func (SubresourceScale) Help() *markers.DefinitionHelp {
|
||||
return &markers.DefinitionHelp{
|
||||
Category: "CRD",
|
||||
@@ -330,6 +374,17 @@ func (UniqueItems) Help() *markers.DefinitionHelp {
|
||||
}
|
||||
}
|
||||
|
||||
func (UnservedVersion) Help() *markers.DefinitionHelp {
|
||||
return &markers.DefinitionHelp{
|
||||
Category: "CRD",
|
||||
DetailedHelp: markers.DetailedHelp{
|
||||
Summary: "does not serve this version. ",
|
||||
Details: "This is useful if you need to drop support for a version in favor of a newer version.",
|
||||
},
|
||||
FieldHelp: map[string]markers.DetailedHelp{},
|
||||
}
|
||||
}
|
||||
|
||||
func (XEmbeddedResource) Help() *markers.DefinitionHelp {
|
||||
return &markers.DefinitionHelp{
|
||||
Category: "CRD validation",
|
||||
|
||||
16
vendor/sigs.k8s.io/controller-tools/pkg/crd/parser.go
generated
vendored
16
vendor/sigs.k8s.io/controller-tools/pkg/crd/parser.go
generated
vendored
@@ -73,6 +73,20 @@ type Parser struct {
|
||||
packages map[*loader.Package]struct{}
|
||||
|
||||
flattener *Flattener
|
||||
|
||||
// AllowDangerousTypes controls the handling of non-recommended types such as float. If
|
||||
// false (the default), these types are not supported.
|
||||
// There is a continuum here:
|
||||
// 1. Types that are always supported.
|
||||
// 2. Types that are allowed by default, but not recommended (warning emitted when they are encountered as per PR #443).
|
||||
// Possibly they are allowed by default for historical reasons and may even be "on their way out" at some point in the future.
|
||||
// 3. Types that are not allowed by default, not recommended, but there are some legitimate reasons to need them in certain corner cases.
|
||||
// Possibly these types should also emit a warning as per PR #443 even when they are "switched on" (an integration point between
|
||||
// this feature and #443 if desired). This is the category that this flag deals with.
|
||||
// 4. Types that are not allowed and will not be allowed, possibly because it just "doesn't make sense" or possibly
|
||||
// because the implementation is too difficult/clunky to promote them to category 3.
|
||||
// TODO: Should we have a more formal mechanism for putting "type patterns" in each of the above categories?
|
||||
AllowDangerousTypes bool
|
||||
}
|
||||
|
||||
func (p *Parser) init() {
|
||||
@@ -162,7 +176,7 @@ func (p *Parser) NeedSchemaFor(typ TypeIdent) {
|
||||
// avoid tripping recursive schemata, like ManagedFields, by adding an empty WIP schema
|
||||
p.Schemata[typ] = apiext.JSONSchemaProps{}
|
||||
|
||||
schemaCtx := newSchemaContext(typ.Package, p)
|
||||
schemaCtx := newSchemaContext(typ.Package, p, p.AllowDangerousTypes)
|
||||
ctxForInfo := schemaCtx.ForInfo(info)
|
||||
|
||||
pkgMarkers, err := markers.PackageMarkers(p.Collector, typ.Package)
|
||||
|
||||
45
vendor/sigs.k8s.io/controller-tools/pkg/crd/schema.go
generated
vendored
45
vendor/sigs.k8s.io/controller-tools/pkg/crd/schema.go
generated
vendored
@@ -19,6 +19,7 @@ package crd
|
||||
import (
|
||||
"fmt"
|
||||
"go/ast"
|
||||
"go/token"
|
||||
"go/types"
|
||||
"strings"
|
||||
|
||||
@@ -67,15 +68,18 @@ type schemaContext struct {
|
||||
|
||||
schemaRequester schemaRequester
|
||||
PackageMarkers markers.MarkerValues
|
||||
|
||||
allowDangerousTypes bool
|
||||
}
|
||||
|
||||
// newSchemaContext constructs a new schemaContext for the given package and schema requester.
|
||||
// It must have type info added before use via ForInfo.
|
||||
func newSchemaContext(pkg *loader.Package, req schemaRequester) *schemaContext {
|
||||
func newSchemaContext(pkg *loader.Package, req schemaRequester, allowDangerousTypes bool) *schemaContext {
|
||||
pkg.NeedTypesInfo()
|
||||
return &schemaContext{
|
||||
pkg: pkg,
|
||||
schemaRequester: req,
|
||||
pkg: pkg,
|
||||
schemaRequester: req,
|
||||
allowDangerousTypes: allowDangerousTypes,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,9 +87,10 @@ func newSchemaContext(pkg *loader.Package, req schemaRequester) *schemaContext {
|
||||
// as this one, except with the given type information.
|
||||
func (c *schemaContext) ForInfo(info *markers.TypeInfo) *schemaContext {
|
||||
return &schemaContext{
|
||||
pkg: c.pkg,
|
||||
info: info,
|
||||
schemaRequester: c.schemaRequester,
|
||||
pkg: c.pkg,
|
||||
info: info,
|
||||
schemaRequester: c.schemaRequester,
|
||||
allowDangerousTypes: c.allowDangerousTypes,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -104,6 +109,11 @@ func (c *schemaContext) requestSchema(pkgPath, typeName string) {
|
||||
|
||||
// infoToSchema creates a schema for the type in the given set of type information.
|
||||
func infoToSchema(ctx *schemaContext) *apiext.JSONSchemaProps {
|
||||
if obj := ctx.pkg.Types.Scope().Lookup(ctx.info.Name); obj != nil && implementsJSONMarshaler(obj.Type()) {
|
||||
schema := &apiext.JSONSchemaProps{Type: "Any"}
|
||||
applyMarkers(ctx, ctx.info.Markers, schema, ctx.info.RawSpec.Type)
|
||||
return schema
|
||||
}
|
||||
return typeToSchema(ctx, ctx.info.RawSpec.Type)
|
||||
}
|
||||
|
||||
@@ -200,7 +210,7 @@ func localNamedToSchema(ctx *schemaContext, ident *ast.Ident) *apiext.JSONSchema
|
||||
return &apiext.JSONSchemaProps{}
|
||||
}
|
||||
if basicInfo, isBasic := typeInfo.(*types.Basic); isBasic {
|
||||
typ, fmt, err := builtinToType(basicInfo)
|
||||
typ, fmt, err := builtinToType(basicInfo, ctx.allowDangerousTypes)
|
||||
if err != nil {
|
||||
ctx.pkg.AddError(loader.ErrFromNode(err, ident))
|
||||
}
|
||||
@@ -297,6 +307,8 @@ func mapToSchema(ctx *schemaContext, mapType *ast.MapType) *apiext.JSONSchemaPro
|
||||
ctx.pkg.AddError(loader.ErrFromNode(fmt.Errorf("map values must be a named type, not %T", mapType.Value), mapType.Value))
|
||||
return &apiext.JSONSchemaProps{}
|
||||
}
|
||||
case *ast.StarExpr:
|
||||
valSchema = typeToSchema(ctx.ForInfo(&markers.TypeInfo{}), val)
|
||||
default:
|
||||
ctx.pkg.AddError(loader.ErrFromNode(fmt.Errorf("map values must be a named type, not %T", mapType.Value), mapType.Value))
|
||||
return &apiext.JSONSchemaProps{}
|
||||
@@ -390,8 +402,8 @@ func structToSchema(ctx *schemaContext, structType *ast.StructType) *apiext.JSON
|
||||
|
||||
// builtinToType converts builtin basic types to their equivalent JSON schema form.
|
||||
// It *only* handles types allowed by the kubernetes API standards. Floats are not
|
||||
// allowed.
|
||||
func builtinToType(basic *types.Basic) (typ string, format string, err error) {
|
||||
// allowed unless allowDangerousTypes is true
|
||||
func builtinToType(basic *types.Basic, allowDangerousTypes bool) (typ string, format string, err error) {
|
||||
// NB(directxman12): formats from OpenAPI v3 are slightly different than those defined
|
||||
// in JSONSchema. This'll use the OpenAPI v3 ones, since they're useful for bounding our
|
||||
// non-string types.
|
||||
@@ -403,6 +415,8 @@ func builtinToType(basic *types.Basic) (typ string, format string, err error) {
|
||||
typ = "string"
|
||||
case basicInfo&types.IsInteger != 0:
|
||||
typ = "integer"
|
||||
case basicInfo&types.IsFloat != 0 && allowDangerousTypes:
|
||||
typ = "number"
|
||||
default:
|
||||
// NB(directxman12): floats are *NOT* allowed in kubernetes APIs
|
||||
return "", "", fmt.Errorf("unsupported type %q", basic.String())
|
||||
@@ -417,3 +431,16 @@ func builtinToType(basic *types.Basic) (typ string, format string, err error) {
|
||||
|
||||
return typ, format, nil
|
||||
}
|
||||
|
||||
// Open coded go/types representation of encoding/json.Marshaller
|
||||
var jsonMarshaler = types.NewInterfaceType([]*types.Func{
|
||||
types.NewFunc(token.NoPos, nil, "MarshalJSON",
|
||||
types.NewSignature(nil, nil,
|
||||
types.NewTuple(
|
||||
types.NewVar(token.NoPos, nil, "", types.NewSlice(types.Universe.Lookup("byte").Type())),
|
||||
types.NewVar(token.NoPos, nil, "", types.Universe.Lookup("error").Type())), false)),
|
||||
}, nil).Complete()
|
||||
|
||||
func implementsJSONMarshaler(typ types.Type) bool {
|
||||
return types.Implements(typ, jsonMarshaler) || types.Implements(types.NewPointer(typ), jsonMarshaler)
|
||||
}
|
||||
|
||||
35
vendor/sigs.k8s.io/controller-tools/pkg/crd/schema_visitor.go
generated
vendored
35
vendor/sigs.k8s.io/controller-tools/pkg/crd/schema_visitor.go
generated
vendored
@@ -44,16 +44,37 @@ type schemaWalker struct {
|
||||
visitor SchemaVisitor
|
||||
}
|
||||
|
||||
// walkSchema walks the given schema, saving modifications made by the
|
||||
// visitor (this is as simple as passing a pointer in most cases,
|
||||
// but special care needs to be taken to persist with maps).
|
||||
// walkSchema walks the given schema, saving modifications made by the visitor
|
||||
// (this is as simple as passing a pointer in most cases, but special care
|
||||
// needs to be taken to persist with maps). It also visits referenced
|
||||
// schemata, dealing with circular references appropriately. The returned
|
||||
// visitor will be used to visit all "children" of the current schema, followed
|
||||
// by a nil schema with the returned visitor to mark completion. If a nil visitor
|
||||
// is returned, traversal will no continue into the children of the current schema.
|
||||
func (w schemaWalker) walkSchema(schema *apiext.JSONSchemaProps) {
|
||||
subVisitor := w.visitor.Visit(schema)
|
||||
if subVisitor == nil {
|
||||
return
|
||||
// Walk a potential chain of schema references, keeping track of seen
|
||||
// references to avoid circular references
|
||||
subVisitor := w.visitor
|
||||
seenRefs := map[string]bool{}
|
||||
if schema.Ref != nil {
|
||||
seenRefs[*schema.Ref] = true
|
||||
}
|
||||
defer subVisitor.Visit(nil)
|
||||
for {
|
||||
subVisitor = subVisitor.Visit(schema)
|
||||
if subVisitor == nil {
|
||||
return
|
||||
}
|
||||
// mark completion of the visitor
|
||||
defer subVisitor.Visit(nil)
|
||||
|
||||
// Break if schema is not a reference or a cycle is detected
|
||||
if schema.Ref == nil || len(*schema.Ref) == 0 || seenRefs[*schema.Ref] {
|
||||
break
|
||||
}
|
||||
seenRefs[*schema.Ref] = true
|
||||
}
|
||||
|
||||
// walk sub-schemata
|
||||
subWalker := schemaWalker{visitor: subVisitor}
|
||||
if schema.Items != nil {
|
||||
subWalker.walkPtr(schema.Items.Schema)
|
||||
|
||||
15
vendor/sigs.k8s.io/controller-tools/pkg/crd/spec.go
generated
vendored
15
vendor/sigs.k8s.io/controller-tools/pkg/crd/spec.go
generated
vendored
@@ -120,7 +120,7 @@ func (p *Parser) NeedCRDFor(groupKind schema.GroupKind, maxDescLen *int) {
|
||||
}
|
||||
}
|
||||
|
||||
// fix the name if the plural was changed (this is the form the name *has* to take, so no harm in chaning it).
|
||||
// fix the name if the plural was changed (this is the form the name *has* to take, so no harm in changing it).
|
||||
crd.Name = crd.Spec.Names.Plural + "." + groupKind.Group
|
||||
|
||||
// nothing to actually write
|
||||
@@ -151,6 +151,19 @@ func (p *Parser) NeedCRDFor(groupKind schema.GroupKind, maxDescLen *int) {
|
||||
packages[0].AddError(fmt.Errorf("CRD for %s has no storage version", groupKind))
|
||||
}
|
||||
|
||||
served := false
|
||||
for _, ver := range crd.Spec.Versions {
|
||||
if ver.Served {
|
||||
served = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !served {
|
||||
// just add the error to the first relevant package for this CRD,
|
||||
// since there's no specific error location
|
||||
packages[0].AddError(fmt.Errorf("CRD for %s with version(s) %v does not serve any version", groupKind, crd.Spec.Versions))
|
||||
}
|
||||
|
||||
// NB(directxman12): CRD's status doesn't have omitempty markers, which means things
|
||||
// get serialized as null, which causes the validator to freak out. Manually set
|
||||
// these to empty till we get a better solution.
|
||||
|
||||
10
vendor/sigs.k8s.io/controller-tools/pkg/crd/zz_generated.markerhelp.go
generated
vendored
10
vendor/sigs.k8s.io/controller-tools/pkg/crd/zz_generated.markerhelp.go
generated
vendored
@@ -36,12 +36,20 @@ func (Generator) Help() *markers.DefinitionHelp {
|
||||
Summary: "indicates that we should produce a single-version CRD. ",
|
||||
Details: "Single \"trivial-version\" CRDs are compatible with older (pre 1.13) Kubernetes API servers. The storage version's schema will be used as the CRD's schema. \n Only works with the v1beta1 CRD version.",
|
||||
},
|
||||
"PreserveUnknownFields": markers.DetailedHelp{
|
||||
Summary: "indicates whether or not we should turn off pruning. ",
|
||||
Details: "Left unspecified, it'll default to true when only a v1beta1 CRD is generated (to preserve compatibility with older versions of this tool), or false otherwise. \n It's required to be false for v1 CRDs.",
|
||||
},
|
||||
"AllowDangerousTypes": markers.DetailedHelp{
|
||||
Summary: "allows types which are usually omitted from CRD generation because they are not recommended. ",
|
||||
Details: "Currently the following additional types are allowed when this is true: float32 float64 \n Left unspecified, the default is false",
|
||||
},
|
||||
"MaxDescLen": markers.DetailedHelp{
|
||||
Summary: "specifies the maximum description length for fields in CRD's OpenAPI schema. ",
|
||||
Details: "0 indicates drop the description for all fields completely. n indicates limit the description to at most n characters and truncate the description to closest sentence boundary if it exceeds n characters.",
|
||||
},
|
||||
"CRDVersions": markers.DetailedHelp{
|
||||
Summary: "specifies the target API versions of the CRD type itself to generate. Defaults to v1beta1. ",
|
||||
Summary: "specifies the target API versions of the CRD type itself to generate. Defaults to v1. ",
|
||||
Details: "The first version listed will be assumed to be the \"default\" version and will not get a version suffix in the output filename. \n You'll need to use \"v1\" to get support for features like defaulting, along with an API server that supports it (Kubernetes 1.16+).",
|
||||
},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user