Skip to content

Commit 390da69

Browse files
committed
Merge branch 'feat/skip-kind' of https://github.com/ian-howell/kubeval into ian-howell-feat/skip-kind
2 parents 6c4cfc0 + feb8bac commit 390da69

File tree

4 files changed

+37
-10
lines changed

4 files changed

+37
-10
lines changed

acceptance.bats

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,49 @@
33
@test "Pass when parsing a valid Kubernetes config YAML file" {
44
run kubeval fixtures/valid.yaml
55
[ "$status" -eq 0 ]
6-
[ "$output" = "The document fixtures/valid.yaml contains a valid ReplicationController" ]
6+
[ "$output" = "The file fixtures/valid.yaml contains a valid ReplicationController" ]
77
}
88

99
@test "Pass when parsing a valid Kubernetes config YAML file on stdin" {
1010
run bash -c "cat fixtures/valid.yaml | kubeval"
1111
[ "$status" -eq 0 ]
12-
[ "$output" = "The document stdin contains a valid ReplicationController" ]
12+
[ "$output" = "The file stdin contains a valid ReplicationController" ]
1313
}
1414

1515
@test "Pass when parsing a valid Kubernetes config YAML file explicitly on stdin" {
1616
run bash -c "cat fixtures/valid.yaml | kubeval -"
1717
[ "$status" -eq 0 ]
18-
[ "$output" = "The document stdin contains a valid ReplicationController" ]
18+
[ "$output" = "The file stdin contains a valid ReplicationController" ]
1919
}
2020

2121
@test "Pass when parsing a valid Kubernetes config JSON file" {
2222
run kubeval fixtures/valid.json
2323
[ "$status" -eq 0 ]
24-
[ "$output" = "The document fixtures/valid.json contains a valid Deployment" ]
24+
[ "$output" = "The file fixtures/valid.json contains a valid Deployment" ]
2525
}
2626

2727
@test "Pass when parsing a Kubernetes file with string and integer quantities" {
2828
run kubeval fixtures/quantity.yaml
2929
[ "$status" -eq 0 ]
30-
[ "$output" = "The document fixtures/quantity.yaml contains a valid LimitRange" ]
30+
[ "$output" = "The file fixtures/quantity.yaml contains a valid LimitRange" ]
3131
}
3232

3333
@test "Pass when parsing a valid Kubernetes config file with int_to_string vars" {
3434
run kubeval fixtures/int_or_string.yaml
3535
[ "$status" -eq 0 ]
36-
[ "$output" = "The document fixtures/int_or_string.yaml contains a valid Service" ]
36+
[ "$output" = "The file fixtures/int_or_string.yaml contains a valid Service" ]
3737
}
3838

3939
@test "Pass when parsing a valid Kubernetes config file with null arrays" {
4040
run kubeval fixtures/null_array.yaml
4141
[ "$status" -eq 0 ]
42-
[ "$output" = "The document fixtures/null_array.yaml contains a valid Deployment" ]
42+
[ "$output" = "The file fixtures/null_array.yaml contains a valid Deployment" ]
4343
}
4444

4545
@test "Pass when parsing a valid Kubernetes config file with null strings" {
4646
run kubeval fixtures/null_string.yaml
4747
[ "$status" -eq 0 ]
48-
[ "$output" = "The document fixtures/null_string.yaml contains a valid Service" ]
48+
[ "$output" = "The file fixtures/null_string.yaml contains a valid Service" ]
4949
}
5050

5151
@test "Pass when parsing a multi-document config file" {
@@ -77,13 +77,13 @@
7777
@test "Pass when parsing a blank config file" {
7878
run kubeval fixtures/blank.yaml
7979
[ "$status" -eq 0 ]
80-
[ "$output" = "The document fixtures/blank.yaml is empty" ]
80+
[ "$output" = "The file fixtures/blank.yaml contains an empty YAML document" ]
8181
}
8282

8383
@test "Pass when parsing a blank config file with a comment" {
8484
run kubeval fixtures/comment.yaml
8585
[ "$status" -eq 0 ]
86-
[ "$output" = "The document fixtures/comment.yaml is empty" ]
86+
[ "$output" = "The file fixtures/comment.yaml contains an empty YAML document" ]
8787
}
8888

8989
@test "Return relevant error for YAML missing kind key" {

kubeval/kubeval.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,20 @@ var IgnoreMissingSchemas bool
4646
// first error encountered or to continue, aggregating all errors
4747
var ExitOnError bool
4848

49+
// KindsToSkip is a list of kubernetes resources types with which to skip
50+
// schema validation
51+
var KindsToSkip []string
52+
53+
// in is a method which tests whether the `key` is in the set
54+
func in(set []string, key string) bool {
55+
for _, k := range set {
56+
if k == key {
57+
return true
58+
}
59+
}
60+
return false
61+
}
62+
4963
// ValidFormat is a type for quickly forcing
5064
// new formats on the gojsonschema loader
5165
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
schemaErrors, err := validateAgainstSchema(body, &result, schemaCache)
193211
if err != nil {
194212
return result, err
@@ -197,6 +215,7 @@ func validateResource(data []byte, fileName string, schemaCache map[string]*gojs
197215
return result, nil
198216
}
199217

218+
200219
func validateAgainstSchema(body interface{}, resource *ValidationResult, schemaCache map[string]*gojsonschema.Schema) ([]gojsonschema.ResultError, error) {
201220
if IgnoreMissingSchemas {
202221
log.Warn("Warning: Set to ignore missing schemas")

kubeval/kubeval_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,4 +207,11 @@ func TestSkipCrdSchemaMiss(t *testing.T) {
207207
if len(results[0].Errors) != 0 {
208208
t.Errorf("For custom CRD's with schema missing we should skip with IgnoreMissingSchemas flag")
209209
}
210+
211+
IgnoreMissingSchemas = false
212+
KindsToSkip = []string{"SealedSecret"}
213+
results, _ = Validate(fileContents, "test_crd.yaml")
214+
if len(results[0].Errors) != 0 {
215+
t.Errorf("We should skip resources listed in KindsToSkip")
216+
}
210217
}

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ func init() {
174174
RootCmd.Flags().StringSliceVarP(&directories, "directories", "d", []string{}, "A comma-separated list of directories to recursively search for YAML documents")
175175
RootCmd.Flags().BoolVarP(&kubeval.ExitOnError, "exit-on-error", "", false, "Immediately stop execution when the first error is encountered")
176176
RootCmd.Flags().BoolVarP(&forceColor, "force-color", "", false, "Force colored output even if stdout is not a TTY")
177+
RootCmd.Flags().StringSliceVar(&kubeval.KindsToSkip, "skip-kinds", []string{}, "Comma-separated list of case-sensitive kinds to skip when validating against schemas")
177178
RootCmd.SetVersionTemplate(`{{.Version}}`)
178179
viper.BindPFlag("schema_location", RootCmd.Flags().Lookup("schema-location"))
179180
RootCmd.PersistentFlags().StringP("filename", "f", "stdin", "filename to be displayed when testing manifests read from stdin")

0 commit comments

Comments
 (0)