Skip to content

Commit

Permalink
Merge pull request #143 from ian-howell/fix/unconventional-keys
Browse files Browse the repository at this point in the history
Fixes panics on non-string YAML keys
  • Loading branch information
garethr committed Jul 20, 2019
2 parents 312975f + 46b2781 commit c992d01
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
8 changes: 8 additions & 0 deletions fixtures/unconventional_keys.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: unconventional-keys
data:
5: "integer"
3.14: "float"
true: "boolean"
4 changes: 2 additions & 2 deletions kubeval/kubeval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func TestValidateValidInputs(t *testing.T) {
"quantity.yaml",
"extra_property.yaml",
"full_domain_group.yaml",
"unconventional_keys.yaml",
}
for _, test := range tests {
filePath, _ := filepath.Abs("../fixtures/" + test)
Expand All @@ -51,6 +52,7 @@ func TestValidateValidInputsWithCache(t *testing.T) {
"quantity.yaml",
"extra_property.yaml",
"full_domain_group.yaml",
"unconventional_keys.yaml",
}
schemaCache := make(map[string]*gojsonschema.Schema, 0)

Expand Down Expand Up @@ -89,7 +91,6 @@ func TestStrictCatchesAdditionalErrors(t *testing.T) {
}
}


func TestValidateMultipleVersions(t *testing.T) {
Strict = true
Version = "1.14.0"
Expand Down Expand Up @@ -171,4 +172,3 @@ func TestSkipCrdSchemaMiss(t *testing.T) {
t.Errorf("For custom CRD's with schema missing we should skip with SkipCrdSchemaMiss flag")
}
}

4 changes: 3 additions & 1 deletion kubeval/utils.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package kubeval

import "fmt"

// Based on https://stackoverflow.com/questions/40737122/convert-yaml-to-json-without-struct-golang
// We unmarshal yaml into a value of type interface{},
// go through the result recursively, and convert each encountered
Expand All @@ -10,7 +12,7 @@ func convertToStringKeys(i interface{}) interface{} {
case map[interface{}]interface{}:
m2 := map[string]interface{}{}
for k, v := range x {
m2[k.(string)] = convertToStringKeys(v)
m2[fmt.Sprintf("%v", k)] = convertToStringKeys(v)
}
return m2
case []interface{}:
Expand Down

0 comments on commit c992d01

Please sign in to comment.