Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prevent all uses of YAML aliases from being overwritten by a transformer #5096

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion kyaml/yaml/rnode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
43 changes: 43 additions & 0 deletions plugin/builtin/imagetagtransformer/ImageTagTransformer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
62 changes: 62 additions & 0 deletions plugin/builtin/patchtransformer/PatchTransformer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
`)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down