@@ -326,6 +326,77 @@ func TestSubstrateProfileArgoDiscoveryCountsExistingResources(t *testing.T) {
326326 }
327327}
328328
329+ func TestSubstrateProfileDiscoveryUsesTypedConfigNamespace (t * testing.T ) {
330+ scheme := runtime .NewScheme ()
331+ if err := kaprov1alpha1 .AddToScheme (scheme ); err != nil {
332+ t .Fatal (err )
333+ }
334+ config := typedSubstrateConfigWithSpecNamespace ("flux.substrate.kapro.io/v1alpha1" , "FluxSubstrateConfig" , "checkout" , "flux-managed" )
335+ profile := & kaprov1alpha1.Substrate {
336+ ObjectMeta : metav1.ObjectMeta {Name : "flux" },
337+ Spec : kaprov1alpha1.SubstrateSpec {
338+ ClassRef : & kaprov1alpha1.SubstrateClassReference {Name : "flux" },
339+ ConfigRef : & kaprov1alpha1.SubstrateObjectReference {
340+ APIVersion : "flux.substrate.kapro.io/v1alpha1" ,
341+ Kind : "FluxSubstrateConfig" ,
342+ Name : "checkout" ,
343+ },
344+ Discovery : & kaprov1alpha1.SubstrateDiscoverySpec {Enabled : true },
345+ Parameters : map [string ]string {"namespace" : "wrong-namespace" },
346+ },
347+ }
348+ gitRepository := & unstructured.Unstructured {}
349+ gitRepository .SetGroupVersionKind (schema.GroupVersionKind {Group : "source.toolkit.fluxcd.io" , Version : "v1" , Kind : "GitRepository" })
350+ gitRepository .SetNamespace ("flux-managed" )
351+ gitRepository .SetName ("checkout-git" )
352+
353+ r := & SubstrateReconciler {
354+ Client : fake .NewClientBuilder ().WithScheme (scheme ).WithObjects (profile , config , gitRepository ).Build (),
355+ }
356+
357+ counts , reason , _ := r .observeDiscovery (context .Background (), profile )
358+ if reason != "DiscoverySucceeded" {
359+ t .Fatalf ("reason=%s" , reason )
360+ }
361+ if counts .applications != 1 {
362+ t .Fatalf ("applications=%d, want typed config namespace object to be discovered" , counts .applications )
363+ }
364+ }
365+
366+ func TestSubstrateProfileObjectWatchUsesTypedConfigNamespace (t * testing.T ) {
367+ scheme := runtime .NewScheme ()
368+ if err := kaprov1alpha1 .AddToScheme (scheme ); err != nil {
369+ t .Fatal (err )
370+ }
371+ config := typedSubstrateConfigWithSpecNamespace ("flux.substrate.kapro.io/v1alpha1" , "FluxSubstrateConfig" , "checkout" , "flux-managed" )
372+ profile := & kaprov1alpha1.Substrate {
373+ ObjectMeta : metav1.ObjectMeta {Name : "flux" },
374+ Spec : kaprov1alpha1.SubstrateSpec {
375+ ClassRef : & kaprov1alpha1.SubstrateClassReference {Name : "flux" },
376+ ConfigRef : & kaprov1alpha1.SubstrateObjectReference {
377+ APIVersion : "flux.substrate.kapro.io/v1alpha1" ,
378+ Kind : "FluxSubstrateConfig" ,
379+ Name : "checkout" ,
380+ },
381+ Discovery : & kaprov1alpha1.SubstrateDiscoverySpec {Enabled : true },
382+ Parameters : map [string ]string {"namespace" : "wrong-namespace" },
383+ },
384+ }
385+ gitRepository := & unstructured.Unstructured {}
386+ gitRepository .SetGroupVersionKind (schema.GroupVersionKind {Group : "source.toolkit.fluxcd.io" , Version : "v1" , Kind : "GitRepository" })
387+ gitRepository .SetNamespace ("flux-managed" )
388+ gitRepository .SetName ("checkout-git" )
389+
390+ r := & SubstrateReconciler {
391+ Client : fake .NewClientBuilder ().WithScheme (scheme ).WithObjects (profile , config ).Build (),
392+ }
393+
394+ requests := r .substrateProfilesForSubstrateObject (context .Background (), gitRepository )
395+ if len (requests ) != 1 || requests [0 ].Name != "flux" {
396+ t .Fatalf ("requests=%#v, want flux profile from typed config namespace" , requests )
397+ }
398+ }
399+
329400func TestSubstrateProfileArgoDiscoveryClassifiesExistingGitOpsPatterns (t * testing.T ) {
330401 scheme := runtime .NewScheme ()
331402 if err := clientgoscheme .AddToScheme (scheme ); err != nil {
@@ -587,6 +658,14 @@ func typedSubstrateConfig(apiVersion, kind, namespace, name string) *unstructure
587658 return config
588659}
589660
661+ func typedSubstrateConfigWithSpecNamespace (apiVersion , kind , name , namespace string ) * unstructured.Unstructured {
662+ config := typedSubstrateConfig (apiVersion , kind , "" , name )
663+ if err := unstructured .SetNestedField (config .Object , namespace , "spec" , "namespace" ); err != nil {
664+ panic (err )
665+ }
666+ return config
667+ }
668+
590669func hasDiscoveryPattern (objects []kaprov1alpha1.DiscoveredSubstrateObject , pattern string ) bool {
591670 for _ , obj := range objects {
592671 if obj .Pattern == pattern {
0 commit comments