mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
FieldSpec Backward Compatibility with K8s Downward API
This commit is contained in:
@@ -76,10 +76,13 @@ func parseFields(path string) ([]PathSection, error) {
|
|||||||
// PathSection, save it to the set, then begin
|
// PathSection, save it to the set, then begin
|
||||||
// a new PathSection
|
// a new PathSection
|
||||||
tmpIdx, err := strconv.Atoi(path[start:i])
|
tmpIdx, err := strconv.Atoi(path[start:i])
|
||||||
if err != nil {
|
if err == nil {
|
||||||
return nil, fmt.Errorf("invalid index %s", path)
|
// We have detected an integer so an array.
|
||||||
}
|
|
||||||
section.idx = tmpIdx
|
section.idx = tmpIdx
|
||||||
|
} else {
|
||||||
|
// We have detected the downwardapi syntax
|
||||||
|
appendNonEmpty(§ion, path[start:i])
|
||||||
|
}
|
||||||
sectionset = append(sectionset, section)
|
sectionset = append(sectionset, section)
|
||||||
section = newPathSection()
|
section = newPathSection()
|
||||||
|
|
||||||
|
|||||||
@@ -94,10 +94,10 @@ func TestParseField(t *testing.T) {
|
|||||||
errorExpected: false,
|
errorExpected: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "validStructSubFieldNoneIntIndex",
|
name: "validStructDownwardAPI",
|
||||||
pathToField: "complextree[thisisnotanint]",
|
pathToField: `metadata.labels["app.kubernetes.io/component"]`,
|
||||||
errorExpected: true,
|
expectedValue: buildPath(-1, "metadata", "labels", "app.kubernetes.io/component"),
|
||||||
errorMsg: "invalid index complextree[thisisnotanint]",
|
errorExpected: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "invalidIndexInIndex",
|
name: "invalidIndexInIndex",
|
||||||
|
|||||||
@@ -24,14 +24,14 @@ import (
|
|||||||
var kunstructured = NewKunstructuredFactoryImpl().FromMap(map[string]interface{}{
|
var kunstructured = NewKunstructuredFactoryImpl().FromMap(map[string]interface{}{
|
||||||
"Kind": "Service",
|
"Kind": "Service",
|
||||||
"metadata": map[string]interface{}{
|
"metadata": map[string]interface{}{
|
||||||
"labels": map[string]string{
|
"labels": map[string]interface{}{
|
||||||
"app": "application-name",
|
"app": "application-name",
|
||||||
},
|
},
|
||||||
"name": "service-name",
|
"name": "service-name",
|
||||||
},
|
},
|
||||||
"spec": map[string]interface{}{
|
"spec": map[string]interface{}{
|
||||||
"ports": map[string]interface{}{
|
"ports": map[string]interface{}{
|
||||||
"port": "80",
|
"port": int64(80),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"this": map[string]interface{}{
|
"this": map[string]interface{}{
|
||||||
@@ -168,7 +168,7 @@ func TestGetFieldValue(t *testing.T) {
|
|||||||
{
|
{
|
||||||
name: "threeFields",
|
name: "threeFields",
|
||||||
pathToField: "spec.ports.port",
|
pathToField: "spec.ports.port",
|
||||||
expectedValue: "80",
|
expectedValue: int64(80),
|
||||||
errorExpected: false,
|
errorExpected: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -249,6 +249,12 @@ func TestGetFieldValue(t *testing.T) {
|
|||||||
errorMsg: "no field named 'that[99]'",
|
errorMsg: "no field named 'that[99]'",
|
||||||
errorExpected: true,
|
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",
|
name: "unknownSlice",
|
||||||
pathToField: "unknown[0]",
|
pathToField: "unknown[0]",
|
||||||
@@ -298,16 +304,34 @@ func TestGetFieldValue(t *testing.T) {
|
|||||||
errorMsg: "no field named 'complextree[1].field2[1].invalidsubfield'",
|
errorMsg: "no field named 'complextree[1].field2[1].invalidsubfield'",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "validStructSubFieldNoneIntIndex",
|
name: "validDownwardAPILabels",
|
||||||
pathToField: "complextree[thisisnotanint]",
|
pathToField: `metadata.labels["app"]`,
|
||||||
errorExpected: true,
|
errorExpected: false,
|
||||||
errorMsg: "no field named 'complextree[thisisnotanint]'",
|
expectedValue: "application-name",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "invalidNoneIntIndex",
|
name: "validDownwardAPISpecs",
|
||||||
pathToField: "complextree[thisisnotanint]",
|
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,
|
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",
|
name: "invalidIndexInIndex",
|
||||||
@@ -362,12 +386,6 @@ func TestGetString(t *testing.T) {
|
|||||||
expectedValue: "service-name",
|
expectedValue: "service-name",
|
||||||
errorExpected: false,
|
errorExpected: false,
|
||||||
},
|
},
|
||||||
{
|
|
||||||
name: "threeFields",
|
|
||||||
pathToField: "spec.ports.port",
|
|
||||||
expectedValue: "80",
|
|
||||||
errorExpected: false,
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
name: "emptyMap",
|
name: "emptyMap",
|
||||||
pathToField: "this.is.anEmptyMap",
|
pathToField: "this.is.anEmptyMap",
|
||||||
@@ -440,6 +458,12 @@ func TestGetString(t *testing.T) {
|
|||||||
errorExpected: true,
|
errorExpected: true,
|
||||||
errorMsg: "no field named 'this.is[1].aString'",
|
errorMsg: "no field named 'this.is[1].aString'",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "validDownwardAPIField",
|
||||||
|
pathToField: `metadata.labels["app"]`,
|
||||||
|
errorExpected: false,
|
||||||
|
expectedValue: "application-name",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
@@ -487,6 +511,12 @@ func TestGetInt64(t *testing.T) {
|
|||||||
errorExpected: true,
|
errorExpected: true,
|
||||||
errorMsg: "no field named 'these[1].field2[99]'",
|
errorMsg: "no field named 'these[1].field2[99]'",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "validDownwardAPISpecs",
|
||||||
|
pathToField: `spec.ports['port']`,
|
||||||
|
errorExpected: false,
|
||||||
|
expectedValue: int64(80),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
@@ -522,6 +552,12 @@ func TestGetFloat64(t *testing.T) {
|
|||||||
errorExpected: false,
|
errorExpected: false,
|
||||||
expectedValue: float64(1.1121),
|
expectedValue: float64(1.1121),
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "validDownwardAPIThis",
|
||||||
|
pathToField: `this.is[aFloat]`,
|
||||||
|
errorExpected: false,
|
||||||
|
expectedValue: float64(1.001),
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: "twoFieldsOneMissing",
|
name: "twoFieldsOneMissing",
|
||||||
pathToField: "metadata.banana",
|
pathToField: "metadata.banana",
|
||||||
|
|||||||
Reference in New Issue
Block a user