diff --git a/pkg/validator/validator.go b/pkg/validator/validator.go index 0af7840..3931d06 100644 --- a/pkg/validator/validator.go +++ b/pkg/validator/validator.go @@ -69,8 +69,10 @@ type Opts struct { func New(schemaLocations []string, opts Opts) (Validator, error) { // Default to our kubernetes-json-schema fork // raw.githubusercontent.com is frontend by Fastly and very fast + defaultLocationsUsed := false if len(schemaLocations) == 0 { schemaLocations = []string{"https://raw.githubusercontent.com/yannh/kubernetes-json-schema/master/{{ .NormalizedKubernetesVersion }}-standalone{{ .StrictSuffix }}/{{ .ResourceKind }}{{ .KindSuffix }}.json"} + defaultLocationsUsed = true } registries := []registry.Registry{} @@ -96,14 +98,17 @@ func New(schemaLocations []string, opts Opts) (Validator, error) { opts.StrictExceptions = map[string]struct{}{} } if len(opts.StrictExceptions) == 0 { + // If no strict exceptions are specified, we add the metadata field + // as it is mostly not autogenerated in the OpenAPI schemas generated from CRDs opts.StrictExceptions["metadata"] = struct{}{} } return &v{ - opts: opts, - schemaDownload: downloadSchema, - schemaCache: cache.NewInMemoryCache(), - regs: registries, + opts: opts, + schemaDownload: downloadSchema, + schemaCache: cache.NewInMemoryCache(), + regs: registries, + defaultLocationsUsed: defaultLocationsUsed, }, nil } @@ -140,10 +145,11 @@ func (val *v) setStrictSchema(schema *jsonschema.Schema, prefixPath string) { } type v struct { - opts Opts - schemaCache cache.Cache - schemaDownload func(registries []registry.Registry, kind, version, k8sVersion string) (*jsonschema.Schema, error) - regs []registry.Registry + opts Opts + schemaCache cache.Cache + schemaDownload func(registries []registry.Registry, kind, version, k8sVersion string) (*jsonschema.Schema, error) + regs []registry.Registry + defaultLocationsUsed bool } // ValidateResource validates a single resource. This allows to validate @@ -232,7 +238,7 @@ func (val *v) ValidateResource(res resource.Resource) Result { // If strict mode is enabled, we set additionalProperties to false // if not explicitly set in the schema - if val.opts.Strict { + if val.opts.Strict && !val.defaultLocationsUsed { val.setStrictSchema(schema, "") } err = schema.Validate(r)