Skip to content

Commit

Permalink
Merge pull request #76 from NatzkaLabsOpenSource/OPS-651-crossplane-k…
Browse files Browse the repository at this point in the history
…ubernetes-provider-finalizer-stuck

Fix removing finalizers when referenced resources are gone
  • Loading branch information
morningspace committed Dec 17, 2022
2 parents b1e7fce + a4c06e4 commit 4fafb7e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
10 changes: 7 additions & 3 deletions internal/controller/object/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ type objFinalizer struct {

type refFinalizerFn func(context.Context, *unstructured.Unstructured, string) error

func (f *objFinalizer) handleRefFinalizer(ctx context.Context, obj *v1alpha1.Object, finalizerFn refFinalizerFn) error {
func (f *objFinalizer) handleRefFinalizer(ctx context.Context, obj *v1alpha1.Object, finalizerFn refFinalizerFn, ignoreNotFound bool) error {
// Loop through references to resolve each referenced resource
for _, ref := range obj.Spec.References {
if ref.DependsOn == nil && ref.PatchesFrom == nil {
Expand All @@ -484,6 +484,10 @@ func (f *objFinalizer) handleRefFinalizer(ctx context.Context, obj *v1alpha1.Obj
}, res)

if err != nil {
if ignoreNotFound && kerrors.IsNotFound(err) {
continue
}

return errors.Wrap(err, errGetReferencedResource)
}

Expand Down Expand Up @@ -523,7 +527,7 @@ func (f *objFinalizer) AddFinalizer(ctx context.Context, res resource.Object) er
}
}
return nil
})
}, false)
return errors.Wrap(err, errAddFinalizer)
}

Expand All @@ -543,7 +547,7 @@ func (f *objFinalizer) RemoveFinalizer(ctx context.Context, res resource.Object)
}
}
return nil
})
}, true)
if err != nil {
return errors.Wrap(err, errRemoveFinalizer)
}
Expand Down
19 changes: 19 additions & 0 deletions internal/controller/object/object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1337,6 +1337,25 @@ func Test_objFinalizer_RemoveFinalizer(t *testing.T) {
finalizers: []string{},
},
},
"ReferenceNotFound": {
args: args{
mg: kubernetesObject(func(obj *v1alpha1.Object) {
obj.ObjectMeta.Finalizers = append(obj.ObjectMeta.Finalizers, objFinalizerName)
obj.Spec.References = objectReferences()
obj.ObjectMeta.UID = "some-uid"
}),
client: resource.ClientApplicator{
Client: &test.MockClient{
MockGet: test.NewMockGetFn(kerrors.NewNotFound(schema.GroupResource{}, testReferenceObjectName)),
MockUpdate: test.NewMockUpdateFn(nil),
},
},
},
want: want{
err: nil,
finalizers: []string{},
},
},
"FailedToRemoveReferenceFinalizer": {
args: args{
mg: kubernetesObject(func(obj *v1alpha1.Object) {
Expand Down

0 comments on commit 4fafb7e

Please sign in to comment.