mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-05-17 18:25:26 +00:00
Merge pull request #3453 from Shell32-Natsu/wnode-field-value
fix GetFieldValue cannot handle slice index
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
---
|
||||
|
||||
Reference in New Issue
Block a user