From ace02d2d013853c38dcfbe5f7628b55b47be5d2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20W=C3=BCrbach?= Date: Fri, 26 Jul 2019 17:32:43 +0200 Subject: [PATCH] Cache missing schemas --- kubeval/kubeval.go | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/kubeval/kubeval.go b/kubeval/kubeval.go index 95be75a..e836d75 100644 --- a/kubeval/kubeval.go +++ b/kubeval/kubeval.go @@ -216,7 +216,6 @@ func validateResource(data []byte, fileName string, schemaCache map[string]*gojs return result, nil } - func validateAgainstSchema(body interface{}, resource *ValidationResult, schemaCache map[string]*gojsonschema.Schema) ([]gojsonschema.ResultError, error) { if IgnoreMissingSchemas { log.Warn("Warning: Set to ignore missing schemas") @@ -227,13 +226,15 @@ func validateAgainstSchema(body interface{}, resource *ValidationResult, schemaC schemaLoader := gojsonschema.NewReferenceLoader(schemaRef) var err error schema, err = gojsonschema.NewSchema(schemaLoader) + schemaCache[schemaRef] = schema + if err != nil { - if IgnoreMissingSchemas { - return []gojsonschema.ResultError{}, nil - } - return []gojsonschema.ResultError{}, fmt.Errorf("Failed initalizing schema %s: %s", schemaRef, err) + return handleMissingSchema(fmt.Errorf("Failed initalizing schema %s: %s", schemaRef, err)) } - schemaCache[schemaRef] = schema + } + + if schema == nil { + return handleMissingSchema(fmt.Errorf("Failed initalizing schema %s: see first error", schemaRef)) } // Without forcing these types the schema fails to load @@ -255,6 +256,13 @@ func validateAgainstSchema(body interface{}, resource *ValidationResult, schemaC return []gojsonschema.ResultError{}, nil } +func handleMissingSchema(err error) ([]gojsonschema.ResultError, error) { + if IgnoreMissingSchemas { + return []gojsonschema.ResultError{}, nil + } + return []gojsonschema.ResultError{}, err +} + // NewSchemaCache returns a new schema cache to be used with // ValidateWithCache func NewSchemaCache() map[string]*gojsonschema.Schema {