Skip to content

Commit c992d01

Browse files
authored
Merge pull request #143 from ian-howell/fix/unconventional-keys
Fixes panics on non-string YAML keys
2 parents 312975f + 46b2781 commit c992d01

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

fixtures/unconventional_keys.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
apiVersion: v1
2+
kind: ConfigMap
3+
metadata:
4+
name: unconventional-keys
5+
data:
6+
5: "integer"
7+
3.14: "float"
8+
true: "boolean"

kubeval/kubeval_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func TestValidateValidInputs(t *testing.T) {
2828
"quantity.yaml",
2929
"extra_property.yaml",
3030
"full_domain_group.yaml",
31+
"unconventional_keys.yaml",
3132
}
3233
for _, test := range tests {
3334
filePath, _ := filepath.Abs("../fixtures/" + test)
@@ -51,6 +52,7 @@ func TestValidateValidInputsWithCache(t *testing.T) {
5152
"quantity.yaml",
5253
"extra_property.yaml",
5354
"full_domain_group.yaml",
55+
"unconventional_keys.yaml",
5456
}
5557
schemaCache := make(map[string]*gojsonschema.Schema, 0)
5658

@@ -89,7 +91,6 @@ func TestStrictCatchesAdditionalErrors(t *testing.T) {
8991
}
9092
}
9193

92-
9394
func TestValidateMultipleVersions(t *testing.T) {
9495
Strict = true
9596
Version = "1.14.0"
@@ -171,4 +172,3 @@ func TestSkipCrdSchemaMiss(t *testing.T) {
171172
t.Errorf("For custom CRD's with schema missing we should skip with SkipCrdSchemaMiss flag")
172173
}
173174
}
174-

kubeval/utils.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package kubeval
22

3+
import "fmt"
4+
35
// Based on https://stackoverflow.com/questions/40737122/convert-yaml-to-json-without-struct-golang
46
// We unmarshal yaml into a value of type interface{},
57
// go through the result recursively, and convert each encountered
@@ -10,7 +12,7 @@ func convertToStringKeys(i interface{}) interface{} {
1012
case map[interface{}]interface{}:
1113
m2 := map[string]interface{}{}
1214
for k, v := range x {
13-
m2[k.(string)] = convertToStringKeys(v)
15+
m2[fmt.Sprintf("%v", k)] = convertToStringKeys(v)
1416
}
1517
return m2
1618
case []interface{}:

0 commit comments

Comments
 (0)