From 64beee22e94ba3a54e1663baa991eb769247bdf2 Mon Sep 17 00:00:00 2001 From: Donny Xia Date: Wed, 13 Jan 2021 14:11:26 -0800 Subject: [PATCH] fix GetFieldValue cannot handle slice index --- api/internal/wrappy/wnode.go | 29 ++++++++++++++++++++++++++++- api/krusty/namespaces_test.go | 7 ------- api/krusty/variableref_test.go | 19 +++++++++---------- 3 files changed, 37 insertions(+), 18 deletions(-) diff --git a/api/internal/wrappy/wnode.go b/api/internal/wrappy/wnode.go index 207ff3a3f..ee9108f49 100644 --- a/api/internal/wrappy/wnode.go +++ b/api/internal/wrappy/wnode.go @@ -6,6 +6,7 @@ package wrappy import ( "fmt" "log" + "regexp" "strings" "sigs.k8s.io/kustomize/api/ifc" @@ -66,9 +67,35 @@ func (wn *WNode) GetAnnotations() map[string]string { 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. 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...)) if err != nil { return nil, err diff --git a/api/krusty/namespaces_test.go b/api/krusty/namespaces_test.go index e78514ba3..cf1b3ca10 100644 --- a/api/krusty/namespaces_test.go +++ b/api/krusty/namespaces_test.go @@ -532,9 +532,6 @@ vars: func TestVariablesAmbiguousWorkaround(t *testing.T) { th := kusttest_test.MakeHarness(t) opts := th.MakeDefaultOptions() - if opts.UseKyaml { - t.Skip("TODO(#3396)") - } th.WriteK("dev", namespaceNeedInVarDevFolder) th.WriteF("dev/elasticsearch-dev-service.yaml", namespaceNeedInVarDevResources) th.WriteK("test", namespaceNeedInVarTestFolder) @@ -591,10 +588,6 @@ vars: // to the variable declarations allows to disambiguate the variables. func TestVariablesDisambiguatedWithNamespace(t *testing.T) { th := kusttest_test.MakeHarness(t) - opts := th.MakeDefaultOptions() - if opts.UseKyaml { - t.Skip("TODO(#3396)") - } th.WriteK(".", namespaceNeedInVarMyAppWithNamespace) th.WriteF("elasticsearch-dev-service.yaml", namespaceNeedInVarDevResources) th.WriteF("elasticsearch-test-service.yaml", namespaceNeedInVarTestResources) diff --git a/api/krusty/variableref_test.go b/api/krusty/variableref_test.go index 973f4ffbb..cf73f27ad 100644 --- a/api/krusty/variableref_test.go +++ b/api/krusty/variableref_test.go @@ -360,12 +360,11 @@ resources: } } +// TODO: varref has some quote issues +// https://github.com/kubernetes-sigs/kustomize/issues/3449 func TestVarRefBig(t *testing.T) { th := kusttest_test.MakeHarness(t) opts := th.MakeDefaultOptions() - if opts.UseKyaml { - t.Skip("TODO(#3396)") - } th.WriteK("/app/base", ` namePrefix: base- resources: @@ -757,9 +756,9 @@ kind: Service metadata: annotations: prometheus.io/path: _status/vars - prometheus.io/port: "8080" - prometheus.io/scrape: "true" - service.alpha.kubernetes.io/tolerate-unready-endpoints: "true" + prometheus.io/port: 8080 + prometheus.io/scrape: true + service.alpha.kubernetes.io/tolerate-unready-endpoints: true labels: app: cockroachdb name: dev-base-cockroachdb @@ -770,8 +769,8 @@ spec: port: 26257 targetPort: 26257 - name: http - port: 8080 - targetPort: 8080 + port: "8080" + targetPort: "8080" selector: app: cockroachdb --- @@ -787,8 +786,8 @@ spec: port: 26257 targetPort: 26257 - name: http - port: 8080 - targetPort: 8080 + port: "8080" + targetPort: "8080" selector: app: cockroachdb ---