diff --git a/k8sdeps/kunstruct/helper.go b/k8sdeps/kunstruct/helper.go index bebd4435c..d2cd1b701 100644 --- a/k8sdeps/kunstruct/helper.go +++ b/k8sdeps/kunstruct/helper.go @@ -76,10 +76,13 @@ func parseFields(path string) ([]PathSection, error) { // PathSection, save it to the set, then begin // a new PathSection tmpIdx, err := strconv.Atoi(path[start:i]) - if err != nil { - return nil, fmt.Errorf("invalid index %s", path) + if err == nil { + // We have detected an integer so an array. + section.idx = tmpIdx + } else { + // We have detected the downwardapi syntax + appendNonEmpty(§ion, path[start:i]) } - section.idx = tmpIdx sectionset = append(sectionset, section) section = newPathSection() diff --git a/k8sdeps/kunstruct/helper_test.go b/k8sdeps/kunstruct/helper_test.go index 46e5011be..6ba59e63d 100644 --- a/k8sdeps/kunstruct/helper_test.go +++ b/k8sdeps/kunstruct/helper_test.go @@ -94,10 +94,10 @@ func TestParseField(t *testing.T) { errorExpected: false, }, { - name: "validStructSubFieldNoneIntIndex", - pathToField: "complextree[thisisnotanint]", - errorExpected: true, - errorMsg: "invalid index complextree[thisisnotanint]", + name: "validStructDownwardAPI", + pathToField: `metadata.labels["app.kubernetes.io/component"]`, + expectedValue: buildPath(-1, "metadata", "labels", "app.kubernetes.io/component"), + errorExpected: false, }, { name: "invalidIndexInIndex", diff --git a/k8sdeps/kunstruct/kunstruct_test.go b/k8sdeps/kunstruct/kunstruct_test.go index c0a675705..ea471f88d 100644 --- a/k8sdeps/kunstruct/kunstruct_test.go +++ b/k8sdeps/kunstruct/kunstruct_test.go @@ -24,14 +24,14 @@ import ( var kunstructured = NewKunstructuredFactoryImpl().FromMap(map[string]interface{}{ "Kind": "Service", "metadata": map[string]interface{}{ - "labels": map[string]string{ + "labels": map[string]interface{}{ "app": "application-name", }, "name": "service-name", }, "spec": map[string]interface{}{ "ports": map[string]interface{}{ - "port": "80", + "port": int64(80), }, }, "this": map[string]interface{}{ @@ -168,7 +168,7 @@ func TestGetFieldValue(t *testing.T) { { name: "threeFields", pathToField: "spec.ports.port", - expectedValue: "80", + expectedValue: int64(80), errorExpected: false, }, { @@ -249,6 +249,12 @@ func TestGetFieldValue(t *testing.T) { errorMsg: "no field named 'that[99]'", errorExpected: true, }, + { + name: "accessorError", + pathToField: "that[downwardapi]", + errorMsg: ".that.downwardapi accessor error: [idx0 idx1 idx2 idx3] is of the type []interface {}, expected map[string]interface{}", + errorExpected: true, + }, { name: "unknownSlice", pathToField: "unknown[0]", @@ -298,16 +304,34 @@ func TestGetFieldValue(t *testing.T) { errorMsg: "no field named 'complextree[1].field2[1].invalidsubfield'", }, { - name: "validStructSubFieldNoneIntIndex", - pathToField: "complextree[thisisnotanint]", - errorExpected: true, - errorMsg: "no field named 'complextree[thisisnotanint]'", + name: "validDownwardAPILabels", + pathToField: `metadata.labels["app"]`, + errorExpected: false, + expectedValue: "application-name", }, { - name: "invalidNoneIntIndex", - pathToField: "complextree[thisisnotanint]", + name: "validDownwardAPISpecs", + pathToField: `spec.ports['port']`, + errorExpected: false, + expectedValue: int64(80), + }, + { + name: "validDownwardAPIThis", + pathToField: `this.is[aFloat]`, + errorExpected: false, + expectedValue: float64(1.001), + }, + { + name: "downwardAPIInvalidLabel", + pathToField: `metadata.labels["theisnotanint"]`, errorExpected: true, - errorMsg: "no field named 'complextree[thisisnotanint]'", + errorMsg: `no field named 'metadata.labels["theisnotanint"]'`, + }, + { + name: "downwardAPIInvalidLabel2", + pathToField: `invalidfield.labels["app"]`, + errorExpected: true, + errorMsg: `no field named 'invalidfield.labels["app"]'`, }, { name: "invalidIndexInIndex", @@ -362,12 +386,6 @@ func TestGetString(t *testing.T) { expectedValue: "service-name", errorExpected: false, }, - { - name: "threeFields", - pathToField: "spec.ports.port", - expectedValue: "80", - errorExpected: false, - }, { name: "emptyMap", pathToField: "this.is.anEmptyMap", @@ -440,6 +458,12 @@ func TestGetString(t *testing.T) { errorExpected: true, errorMsg: "no field named 'this.is[1].aString'", }, + { + name: "validDownwardAPIField", + pathToField: `metadata.labels["app"]`, + errorExpected: false, + expectedValue: "application-name", + }, } for _, test := range tests { @@ -487,6 +511,12 @@ func TestGetInt64(t *testing.T) { errorExpected: true, errorMsg: "no field named 'these[1].field2[99]'", }, + { + name: "validDownwardAPISpecs", + pathToField: `spec.ports['port']`, + errorExpected: false, + expectedValue: int64(80), + }, } for _, test := range tests { @@ -522,6 +552,12 @@ func TestGetFloat64(t *testing.T) { errorExpected: false, expectedValue: float64(1.1121), }, + { + name: "validDownwardAPIThis", + pathToField: `this.is[aFloat]`, + errorExpected: false, + expectedValue: float64(1.001), + }, { name: "twoFieldsOneMissing", pathToField: "metadata.banana",