@@ -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+
246321func Test_IsLocalRelativePath (t * testing.T ) {
247322 tests := []struct {
248323 path string
0 commit comments