Skip to content

Commit dcf55bd

Browse files
authored
Merge pull request kubernetes-sigs#2508 from acumino/fake-client-sub
🐛 Correctly identify if patch call was made on status
2 parents af3afdb + 6288e08 commit dcf55bd

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

pkg/client/fake/client.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ func (t versionedTracker) Update(gvr schema.GroupVersionResource, obj runtime.Ob
361361
isStatus := false
362362
// We apply patches using a client-go reaction that ends up calling the trackers Update. As we can't change
363363
// that reaction, we use the callstack to figure out if this originated from the status client.
364-
if bytes.Contains(debug.Stack(), []byte("sigs.k8s.io/controller-runtime/pkg/client/fake.(*fakeSubResourceClient).Patch")) {
364+
if bytes.Contains(debug.Stack(), []byte("sigs.k8s.io/controller-runtime/pkg/client/fake.(*fakeSubResourceClient).statusPatch")) {
365365
isStatus = true
366366
}
367367
return t.update(gvr, obj, ns, isStatus, false)
@@ -1090,6 +1090,15 @@ func (sw *fakeSubResourceClient) Patch(ctx context.Context, obj client.Object, p
10901090
body = patchOptions.SubResourceBody
10911091
}
10921092

1093+
// this is necessary to identify that last call was made for status patch, through stack trace.
1094+
if sw.subResource == "status" {
1095+
return sw.statusPatch(body, patch, patchOptions)
1096+
}
1097+
1098+
return sw.client.patch(body, patch, &patchOptions.PatchOptions)
1099+
}
1100+
1101+
func (sw *fakeSubResourceClient) statusPatch(body client.Object, patch client.Patch, patchOptions client.SubResourcePatchOptions) error {
10931102
return sw.client.patch(body, patch, &patchOptions.PatchOptions)
10941103
}
10951104

pkg/client/fake/client_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import (
4040
"k8s.io/apimachinery/pkg/types"
4141
"k8s.io/apimachinery/pkg/watch"
4242
"k8s.io/client-go/kubernetes/fake"
43+
"k8s.io/utils/pointer"
4344

4445
"sigs.k8s.io/controller-runtime/pkg/client"
4546
"sigs.k8s.io/controller-runtime/pkg/client/interceptor"
@@ -1590,6 +1591,28 @@ var _ = Describe("Fake client", func() {
15901591
Expect(objOriginal.Status.Phase).ToNot(Equal(actual.Status.Phase))
15911592
})
15921593

1594+
It("should be able to change typed objects that have a scale subresource on patch", func() {
1595+
obj := &appsv1.Deployment{
1596+
ObjectMeta: metav1.ObjectMeta{
1597+
Name: "deploy",
1598+
},
1599+
}
1600+
cl := NewClientBuilder().WithObjects(obj).Build()
1601+
objOriginal := obj.DeepCopy()
1602+
1603+
patch := []byte(fmt.Sprintf(`{"spec":{"replicas":%d}}`, 2))
1604+
Expect(cl.SubResource("scale").Patch(context.Background(), obj, client.RawPatch(types.MergePatchType, patch))).NotTo(HaveOccurred())
1605+
1606+
actual := &appsv1.Deployment{ObjectMeta: metav1.ObjectMeta{Name: obj.Name}}
1607+
Expect(cl.Get(context.Background(), client.ObjectKeyFromObject(actual), actual)).To(Succeed())
1608+
1609+
objOriginal.APIVersion = actual.APIVersion
1610+
objOriginal.Kind = actual.Kind
1611+
objOriginal.ResourceVersion = actual.ResourceVersion
1612+
objOriginal.Spec.Replicas = pointer.Int32(2)
1613+
Expect(cmp.Diff(objOriginal, actual)).To(BeEmpty())
1614+
})
1615+
15931616
It("should not change the status of typed objects that have a status subresource on patch", func() {
15941617
obj := &corev1.Pod{
15951618
ObjectMeta: metav1.ObjectMeta{

0 commit comments

Comments
 (0)