Skip to content

Commit 515925b

Browse files
Pierre-Gilles Mialonpmialon
authored andcommitted
Add support for buildMetadata in the kustomize generator
Read the buildMetadata field from the Flux Kustomization spec and pass it through to the generated kustomization.yaml. This allows users to enable originAnnotations and transformerAnnotations per Kustomization without modifying the source repository. Ref: fluxcd/kustomize-controller#1579 Signed-off-by: Pierre-Gilles Mialon <pmialon@gmail.com>
1 parent 978262e commit 515925b

4 files changed

Lines changed: 108 additions & 0 deletions

File tree

kustomize/kustomize_generator.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ const (
5656
imagesField = "images"
5757
namePrefixField = "namePrefix"
5858
nameSuffixField = "nameSuffix"
59+
buildMetadataField = "buildMetadata"
5960
)
6061

6162
// Action is the action that was taken on the kustomization file
@@ -296,6 +297,15 @@ func (g *Generator) WriteFile(dirPath string, opts ...SavingOptions) (Action, er
296297
}
297298
}
298299

300+
buildMetadata, _, err := g.getNestedStringSlice(specField, buildMetadataField)
301+
if err != nil {
302+
errf := CleanDirectory(dirPath, action)
303+
return action, fmt.Errorf("unable to get buildMetadata: %w", fmt.Errorf("%v %v", err, errf))
304+
}
305+
if len(buildMetadata) > 0 {
306+
kus.BuildMetadata = buildMetadata
307+
}
308+
299309
manifest, err := yaml.Marshal(kus)
300310
if err != nil {
301311
errf := CleanDirectory(dirPath, action)

kustomize/kustomize_generator_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,81 @@ func Test_Components(t *testing.T) {
243243
}
244244
}
245245

246+
func TestGenerator_BuildMetadata(t *testing.T) {
247+
g := NewWithT(t)
248+
dataKS, err := os.ReadFile("./testdata/buildMetadata/ks.yaml")
249+
g.Expect(err).NotTo(HaveOccurred())
250+
251+
ks, err := readYamlObjects(strings.NewReader(string(dataKS)))
252+
g.Expect(err).NotTo(HaveOccurred())
253+
254+
tmpDir, err := testTempDir(t)
255+
g.Expect(err).ToNot(HaveOccurred())
256+
g.Expect(copy.Copy("testdata/buildMetadata", tmpDir)).To(Succeed())
257+
_, err = kustomize.NewGenerator(tmpDir, ks[0]).WriteFile(tmpDir)
258+
g.Expect(err).NotTo(HaveOccurred())
259+
260+
// Read the generated kustomization.yaml and verify buildMetadata is set
261+
kfileYAML, err := os.ReadFile(filepath.Join(tmpDir, "kustomization.yaml"))
262+
g.Expect(err).NotTo(HaveOccurred())
263+
264+
var kus kustypes.Kustomization
265+
g.Expect(yaml.Unmarshal(kfileYAML, &kus)).To(Succeed())
266+
g.Expect(kus.BuildMetadata).To(Equal([]string{"originAnnotations", "transformerAnnotations"}))
267+
268+
// Verify that the build succeeds with buildMetadata set
269+
resMap, err := kustomize.SecureBuild(tmpDir, tmpDir, false)
270+
g.Expect(err).NotTo(HaveOccurred())
271+
g.Expect(resMap.Resources()).To(HaveLen(1))
272+
}
273+
274+
func TestGenerator_BuildMetadata_EmptySpec(t *testing.T) {
275+
g := NewWithT(t)
276+
277+
// Flux Kustomization with no buildMetadata should not override defaults
278+
ks := unstructured.Unstructured{Object: map[string]any{}}
279+
280+
tmpDir, err := testTempDir(t)
281+
g.Expect(err).ToNot(HaveOccurred())
282+
g.Expect(copy.Copy("testdata/buildMetadata", tmpDir)).To(Succeed())
283+
_, err = kustomize.NewGenerator(tmpDir, ks).WriteFile(tmpDir)
284+
g.Expect(err).NotTo(HaveOccurred())
285+
286+
kfileYAML, err := os.ReadFile(filepath.Join(tmpDir, "kustomization.yaml"))
287+
g.Expect(err).NotTo(HaveOccurred())
288+
289+
var kus kustypes.Kustomization
290+
g.Expect(yaml.Unmarshal(kfileYAML, &kus)).To(Succeed())
291+
// The kustomization.yaml has no .resources field (only configMapGenerator),
292+
// so the fallback "originAnnotations" is set to avoid empty build errors
293+
g.Expect(kus.BuildMetadata).To(Equal([]string{"originAnnotations"}))
294+
}
295+
296+
func TestGenerator_BuildMetadata_NoResources(t *testing.T) {
297+
g := NewWithT(t)
298+
299+
// Flux Kustomization with buildMetadata but no resources in the kustomization.yaml
300+
// The spec buildMetadata should override the fallback "originAnnotations"
301+
ks := unstructured.Unstructured{Object: map[string]any{}}
302+
err := unstructured.SetNestedStringSlice(ks.Object,
303+
[]string{"transformerAnnotations"}, "spec", "buildMetadata")
304+
g.Expect(err).ToNot(HaveOccurred())
305+
306+
tmpDir, err := testTempDir(t)
307+
g.Expect(err).ToNot(HaveOccurred())
308+
g.Expect(copy.Copy("testdata/noResources", tmpDir)).To(Succeed())
309+
_, err = kustomize.NewGenerator(tmpDir, ks).WriteFile(tmpDir)
310+
g.Expect(err).NotTo(HaveOccurred())
311+
312+
kfileYAML, err := os.ReadFile(filepath.Join(tmpDir, "kustomization.yaml"))
313+
g.Expect(err).NotTo(HaveOccurred())
314+
315+
var kus kustypes.Kustomization
316+
g.Expect(yaml.Unmarshal(kfileYAML, &kus)).To(Succeed())
317+
// User-specified buildMetadata should override the fallback
318+
g.Expect(kus.BuildMetadata).To(Equal([]string{"transformerAnnotations"}))
319+
}
320+
246321
func Test_IsLocalRelativePath(t *testing.T) {
247322
tests := []struct {
248323
path string
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-buildmetadata
6+
namespace: test-namespace
7+
spec:
8+
buildMetadata:
9+
- originAnnotations
10+
- transformerAnnotations
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)