Skip to content

Commit 6081556

Browse files
authored
Merge pull request #761 from fluxcd/kustomize-name-prefix-suffix
kustomize: Add support for `namePrefix` and `nameSuffix`
2 parents 98d2522 + abf5675 commit 6081556

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed

kustomize/kustomize_generator.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ const (
5252
patchesSMField = "patchesStrategicMerge"
5353
patchesJson6902Field = "patchesJson6902"
5454
imagesField = "images"
55+
namePrefixField = "namePrefix"
56+
nameSuffixField = "nameSuffix"
5557
)
5658

5759
// Action is the action that was taken on the kustomization file
@@ -173,6 +175,24 @@ func (g *Generator) WriteFile(dirPath string, opts ...SavingOptions) (Action, er
173175
kus.Namespace = tg
174176
}
175177

178+
nprefix, ok, err := g.getNestedString(specField, namePrefixField)
179+
if err != nil {
180+
errf := CleanDirectory(dirPath, action)
181+
return action, fmt.Errorf("%v %v", err, errf)
182+
}
183+
if ok {
184+
kus.NamePrefix = nprefix
185+
}
186+
187+
nsuffix, ok, err := g.getNestedString(specField, nameSuffixField)
188+
if err != nil {
189+
errf := CleanDirectory(dirPath, action)
190+
return action, fmt.Errorf("%v %v", err, errf)
191+
}
192+
if ok {
193+
kus.NameSuffix = nsuffix
194+
}
195+
176196
patches, err := g.getPatches()
177197
if err != nil {
178198
errf := CleanDirectory(dirPath, action)

kustomize/kustomize_generator_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,27 @@ func TestGenerator_NoResources(t *testing.T) {
8282
g.Expect(string(data)).To(ContainSubstring("originAnnotations"))
8383
}
8484

85+
func TestGenerator_NameTransformer(t *testing.T) {
86+
g := NewWithT(t)
87+
dataKS, err := os.ReadFile("./testdata/name/ks.yaml")
88+
g.Expect(err).NotTo(HaveOccurred())
89+
90+
ks, err := readYamlObjects(strings.NewReader(string(dataKS)))
91+
g.Expect(err).NotTo(HaveOccurred())
92+
93+
tmpDir, err := testTempDir(t)
94+
g.Expect(err).ToNot(HaveOccurred())
95+
g.Expect(copy.Copy("testdata/name", tmpDir)).To(Succeed())
96+
_, err = kustomize.NewGenerator(tmpDir, ks[0]).WriteFile(tmpDir)
97+
g.Expect(err).NotTo(HaveOccurred())
98+
99+
resMap, err := kustomize.SecureBuild(tmpDir, tmpDir, false)
100+
g.Expect(err).NotTo(HaveOccurred())
101+
g.Expect(resMap.Resources()).To(HaveLen(1))
102+
g.Expect(resMap.Resources()[0].GetName()).To(ContainSubstring("prefix-test-configmap-suffix"))
103+
g.Expect(resMap.Resources()[0].GetNamespace()).To(Equal("test-namespace"))
104+
}
105+
85106
func TestKustomizationGenerator(t *testing.T) {
86107
tests := []struct {
87108
name string

kustomize/testdata/name/ks.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
apiVersion: kustomize.toolkit.fluxcd.io/v1
3+
kind: Kustomization
4+
metadata:
5+
name: test-name
6+
namespace: test-namespace
7+
spec:
8+
targetNamespace: test-namespace
9+
namePrefix: prefix-
10+
nameSuffix: -suffix
11+
interval: 4m0s
12+
path: ./
13+
prune: true
14+
sourceRef:
15+
kind: GitRepository
16+
name: app
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
apiVersion: kustomize.config.k8s.io/v1beta1
2+
kind: Kustomization
3+
configMapGenerator:
4+
- name: test-configmap
5+
literals:
6+
- foo=bar
7+
- baz=qux

0 commit comments

Comments
 (0)