Skip to content

Commit

Permalink
Fix cleaning K8s resources (gardener#5484)
Browse files Browse the repository at this point in the history
  • Loading branch information
timuthy authored and Kristiyan Gostev committed Apr 21, 2022
1 parent eb876d5 commit ca1eb61
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 8 deletions.
17 changes: 10 additions & 7 deletions pkg/utils/kubernetes/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,10 +349,7 @@ func EnsureGone(ctx context.Context, c client.Client, obj runtime.Object, opts .

func ensureGone(ctx context.Context, c client.Client, obj client.Object) error {
if err := c.Get(ctx, client.ObjectKeyFromObject(obj), obj); err != nil {
if apierrors.IsNotFound(err) {
return nil
}
if meta.IsNoMatchError(err) {
if meta.IsNoMatchError(err) || apierrors.IsNotFound(err) {
return nil
}
return err
Expand All @@ -361,7 +358,10 @@ func ensureGone(ctx context.Context, c client.Client, obj client.Object) error {
}

func ensureCollectionGone(ctx context.Context, c client.Client, list client.ObjectList, opts ...client.ListOption) error {
if err := c.List(ctx, list, opts...); err != nil && !meta.IsNoMatchError(err) {
if err := c.List(ctx, list, opts...); err != nil {
if meta.IsNoMatchError(err) || apierrors.IsNotFound(err) {
return nil
}
return err
}

Expand All @@ -380,7 +380,10 @@ func cleanAction(
cleanOptions *CleanOptions,
action actionFunc,
) error {
if err := c.Get(ctx, client.ObjectKeyFromObject(obj), obj); client.IgnoreNotFound(err) != nil {
if err := c.Get(ctx, client.ObjectKeyFromObject(obj), obj); err != nil {
if meta.IsNoMatchError(err) || apierrors.IsNotFound(err) {
return nil
}
return err
}

Expand All @@ -395,7 +398,7 @@ func cleanCollectionAction(
action actionFunc,
) error {
if err := c.List(ctx, list, cleanOptions.ListOptions...); err != nil {
if meta.IsNoMatchError(err) {
if meta.IsNoMatchError(err) || apierrors.IsNotFound(err) {
return nil
}
return err
Expand Down
78 changes: 77 additions & 1 deletion pkg/utils/kubernetes/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,28 @@ var _ = Describe("Cleaner", func() {
Expect(cleaner.Clean(ctx, c, &cm1)).To(Succeed())
})

It("should succeed if not found error occurs for target object", func() {
var (
ctx = context.TODO()
cleaner = NewCleaner(timeOps, NewFinalizer())
)

c.EXPECT().Get(ctx, cm1Key, &cm1).Return(apierrors.NewNotFound(schema.GroupResource{}, ""))

Expect(cleaner.Clean(ctx, c, &cm1)).To(Succeed())
})

It("should succeed if no match error occurs for target object", func() {
var (
ctx = context.TODO()
cleaner = NewCleaner(timeOps, NewFinalizer())
)

c.EXPECT().Get(ctx, cm1Key, &cm1).Return(&meta.NoResourceMatchError{PartialResource: schema.GroupVersionResource{}})

Expect(cleaner.Clean(ctx, c, &cm1)).To(Succeed())
})

It("should delete all objects matching the selector", func() {
var (
ctx = context.TODO()
Expand All @@ -124,6 +146,30 @@ var _ = Describe("Cleaner", func() {
Expect(cleaner.Clean(ctx, c, list)).To(Succeed())
})

It("should succeed if not found error occurs for list type", func() {
var (
ctx = context.TODO()
list = &corev1.ConfigMapList{}
cleaner = NewCleaner(timeOps, NewFinalizer())
)

c.EXPECT().List(ctx, list).Return(apierrors.NewNotFound(schema.GroupResource{}, ""))

Expect(cleaner.Clean(ctx, c, list)).To(Succeed())
})

It("should succeed if no match error occurs for list type", func() {
var (
ctx = context.TODO()
list = &corev1.ConfigMapList{}
cleaner = NewCleaner(timeOps, NewFinalizer())
)

c.EXPECT().List(ctx, list).Return(&meta.NoResourceMatchError{PartialResource: schema.GroupVersionResource{}})

Expect(cleaner.Clean(ctx, c, list)).To(Succeed())
})

It("should finalize the object if its deletion timestamp is over the finalize grace period", func() {
var (
ctx = context.TODO()
Expand Down Expand Up @@ -428,14 +474,22 @@ var _ = Describe("Cleaner", func() {
})

Describe("#EnsureGone", func() {
It("should ensure that the object is gone", func() {
It("should ensure that the object is gone when not found error occurs", func() {
ctx := context.TODO()

c.EXPECT().Get(ctx, cm1Key, &cm1).Return(apierrors.NewNotFound(schema.GroupResource{}, ""))

Expect(EnsureGone(ctx, c, &cm1)).To(Succeed())
})

It("should ensure that the object is gone when no match error occurs", func() {
ctx := context.TODO()

c.EXPECT().Get(ctx, cm1Key, &cm1).Return(&meta.NoResourceMatchError{PartialResource: schema.GroupVersionResource{}})

Expect(EnsureGone(ctx, c, &cm1)).To(Succeed())
})

It("should ensure that the list is gone", func() {
var (
ctx = context.TODO()
Expand All @@ -447,6 +501,28 @@ var _ = Describe("Cleaner", func() {
Expect(EnsureGone(ctx, c, &list)).To(Succeed())
})

It("should ensure that the list is gone when not found error occurs", func() {
var (
ctx = context.TODO()
list = corev1.ConfigMapList{}
)

c.EXPECT().List(ctx, &list).Return(apierrors.NewNotFound(schema.GroupResource{}, ""))

Expect(EnsureGone(ctx, c, &list)).To(Succeed())
})

It("should ensure that the list is gone when no match error occurs", func() {
var (
ctx = context.TODO()
list = corev1.ConfigMapList{}
)

c.EXPECT().List(ctx, &list).Return(&meta.NoResourceMatchError{PartialResource: schema.GroupVersionResource{}})

Expect(EnsureGone(ctx, c, &list)).To(Succeed())
})

It("should error that the object is still present", func() {
ctx := context.TODO()

Expand Down

0 comments on commit ca1eb61

Please sign in to comment.