mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-13 01:50:55 +00:00
fix GetFieldValue cannot handle slice index
This commit is contained in:
@@ -6,6 +6,7 @@ package wrappy
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"sigs.k8s.io/kustomize/api/ifc"
|
"sigs.k8s.io/kustomize/api/ifc"
|
||||||
@@ -66,9 +67,35 @@ func (wn *WNode) GetAnnotations() map[string]string {
|
|||||||
return wn.demandMetaData("GetAnnotations").Annotations
|
return wn.demandMetaData("GetAnnotations").Annotations
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// convertSliceIndex traverses the items in `fields` and find
|
||||||
|
// if there is a slice index in the item and change it to a
|
||||||
|
// valid Lookup field path. For example, 'ports[0]' will be
|
||||||
|
// converted to 'ports' and '0'.
|
||||||
|
func convertSliceIndex(fields []string) []string {
|
||||||
|
var res []string
|
||||||
|
for _, s := range fields {
|
||||||
|
if !strings.HasSuffix(s, "]") {
|
||||||
|
res = append(res, s)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
re := regexp.MustCompile(`^(.*)\[(\d+)\]$`)
|
||||||
|
groups := re.FindStringSubmatch(s)
|
||||||
|
if len(groups) == 0 {
|
||||||
|
// no match, add to result
|
||||||
|
res = append(res, s)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if groups[1] != "" {
|
||||||
|
res = append(res, groups[1])
|
||||||
|
}
|
||||||
|
res = append(res, groups[2])
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
// GetFieldValue implements ifc.Kunstructured.
|
// GetFieldValue implements ifc.Kunstructured.
|
||||||
func (wn *WNode) GetFieldValue(path string) (interface{}, error) {
|
func (wn *WNode) GetFieldValue(path string) (interface{}, error) {
|
||||||
fields := strings.Split(path, ".")
|
fields := convertSliceIndex(strings.Split(path, "."))
|
||||||
rn, err := wn.node.Pipe(yaml.Lookup(fields...))
|
rn, err := wn.node.Pipe(yaml.Lookup(fields...))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
@@ -532,9 +532,6 @@ vars:
|
|||||||
func TestVariablesAmbiguousWorkaround(t *testing.T) {
|
func TestVariablesAmbiguousWorkaround(t *testing.T) {
|
||||||
th := kusttest_test.MakeHarness(t)
|
th := kusttest_test.MakeHarness(t)
|
||||||
opts := th.MakeDefaultOptions()
|
opts := th.MakeDefaultOptions()
|
||||||
if opts.UseKyaml {
|
|
||||||
t.Skip("TODO(#3396)")
|
|
||||||
}
|
|
||||||
th.WriteK("dev", namespaceNeedInVarDevFolder)
|
th.WriteK("dev", namespaceNeedInVarDevFolder)
|
||||||
th.WriteF("dev/elasticsearch-dev-service.yaml", namespaceNeedInVarDevResources)
|
th.WriteF("dev/elasticsearch-dev-service.yaml", namespaceNeedInVarDevResources)
|
||||||
th.WriteK("test", namespaceNeedInVarTestFolder)
|
th.WriteK("test", namespaceNeedInVarTestFolder)
|
||||||
@@ -591,10 +588,6 @@ vars:
|
|||||||
// to the variable declarations allows to disambiguate the variables.
|
// to the variable declarations allows to disambiguate the variables.
|
||||||
func TestVariablesDisambiguatedWithNamespace(t *testing.T) {
|
func TestVariablesDisambiguatedWithNamespace(t *testing.T) {
|
||||||
th := kusttest_test.MakeHarness(t)
|
th := kusttest_test.MakeHarness(t)
|
||||||
opts := th.MakeDefaultOptions()
|
|
||||||
if opts.UseKyaml {
|
|
||||||
t.Skip("TODO(#3396)")
|
|
||||||
}
|
|
||||||
th.WriteK(".", namespaceNeedInVarMyAppWithNamespace)
|
th.WriteK(".", namespaceNeedInVarMyAppWithNamespace)
|
||||||
th.WriteF("elasticsearch-dev-service.yaml", namespaceNeedInVarDevResources)
|
th.WriteF("elasticsearch-dev-service.yaml", namespaceNeedInVarDevResources)
|
||||||
th.WriteF("elasticsearch-test-service.yaml", namespaceNeedInVarTestResources)
|
th.WriteF("elasticsearch-test-service.yaml", namespaceNeedInVarTestResources)
|
||||||
|
|||||||
@@ -360,12 +360,11 @@ resources:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: varref has some quote issues
|
||||||
|
// https://github.com/kubernetes-sigs/kustomize/issues/3449
|
||||||
func TestVarRefBig(t *testing.T) {
|
func TestVarRefBig(t *testing.T) {
|
||||||
th := kusttest_test.MakeHarness(t)
|
th := kusttest_test.MakeHarness(t)
|
||||||
opts := th.MakeDefaultOptions()
|
opts := th.MakeDefaultOptions()
|
||||||
if opts.UseKyaml {
|
|
||||||
t.Skip("TODO(#3396)")
|
|
||||||
}
|
|
||||||
th.WriteK("/app/base", `
|
th.WriteK("/app/base", `
|
||||||
namePrefix: base-
|
namePrefix: base-
|
||||||
resources:
|
resources:
|
||||||
@@ -757,9 +756,9 @@ kind: Service
|
|||||||
metadata:
|
metadata:
|
||||||
annotations:
|
annotations:
|
||||||
prometheus.io/path: _status/vars
|
prometheus.io/path: _status/vars
|
||||||
prometheus.io/port: "8080"
|
prometheus.io/port: 8080
|
||||||
prometheus.io/scrape: "true"
|
prometheus.io/scrape: true
|
||||||
service.alpha.kubernetes.io/tolerate-unready-endpoints: "true"
|
service.alpha.kubernetes.io/tolerate-unready-endpoints: true
|
||||||
labels:
|
labels:
|
||||||
app: cockroachdb
|
app: cockroachdb
|
||||||
name: dev-base-cockroachdb
|
name: dev-base-cockroachdb
|
||||||
@@ -770,8 +769,8 @@ spec:
|
|||||||
port: 26257
|
port: 26257
|
||||||
targetPort: 26257
|
targetPort: 26257
|
||||||
- name: http
|
- name: http
|
||||||
port: 8080
|
port: "8080"
|
||||||
targetPort: 8080
|
targetPort: "8080"
|
||||||
selector:
|
selector:
|
||||||
app: cockroachdb
|
app: cockroachdb
|
||||||
---
|
---
|
||||||
@@ -787,8 +786,8 @@ spec:
|
|||||||
port: 26257
|
port: 26257
|
||||||
targetPort: 26257
|
targetPort: 26257
|
||||||
- name: http
|
- name: http
|
||||||
port: 8080
|
port: "8080"
|
||||||
targetPort: 8080
|
targetPort: "8080"
|
||||||
selector:
|
selector:
|
||||||
app: cockroachdb
|
app: cockroachdb
|
||||||
---
|
---
|
||||||
|
|||||||
Reference in New Issue
Block a user