@@ -276,17 +276,38 @@ func (r *ClusterResourceSetReconciler) ApplyClusterResourceSet(ctx context.Conte
276
276
log := ctrl .LoggerFrom (ctx , "Cluster" , klog .KObj (cluster ))
277
277
ctx = ctrl .LoggerInto (ctx , log )
278
278
279
- remoteClient , err := r .Tracker .GetClient (ctx , util .ObjectKey (cluster ))
280
- if err != nil {
281
- conditions .MarkFalse (clusterResourceSet , addonsv1 .ResourcesAppliedCondition , addonsv1 .RemoteClusterClientFailedReason , clusterv1 .ConditionSeverityError , err .Error ())
282
- return err
283
- }
279
+ // Iterate all resources and ensure an ownerReference to the clusterResourceSet is on the resource.
280
+ // NOTE: we have to do this before getting a remote client, otherwise owner reference won't be created until it is
281
+ // possible to connect to the remote cluster.
282
+ errList := []error {}
283
+ objList := make ([]* unstructured.Unstructured , len (clusterResourceSet .Spec .Resources ))
284
+ for i , resource := range clusterResourceSet .Spec .Resources {
285
+ unstructuredObj , err := r .getResource (ctx , resource , cluster .GetNamespace ())
286
+ if err != nil {
287
+ if err == ErrSecretTypeNotSupported {
288
+ conditions .MarkFalse (clusterResourceSet , addonsv1 .ResourcesAppliedCondition , addonsv1 .WrongSecretTypeReason , clusterv1 .ConditionSeverityWarning , err .Error ())
289
+ } else {
290
+ conditions .MarkFalse (clusterResourceSet , addonsv1 .ResourcesAppliedCondition , addonsv1 .RetrievingResourceFailedReason , clusterv1 .ConditionSeverityWarning , err .Error ())
284
291
285
- // Ensure that the Kubernetes API Server service has been created in the remote cluster before applying the ClusterResourceSet to avoid service IP conflict.
286
- // This action is required when the remote cluster Kubernetes version is lower than v1.25.
287
- // TODO: Remove this action once CAPI no longer supports Kubernetes versions below v1.25. See: https://github.com/kubernetes-sigs/cluster-api/issues/7804
288
- if err = ensureKubernetesServiceCreated (ctx , remoteClient ); err != nil {
289
- return errors .Wrapf (err , "failed to retrieve the Service for Kubernetes API Server of the cluster %s/%s" , cluster .Namespace , cluster .Name )
292
+ // Continue without adding the error to the aggregate if we can't find the resource.
293
+ if apierrors .IsNotFound (err ) {
294
+ continue
295
+ }
296
+ }
297
+ errList = append (errList , err )
298
+ continue
299
+ }
300
+
301
+ // Ensure an ownerReference to the clusterResourceSet is on the resource.
302
+ if err := r .ensureResourceOwnerRef (ctx , clusterResourceSet , unstructuredObj ); err != nil {
303
+ log .Error (err , "Failed to add ClusterResourceSet as resource owner reference" ,
304
+ "Resource type" , unstructuredObj .GetKind (), "Resource name" , unstructuredObj .GetName ())
305
+ errList = append (errList , err )
306
+ }
307
+ objList [i ] = unstructuredObj
308
+ }
309
+ if len (errList ) > 0 {
310
+ return kerrors .NewAggregate (errList )
290
311
}
291
312
292
313
// Get ClusterResourceSetBinding object for the cluster.
@@ -313,32 +334,28 @@ func (r *ClusterResourceSetReconciler) ApplyClusterResourceSet(ctx context.Conte
313
334
Name : clusterResourceSet .Name ,
314
335
UID : clusterResourceSet .UID ,
315
336
}))
316
- var errList [] error
337
+
317
338
resourceSetBinding := clusterResourceSetBinding .GetOrCreateBinding (clusterResourceSet )
318
339
319
- // Iterate all resources and apply them to the cluster and update the resource status in the ClusterResourceSetBinding object.
320
- for _ , resource := range clusterResourceSet .Spec .Resources {
321
- unstructuredObj , err := r .getResource (ctx , resource , cluster .GetNamespace ())
322
- if err != nil {
323
- if err == ErrSecretTypeNotSupported {
324
- conditions .MarkFalse (clusterResourceSet , addonsv1 .ResourcesAppliedCondition , addonsv1 .WrongSecretTypeReason , clusterv1 .ConditionSeverityWarning , err .Error ())
325
- } else {
326
- conditions .MarkFalse (clusterResourceSet , addonsv1 .ResourcesAppliedCondition , addonsv1 .RetrievingResourceFailedReason , clusterv1 .ConditionSeverityWarning , err .Error ())
340
+ remoteClient , err := r .Tracker .GetClient (ctx , util .ObjectKey (cluster ))
341
+ if err != nil {
342
+ conditions .MarkFalse (clusterResourceSet , addonsv1 .ResourcesAppliedCondition , addonsv1 .RemoteClusterClientFailedReason , clusterv1 .ConditionSeverityError , err .Error ())
343
+ return err
344
+ }
327
345
328
- // Continue without adding the error to the aggregate if we can't find the resource.
329
- if apierrors .IsNotFound (err ) {
330
- continue
331
- }
332
- }
333
- errList = append (errList , err )
334
- continue
335
- }
346
+ // Ensure that the Kubernetes API Server service has been created in the remote cluster before applying the ClusterResourceSet to avoid service IP conflict.
347
+ // This action is required when the remote cluster Kubernetes version is lower than v1.25.
348
+ // TODO: Remove this action once CAPI no longer supports Kubernetes versions below v1.25. See: https://github.com/kubernetes-sigs/cluster-api/issues/7804
349
+ if err := ensureKubernetesServiceCreated (ctx , remoteClient ); err != nil {
350
+ return errors .Wrapf (err , "failed to retrieve the Service for Kubernetes API Server of the cluster %s/%s" , cluster .Namespace , cluster .Name )
351
+ }
336
352
337
- // Ensure an ownerReference to the clusterResourceSet is on the resource.
338
- if err := r .ensureResourceOwnerRef (ctx , clusterResourceSet , unstructuredObj ); err != nil {
339
- log .Error (err , "Failed to add ClusterResourceSet as resource owner reference" ,
340
- "Resource type" , unstructuredObj .GetKind (), "Resource name" , unstructuredObj .GetName ())
341
- errList = append (errList , err )
353
+ // Iterate all resources and apply them to the cluster and update the resource status in the ClusterResourceSetBinding object.
354
+ for i , resource := range clusterResourceSet .Spec .Resources {
355
+ unstructuredObj := objList [i ]
356
+ if unstructuredObj == nil {
357
+ // Continue without adding the error to the aggregate if we can't find the resource.
358
+ continue
342
359
}
343
360
344
361
resourceScope , err := reconcileScopeForResource (clusterResourceSet , resource , resourceSetBinding , unstructuredObj )
0 commit comments