From c53a829fc2d8ad360887bf3e0807b023b9510df9 Mon Sep 17 00:00:00 2001 From: Alvaro Aleman Date: Sun, 7 May 2023 16:14:17 -0400 Subject: [PATCH] :sparkles: Fakeclient: Add support for evictions This change adds support for evictions into the fakeclient. Prior to this, it would just error on any subresource creation. --- pkg/client/fake/client.go | 26 ++++++++++++++++-- pkg/client/fake/client_test.go | 50 ++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 3 deletions(-) diff --git a/pkg/client/fake/client.go b/pkg/client/fake/client.go index 910409e2ef..49b81140d1 100644 --- a/pkg/client/fake/client.go +++ b/pkg/client/fake/client.go @@ -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" @@ -931,7 +934,7 @@ func (c *fakeClient) Status() client.SubResourceWriter { } func (c *fakeClient) SubResource(subResource string) client.SubResourceClient { - return &fakeSubResourceClient{client: c} + return &fakeSubResourceClient{client: c, subResource: subResource} } func (c *fakeClient) deleteObject(gvr schema.GroupVersionResource, accessor metav1.Object) error { @@ -961,7 +964,8 @@ func getGVRFromObject(obj runtime.Object, scheme *runtime.Scheme) (schema.GroupV } type fakeSubResourceClient struct { - client *fakeClient + client *fakeClient + subResource string } func (sw *fakeSubResourceClient) Get(ctx context.Context, obj, subResource client.Object, opts ...client.SubResourceGetOption) error { @@ -969,7 +973,23 @@ 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 sw.subResource { + case "eviction": + _, isEviction := subResource.(*policyv1beta1.Eviction) + if !isEviction { + _, isEviction = subResource.(*policyv1.Eviction) + } + if !isEviction { + return apierrors.NewBadRequest(fmt.Sprintf("got invalid type %t, expected Eviction", subResource)) + } + 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 %s", sw.subResource) + } } func (sw *fakeSubResourceClient) Update(ctx context.Context, obj client.Object, opts ...client.SubResourceUpdateOption) error { diff --git a/pkg/client/fake/client_test.go b/pkg/client/fake/client_test.go index 84711da06d..a5e8abeb5a 100644 --- a/pkg/client/fake/client_test.go +++ b/pkg/client/fake/client_test.go @@ -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" @@ -1436,6 +1438,54 @@ 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()) + }) + + It("should return an error when using the wrong subresource", func() { + cl := NewClientBuilder().Build() + + err := cl.SubResource("eviction-subresource").Create(context.Background(), &corev1.Namespace{}, tp) + Expect(err).NotTo(BeNil()) + }) + } + + It("should error when creating an eviction with the wrong type", func() { + + cl := NewClientBuilder().Build() + err := cl.SubResource("eviction").Create(context.Background(), &corev1.Pod{}, &corev1.Namespace{}) + Expect(apierrors.IsBadRequest(err)).To(BeTrue()) + }) }) var _ = Describe("Fake client builder", func() {