Skip to content

Commit

Permalink
Merge pull request #761 from fluxcd/kustomize-name-prefix-suffix
Browse files Browse the repository at this point in the history
kustomize: Add support for `namePrefix` and `nameSuffix`
  • Loading branch information
stefanprodan committed Apr 9, 2024
2 parents 98d2522 + abf5675 commit 6081556
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
20 changes: 20 additions & 0 deletions kustomize/kustomize_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
21 changes: 21 additions & 0 deletions kustomize/kustomize_generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions kustomize/testdata/name/ks.yaml
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions kustomize/testdata/name/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
configMapGenerator:
- name: test-configmap
literals:
- foo=bar
- baz=qux

0 comments on commit 6081556

Please sign in to comment.