Skip to content

Commit

Permalink
Fix deletion of resources with deleted CRDs (#778)
Browse files Browse the repository at this point in the history
We don't have any yet, but when VPA is added, the reconciler-manager
cleanup will need to work with resources whos CRD may have been
removed. Also fix the errors so they can be unwrapped correctly.
This will fix error messages to be more clear.
  • Loading branch information
karlkfi committed Jul 29, 2023
1 parent 4942bc2 commit b2520a2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pkg/reconcilermanager/controllers/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ func (ooe *ObjectOperationError) Error() string {
ooe.ID.Kind, ooe.ID.ObjectKey, ooe.Operation, ooe.Cause)
}

// Unwrap returns the cause of the error, to allow type checking with errors.Is
// and errors.As.
func (ooe *ObjectOperationError) Unwrap() error {
return ooe.Cause
}

// NewObjectOperationError constructs a new ObjectOperationError
func NewObjectOperationError(err error, obj client.Object, op Operation) *ObjectOperationError {
id := core.IDOf(obj)
Expand Down Expand Up @@ -144,6 +150,12 @@ func (oripe *ObjectReconcileError) Error() string {
oripe.ID.Kind, oripe.ID.ObjectKey, oripe.Status, oripe.Cause)
}

// Unwrap returns the cause of the error, to allow type checking with errors.Is
// and errors.As.
func (oripe *ObjectReconcileError) Unwrap() error {
return oripe.Cause
}

// NewObjectReconcileError constructs a new ObjectReconcileError
func NewObjectReconcileError(err error, obj client.Object, status kstatus.Status) *ObjectReconcileError {
id := core.IDOf(obj)
Expand Down
8 changes: 8 additions & 0 deletions pkg/util/watch/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"github.com/pkg/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/cache"
"k8s.io/klog/v2"
Expand Down Expand Up @@ -166,6 +167,13 @@ func DeleteAndWait(ctx context.Context, c client.WithWatch, obj client.Object, r
// Stop waiting - object already deleted
return true, nil
}
if meta.IsNoMatchError(err) {
klog.V(3).Infof("Resource not registered %q=%q, %q=%q",
logFieldObjectRef, id.ObjectKey.String(),
logFieldObjectKind, id.Kind)
// Stop waiting - resource not registered
return true, nil
}
// Stop waiting
return false, err
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/util/watch/until.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ func UntilDeletedWithSync(ctx context.Context, c client.WithWatch, obj client.Ob
if err != nil {
return err
}
// Validate that the resource API is registered.
// If not, error and let the caller deal with whether NoMatchError is
// tolerated or not.
// Without this check, the informer will retry endlessly waiting for the
// ListAndWatch to succeed.
_, err = c.RESTMapper().RESTMapping(itemGVK.GroupKind(), itemGVK.Version)
if err != nil {
return err
}
var objList client.ObjectList
if _, ok := obj.(*unstructured.Unstructured); ok {
objList = kinds.NewUnstructuredListForItemGVK(itemGVK)
Expand Down

0 comments on commit b2520a2

Please sign in to comment.