69
vendor/sigs.k8s.io/controller-tools/pkg/crd/gen.go
generated
vendored
69
vendor/sigs.k8s.io/controller-tools/pkg/crd/gen.go
generated
vendored
@@ -84,6 +84,9 @@ type Generator struct {
|
||||
// You'll need to use "v1" to get support for features like defaulting,
|
||||
// along with an API server that supports it (Kubernetes 1.16+).
|
||||
CRDVersions []string `marker:"crdVersions,optional"`
|
||||
|
||||
// GenerateEmbeddedObjectMeta specifies if any embedded ObjectMeta in the CRD should be generated
|
||||
GenerateEmbeddedObjectMeta *bool `marker:",optional"`
|
||||
}
|
||||
|
||||
func (Generator) CheckFilter() loader.NodeFilter {
|
||||
@@ -98,6 +101,8 @@ func (g Generator) Generate(ctx *genall.GenerationContext) error {
|
||||
Checker: ctx.Checker,
|
||||
// Perform defaulting here to avoid ambiguity later
|
||||
AllowDangerousTypes: g.AllowDangerousTypes != nil && *g.AllowDangerousTypes == true,
|
||||
// Indicates the parser on whether to register the ObjectMeta type or not
|
||||
GenerateEmbeddedObjectMeta: g.GenerateEmbeddedObjectMeta != nil && *g.GenerateEmbeddedObjectMeta == true,
|
||||
}
|
||||
|
||||
AddKnownTypes(parser)
|
||||
@@ -129,6 +134,9 @@ func (g Generator) Generate(ctx *genall.GenerationContext) error {
|
||||
crdRaw := parser.CustomResourceDefinitions[groupKind]
|
||||
addAttribution(&crdRaw)
|
||||
|
||||
// Prevent the top level metadata for the CRD to be generate regardless of the intention in the arguments
|
||||
FixTopLevelMetadata(crdRaw)
|
||||
|
||||
versionedCRDs := make([]interface{}, len(crdVersions))
|
||||
for i, ver := range crdVersions {
|
||||
conv, err := AsVersion(crdRaw, schema.GroupVersion{Group: apiext.SchemeGroupVersion.Group, Version: ver})
|
||||
@@ -161,10 +169,14 @@ func (g Generator) Generate(ctx *genall.GenerationContext) error {
|
||||
}
|
||||
|
||||
for i, crd := range versionedCRDs {
|
||||
// defaults are not allowed to be specified in v1beta1 CRDs, so strip them
|
||||
// before writing to a file
|
||||
// defaults are not allowed to be specified in v1beta1 CRDs and
|
||||
// decriptions are not allowed on the metadata regardless of version
|
||||
// strip them before writing to a file
|
||||
if crdVersions[i] == "v1beta1" {
|
||||
removeDefaultsFromSchemas(crd.(*apiextlegacy.CustomResourceDefinition))
|
||||
removeDescriptionFromMetadataLegacy(crd.(*apiextlegacy.CustomResourceDefinition))
|
||||
} else {
|
||||
removeDescriptionFromMetadata(crd.(*apiext.CustomResourceDefinition))
|
||||
}
|
||||
var fileName string
|
||||
if i == 0 {
|
||||
@@ -181,6 +193,47 @@ func (g Generator) Generate(ctx *genall.GenerationContext) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func removeDescriptionFromMetadata(crd *apiext.CustomResourceDefinition) {
|
||||
for _, versionSpec := range crd.Spec.Versions {
|
||||
if versionSpec.Schema != nil {
|
||||
removeDescriptionFromMetadataProps(versionSpec.Schema.OpenAPIV3Schema)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func removeDescriptionFromMetadataProps(v *apiext.JSONSchemaProps) {
|
||||
if m, ok := v.Properties["metadata"]; ok {
|
||||
meta := &m
|
||||
if meta.Description != "" {
|
||||
meta.Description = ""
|
||||
v.Properties["metadata"] = m
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func removeDescriptionFromMetadataLegacy(crd *apiextlegacy.CustomResourceDefinition) {
|
||||
if crd.Spec.Validation != nil {
|
||||
removeDescriptionFromMetadataPropsLegacy(crd.Spec.Validation.OpenAPIV3Schema)
|
||||
}
|
||||
for _, versionSpec := range crd.Spec.Versions {
|
||||
if versionSpec.Schema != nil {
|
||||
removeDescriptionFromMetadataPropsLegacy(versionSpec.Schema.OpenAPIV3Schema)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func removeDescriptionFromMetadataPropsLegacy(v *apiextlegacy.JSONSchemaProps) {
|
||||
if m, ok := v.Properties["metadata"]; ok {
|
||||
meta := &m
|
||||
if meta.Description != "" {
|
||||
meta.Description = ""
|
||||
v.Properties["metadata"] = m
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// removeDefaultsFromSchemas will remove all instances of default values being
|
||||
// specified across all defined API versions
|
||||
func removeDefaultsFromSchemas(crd *apiextlegacy.CustomResourceDefinition) {
|
||||
@@ -224,6 +277,18 @@ func removeDefaultsFromSchemaProps(v *apiextlegacy.JSONSchemaProps) {
|
||||
}
|
||||
}
|
||||
|
||||
// FixTopLevelMetadata resets the schema for the top-level metadata field which is needed for CRD validation
|
||||
func FixTopLevelMetadata(crd apiext.CustomResourceDefinition) {
|
||||
for _, v := range crd.Spec.Versions {
|
||||
if v.Schema != nil && v.Schema.OpenAPIV3Schema != nil && v.Schema.OpenAPIV3Schema.Properties != nil {
|
||||
schemaProperties := v.Schema.OpenAPIV3Schema.Properties
|
||||
if _, ok := schemaProperties["metadata"]; ok {
|
||||
schemaProperties["metadata"] = apiext.JSONSchemaProps{Type: "object"}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// toTrivialVersions strips out all schemata except for the storage schema,
|
||||
// and moves that up into the root object. This makes the CRD compatible
|
||||
// with pre 1.13 clusters.
|
||||
|
||||
Reference in New Issue
Block a user