Skip to content

Commit f879c2a

Browse files
committed
do not always error out while checking for existence of resources
controllers also get reconciles for deletion events and it may happen that resource at api server be deleted first before it being purged from cache and during that time if we error out the reconciliation will never be successful in standard conditions. Signed-off-by: Leela Venkaiah G <lgangava@ibm.com>
1 parent 04cf55c commit f879c2a

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

internal/controller/clientprofile_controller.go

+4
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ func (r *ClientProfileReconciler) Reconcile(ctx context.Context, req ctrl.Reques
130130

131131
func (r *ClientProfileReconcile) reconcile() error {
132132
if err := r.loadAndValidate(); err != nil {
133+
if utils.IsNotFoundWithName(err, r.clientProfile.Name) {
134+
r.log.Info("Client profile resource does not exists anymore, skipping reconcile")
135+
return nil
136+
}
133137
return err
134138
}
135139

internal/controller/driver_controller.go

+4
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,10 @@ func (r *DriverReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
226226
func (r *driverReconcile) reconcile() error {
227227
// Load the driver desired state based on driver resource, operator config resource and default values.
228228
if err := r.LoadAndValidateDesiredState(); err != nil {
229+
if utils.IsNotFoundWithName(err, r.driver.Name) {
230+
r.log.Info("Driver resource does not exist anymore, skipping reconcile")
231+
return nil
232+
}
229233
return err
230234
}
231235

internal/utils/meta.go

+13
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ limitations under the License.
1717
package utils
1818

1919
import (
20+
"errors"
21+
22+
k8serrors "k8s.io/apimachinery/pkg/api/errors"
2023
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2124
"k8s.io/apimachinery/pkg/runtime"
2225
ctrlutil "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
@@ -62,3 +65,13 @@ func ToggleOwnerReference(on bool, obj, owner metav1.Object, scheme *runtime.Sch
6265
}
6366
return false, nil
6467
}
68+
69+
func IsNotFoundWithName(err error, name string) bool {
70+
if !k8serrors.IsNotFound(err) {
71+
return false
72+
}
73+
status, ok := err.(k8serrors.APIStatus)
74+
return (ok || errors.As(err, &status)) &&
75+
status.Status().Details != nil &&
76+
status.Status().Details.Name == name
77+
}

0 commit comments

Comments
 (0)