Skip to content

Commit

Permalink
Merge pull request #4485 from roopeshvs/master
Browse files Browse the repository at this point in the history
Support for multiple replacements in a single file when replacing using path
  • Loading branch information
k8s-ci-robot authored Mar 18, 2022
2 parents b0d7721 + d1d578c commit 2fe0449
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 6 deletions.
25 changes: 22 additions & 3 deletions api/internal/builtins/ReplacementTransformer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions api/krusty/replacementtransformer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,67 @@ spec:
th.AssertActualEqualsExpected(m, expected)
}

func TestReplacementsFieldWithPathMultiple(t *testing.T) {
th := kusttest_test.MakeEnhancedHarness(t)
defer th.Reset()

th.WriteK(".", `
resources:
- resource.yaml
replacements:
- path: replacement.yaml
`)
th.WriteF("replacement.yaml", `
- source:
kind: Deployment
fieldPath: spec.template.spec.containers.0.image
targets:
- select:
kind: Deployment
fieldPaths:
- spec.template.spec.containers.1.image
- source:
kind: Deployment
fieldPath: spec.template.spec.containers.0.name
targets:
- select:
kind: Deployment
fieldPaths:
- spec.template.spec.containers.1.name
`)
th.WriteF("resource.yaml", `
apiVersion: apps/v1
kind: Deployment
metadata:
name: deploy
spec:
template:
spec:
containers:
- image: foobar:1
name: replaced-with-digest
- image: postgres:1.8.0
name: postgresdb
`)
expected := `
apiVersion: apps/v1
kind: Deployment
metadata:
name: deploy
spec:
template:
spec:
containers:
- image: foobar:1
name: replaced-with-digest
- image: foobar:1
name: replaced-with-digest
`
m := th.Run(".", th.MakeDefaultOptions())
th.AssertActualEqualsExpected(m, expected)
}

func TestReplacementTransformerWithDiamondShape(t *testing.T) {
th := kusttest_test.MakeEnhancedHarness(t)
defer th.Reset()
Expand Down
25 changes: 22 additions & 3 deletions plugin/builtin/replacementtransformer/ReplacementTransformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package main

import (
"fmt"
"reflect"

"sigs.k8s.io/kustomize/api/filters/replacement"
"sigs.k8s.io/kustomize/api/resmap"
Expand Down Expand Up @@ -39,11 +40,29 @@ func (p *plugin) Config(
if err != nil {
return err
}
repl := types.Replacement{}
if err := yaml.Unmarshal(content, &repl); err != nil {
// find if the path contains a a list of replacements or a single replacement
var replacement interface{}
err = yaml.Unmarshal(content, &replacement)
if err != nil {
return err
}
p.Replacements = append(p.Replacements, repl)
items := reflect.ValueOf(replacement)
switch items.Kind() {
case reflect.Slice:
repl := []types.Replacement{}
if err := yaml.Unmarshal(content, &repl); err != nil {
return err
}
p.Replacements = append(p.Replacements, repl...)
case reflect.Map:
repl := types.Replacement{}
if err := yaml.Unmarshal(content, &repl); err != nil {
return err
}
p.Replacements = append(p.Replacements, repl)
default:
return fmt.Errorf("unsupported replacement type encountered within replacement path: %v", items.Kind())
}
} else {
// replacement information is already loaded
p.Replacements = append(p.Replacements, r.Replacement)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,68 @@ spec:
`)
}

func TestReplacementTransformerFromPathMultiple(t *testing.T) {
th := kusttest_test.MakeEnhancedHarness(t).
PrepBuiltin("ReplacementTransformer")
defer th.Reset()

th.WriteF("replacement.yaml", `
- source:
kind: Deployment
fieldPath: spec.template.spec.containers.0.image
targets:
- select:
kind: Deployment
fieldPaths:
- spec.template.spec.containers.1.image
- source:
kind: Deployment
fieldPath: spec.template.spec.containers.0.name
targets:
- select:
kind: Deployment
fieldPaths:
- spec.template.spec.containers.1.name`)

rm := th.LoadAndRunTransformer(`
apiVersion: builtin
kind: ReplacementTransformer
metadata:
name: notImportantHere
replacements:
- path: replacement.yaml
`, `
apiVersion: apps/v1
kind: Deployment
metadata:
name: deploy
spec:
template:
spec:
containers:
- image: foobar:1
name: replaced-with-digest
- image: postgres:1.8.0
name: postgresdb
`)

th.AssertActualEqualsExpected(rm, `
apiVersion: apps/v1
kind: Deployment
metadata:
name: deploy
spec:
template:
spec:
containers:
- image: foobar:1
name: replaced-with-digest
- image: foobar:1
name: replaced-with-digest
`)
}

func TestReplacementTransformerComplexType(t *testing.T) {
th := kusttest_test.MakeEnhancedHarness(t).
PrepBuiltin("ReplacementTransformer")
Expand Down

0 comments on commit 2fe0449

Please sign in to comment.