Skip to content

Commit 2a7fce4

Browse files
Fix adopting ClusterResourceSet resource by CRS when already applied
Signed-off-by: killianmuldoon <[email protected]>
1 parent 844eff2 commit 2a7fce4

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

exp/addons/internal/controllers/clusterresourceset_controller.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -278,11 +278,6 @@ func (r *ClusterResourceSetReconciler) ApplyClusterResourceSet(ctx context.Conte
278278

279279
// Iterate all resources and apply them to the cluster and update the resource status in the ClusterResourceSetBinding object.
280280
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-
286281
unstructuredObj, err := r.getResource(ctx, resource, cluster.GetNamespace())
287282
if err != nil {
288283
if err == ErrSecretTypeNotSupported {
@@ -299,6 +294,18 @@ func (r *ClusterResourceSetReconciler) ApplyClusterResourceSet(ctx context.Conte
299294
continue
300295
}
301296

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+
302309
// Set status in ClusterResourceSetBinding in case of early continue due to a failure.
303310
// Set only when resource is retrieved successfully.
304311
resourceSetBinding.SetBinding(addonsv1.ResourceBinding{
@@ -307,13 +314,6 @@ func (r *ClusterResourceSetReconciler) ApplyClusterResourceSet(ctx context.Conte
307314
Applied: false,
308315
LastAppliedTime: &metav1.Time{Time: time.Now().UTC()},
309316
})
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-
317317
// Since maps are not ordered, we need to order them to get the same hash at each reconcile.
318318
keys := make([]string, 0)
319319
data, ok := unstructuredObj.UnstructuredContent()["data"]
@@ -411,8 +411,8 @@ func (r *ClusterResourceSetReconciler) getResource(ctx context.Context, resource
411411
return raw, nil
412412
}
413413

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 {
416416
newRef := metav1.OwnerReference{
417417
APIVersion: clusterResourceSet.GroupVersionKind().GroupVersion().String(),
418418
Kind: clusterResourceSet.GroupVersionKind().Kind,

0 commit comments

Comments
 (0)