mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-13 01:50:55 +00:00
Merge pull request #1231 from arnodel/fix-1228
Iterate over fieldspecs for name tranformations (fixes #1228)
This commit is contained in:
@@ -56,21 +56,23 @@ func (p *PrefixSuffixTransformerPlugin) 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
|
||||
@@ -80,16 +82,6 @@ func smellsLikeANameChange(fs *config.FieldSpec) bool {
|
||||
return fs.Path == "metadata/name"
|
||||
}
|
||||
|
||||
func (p *PrefixSuffixTransformerPlugin) 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 *PrefixSuffixTransformerPlugin) shouldSkip(
|
||||
id resid.ResId) bool {
|
||||
for _, path := range prefixSuffixFieldSpecsToSkip {
|
||||
|
||||
@@ -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
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
`)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user