adding exception for the strict mode, if the default schema locations are used. As those already ship strict schemas

This commit is contained in:
lukas.luedke@telekom.de 2023-07-17 16:23:06 +02:00
parent 5b0e9e5c86
commit 6d5b7385f2

View file

@ -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)