FieldSpec Backward Compatibility with K8s Downward API

This commit is contained in:
Jerome Brette
2019-06-20 09:22:47 -05:00
parent a5b97cbd9b
commit c925b43090
3 changed files with 62 additions and 23 deletions

View File

@@ -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(&section, path[start:i])
}
section.idx = tmpIdx
sectionset = append(sectionset, section)
section = newPathSection()

View File

@@ -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",

View File

@@ -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",