diff --git a/kustomize/kustomize_generator.go b/kustomize/kustomize_generator.go index 03dc1166..dd27a8dd 100644 --- a/kustomize/kustomize_generator.go +++ b/kustomize/kustomize_generator.go @@ -52,6 +52,8 @@ const ( patchesSMField = "patchesStrategicMerge" patchesJson6902Field = "patchesJson6902" imagesField = "images" + namePrefixField = "namePrefix" + nameSuffixField = "nameSuffix" ) // 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 kus.Namespace = tg } + nprefix, ok, err := g.getNestedString(specField, namePrefixField) + if err != nil { + errf := CleanDirectory(dirPath, action) + return action, fmt.Errorf("%v %v", err, errf) + } + if ok { + kus.NamePrefix = nprefix + } + + nsuffix, ok, err := g.getNestedString(specField, nameSuffixField) + if err != nil { + errf := CleanDirectory(dirPath, action) + return action, fmt.Errorf("%v %v", err, errf) + } + if ok { + kus.NameSuffix = nsuffix + } + patches, err := g.getPatches() if err != nil { errf := CleanDirectory(dirPath, action) diff --git a/kustomize/kustomize_generator_test.go b/kustomize/kustomize_generator_test.go index c64407c4..a4148d3f 100644 --- a/kustomize/kustomize_generator_test.go +++ b/kustomize/kustomize_generator_test.go @@ -82,6 +82,27 @@ func TestGenerator_NoResources(t *testing.T) { g.Expect(string(data)).To(ContainSubstring("originAnnotations")) } +func TestGenerator_NameTransformer(t *testing.T) { + g := NewWithT(t) + dataKS, err := os.ReadFile("./testdata/name/ks.yaml") + g.Expect(err).NotTo(HaveOccurred()) + + ks, err := readYamlObjects(strings.NewReader(string(dataKS))) + g.Expect(err).NotTo(HaveOccurred()) + + tmpDir, err := testTempDir(t) + g.Expect(err).ToNot(HaveOccurred()) + g.Expect(copy.Copy("testdata/name", tmpDir)).To(Succeed()) + _, err = kustomize.NewGenerator(tmpDir, ks[0]).WriteFile(tmpDir) + g.Expect(err).NotTo(HaveOccurred()) + + resMap, err := kustomize.SecureBuild(tmpDir, tmpDir, false) + g.Expect(err).NotTo(HaveOccurred()) + g.Expect(resMap.Resources()).To(HaveLen(1)) + g.Expect(resMap.Resources()[0].GetName()).To(ContainSubstring("prefix-test-configmap-suffix")) + g.Expect(resMap.Resources()[0].GetNamespace()).To(Equal("test-namespace")) +} + func TestKustomizationGenerator(t *testing.T) { tests := []struct { name string diff --git a/kustomize/testdata/name/ks.yaml b/kustomize/testdata/name/ks.yaml new file mode 100644 index 00000000..b98ebff6 --- /dev/null +++ b/kustomize/testdata/name/ks.yaml @@ -0,0 +1,16 @@ +--- +apiVersion: kustomize.toolkit.fluxcd.io/v1 +kind: Kustomization +metadata: + name: test-name + namespace: test-namespace +spec: + targetNamespace: test-namespace + namePrefix: prefix- + nameSuffix: -suffix + interval: 4m0s + path: ./ + prune: true + sourceRef: + kind: GitRepository + name: app diff --git a/kustomize/testdata/name/kustomization.yaml b/kustomize/testdata/name/kustomization.yaml new file mode 100644 index 00000000..6843cf49 --- /dev/null +++ b/kustomize/testdata/name/kustomization.yaml @@ -0,0 +1,7 @@ +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization +configMapGenerator: + - name: test-configmap + literals: + - foo=bar + - baz=qux