Skip to content

Commit

Permalink
Merge pull request #1231 from arnodel/fix-1228
Browse files Browse the repository at this point in the history
Iterate over fieldspecs for name tranformations (fixes #1228)
  • Loading branch information
monopole committed Jun 23, 2019
2 parents 2da2006 + d3f5069 commit 694c868
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 50 deletions.
42 changes: 17 additions & 25 deletions plugin/builtin/PrefixSuffixTransformer.go

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

42 changes: 17 additions & 25 deletions plugin/builtin/prefixsuffixtransformer/PrefixSuffixTransformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,23 @@ func (p *plugin) Transform(m resmap.ResMap) error {
if p.shouldSkip(r.OrgId()) {
continue
}
fs, ok := p.shouldInclude(r.OrgId())
if !ok {
continue
}
if smellsLikeANameChange(fs) {
r.AddNamePrefix(p.Prefix)
r.AddNameSuffix(p.Suffix)
}
err := transformers.MutateField(
r.Map(),
fs.PathSlice(),
fs.CreateIfNotPresent,
p.addPrefixSuffix)
if err != nil {
return err
id := r.OrgId()
for _, path := range p.FieldSpecs {
if !id.IsSelected(&path.Gvk) {
continue
}
if smellsLikeANameChange(&path) {
r.AddNamePrefix(p.Prefix)
r.AddNameSuffix(p.Suffix)
}
err := transformers.MutateField(
r.Map(),
path.PathSlice(),
path.CreateIfNotPresent,
p.addPrefixSuffix)
if err != nil {
return err
}
}
}
return nil
Expand All @@ -81,16 +83,6 @@ func smellsLikeANameChange(fs *config.FieldSpec) bool {
return fs.Path == "metadata/name"
}

func (p *plugin) shouldInclude(
id resid.ResId) (*config.FieldSpec, bool) {
for _, path := range p.FieldSpecs {
if id.IsSelected(&path.Gvk) {
return &path, true
}
}
return nil, false
}

func (p *plugin) shouldSkip(
id resid.ResId) bool {
for _, path := range prefixSuffixFieldSpecsToSkip {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,61 @@ kind: ConfigMap
metadata:
name: baked-cm-pie
`)

rm = th.LoadAndRunTransformer(`
apiVersion: builtin
kind: PrefixSuffixTransformer
metadata:
name: notImportantHere
prefix: test-
fieldSpecs:
- kind: Deployment
path: metadata/name
- kind: Deployment
path: spec/template/spec/containers/name
`, `
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment
spec:
template:
spec:
containers:
- image: myapp
name: main
---
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: crd
---
apiVersion: v1
kind: ConfigMap
metadata:
name: cm
`)

th.AssertActualEqualsExpected(rm, `
apiVersion: apps/v1
kind: Deployment
metadata:
name: test-deployment
spec:
template:
spec:
containers:
- image: myapp
name: test-main
---
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: crd
---
apiVersion: v1
kind: ConfigMap
metadata:
name: cm
`)
}

0 comments on commit 694c868

Please sign in to comment.