Manage name changes (prefix/suffix) via YAML annotations rather than via in-memory-only fields.

This commit is contained in:
Natasha Sarkar
2021-01-11 13:06:21 -08:00
parent ea5d08bac5
commit bd4580d73a
34 changed files with 539 additions and 61 deletions

View File

@@ -32,6 +32,7 @@ func (p *plugin) Transform(m resmap.ResMap) error {
if err != nil {
return err
}
res.SetOriginalName(res.GetName(), false)
res.SetName(fmt.Sprintf("%s-%s", res.GetName(), h))
}
}

View File

@@ -53,7 +53,7 @@ spec:
image: nginx:1.7.9
`)
th.AssertActualEqualsExpected(rm, `
th.AssertActualEqualsExpectedNoIdAnnotations(rm, `
apiVersion: v1
kind: ConfigMap
metadata:

View File

@@ -52,7 +52,7 @@ spec:
name: init-alpine
`)
th.AssertActualEqualsExpected(rm, `
th.AssertActualEqualsExpectedNoIdAnnotations(rm, `
apiVersion: v1
group: apps
kind: Deployment
@@ -122,7 +122,7 @@ spec:
name: init-alpine
`)
th.AssertActualEqualsExpected(rm, `
th.AssertActualEqualsExpectedNoIdAnnotations(rm, `
apiVersion: v1
group: apps
kind: Deployment
@@ -194,7 +194,7 @@ spec:
name: init-alpine
`)
th.AssertActualEqualsExpected(rm, `
th.AssertActualEqualsExpectedNoIdAnnotations(rm, `
apiVersion: v1
group: apps
kind: Deployment
@@ -265,7 +265,7 @@ spec:
name: init-alpine
`)
th.AssertActualEqualsExpected(rm, `
th.AssertActualEqualsExpectedNoIdAnnotations(rm, `
apiVersion: v1
group: apps
kind: Deployment
@@ -337,7 +337,7 @@ spec:
name: init-alpine
`)
th.AssertActualEqualsExpected(rm, `
th.AssertActualEqualsExpectedNoIdAnnotations(rm, `
apiVersion: v1
group: apps
kind: Deployment
@@ -395,7 +395,7 @@ spec:
containers:
initContainers:
`)
th.AssertActualEqualsExpected(rm, `
th.AssertActualEqualsExpectedNoIdAnnotations(rm, `
apiVersion: v1
group: apps
kind: Deployment
@@ -438,7 +438,7 @@ spec:
name: my-image
`)
th.AssertActualEqualsExpected(rm, `
th.AssertActualEqualsExpectedNoIdAnnotations(rm, `
apiVersion: v1
group: apps
kind: Deployment
@@ -480,7 +480,7 @@ spec:
name: my-image
`)
th.AssertActualEqualsExpected(rm, `
th.AssertActualEqualsExpectedNoIdAnnotations(rm, `
apiVersion: v1
group: apps
kind: Deployment

View File

@@ -65,7 +65,7 @@ spec:
image: nginx
`)
th.AssertActualEqualsExpected(rm, `
th.AssertActualEqualsExpectedNoIdAnnotations(rm, `
apiVersion: v1
kind: Service
metadata:

View File

@@ -65,7 +65,7 @@ metadata:
name: apricot
`)
th.AssertActualEqualsExpected(rm, `
th.AssertActualEqualsExpectedNoIdAnnotations(rm, `
apiVersion: v1
kind: Namespace
metadata:

View File

@@ -38,6 +38,7 @@ func (p *plugin) Transform(m resmap.ResMap) error {
// Don't mutate empty objects?
continue
}
r.SetOriginalNs(r.GetNamespace(), false)
err := r.ApplyFilter(namespace.Filter{
Namespace: p.Namespace,
FsSlice: p.FieldSpecs,

View File

@@ -603,7 +603,7 @@ spec:
B: Y
`)
assert.NoError(t, err)
th.AssertActualEqualsExpected(
th.AssertActualEqualsExpectedNoIdAnnotations(
resMap,
// In kyaml/yaml.merge2, the empty "B: " is dropped
// when patch1 and patch2 are merged, so the patch
@@ -652,7 +652,7 @@ spec:
B: Y
`)
assert.NoError(t, err)
th.AssertActualEqualsExpected(
th.AssertActualEqualsExpectedNoIdAnnotations(
resMap,
// This time only patch2 was applied. Same answer on the kyaml
// path, but different answer on apimachinery path (B becomes "true"?)
@@ -1388,7 +1388,7 @@ paths:
- patch.yaml
`, target)
th.AssertActualEqualsExpected(rm, `
th.AssertActualEqualsExpectedNoIdAnnotations(rm, `
apiVersion: apps/v1
kind: Deployment
metadata:
@@ -1430,7 +1430,7 @@ paths:
- patch.yaml
`, target)
th.AssertActualEqualsExpected(rm, `
th.AssertActualEqualsExpectedNoIdAnnotations(rm, `
apiVersion: apps/v1
kind: Deployment
metadata:

View File

@@ -108,6 +108,7 @@ func (p *plugin) transformJson6902(m resmap.ResMap, patch jsonpatch.Patch) error
return err
}
for _, res := range resources {
res.SetOriginalName(res.GetName(), false)
err = res.ApplyFilter(patchjson6902.Filter{
Patch: p.Patch,
})

View File

@@ -306,7 +306,7 @@ path: patch.yaml
target:
name: myDeploy
`, someDeploymentResources)
th.AssertActualEqualsExpected(rm, `
th.AssertActualEqualsExpectedNoIdAnnotations(rm, `
apiVersion: apps/v1
kind: Deployment
metadata:

View File

@@ -6,7 +6,6 @@ package main
import (
"errors"
"sigs.k8s.io/kustomize/api/filters/prefixsuffix"
"sigs.k8s.io/kustomize/api/resid"
"sigs.k8s.io/kustomize/api/resmap"
@@ -70,8 +69,12 @@ func (p *plugin) Transform(m resmap.ResMap) error {
// this will add a prefix and a suffix
// to the resource even if those are
// empty
r.AddNamePrefix(p.Prefix)
r.AddNameSuffix(p.Suffix)
if p.Prefix != "" || p.Suffix != "" {
r.SetOriginalName(r.GetName(), false)
}
}
err := r.ApplyFilter(prefixsuffix.Filter{
Prefix: p.Prefix,

View File

@@ -62,6 +62,10 @@ metadata:
apiVersion: v1
kind: Service
metadata:
annotations:
config.kubernetes.io/originalName: apple
config.kubernetes.io/prefixes: baked-
config.kubernetes.io/suffixes: -pie
name: baked-apple-pie
spec:
ports:
@@ -80,6 +84,10 @@ metadata:
apiVersion: v1
kind: ConfigMap
metadata:
annotations:
config.kubernetes.io/originalName: cm
config.kubernetes.io/prefixes: baked-
config.kubernetes.io/suffixes: -pie
name: baked-cm-pie
`)
@@ -126,6 +134,10 @@ metadata:
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
config.kubernetes.io/originalName: deployment
config.kubernetes.io/prefixes: test-
config.kubernetes.io/suffixes: null
name: test-deployment
spec:
template:

View File

@@ -91,7 +91,7 @@ spec:
app: app
`)
th.AssertActualEqualsExpected(rm, `
th.AssertActualEqualsExpectedNoIdAnnotations(rm, `
apiVersion: apps/v1
kind: Service
metadata:
@@ -180,7 +180,7 @@ fieldSpecs:
- path: spec/replicas
create: true
kind: Deployment`, rm)
th.AssertActualEqualsExpected(rm, `
th.AssertActualEqualsExpectedNoIdAnnotations(rm, `
apiVersion: apps/v1
kind: Deployment
metadata:

View File

@@ -26,7 +26,7 @@ metadata:
name: meatball
`)
th.AssertActualEqualsExpected(m, `
th.AssertActualEqualsExpectedNoIdAnnotations(m, `
apiVersion: apps/v1
kind: MeatBall
metadata:

View File

@@ -36,7 +36,7 @@ fruit: $FRUIT
vegetable: $VEGGIE
`)
th.AssertActualEqualsExpected(rm, `
th.AssertActualEqualsExpectedNoIdAnnotations(rm, `
apiVersion: apps/v1
beans: two two two two
fruit: orange

View File

@@ -27,7 +27,7 @@ metadata:
name: meatball
`)
th.AssertActualEqualsExpected(m, `
th.AssertActualEqualsExpectedNoIdAnnotations(m, `
apiVersion: apps/v1
kind: MeatBall
metadata: