@@ -61,6 +61,10 @@ func (r *reconciler) reconcile(ctx context.Context, cl client.Client, cache cach
6161 return err
6262 }
6363
64+ // TODO(mjudeikis): we could potentially add finallizer to APIServiceExport above or "adopt" boundschemas
65+ // with owner references once export is created.
66+ // https://github.com/kube-bind/kube-bind/issues/297
67+
6468 conditions .SetSummary (req )
6569
6670 return nil
@@ -69,10 +73,6 @@ func (r *reconciler) reconcile(ctx context.Context, cl client.Client, cache cach
6973func (r * reconciler ) ensureBoundSchemas (ctx context.Context , cl client.Client , cache cache.Cache , req * kubebindv1alpha2.APIServiceExportRequest ) error {
7074 // Ensure all bound schemas exist
7175 for _ , res := range req .Spec .Resources {
72- if len (res .Versions ) == 0 {
73- continue
74- }
75-
7676 parts := strings .SplitN (r .schemaSource , "." , 3 )
7777 if len (parts ) != 3 { // We check this in validation, but just in case.
7878 return fmt .Errorf ("invalid schema source: %q" , r .schemaSource )
@@ -106,23 +106,25 @@ func (r *reconciler) ensureBoundSchemas(ctx context.Context, cl client.Client, c
106106 }
107107
108108 for _ , item := range list .Items {
109- spec := item .Object ["spec" ]
110- if spec == nil {
111- return fmt .Errorf ("invalid schema %s/%s: no spec found" , item .GetNamespace (), item .GetName ())
109+ obj := item .UnstructuredContent ()
110+ spec , ok := obj ["spec" ].(map [string ]interface {})
111+ if ! ok {
112+ klog .FromContext (ctx ).Error (nil , "Skipping invalid schema: missing spec" , "ns" , item .GetNamespace (), "name" , item .GetName ())
113+ continue
112114 }
113- group := spec .(map [string ]interface {})["group" ]
114- names := spec .(map [string ]interface {})["names" ]
115- if group == nil || names == nil {
116- return fmt .Errorf ("invalid schema %s/%s: no group or names found" , item .GetNamespace (), item .GetName ())
115+ g , _ := spec ["group" ].(string )
116+ names , _ := spec ["names" ].(map [string ]interface {})
117+ p , _ := names ["plural" ].(string )
118+ if g == "" || p == "" {
119+ klog .FromContext (ctx ).Error (nil , "Skipping invalid schema: missing group or names.plural" , "ns" , item .GetNamespace (), "name" , item .GetName ())
120+ continue
117121 }
118- plural := names .(map [string ]interface {})["plural" ]
119-
120- if group == res .Group && plural == res .Resource {
122+ if g == res .Group && p == res .Resource {
121123 boundSchema , err := helpers .UnstructuredToBoundSchema (item )
122124 if err != nil {
123125 return err
124126 }
125- boundSchema .Name = res .Resource + "." + res . Group
127+ boundSchema .Name = res .ResourceGroupName ()
126128 boundSchema .Namespace = req .Namespace
127129 boundSchema .Spec .InformerScope = r .informerScope
128130 boundSchema .ResourceVersion = ""
@@ -154,7 +156,7 @@ func (r *reconciler) ensureExports(ctx context.Context, cl client.Client, cache
154156 var scope apiextensionsv1.ResourceScope
155157 if req .Status .Phase == kubebindv1alpha2 .APIServiceExportRequestPhasePending {
156158 for _ , res := range req .Spec .Resources {
157- name := res .Resource + "." + res . Group
159+ name := res .ResourceGroupName ()
158160 boundSchema , err := r .getBoundSchema (ctx , cache , req .Namespace , name )
159161 if err != nil {
160162 if apierrors .IsNotFound (err ) {
@@ -177,8 +179,14 @@ func (r *reconciler) ensureExports(ctx context.Context, cl client.Client, cache
177179 schemas = append (schemas , boundSchema )
178180 }
179181
180- if _ , err := r .getServiceExport (ctx , cache , req .Namespace , req .Name ); err != nil && ! apierrors .IsNotFound (err ) {
181- return err
182+ if _ , err := r .getServiceExport (ctx , cache , req .Namespace , req .Name ); err != nil {
183+ if ! apierrors .IsNotFound (err ) {
184+ return err
185+ }
186+ } else {
187+ // already exists; nothing to do
188+ conditions .MarkTrue (req , kubebindv1alpha2 .APIServiceExportRequestConditionExportsReady )
189+ return nil
182190 }
183191
184192 hash := helpers .BoundSchemasSpecHash (schemas )
@@ -210,6 +218,9 @@ func (r *reconciler) ensureExports(ctx context.Context, cl client.Client, cache
210218
211219 logger .V (1 ).Info ("Creating APIServiceExport" , "name" , export .Name , "namespace" , export .Namespace )
212220 if err := r .createServiceExport (ctx , cl , export ); err != nil {
221+ if apierrors .IsAlreadyExists (err ) {
222+ return nil
223+ }
213224 return err
214225 }
215226
0 commit comments