@@ -70,6 +70,11 @@ func (r *reconciler) reconcile(ctx context.Context, cl client.Client, cache cach
7070 return fmt .Errorf ("failed to ensure exports: %w" , err )
7171 }
7272
73+ if err := r .ensureAPIServiceNamespaces (ctx , cl , cache , req ); err != nil {
74+ conditions .SetSummary (req )
75+ return fmt .Errorf ("failed to ensure APIServiceNamespaces: %w" , err )
76+ }
77+
7378 // TODO(mjudeikis): we could potentially add finallizer to APIServiceExport above or "adopt" boundschemas
7479 // with owner references once export is created.
7580 // https://github.com/kube-bind/kube-bind/issues/297
@@ -367,3 +372,34 @@ func isClaimableAPI(claim kubebindv1alpha2.PermissionClaim) bool {
367372 }
368373 return false
369374}
375+
376+ func (r * reconciler ) ensureAPIServiceNamespaces (ctx context.Context , cl client.Client , cache cache.Cache , req * kubebindv1alpha2.APIServiceExportRequest ) error {
377+ logger := klog .FromContext (ctx )
378+
379+ // TODO(mjudeikis): We have this object above already, pass it down to avoid extra get.
380+ export := & kubebindv1alpha2.APIServiceExport {}
381+ if err := cl .Get (ctx , client.ObjectKey {Namespace : req .Namespace , Name : req .Name }, export ); err != nil {
382+ return fmt .Errorf ("failed to get APIServiceExport %s/%s: %w" , req .Namespace , req .Name , err )
383+ }
384+
385+ for _ , ns := range req .Spec .Namespaces {
386+ apiServiceNamespace := helpers .APIServiceNamespaceFromExport (export , ns .Name )
387+ currentAPIServiceNamespace := & kubebindv1alpha2.APIServiceNamespace {}
388+ err := cache .Get (ctx , client.ObjectKey {Namespace : apiServiceNamespace .Namespace , Name : apiServiceNamespace .Name }, currentAPIServiceNamespace )
389+ if err != nil {
390+ if apierrors .IsNotFound (err ) {
391+ logger .V (1 ).Info ("Creating APIServiceNamespace" , "name" , apiServiceNamespace .Name , "namespace" , apiServiceNamespace .Namespace )
392+ if err := cl .Create (ctx , apiServiceNamespace ); err != nil {
393+ if apierrors .IsAlreadyExists (err ) {
394+ continue
395+ }
396+ return err
397+ }
398+ } else {
399+ return err
400+ }
401+ }
402+ }
403+
404+ return nil
405+ }
0 commit comments