From 188e35fbfd5fcaee208e01027b37db11ee183b49 Mon Sep 17 00:00:00 2001 From: annelausf <90939451+annelausf@users.noreply.github.com> Date: Mon, 2 May 2022 14:30:33 -0700 Subject: [PATCH] Added code for fields with dots(.) in their names work as needed (#4591) * Added code for fields with dots(.) in their names work as needed Co-authored-by: sarjamil * Fixed imports Co-authored-by: sarjamil * Removed comments Co-authored-by: sarjamil * Fix filter method Co-authored-by: sarjamil * Fixed failing tests Co-authored-by: sarjamil * Remove comments Add co-author to PR. Co-authored-by: sarjamil * Add co-author to PR. Co-authored-by: sarjamil sjamil@salesforce.com Co-authored-by: sarjamil --- api/filters/fieldspec/fieldspec.go | 2 +- api/filters/replacement/replacement.go | 5 +++-- {api/internal => kyaml}/utils/pathsplitter.go | 0 {api/internal => kyaml}/utils/pathsplitter_test.go | 2 +- kyaml/yaml/rnode.go | 3 ++- kyaml/yaml/rnode_test.go | 4 ---- 6 files changed, 7 insertions(+), 9 deletions(-) rename {api/internal => kyaml}/utils/pathsplitter.go (100%) rename {api/internal => kyaml}/utils/pathsplitter_test.go (97%) diff --git a/api/filters/fieldspec/fieldspec.go b/api/filters/fieldspec/fieldspec.go index 8ee0528654..8e4e78ca6e 100644 --- a/api/filters/fieldspec/fieldspec.go +++ b/api/filters/fieldspec/fieldspec.go @@ -8,10 +8,10 @@ import ( "strings" "sigs.k8s.io/kustomize/api/filters/filtersutil" - "sigs.k8s.io/kustomize/api/internal/utils" "sigs.k8s.io/kustomize/api/types" "sigs.k8s.io/kustomize/kyaml/errors" "sigs.k8s.io/kustomize/kyaml/resid" + "sigs.k8s.io/kustomize/kyaml/utils" "sigs.k8s.io/kustomize/kyaml/yaml" ) diff --git a/api/filters/replacement/replacement.go b/api/filters/replacement/replacement.go index 0201905bef..5f4667a9c6 100644 --- a/api/filters/replacement/replacement.go +++ b/api/filters/replacement/replacement.go @@ -12,6 +12,7 @@ import ( "sigs.k8s.io/kustomize/api/resource" "sigs.k8s.io/kustomize/api/types" "sigs.k8s.io/kustomize/kyaml/resid" + kyaml_utils "sigs.k8s.io/kustomize/kyaml/utils" "sigs.k8s.io/kustomize/kyaml/yaml" ) @@ -114,7 +115,7 @@ func rejectId(rejects []*types.Selector, id *resid.ResId) bool { func applyToNode(node *yaml.RNode, value *yaml.RNode, target *types.TargetSelector) error { for _, fp := range target.FieldPaths { - fieldPath := utils.SmarterPathSplitter(fp, ".") + fieldPath := kyaml_utils.SmarterPathSplitter(fp, ".") var t *yaml.RNode var err error if target.Options != nil && target.Options.Create { @@ -199,7 +200,7 @@ func getReplacement(nodes []*yaml.RNode, r *types.Replacement) (*yaml.RNode, err if r.Source.FieldPath == "" { r.Source.FieldPath = types.DefaultReplacementFieldPath } - fieldPath := utils.SmarterPathSplitter(r.Source.FieldPath, ".") + fieldPath := kyaml_utils.SmarterPathSplitter(r.Source.FieldPath, ".") rn, err := source.Pipe(yaml.Lookup(fieldPath...)) if err != nil { diff --git a/api/internal/utils/pathsplitter.go b/kyaml/utils/pathsplitter.go similarity index 100% rename from api/internal/utils/pathsplitter.go rename to kyaml/utils/pathsplitter.go diff --git a/api/internal/utils/pathsplitter_test.go b/kyaml/utils/pathsplitter_test.go similarity index 97% rename from api/internal/utils/pathsplitter_test.go rename to kyaml/utils/pathsplitter_test.go index 21dcc83911..180f3e2595 100644 --- a/api/internal/utils/pathsplitter_test.go +++ b/kyaml/utils/pathsplitter_test.go @@ -7,7 +7,7 @@ import ( "testing" "github.com/stretchr/testify/assert" - . "sigs.k8s.io/kustomize/api/internal/utils" + . "sigs.k8s.io/kustomize/kyaml/utils" ) func TestPathSplitter(t *testing.T) { diff --git a/kyaml/yaml/rnode.go b/kyaml/yaml/rnode.go index ed69fe992d..07bb8a0e1c 100644 --- a/kyaml/yaml/rnode.go +++ b/kyaml/yaml/rnode.go @@ -15,6 +15,7 @@ import ( "sigs.k8s.io/kustomize/kyaml/errors" "sigs.k8s.io/kustomize/kyaml/internal/forked/github.com/go-yaml/yaml" "sigs.k8s.io/kustomize/kyaml/sliceutil" + "sigs.k8s.io/kustomize/kyaml/utils" "sigs.k8s.io/kustomize/kyaml/yaml/internal/k8sgen/pkg/labels" ) @@ -1207,7 +1208,7 @@ func (rn *RNode) GetString(path string) (string, error) { // assuming we keep the var feature in kustomize. // The other end of this is: refvar.go:updateNodeValue. func (rn *RNode) GetFieldValue(path string) (interface{}, error) { - fields := convertSliceIndex(strings.Split(path, ".")) + fields := convertSliceIndex(utils.SmarterPathSplitter(path, ".")) rn, err := rn.Pipe(Lookup(fields...)) if err != nil { return nil, err diff --git a/kyaml/yaml/rnode_test.go b/kyaml/yaml/rnode_test.go index bb93987341..5e490f5328 100644 --- a/kyaml/yaml/rnode_test.go +++ b/kyaml/yaml/rnode_test.go @@ -2310,8 +2310,6 @@ func TestGetAnnotations(t *testing.T) { } func TestGetFieldValueWithDot(t *testing.T) { - t.Skip() - const input = ` kind: Pod metadata: @@ -2330,8 +2328,6 @@ metadata: require.NoError(t, err) require.Equal(t, "hello-world-app", app) - // TODO: doesn't currently work; we expect to be able to escape the dot in future - // https://github.com/kubernetes-sigs/kustomize/issues/4487 fooAppName, err := labelRNode.GetFieldValue(`foo\.appname`) require.NoError(t, err) require.Equal(t, "hello-world-foo", fooAppName) // no field named 'foo.appname'