Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

do not always error out while checking for existence of resources #151

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions internal/controller/clientprofile_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ func (r *ClientProfileReconciler) Reconcile(ctx context.Context, req ctrl.Reques

func (r *ClientProfileReconcile) reconcile() error {
if err := r.loadAndValidate(); err != nil {
if utils.IsNotFoundWithName(err, r.clientProfile.Name) {
r.log.Info("Client profile resource does not exists anymore, skipping reconcile")
return nil
}
return err
}

Expand Down
4 changes: 4 additions & 0 deletions internal/controller/driver_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,10 @@ func (r *DriverReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
func (r *driverReconcile) reconcile() error {
// Load the driver desired state based on driver resource, operator config resource and default values.
if err := r.LoadAndValidateDesiredState(); err != nil {
if utils.IsNotFoundWithName(err, r.driver.Name) {
r.log.Info("Driver resource does not exist anymore, skipping reconcile")
return nil
}
return err
}

Expand Down
13 changes: 13 additions & 0 deletions internal/utils/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ limitations under the License.
package utils

import (
"errors"

k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
ctrlutil "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
Expand Down Expand Up @@ -62,3 +65,13 @@ func ToggleOwnerReference(on bool, obj, owner metav1.Object, scheme *runtime.Sch
}
return false, nil
}

func IsNotFoundWithName(err error, name string) bool {
if !k8serrors.IsNotFound(err) {
return false
}
status, ok := err.(k8serrors.APIStatus)
return (ok || errors.As(err, &status)) &&
Comment on lines +73 to +74
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@leelavg Why the two different approaches? can we always use errors.As?

Copy link
Contributor Author

@leelavg leelavg Nov 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is a possibility that APIStatus could be wrapped and so the interface checks fails but unwrapping it can be successful which provides the Status() function satisfying this interface and query the error details. ref: https://github.com/kubernetes/apimachinery/blob/master/pkg/api/errors/errors.go#L809-L814

another thing to note here is, API could just return httpStatusNotFound without setting details of the resource in which case this function fails to work as intended, based on the review I could return true for both if status not found and resource is intended object, if status is not found but resource isn't set for us to verify.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to complicate this so much, we know exactly the function that produced the error and the potential error space coming from it, Also we are looking for a subset of a specific error code that comes from a trusted source (the k8s client). With all of this in mind, you don't need to write code that is so defensive

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

initial ask from #151 (comment) is the error object to see the target of the not found error? and I see it's not always possible to get the target name which was not found.

status.Status().Details != nil &&
status.Status().Details.Name == name
}
Loading