From a40c698b56021cbd85f633d1ce9f325383d41b25 Mon Sep 17 00:00:00 2001 From: yutaroyamanaka Date: Fri, 17 Mar 2023 21:01:25 +0900 Subject: [PATCH 1/4] return copied Node --- kyaml/yaml/rnode.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/kyaml/yaml/rnode.go b/kyaml/yaml/rnode.go index fc28ba7712..266b4153f8 100644 --- a/kyaml/yaml/rnode.go +++ b/kyaml/yaml/rnode.go @@ -1007,7 +1007,11 @@ func deAnchor(yn *yaml.Node) (res *yaml.Node, err error) { case yaml.ScalarNode: return yn, nil case yaml.AliasNode: - return deAnchor(yn.Alias) + result, err := deAnchor(yn.Alias) + if err != nil { + return nil, err + } + return CopyYNode(result), nil case yaml.MappingNode: toMerge, err := removeMergeTags(yn) if err != nil { From 994a27a2678e8138f2494f0cb66dfa376b0bfea8 Mon Sep 17 00:00:00 2001 From: yutaroyamanaka Date: Fri, 17 Mar 2023 21:02:22 +0900 Subject: [PATCH 2/4] add a test case about imageTagTransformer for anchor scenario --- .../ImageTagTransformer_test.go | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/plugin/builtin/imagetagtransformer/ImageTagTransformer_test.go b/plugin/builtin/imagetagtransformer/ImageTagTransformer_test.go index d7fb186798..0c438948ef 100644 --- a/plugin/builtin/imagetagtransformer/ImageTagTransformer_test.go +++ b/plugin/builtin/imagetagtransformer/ImageTagTransformer_test.go @@ -409,6 +409,49 @@ spec: `) } +func TestImageTagTransformerAnchor(t *testing.T) { + th := kusttest_test.MakeEnhancedHarness(t). + PrepBuiltin("ImageTagTransformer") + defer th.Reset() + + rm := th.LoadAndRunTransformer(` +apiVersion: builtin +kind: ImageTagTransformer +metadata: + name: notImportantHere +imageTag: + name: nginx + newName: my-nginx +fieldSpecs: +- path: spec/template/spec/containers[]/image +`, ` +group: apps +apiVersion: v1 +kind: Deployment +metadata: + name: &name nginx +spec: + template: + spec: + containers: + - image: *name + name: *name +`) + th.AssertActualEqualsExpectedNoIdAnnotations(rm, ` +apiVersion: v1 +group: apps +kind: Deployment +metadata: + name: nginx +spec: + template: + spec: + containers: + - image: my-nginx + name: nginx +`) +} + func TestImageTagTransformerTagWithBraces(t *testing.T) { th := kusttest_test.MakeEnhancedHarness(t). PrepBuiltin("ImageTagTransformer") From 690d901ff4ed0c6ee5919481bebb84fac3cb5793 Mon Sep 17 00:00:00 2001 From: yutaroyamanaka Date: Fri, 31 Mar 2023 18:30:22 +0900 Subject: [PATCH 3/4] add TestPatchTransformerAnchor --- .../patchtransformer/PatchTransformer_test.go | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/plugin/builtin/patchtransformer/PatchTransformer_test.go b/plugin/builtin/patchtransformer/PatchTransformer_test.go index 9f94263d62..619d24c36c 100644 --- a/plugin/builtin/patchtransformer/PatchTransformer_test.go +++ b/plugin/builtin/patchtransformer/PatchTransformer_test.go @@ -828,3 +828,65 @@ spec: protocol: TCP `) } + +func TestPatchTransformerAnchor(t *testing.T) { + th := kusttest_test.MakeEnhancedHarness(t). + PrepBuiltin("PatchTransformer") + defer th.Reset() + + th.RunTransformerAndCheckResult(` +apiVersion: builtin +kind: PatchTransformer +metadata: + name: test-transformer +patch: |- + apiVersion: apps/v1 + kind: Deployment + metadata: + name: test-deployment + spec: + selector: + matchLabels: + app: &name test-label + template: + metadata: + labels: + app: *name +target: + kind: Deployment + name: test-deployment +`, `apiVersion: apps/v1 +kind: Deployment +metadata: + name: &name test-deployment +spec: + selector: + matchLabels: + app: *name + template: + metadata: + labels: + app: *name + spec: + containers: + - image: test-image + name: *name +`, ` +apiVersion: apps/v1 +kind: Deployment +metadata: + name: test-deployment +spec: + selector: + matchLabels: + app: test-label + template: + metadata: + labels: + app: test-label + spec: + containers: + - image: test-image + name: test-deployment +`) +} From 8499a2484081fed214404cc994838f5a3c57c9e9 Mon Sep 17 00:00:00 2001 From: yutaroyamanaka Date: Fri, 31 Mar 2023 18:32:02 +0900 Subject: [PATCH 4/4] TestReplacementTransformerAnchor --- .../ReplacementTransformer_test.go | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/plugin/builtin/replacementtransformer/ReplacementTransformer_test.go b/plugin/builtin/replacementtransformer/ReplacementTransformer_test.go index 13bb9a3699..c96343d6b4 100644 --- a/plugin/builtin/replacementtransformer/ReplacementTransformer_test.go +++ b/plugin/builtin/replacementtransformer/ReplacementTransformer_test.go @@ -174,6 +174,60 @@ spec: `) } +func TestReplacementTransformerAnchor(t *testing.T) { + th := kusttest_test.MakeEnhancedHarness(t). + PrepBuiltin("ReplacementTransformer") + defer th.Reset() + + rm := th.LoadAndRunTransformer(` +apiVersion: builtin +kind: ReplacementTransformer +metadata: + name: notImportantHere +replacements: +- source: + kind: Deployment + fieldPath: spec.template.spec.containers.0.name + targets: + - select: + kind: Deployment + fieldPaths: + - spec.template.spec.containers.1.name +`, ` +apiVersion: apps/v1 +kind: Deployment +metadata: + name: &name origin +spec: + template: + spec: + containers: + - image: foobar:1 + name: replaced + - image: foobar:1 + name: *name + - image: foobar:1 + name: *name +`) + + th.AssertActualEqualsExpected(rm, ` +apiVersion: apps/v1 +kind: Deployment +metadata: + name: origin +spec: + template: + spec: + containers: + - image: foobar:1 + name: replaced + - image: foobar:1 + name: replaced + - image: foobar:1 + name: origin +`) +} + func TestReplacementTransformerComplexType(t *testing.T) { th := kusttest_test.MakeEnhancedHarness(t). PrepBuiltin("ReplacementTransformer")