@@ -294,6 +294,114 @@ func Test_IsLocalRelativePath(t *testing.T) {
294294 }
295295}
296296
297+ func Test_IgnoreMissingComponents (t * testing.T ) {
298+ tests := []struct {
299+ name string
300+ components []string
301+ ignoreMissingComponents bool
302+ expectError bool
303+ expectedComponents []string
304+ }{
305+ {
306+ name : "missing components with ignore enabled" ,
307+ components : []string {"existing-component" , "missing-component" },
308+ ignoreMissingComponents : true ,
309+ expectError : false ,
310+ expectedComponents : []string {"existing-component" },
311+ },
312+ {
313+ name : "all components missing with ignore enabled" ,
314+ components : []string {"missing-component-1" , "missing-component-2" },
315+ ignoreMissingComponents : true ,
316+ expectError : false ,
317+ expectedComponents : nil ,
318+ },
319+ {
320+ name : "all components exist" ,
321+ components : []string {"existing-component" },
322+ ignoreMissingComponents : true ,
323+ expectError : false ,
324+ expectedComponents : []string {"existing-component" },
325+ },
326+ {
327+ name : "missing components with ignore disabled - should fail during build" ,
328+ components : []string {"existing-component" , "missing-component" },
329+ ignoreMissingComponents : false ,
330+ expectError : true ,
331+ expectedComponents : []string {"existing-component" , "missing-component" },
332+ },
333+ }
334+
335+ for _ , tt := range tests {
336+ t .Run (tt .name , func (t * testing.T ) {
337+ g := NewWithT (t )
338+ baseDir , err := testTempDir (t )
339+ g .Expect (err ).ToNot (HaveOccurred ())
340+
341+ // Create an existing component directory in the base directory
342+ existingComponentDir := filepath .Join (baseDir , "existing-component" )
343+ g .Expect (os .MkdirAll (existingComponentDir , 0755 )).To (Succeed ())
344+ componentYaml := `apiVersion: kustomize.config.k8s.io/v1alpha1
345+ kind: Component
346+ resources: []
347+ `
348+ g .Expect (os .WriteFile (filepath .Join (existingComponentDir , "kustomization.yaml" ), []byte (componentYaml ), 0644 )).To (Succeed ())
349+
350+ // Create overlay directory where kustomization.yaml will be generated
351+ overlayDir := filepath .Join (baseDir , "overlay" )
352+ g .Expect (os .MkdirAll (overlayDir , 0755 )).To (Succeed ())
353+
354+ // Update component paths to be relative from overlay directory
355+ var overlayComponents []string
356+ for _ , comp := range tt .components {
357+ overlayComponents = append (overlayComponents , "../" + comp )
358+ }
359+
360+ // Update expected components to match the relative paths
361+ var expectedOverlayComponents []any
362+ if tt .expectedComponents != nil {
363+ for _ , comp := range tt .expectedComponents {
364+ expectedOverlayComponents = append (expectedOverlayComponents , "../" + comp )
365+ }
366+ }
367+
368+ // Create Flux Kustomization object with relative paths
369+ ks := unstructured.Unstructured {Object : map [string ]any {}}
370+ err = unstructured .SetNestedStringSlice (ks .Object , overlayComponents , "spec" , "components" )
371+ g .Expect (err ).ToNot (HaveOccurred ())
372+ err = unstructured .SetNestedField (ks .Object , tt .ignoreMissingComponents , "spec" , "ignoreMissingComponents" )
373+ g .Expect (err ).ToNot (HaveOccurred ())
374+
375+ // Generate kustomization in the overlay directory
376+ _ , err = kustomize .NewGenerator (overlayDir , ks ).WriteFile (overlayDir )
377+ g .Expect (err ).ToNot (HaveOccurred ())
378+
379+ // Read generated kustomization.yaml
380+ kfileYAML , err := os .ReadFile (filepath .Join (overlayDir , "kustomization.yaml" ))
381+ g .Expect (err ).ToNot (HaveOccurred ())
382+ var k any
383+ g .Expect (yaml .Unmarshal (kfileYAML , & k )).To (Succeed ())
384+
385+ // Check components field against the expected overlay components
386+ components := k .(map [string ]any )["components" ]
387+ if expectedOverlayComponents == nil {
388+ g .Expect (components ).To (BeNil ())
389+ } else {
390+ g .Expect (components ).To (Equal (expectedOverlayComponents ))
391+ }
392+
393+ // Build the kustomization
394+ _ , buildErr := kustomize .SecureBuild (baseDir , overlayDir , false )
395+ if tt .expectError {
396+ g .Expect (buildErr ).To (HaveOccurred ())
397+ g .Expect (buildErr .Error ()).To (ContainSubstring ("missing-component" ))
398+ } else {
399+ g .Expect (buildErr ).ToNot (HaveOccurred ())
400+ }
401+ })
402+ }
403+ }
404+
297405func testTempDir (t * testing.T ) (string , error ) {
298406 tmpDir := t .TempDir ()
299407
0 commit comments