Skip to content

Commit

Permalink
Add the --skip-kinds flag
Browse files Browse the repository at this point in the history
This adds the skip-kinds flag which expects a comma-separated list of
kubernetes resources (labeled by case-sensitive Kind) for which to skip
schema validation. This is useful when validating CRDs for which the
user may not have a schema.
  • Loading branch information
ian-howell committed Jul 19, 2019
1 parent 312975f commit feb8bac
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
18 changes: 18 additions & 0 deletions kubeval/kubeval.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@ var Strict bool
// for resource definitions without an available schema
var IgnoreMissingSchemas bool

// KindsToSkip is a list of kubernetes resources types with which to skip
// schema validation
var KindsToSkip []string

// in is a method which tests whether the `key` is in the set
func in(set []string, key string) bool {
for _, k := range set {
if k == key {
return true
}
}
return false
}

// ValidFormat is a type for quickly forcing
// new formats on the gojsonschema loader
type ValidFormat struct{}
Expand Down Expand Up @@ -189,6 +203,10 @@ func validateResource(data []byte, fileName string, schemaCache map[string]*gojs
}
result.APIVersion = apiVersion

if in(KindsToSkip, kind) {
return result, nil
}

schemaRef := determineSchema(kind, apiVersion)

schema, ok := schemaCache[schemaRef]
Expand Down
9 changes: 8 additions & 1 deletion kubeval/kubeval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,14 @@ func TestSkipCrdSchemaMiss(t *testing.T) {
fileContents, _ := ioutil.ReadFile(filePath)
results, _ := Validate(fileContents, "test_crd.yaml")
if len(results[0].Errors) != 0 {
t.Errorf("For custom CRD's with schema missing we should skip with SkipCrdSchemaMiss flag")
t.Errorf("For custom CRD's with schema missing we should skip with IgnoreMissingSchemas flag")
}

IgnoreMissingSchemas = false
KindsToSkip = []string{"SealedSecret"}
results, _ = Validate(fileContents, "test_crd.yaml")
if len(results[0].Errors) != 0 {
t.Errorf("We should skip resources listed in KindsToSkip")
}
}

1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func init() {
RootCmd.Flags().BoolVarP(&kubeval.OpenShift, "openshift", "", false, "Use OpenShift schemas instead of upstream Kubernetes")
RootCmd.Flags().BoolVarP(&kubeval.Strict, "strict", "", false, "Disallow additional properties not in schema")
RootCmd.Flags().BoolVarP(&kubeval.IgnoreMissingSchemas, "ignore-missing-schemas", "", false, "Skip validation for resource definitions without a schema")
RootCmd.Flags().StringSliceVar(&kubeval.KindsToSkip, "skip-kinds", []string{}, "Comma-separated list of case-sensitive kinds to skip when validating against schemas")
RootCmd.SetVersionTemplate(`{{.Version}}`)
viper.BindPFlag("schema_location", RootCmd.Flags().Lookup("schema-location"))
RootCmd.PersistentFlags().StringP("filename", "f", "stdin", "filename to be displayed when testing manifests read from stdin")
Expand Down

0 comments on commit feb8bac

Please sign in to comment.