Skip to content

Commit

Permalink
Merge branch 'feat/skip-kind' of https://github.com/ian-howell/kubeval
Browse files Browse the repository at this point in the history
…into ian-howell-feat/skip-kind
  • Loading branch information
garethr committed Jul 20, 2019
2 parents 6c4cfc0 + feb8bac commit 390da69
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
20 changes: 10 additions & 10 deletions acceptance.bats
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,49 @@
@test "Pass when parsing a valid Kubernetes config YAML file" {
run kubeval fixtures/valid.yaml
[ "$status" -eq 0 ]
[ "$output" = "The document fixtures/valid.yaml contains a valid ReplicationController" ]
[ "$output" = "The file fixtures/valid.yaml contains a valid ReplicationController" ]
}

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

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

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

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

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

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

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

@test "Pass when parsing a multi-document config file" {
Expand Down Expand Up @@ -77,13 +77,13 @@
@test "Pass when parsing a blank config file" {
run kubeval fixtures/blank.yaml
[ "$status" -eq 0 ]
[ "$output" = "The document fixtures/blank.yaml is empty" ]
[ "$output" = "The file fixtures/blank.yaml contains an empty YAML document" ]
}

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

@test "Return relevant error for YAML missing kind key" {
Expand Down
19 changes: 19 additions & 0 deletions kubeval/kubeval.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ var IgnoreMissingSchemas bool
// first error encountered or to continue, aggregating all errors
var ExitOnError 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
}

schemaErrors, err := validateAgainstSchema(body, &result, schemaCache)
if err != nil {
return result, err
Expand All @@ -197,6 +215,7 @@ 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")
Expand Down
7 changes: 7 additions & 0 deletions kubeval/kubeval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,11 @@ func TestSkipCrdSchemaMiss(t *testing.T) {
if len(results[0].Errors) != 0 {
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 @@ -174,6 +174,7 @@ func init() {
RootCmd.Flags().StringSliceVarP(&directories, "directories", "d", []string{}, "A comma-separated list of directories to recursively search for YAML documents")
RootCmd.Flags().BoolVarP(&kubeval.ExitOnError, "exit-on-error", "", false, "Immediately stop execution when the first error is encountered")
RootCmd.Flags().BoolVarP(&forceColor, "force-color", "", false, "Force colored output even if stdout is not a TTY")
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 390da69

Please sign in to comment.