Skip to content

Commit 44318ed

Browse files
committed
chore: ensure we patch RGD status for any Reconcile errors
During RGD reconcile, if there are any issues when setting RGD CR as managed or unmanaged, any issues that may occur are not reflected on the RGD CR status. With these changes, we will ensure any issues are patched to the RGD status
1 parent b52020a commit 44318ed

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

pkg/controller/resourcegraphdefinition/controller.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020

2121
"github.com/go-logr/logr"
2222
extv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
23+
apierrors "k8s.io/apimachinery/pkg/api/errors"
2324
"k8s.io/apimachinery/pkg/types"
2425
ctrl "sigs.k8s.io/controller-runtime"
2526
"sigs.k8s.io/controller-runtime/pkg/builder"
@@ -154,7 +155,23 @@ func (r *ResourceGraphDefinitionReconciler) findRGDsForCRD(ctx context.Context,
154155
}
155156
}
156157

157-
func (r *ResourceGraphDefinitionReconciler) Reconcile(ctx context.Context, o *v1alpha1.ResourceGraphDefinition) (ctrl.Result, error) {
158+
func (r *ResourceGraphDefinitionReconciler) Reconcile(ctx context.Context, o *v1alpha1.ResourceGraphDefinition) (res ctrl.Result, reconcileErr error) {
159+
var topologicalOrder []string
160+
var resourcesInformation []v1alpha1.ResourceInformation
161+
// defer funciton will ensure any errors that occur during reconcile will eventually be
162+
// patched to the resource
163+
defer func() {
164+
// if the updateStatus error is a k8s NotFound error we want to skip it.
165+
// If not, we will merge it to the returned error
166+
if err := r.updateStatus(ctx, o, topologicalOrder, resourcesInformation); err != nil {
167+
// we don't want to reconcile if resource is deleted
168+
if !apierrors.IsNotFound(err) {
169+
reconcileErr = errors.Join(reconcileErr, err)
170+
} else {
171+
reconcileErr = nil
172+
}
173+
}
174+
}()
158175
if !o.DeletionTimestamp.IsZero() {
159176
if err := r.cleanupResourceGraphDefinition(ctx, o); err != nil {
160177
return ctrl.Result{}, err
@@ -169,11 +186,7 @@ func (r *ResourceGraphDefinitionReconciler) Reconcile(ctx context.Context, o *v1
169186
return ctrl.Result{}, err
170187
}
171188

172-
topologicalOrder, resourcesInformation, reconcileErr := r.reconcileResourceGraphDefinition(ctx, o)
173-
174-
if err := r.updateStatus(ctx, o, topologicalOrder, resourcesInformation); err != nil {
175-
reconcileErr = errors.Join(reconcileErr, err)
176-
}
189+
topologicalOrder, resourcesInformation, reconcileErr = r.reconcileResourceGraphDefinition(ctx, o)
177190

178191
return ctrl.Result{}, reconcileErr
179192
}

0 commit comments

Comments
 (0)