Skip to content

Commit

Permalink
Fixes panics on non-string YAML keys
Browse files Browse the repository at this point in the history
This resolves #91 and
#98
  • Loading branch information
ian-howell committed Jul 16, 2019
1 parent 312975f commit 46b2781
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 46b2781

Please sign in to comment.