Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃悰 Fix object overwrite on patch for fake client #1651

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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