From 12f06d181ca242af01e42aabbc275b4468a45240 Mon Sep 17 00:00:00 2001 From: Anne Lau Date: Tue, 19 Apr 2022 12:51:42 -0700 Subject: [PATCH 1/7] Added code for fields with dots(.) in their names work as needed Co-authored-by: sarjamil --- api/filters/fieldspec/fieldspec.go | 4 ++-- api/filters/replacement/replacement.go | 5 +++-- api/internal/builtins/AnnotationsTransformer.go | 5 +---- .../utils => kyaml/pathsplitterutil}/pathsplitter.go | 2 +- .../utils => kyaml/pathsplitterutil}/pathsplitter_test.go | 4 ++-- kyaml/yaml/rnode.go | 3 ++- kyaml/yaml/rnode_test.go | 2 +- 7 files changed, 12 insertions(+), 13 deletions(-) rename {api/internal/utils => kyaml/pathsplitterutil}/pathsplitter.go (98%) rename {api/internal/utils => kyaml/pathsplitterutil}/pathsplitter_test.go (96%) diff --git a/api/filters/fieldspec/fieldspec.go b/api/filters/fieldspec/fieldspec.go index 8ee0528654..16c112f379 100644 --- a/api/filters/fieldspec/fieldspec.go +++ b/api/filters/fieldspec/fieldspec.go @@ -5,10 +5,10 @@ package fieldspec import ( "fmt" + "sigs.k8s.io/kustomize/kyaml/pathsplitterutil" "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" @@ -49,7 +49,7 @@ func (fltr Filter) Filter(obj *yaml.RNode) (*yaml.RNode, error) { if match := isMatchGVK(fltr.FieldSpec, obj); !match { return obj, nil } - fltr.path = utils.PathSplitter(fltr.FieldSpec.Path, "/") + fltr.path = pathsplitterutil.PathSplitter(fltr.FieldSpec.Path, "/") if err := fltr.filter(obj); err != nil { return nil, errors.WrapPrefixf(err, "considering field '%s' of object %s", fltr.FieldSpec.Path, resid.FromRNode(obj)) diff --git a/api/filters/replacement/replacement.go b/api/filters/replacement/replacement.go index 0201905bef..2471b64871 100644 --- a/api/filters/replacement/replacement.go +++ b/api/filters/replacement/replacement.go @@ -6,6 +6,7 @@ package replacement import ( "errors" "fmt" + "sigs.k8s.io/kustomize/kyaml/pathsplitterutil" "strings" "sigs.k8s.io/kustomize/api/internal/utils" @@ -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 := pathsplitterutil.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 := pathsplitterutil.SmarterPathSplitter(r.Source.FieldPath, ".") rn, err := source.Pipe(yaml.Lookup(fieldPath...)) if err != nil { diff --git a/api/internal/builtins/AnnotationsTransformer.go b/api/internal/builtins/AnnotationsTransformer.go index c84c0d1862..7064fa80c0 100644 --- a/api/internal/builtins/AnnotationsTransformer.go +++ b/api/internal/builtins/AnnotationsTransformer.go @@ -1,8 +1,6 @@ // Code generated by pluginator on AnnotationsTransformer; DO NOT EDIT. // pluginator {unknown 1970-01-01T00:00:00Z } - - package builtins import ( @@ -18,7 +16,6 @@ type AnnotationsTransformerPlugin struct { FieldSpecs []types.FieldSpec `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"` } - func (p *AnnotationsTransformerPlugin) Config( _ *resmap.PluginHelpers, c []byte) (err error) { p.Annotations = nil @@ -37,5 +34,5 @@ func (p *AnnotationsTransformerPlugin) Transform(m resmap.ResMap) error { } func NewAnnotationsTransformerPlugin() resmap.TransformerPlugin { - return &AnnotationsTransformerPlugin{} + return &AnnotationsTransformerPlugin{} } diff --git a/api/internal/utils/pathsplitter.go b/kyaml/pathsplitterutil/pathsplitter.go similarity index 98% rename from api/internal/utils/pathsplitter.go rename to kyaml/pathsplitterutil/pathsplitter.go index aa560299f7..55c8f0fb38 100644 --- a/api/internal/utils/pathsplitter.go +++ b/kyaml/pathsplitterutil/pathsplitter.go @@ -1,7 +1,7 @@ // Copyright 2021 The Kubernetes Authors. // SPDX-License-Identifier: Apache-2.0 -package utils +package pathsplitterutil import "strings" diff --git a/api/internal/utils/pathsplitter_test.go b/kyaml/pathsplitterutil/pathsplitter_test.go similarity index 96% rename from api/internal/utils/pathsplitter_test.go rename to kyaml/pathsplitterutil/pathsplitter_test.go index 21dcc83911..1e3ae880c2 100644 --- a/api/internal/utils/pathsplitter_test.go +++ b/kyaml/pathsplitterutil/pathsplitter_test.go @@ -1,13 +1,13 @@ // Copyright 2021 The Kubernetes Authors. // SPDX-License-Identifier: Apache-2.0 -package utils_test +package pathsplitterutil_test import ( + . "sigs.k8s.io/kustomize/kyaml/pathsplitterutil" "testing" "github.com/stretchr/testify/assert" - . "sigs.k8s.io/kustomize/api/internal/utils" ) func TestPathSplitter(t *testing.T) { diff --git a/kyaml/yaml/rnode.go b/kyaml/yaml/rnode.go index ed69fe992d..83027c1f7d 100644 --- a/kyaml/yaml/rnode.go +++ b/kyaml/yaml/rnode.go @@ -9,6 +9,7 @@ import ( "io/ioutil" "log" "regexp" + "sigs.k8s.io/kustomize/kyaml/pathsplitterutil" "strconv" "strings" @@ -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 := pathsplitterutil.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..886770af3e 100644 --- a/kyaml/yaml/rnode_test.go +++ b/kyaml/yaml/rnode_test.go @@ -2310,7 +2310,7 @@ func TestGetAnnotations(t *testing.T) { } func TestGetFieldValueWithDot(t *testing.T) { - t.Skip() + //t.Skip() const input = ` kind: Pod From 32fd00700eab87e435c0295907246807cea2559f Mon Sep 17 00:00:00 2001 From: Anne Lau Date: Tue, 19 Apr 2022 13:20:53 -0700 Subject: [PATCH 2/7] Fixed imports Co-authored-by: sarjamil --- api/filters/fieldspec/fieldspec.go | 4 ++-- api/filters/replacement/replacement.go | 6 +++--- api/internal/builtins/AnnotationsTransformer.go | 5 ++++- kyaml/{pathsplitterutil => utils}/pathsplitter.go | 2 +- kyaml/{pathsplitterutil => utils}/pathsplitter_test.go | 4 ++-- kyaml/yaml/rnode.go | 4 ++-- 6 files changed, 14 insertions(+), 11 deletions(-) rename kyaml/{pathsplitterutil => utils}/pathsplitter.go (98%) rename kyaml/{pathsplitterutil => utils}/pathsplitter_test.go (96%) diff --git a/api/filters/fieldspec/fieldspec.go b/api/filters/fieldspec/fieldspec.go index 16c112f379..8e4e78ca6e 100644 --- a/api/filters/fieldspec/fieldspec.go +++ b/api/filters/fieldspec/fieldspec.go @@ -5,13 +5,13 @@ package fieldspec import ( "fmt" - "sigs.k8s.io/kustomize/kyaml/pathsplitterutil" "strings" "sigs.k8s.io/kustomize/api/filters/filtersutil" "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" ) @@ -49,7 +49,7 @@ func (fltr Filter) Filter(obj *yaml.RNode) (*yaml.RNode, error) { if match := isMatchGVK(fltr.FieldSpec, obj); !match { return obj, nil } - fltr.path = pathsplitterutil.PathSplitter(fltr.FieldSpec.Path, "/") + fltr.path = utils.PathSplitter(fltr.FieldSpec.Path, "/") if err := fltr.filter(obj); err != nil { return nil, errors.WrapPrefixf(err, "considering field '%s' of object %s", fltr.FieldSpec.Path, resid.FromRNode(obj)) diff --git a/api/filters/replacement/replacement.go b/api/filters/replacement/replacement.go index 2471b64871..c3b5c488bc 100644 --- a/api/filters/replacement/replacement.go +++ b/api/filters/replacement/replacement.go @@ -6,7 +6,7 @@ package replacement import ( "errors" "fmt" - "sigs.k8s.io/kustomize/kyaml/pathsplitterutil" + utils2 "sigs.k8s.io/kustomize/kyaml/utils" "strings" "sigs.k8s.io/kustomize/api/internal/utils" @@ -115,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 := pathsplitterutil.SmarterPathSplitter(fp, ".") + fieldPath := utils2.SmarterPathSplitter(fp, ".") var t *yaml.RNode var err error if target.Options != nil && target.Options.Create { @@ -200,7 +200,7 @@ func getReplacement(nodes []*yaml.RNode, r *types.Replacement) (*yaml.RNode, err if r.Source.FieldPath == "" { r.Source.FieldPath = types.DefaultReplacementFieldPath } - fieldPath := pathsplitterutil.SmarterPathSplitter(r.Source.FieldPath, ".") + fieldPath := utils2.SmarterPathSplitter(r.Source.FieldPath, ".") rn, err := source.Pipe(yaml.Lookup(fieldPath...)) if err != nil { diff --git a/api/internal/builtins/AnnotationsTransformer.go b/api/internal/builtins/AnnotationsTransformer.go index 7064fa80c0..c932bd6350 100644 --- a/api/internal/builtins/AnnotationsTransformer.go +++ b/api/internal/builtins/AnnotationsTransformer.go @@ -1,6 +1,8 @@ // Code generated by pluginator on AnnotationsTransformer; DO NOT EDIT. // pluginator {unknown 1970-01-01T00:00:00Z } + + package builtins import ( @@ -16,6 +18,7 @@ type AnnotationsTransformerPlugin struct { FieldSpecs []types.FieldSpec `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"` } + func (p *AnnotationsTransformerPlugin) Config( _ *resmap.PluginHelpers, c []byte) (err error) { p.Annotations = nil @@ -34,5 +37,5 @@ func (p *AnnotationsTransformerPlugin) Transform(m resmap.ResMap) error { } func NewAnnotationsTransformerPlugin() resmap.TransformerPlugin { - return &AnnotationsTransformerPlugin{} + return &AnnotationsTransformerPlugin{} } diff --git a/kyaml/pathsplitterutil/pathsplitter.go b/kyaml/utils/pathsplitter.go similarity index 98% rename from kyaml/pathsplitterutil/pathsplitter.go rename to kyaml/utils/pathsplitter.go index 55c8f0fb38..aa560299f7 100644 --- a/kyaml/pathsplitterutil/pathsplitter.go +++ b/kyaml/utils/pathsplitter.go @@ -1,7 +1,7 @@ // Copyright 2021 The Kubernetes Authors. // SPDX-License-Identifier: Apache-2.0 -package pathsplitterutil +package utils import "strings" diff --git a/kyaml/pathsplitterutil/pathsplitter_test.go b/kyaml/utils/pathsplitter_test.go similarity index 96% rename from kyaml/pathsplitterutil/pathsplitter_test.go rename to kyaml/utils/pathsplitter_test.go index 1e3ae880c2..180f3e2595 100644 --- a/kyaml/pathsplitterutil/pathsplitter_test.go +++ b/kyaml/utils/pathsplitter_test.go @@ -1,13 +1,13 @@ // Copyright 2021 The Kubernetes Authors. // SPDX-License-Identifier: Apache-2.0 -package pathsplitterutil_test +package utils_test import ( - . "sigs.k8s.io/kustomize/kyaml/pathsplitterutil" "testing" "github.com/stretchr/testify/assert" + . "sigs.k8s.io/kustomize/kyaml/utils" ) func TestPathSplitter(t *testing.T) { diff --git a/kyaml/yaml/rnode.go b/kyaml/yaml/rnode.go index 83027c1f7d..28e991fa79 100644 --- a/kyaml/yaml/rnode.go +++ b/kyaml/yaml/rnode.go @@ -9,13 +9,13 @@ import ( "io/ioutil" "log" "regexp" - "sigs.k8s.io/kustomize/kyaml/pathsplitterutil" "strconv" "strings" "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" ) @@ -1208,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 := pathsplitterutil.SmarterPathSplitter(path, ".") + fields := utils.SmarterPathSplitter(path, ".") rn, err := rn.Pipe(Lookup(fields...)) if err != nil { return nil, err From 4482fbecb9ea0b1a0e1b963ccb709da6eef11777 Mon Sep 17 00:00:00 2001 From: Anne Lau Date: Tue, 19 Apr 2022 13:21:32 -0700 Subject: [PATCH 3/7] Removed comments Co-authored-by: sarjamil --- api/internal/builtins/AnnotationsTransformer.go | 2 +- kyaml/yaml/rnode_test.go | 4 ---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/api/internal/builtins/AnnotationsTransformer.go b/api/internal/builtins/AnnotationsTransformer.go index c932bd6350..c84c0d1862 100644 --- a/api/internal/builtins/AnnotationsTransformer.go +++ b/api/internal/builtins/AnnotationsTransformer.go @@ -37,5 +37,5 @@ func (p *AnnotationsTransformerPlugin) Transform(m resmap.ResMap) error { } func NewAnnotationsTransformerPlugin() resmap.TransformerPlugin { - return &AnnotationsTransformerPlugin{} + return &AnnotationsTransformerPlugin{} } diff --git a/kyaml/yaml/rnode_test.go b/kyaml/yaml/rnode_test.go index 886770af3e..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' From 4036f2c83f3a3929c7d8bf0e29d36de4fffc7c63 Mon Sep 17 00:00:00 2001 From: Anne Lau Date: Tue, 19 Apr 2022 14:45:12 -0700 Subject: [PATCH 4/7] Fix filter method Co-authored-by: sarjamil --- kyaml/yaml/rnode.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/kyaml/yaml/rnode.go b/kyaml/yaml/rnode.go index 28e991fa79..2ce5a5a860 100644 --- a/kyaml/yaml/rnode.go +++ b/kyaml/yaml/rnode.go @@ -1206,9 +1206,12 @@ func (rn *RNode) GetString(path string) (string, error) { // rather than string, we do conversion from Nodes to Go types and back // to nodes. We should figure out how to do replacement using raw nodes, // assuming we keep the var feature in kustomize. -// The other end of this is: refvar.go:updateNodeValue. +// The other end of this is: refvar.go:updateNodeValue.ß func (rn *RNode) GetFieldValue(path string) (interface{}, error) { - fields := utils.SmarterPathSplitter(path, ".") + fields := convertSliceIndex(utils.SmarterPathSplitter(path, ".")) + fields2 := utils.SmarterPathSplitter(path, ".") + fields3 := convertSliceIndex(strings.Split(path, ".")) + fmt.Println(fields2, fields3) rn, err := rn.Pipe(Lookup(fields...)) if err != nil { return nil, err From 540adc75971a3d9c64bff9b6f1eefe4108bc3a35 Mon Sep 17 00:00:00 2001 From: Anne Lau Date: Tue, 19 Apr 2022 15:01:17 -0700 Subject: [PATCH 5/7] Fixed failing tests Co-authored-by: sarjamil --- api/filters/replacement/replacement.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/api/filters/replacement/replacement.go b/api/filters/replacement/replacement.go index c3b5c488bc..5f4667a9c6 100644 --- a/api/filters/replacement/replacement.go +++ b/api/filters/replacement/replacement.go @@ -6,13 +6,13 @@ package replacement import ( "errors" "fmt" - utils2 "sigs.k8s.io/kustomize/kyaml/utils" "strings" "sigs.k8s.io/kustomize/api/internal/utils" "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" ) @@ -115,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 := utils2.SmarterPathSplitter(fp, ".") + fieldPath := kyaml_utils.SmarterPathSplitter(fp, ".") var t *yaml.RNode var err error if target.Options != nil && target.Options.Create { @@ -200,7 +200,7 @@ func getReplacement(nodes []*yaml.RNode, r *types.Replacement) (*yaml.RNode, err if r.Source.FieldPath == "" { r.Source.FieldPath = types.DefaultReplacementFieldPath } - fieldPath := utils2.SmarterPathSplitter(r.Source.FieldPath, ".") + fieldPath := kyaml_utils.SmarterPathSplitter(r.Source.FieldPath, ".") rn, err := source.Pipe(yaml.Lookup(fieldPath...)) if err != nil { From a793066e73524ced3101551178c377ef47fe62cc Mon Sep 17 00:00:00 2001 From: Anne Lau Date: Tue, 19 Apr 2022 15:06:54 -0700 Subject: [PATCH 6/7] Remove comments Add co-author to PR. Co-authored-by: sarjamil --- kyaml/yaml/rnode.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/kyaml/yaml/rnode.go b/kyaml/yaml/rnode.go index 2ce5a5a860..07bb8a0e1c 100644 --- a/kyaml/yaml/rnode.go +++ b/kyaml/yaml/rnode.go @@ -1206,12 +1206,9 @@ func (rn *RNode) GetString(path string) (string, error) { // rather than string, we do conversion from Nodes to Go types and back // to nodes. We should figure out how to do replacement using raw nodes, // assuming we keep the var feature in kustomize. -// The other end of this is: refvar.go:updateNodeValue.ß +// The other end of this is: refvar.go:updateNodeValue. func (rn *RNode) GetFieldValue(path string) (interface{}, error) { fields := convertSliceIndex(utils.SmarterPathSplitter(path, ".")) - fields2 := utils.SmarterPathSplitter(path, ".") - fields3 := convertSliceIndex(strings.Split(path, ".")) - fmt.Println(fields2, fields3) rn, err := rn.Pipe(Lookup(fields...)) if err != nil { return nil, err From 6bc046954bc9c2090e1ee4b3bb8a58abe661dfaf Mon Sep 17 00:00:00 2001 From: Anne Lau Date: Tue, 19 Apr 2022 16:27:35 -0700 Subject: [PATCH 7/7] Add co-author to PR. Co-authored-by: sarjamil sjamil@salesforce.com