Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kustomize: Add support for namePrefix and nameSuffix #761

Merged
merged 1 commit into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading