@@ -278,11 +278,6 @@ func (r *ClusterResourceSetReconciler) ApplyClusterResourceSet(ctx context.Conte
278
278
279
279
// Iterate all resources and apply them to the cluster and update the resource status in the ClusterResourceSetBinding object.
280
280
for _ , resource := range clusterResourceSet .Spec .Resources {
281
- // If resource is already applied successfully and clusterResourceSet mode is "ApplyOnce", continue. (No need to check hash changes here)
282
- if resourceSetBinding .IsApplied (resource ) {
283
- continue
284
- }
285
-
286
281
unstructuredObj , err := r .getResource (ctx , resource , cluster .GetNamespace ())
287
282
if err != nil {
288
283
if err == ErrSecretTypeNotSupported {
@@ -299,6 +294,18 @@ func (r *ClusterResourceSetReconciler) ApplyClusterResourceSet(ctx context.Conte
299
294
continue
300
295
}
301
296
297
+ // Ensure an ownerReference to the clusterResourceSet is on the resource.
298
+ if err := r .ensureResourceOwnerRef (ctx , clusterResourceSet , unstructuredObj ); err != nil {
299
+ log .Error (err , "Failed to add ClusterResourceSet as resource owner reference" ,
300
+ "Resource type" , unstructuredObj .GetKind (), "Resource name" , unstructuredObj .GetName ())
301
+ errList = append (errList , err )
302
+ }
303
+
304
+ // If resource is already applied successfully and clusterResourceSet mode is "ApplyOnce", continue. (No need to check hash changes here)
305
+ if resourceSetBinding .IsApplied (resource ) {
306
+ continue
307
+ }
308
+
302
309
// Set status in ClusterResourceSetBinding in case of early continue due to a failure.
303
310
// Set only when resource is retrieved successfully.
304
311
resourceSetBinding .SetBinding (addonsv1.ResourceBinding {
@@ -307,13 +314,6 @@ func (r *ClusterResourceSetReconciler) ApplyClusterResourceSet(ctx context.Conte
307
314
Applied : false ,
308
315
LastAppliedTime : & metav1.Time {Time : time .Now ().UTC ()},
309
316
})
310
-
311
- if err := r .patchOwnerRefToResource (ctx , clusterResourceSet , unstructuredObj ); err != nil {
312
- log .Error (err , "Failed to patch ClusterResourceSet as resource owner reference" ,
313
- "Resource type" , unstructuredObj .GetKind (), "Resource name" , unstructuredObj .GetName ())
314
- errList = append (errList , err )
315
- }
316
-
317
317
// Since maps are not ordered, we need to order them to get the same hash at each reconcile.
318
318
keys := make ([]string , 0 )
319
319
data , ok := unstructuredObj .UnstructuredContent ()["data" ]
@@ -411,8 +411,8 @@ func (r *ClusterResourceSetReconciler) getResource(ctx context.Context, resource
411
411
return raw , nil
412
412
}
413
413
414
- // patchOwnerRefToResource adds the ClusterResourceSet as a OwnerReference to the resource.
415
- func (r * ClusterResourceSetReconciler ) patchOwnerRefToResource (ctx context.Context , clusterResourceSet * addonsv1.ClusterResourceSet , resource * unstructured.Unstructured ) error {
414
+ // ensureResourceOwnerRef adds the ClusterResourceSet as a OwnerReference to the resource.
415
+ func (r * ClusterResourceSetReconciler ) ensureResourceOwnerRef (ctx context.Context , clusterResourceSet * addonsv1.ClusterResourceSet , resource * unstructured.Unstructured ) error {
416
416
newRef := metav1.OwnerReference {
417
417
APIVersion : clusterResourceSet .GroupVersionKind ().GroupVersion ().String (),
418
418
Kind : clusterResourceSet .GroupVersionKind ().Kind ,
0 commit comments