Skip to content

Commit feb8bac

Browse files
committed
Add the --skip-kinds flag
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.
1 parent 312975f commit feb8bac

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

kubeval/kubeval.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,20 @@ var Strict bool
4242
// for resource definitions without an available schema
4343
var IgnoreMissingSchemas bool
4444

45+
// KindsToSkip is a list of kubernetes resources types with which to skip
46+
// schema validation
47+
var KindsToSkip []string
48+
49+
// in is a method which tests whether the `key` is in the set
50+
func in(set []string, key string) bool {
51+
for _, k := range set {
52+
if k == key {
53+
return true
54+
}
55+
}
56+
return false
57+
}
58+
4559
// ValidFormat is a type for quickly forcing
4660
// new formats on the gojsonschema loader
4761
type ValidFormat struct{}
@@ -189,6 +203,10 @@ func validateResource(data []byte, fileName string, schemaCache map[string]*gojs
189203
}
190204
result.APIVersion = apiVersion
191205

206+
if in(KindsToSkip, kind) {
207+
return result, nil
208+
}
209+
192210
schemaRef := determineSchema(kind, apiVersion)
193211

194212
schema, ok := schemaCache[schemaRef]

kubeval/kubeval_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,14 @@ func TestSkipCrdSchemaMiss(t *testing.T) {
168168
fileContents, _ := ioutil.ReadFile(filePath)
169169
results, _ := Validate(fileContents, "test_crd.yaml")
170170
if len(results[0].Errors) != 0 {
171-
t.Errorf("For custom CRD's with schema missing we should skip with SkipCrdSchemaMiss flag")
171+
t.Errorf("For custom CRD's with schema missing we should skip with IgnoreMissingSchemas flag")
172+
}
173+
174+
IgnoreMissingSchemas = false
175+
KindsToSkip = []string{"SealedSecret"}
176+
results, _ = Validate(fileContents, "test_crd.yaml")
177+
if len(results[0].Errors) != 0 {
178+
t.Errorf("We should skip resources listed in KindsToSkip")
172179
}
173180
}
174181

main.go

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

0 commit comments

Comments
 (0)