Skip to content

Commit

Permalink
✨ Fakeclient: Add support for evictions
Browse files Browse the repository at this point in the history
This change adds support for evictions into the fakeclient. Prior to
this, it would just error on any subresource creation.
  • Loading branch information
alvaroaleman committed May 7, 2023
1 parent 98e2435 commit c26435c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
14 changes: 13 additions & 1 deletion pkg/client/fake/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ import (

"sigs.k8s.io/controller-runtime/pkg/client/interceptor"

corev1 "k8s.io/api/core/v1"
policyv1 "k8s.io/api/policy/v1"
policyv1beta1 "k8s.io/api/policy/v1beta1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -969,7 +972,16 @@ func (sw *fakeSubResourceClient) Get(ctx context.Context, obj, subResource clien
}

func (sw *fakeSubResourceClient) Create(ctx context.Context, obj client.Object, subResource client.Object, opts ...client.SubResourceCreateOption) error {
panic("fakeSubResourceWriter does not support create")
switch subResource.(type) {
case *policyv1beta1.Eviction, *policyv1.Eviction:
if _, isPod := obj.(*corev1.Pod); !isPod {
return apierrors.NewNotFound(schema.GroupResource{}, "")
}
return sw.client.Delete(ctx, obj)
default:
return fmt.Errorf("fakeSubResourceWriter does not support create for %T", subResource)

Check failure on line 983 in pkg/client/fake/client.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary trailing newline (whitespace)
}
}

func (sw *fakeSubResourceClient) Update(ctx context.Context, obj client.Object, opts ...client.SubResourceUpdateOption) error {
Expand Down
36 changes: 36 additions & 0 deletions pkg/client/fake/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import (
appsv1 "k8s.io/api/apps/v1"
coordinationv1 "k8s.io/api/coordination/v1"
corev1 "k8s.io/api/core/v1"
policyv1 "k8s.io/api/policy/v1"
policyv1beta1 "k8s.io/api/policy/v1beta1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -1436,6 +1438,40 @@ var _ = Describe("Fake client", func() {
err := cl.Status().Update(context.Background(), obj)
Expect(apierrors.IsNotFound(err)).To(BeTrue())
})

evictionTypes := []client.Object{
&policyv1beta1.Eviction{},
&policyv1.Eviction{},
}
for _, tp := range evictionTypes {
It("should delete a pod through the eviction subresource", func() {
pod := &corev1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}

cl := NewClientBuilder().WithObjects(pod).Build()

err := cl.SubResource("eviction").Create(context.Background(), pod, tp)
Expect(err).NotTo(HaveOccurred())

err = cl.Get(context.Background(), client.ObjectKeyFromObject(pod), pod)
Expect(apierrors.IsNotFound(err)).To(BeTrue())
})

It("should return not found when attempting to evict a pod that doesn't exist", func() {
cl := NewClientBuilder().Build()

pod := &corev1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}
err := cl.SubResource("eviction").Create(context.Background(), pod, tp)
Expect(apierrors.IsNotFound(err)).To(BeTrue())
})

It("should return not found when attempting to evict something other than a pod", func() {
ns := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}
cl := NewClientBuilder().WithObjects(ns).Build()

err := cl.SubResource("eviction").Create(context.Background(), ns, tp)
Expect(apierrors.IsNotFound(err)).To(BeTrue())
})
}
})

var _ = Describe("Fake client builder", func() {
Expand Down

0 comments on commit c26435c

Please sign in to comment.