Skip to content

Commit

Permalink
Merge pull request #2305 from alvaroaleman/fake-eviction
Browse files Browse the repository at this point in the history
✨ Fakeclient: Add support for evictions
  • Loading branch information
k8s-ci-robot authored May 9, 2023
2 parents edaa282 + c53a829 commit f7ea9c0
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 3 deletions.
26 changes: 23 additions & 3 deletions 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 @@ -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 {
Expand Down Expand Up @@ -961,15 +964,32 @@ 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 {
panic("fakeSubResourceClient does not support get")
}

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 {
Expand Down
50 changes: 50 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,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() {
Expand Down

0 comments on commit f7ea9c0

Please sign in to comment.