Skip to content

Commit c26435c

Browse files
committed
✨ 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.
1 parent 98e2435 commit c26435c

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

pkg/client/fake/client.go

Lines changed: 13 additions & 1 deletion
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"
@@ -969,7 +972,16 @@ func (sw *fakeSubResourceClient) Get(ctx context.Context, obj, subResource clien
969972
}
970973

971974
func (sw *fakeSubResourceClient) Create(ctx context.Context, obj client.Object, subResource client.Object, opts ...client.SubResourceCreateOption) error {
972-
panic("fakeSubResourceWriter does not support create")
975+
switch subResource.(type) {
976+
case *policyv1beta1.Eviction, *policyv1.Eviction:
977+
if _, isPod := obj.(*corev1.Pod); !isPod {
978+
return apierrors.NewNotFound(schema.GroupResource{}, "")
979+
}
980+
return sw.client.Delete(ctx, obj)
981+
default:
982+
return fmt.Errorf("fakeSubResourceWriter does not support create for %T", subResource)
983+
984+
}
973985
}
974986

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

pkg/client/fake/client_test.go

Lines changed: 36 additions & 0 deletions
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,40 @@ 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+
}
14391475
})
14401476

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

0 commit comments

Comments
 (0)