Skip to content

Commit

Permalink
Merge pull request #1651 from FillZpp/fix-fake-client-patch-overwrite
Browse files Browse the repository at this point in the history
🐛 Fix object overwrite on patch for fake client
  • Loading branch information
k8s-ci-robot authored Sep 6, 2021
2 parents 498ee8a + af7484e commit 386c2b5
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
13 changes: 13 additions & 0 deletions pkg/client/fake/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"encoding/json"
"errors"
"fmt"
"reflect"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -284,6 +285,7 @@ func (c *fakeClient) Get(ctx context.Context, key client.ObjectKey, obj client.O
return err
}
decoder := scheme.Codecs.UniversalDecoder()
zero(obj)
_, _, err = decoder.Decode(j, nil, obj)
return err
}
Expand Down Expand Up @@ -346,6 +348,7 @@ func (c *fakeClient) List(ctx context.Context, obj client.ObjectList, opts ...cl
return err
}
decoder := scheme.Codecs.UniversalDecoder()
zero(obj)
_, _, err = decoder.Decode(j, nil, obj)
if err != nil {
return err
Expand Down Expand Up @@ -549,6 +552,7 @@ func (c *fakeClient) Patch(ctx context.Context, obj client.Object, patch client.
return err
}
decoder := scheme.Codecs.UniversalDecoder()
zero(obj)
_, _, err = decoder.Decode(j, nil, obj)
return err
}
Expand Down Expand Up @@ -695,3 +699,12 @@ func allowsCreateOnUpdate(gvk schema.GroupVersionKind) bool {

return false
}

// zero zeros the value of a pointer.
func zero(x interface{}) {
if x == nil {
return
}
res := reflect.ValueOf(x).Elem()
res.Set(reflect.Zero(res.Type()))
}
41 changes: 41 additions & 0 deletions pkg/client/fake/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,47 @@ var _ = Describe("Fake client", func() {
err = cl.Get(context.Background(), namespacedName, obj)
Expect(apierrors.IsNotFound(err)).To(BeTrue())
})

It("should remove finalizers of the object on Patch", func() {
namespacedName := types.NamespacedName{
Name: "test-cm",
Namespace: "patch-finalizers-in-obj",
}
By("Creating a new object")
obj := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: namespacedName.Name,
Namespace: namespacedName.Namespace,
Finalizers: []string{"finalizers.sigs.k8s.io/test"},
},
Data: map[string]string{
"test-key": "new-value",
},
}
err := cl.Create(context.Background(), obj)
Expect(err).To(BeNil())

By("Removing the finalizer")
mergePatch, err := json.Marshal(map[string]interface{}{
"metadata": map[string]interface{}{
"$deleteFromPrimitiveList/finalizers": []string{
"finalizers.sigs.k8s.io/test",
},
},
})
Expect(err).To(BeNil())
err = cl.Patch(context.Background(), obj, client.RawPatch(types.StrategicMergePatchType, mergePatch))
Expect(err).To(BeNil())

By("Check the finalizer has been removed in the object")
Expect(len(obj.Finalizers)).To(Equal(0))

By("Check the finalizer has been removed in client")
newObj := &corev1.ConfigMap{}
err = cl.Get(context.Background(), namespacedName, newObj)
Expect(err).To(BeNil())
Expect(len(newObj.Finalizers)).To(Equal(0))
})
}

Context("with default scheme.Scheme", func() {
Expand Down

0 comments on commit 386c2b5

Please sign in to comment.