Skip to content

Commit f7ea9c0

Browse files
authored
Merge pull request #2305 from alvaroaleman/fake-eviction
✨ Fakeclient: Add support for evictions
2 parents edaa282 + c53a829 commit f7ea9c0

File tree

2 files changed

+73
-3
lines changed

2 files changed

+73
-3
lines changed

Diff for: pkg/client/fake/client.go

+23-3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ import (
3030

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

33+
corev1 "k8s.io/api/core/v1"
34+
policyv1 "k8s.io/api/policy/v1"
35+
policyv1beta1 "k8s.io/api/policy/v1beta1"
3336
apierrors "k8s.io/apimachinery/pkg/api/errors"
3437
"k8s.io/apimachinery/pkg/api/meta"
3538
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -931,7 +934,7 @@ func (c *fakeClient) Status() client.SubResourceWriter {
931934
}
932935

933936
func (c *fakeClient) SubResource(subResource string) client.SubResourceClient {
934-
return &fakeSubResourceClient{client: c}
937+
return &fakeSubResourceClient{client: c, subResource: subResource}
935938
}
936939

937940
func (c *fakeClient) deleteObject(gvr schema.GroupVersionResource, accessor metav1.Object) error {
@@ -961,15 +964,32 @@ func getGVRFromObject(obj runtime.Object, scheme *runtime.Scheme) (schema.GroupV
961964
}
962965

963966
type fakeSubResourceClient struct {
964-
client *fakeClient
967+
client *fakeClient
968+
subResource string
965969
}
966970

967971
func (sw *fakeSubResourceClient) Get(ctx context.Context, obj, subResource client.Object, opts ...client.SubResourceGetOption) error {
968972
panic("fakeSubResourceClient does not support get")
969973
}
970974

971975
func (sw *fakeSubResourceClient) Create(ctx context.Context, obj client.Object, subResource client.Object, opts ...client.SubResourceCreateOption) error {
972-
panic("fakeSubResourceWriter does not support create")
976+
switch sw.subResource {
977+
case "eviction":
978+
_, isEviction := subResource.(*policyv1beta1.Eviction)
979+
if !isEviction {
980+
_, isEviction = subResource.(*policyv1.Eviction)
981+
}
982+
if !isEviction {
983+
return apierrors.NewBadRequest(fmt.Sprintf("got invalid type %t, expected Eviction", subResource))
984+
}
985+
if _, isPod := obj.(*corev1.Pod); !isPod {
986+
return apierrors.NewNotFound(schema.GroupResource{}, "")
987+
}
988+
989+
return sw.client.Delete(ctx, obj)
990+
default:
991+
return fmt.Errorf("fakeSubResourceWriter does not support create for %s", sw.subResource)
992+
}
973993
}
974994

975995
func (sw *fakeSubResourceClient) Update(ctx context.Context, obj client.Object, opts ...client.SubResourceUpdateOption) error {

Diff for: pkg/client/fake/client_test.go

+50
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ import (
3636
appsv1 "k8s.io/api/apps/v1"
3737
coordinationv1 "k8s.io/api/coordination/v1"
3838
corev1 "k8s.io/api/core/v1"
39+
policyv1 "k8s.io/api/policy/v1"
40+
policyv1beta1 "k8s.io/api/policy/v1beta1"
3941
apierrors "k8s.io/apimachinery/pkg/api/errors"
4042
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
4143
"k8s.io/apimachinery/pkg/runtime"
@@ -1436,6 +1438,54 @@ var _ = Describe("Fake client", func() {
14361438
err := cl.Status().Update(context.Background(), obj)
14371439
Expect(apierrors.IsNotFound(err)).To(BeTrue())
14381440
})
1441+
1442+
evictionTypes := []client.Object{
1443+
&policyv1beta1.Eviction{},
1444+
&policyv1.Eviction{},
1445+
}
1446+
for _, tp := range evictionTypes {
1447+
It("should delete a pod through the eviction subresource", func() {
1448+
pod := &corev1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}
1449+
1450+
cl := NewClientBuilder().WithObjects(pod).Build()
1451+
1452+
err := cl.SubResource("eviction").Create(context.Background(), pod, tp)
1453+
Expect(err).NotTo(HaveOccurred())
1454+
1455+
err = cl.Get(context.Background(), client.ObjectKeyFromObject(pod), pod)
1456+
Expect(apierrors.IsNotFound(err)).To(BeTrue())
1457+
})
1458+
1459+
It("should return not found when attempting to evict a pod that doesn't exist", func() {
1460+
cl := NewClientBuilder().Build()
1461+
1462+
pod := &corev1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}
1463+
err := cl.SubResource("eviction").Create(context.Background(), pod, tp)
1464+
Expect(apierrors.IsNotFound(err)).To(BeTrue())
1465+
})
1466+
1467+
It("should return not found when attempting to evict something other than a pod", func() {
1468+
ns := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}
1469+
cl := NewClientBuilder().WithObjects(ns).Build()
1470+
1471+
err := cl.SubResource("eviction").Create(context.Background(), ns, tp)
1472+
Expect(apierrors.IsNotFound(err)).To(BeTrue())
1473+
})
1474+
1475+
It("should return an error when using the wrong subresource", func() {
1476+
cl := NewClientBuilder().Build()
1477+
1478+
err := cl.SubResource("eviction-subresource").Create(context.Background(), &corev1.Namespace{}, tp)
1479+
Expect(err).NotTo(BeNil())
1480+
})
1481+
}
1482+
1483+
It("should error when creating an eviction with the wrong type", func() {
1484+
1485+
cl := NewClientBuilder().Build()
1486+
err := cl.SubResource("eviction").Create(context.Background(), &corev1.Pod{}, &corev1.Namespace{})
1487+
Expect(apierrors.IsBadRequest(err)).To(BeTrue())
1488+
})
14391489
})
14401490

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

0 commit comments

Comments
 (0)