From ff44d62e00f5bfe114574d6247b434e8a04db1f4 Mon Sep 17 00:00:00 2001 From: Furkhat Kasymovgeniiuulu Date: Mon, 3 May 2021 14:05:34 +0300 Subject: [PATCH] metadata client applies patch options Signed-off-by: Furkhat Kasymovgeniiuulu --- pkg/client/client_test.go | 41 +++++++++++++++++++++++++++++++++++ pkg/client/metadata_client.go | 2 ++ 2 files changed, 43 insertions(+) diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index e62d8ea8e1..617fae6f5a 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -98,6 +98,14 @@ WAIT_LOOP: Fail(fmt.Sprintf("timed out waiting for namespace %q to be deleted", ns.Name)) } +type mockPatchOption struct { + applied bool +} + +func (o *mockPatchOption) ApplyToPatch(_ *client.PatchOptions) { + o.applied = true +} + // metaOnlyFromObj returns PartialObjectMetadata from a concrete Go struct that // returns a concrete *metav1.ObjectMeta from GetObjectMeta (yes, that plays a // bit fast and loose, but the only other options are serializing and then @@ -715,6 +723,39 @@ var _ = Describe("Client", func() { }) }) + Describe("Patch", func() { + Context("Metadata Client", func() { + It("should merge patch with options", func(done Done) { + cl, err := client.New(cfg, client.Options{}) + Expect(err).NotTo(HaveOccurred()) + Expect(cl).NotTo(BeNil()) + + By("initially creating a Deployment") + dep, err := clientset.AppsV1().Deployments(ns).Create(ctx, dep, metav1.CreateOptions{}) + Expect(err).NotTo(HaveOccurred()) + + metadata := metaOnlyFromObj(dep, scheme) + if metadata.Labels == nil { + metadata.Labels = make(map[string]string) + } + metadata.Labels["foo"] = "bar" + + testOption := &mockPatchOption{} + Expect(cl.Patch(context.TODO(), metadata, client.Merge, testOption)).To(Succeed()) + + By("validating that patched metadata has new labels") + actual, err := clientset.AppsV1().Deployments(ns).Get(ctx, dep.Name, metav1.GetOptions{}) + Expect(err).NotTo(HaveOccurred()) + Expect(actual).NotTo(BeNil()) + Expect(actual.Labels["foo"]).To(Equal("bar")) + + By("validating patch options were applied") + Expect(testOption.applied).To(Equal(true)) + close(done) + }) + }) + }) + Describe("StatusClient", func() { Context("with structured objects", func() { It("should update status of an existing object", func(done Done) { diff --git a/pkg/client/metadata_client.go b/pkg/client/metadata_client.go index 6587a19407..c0fc72c5b7 100644 --- a/pkg/client/metadata_client.go +++ b/pkg/client/metadata_client.go @@ -104,6 +104,8 @@ func (mc *metadataClient) Patch(ctx context.Context, obj Object, patch Patch, op } patchOpts := &PatchOptions{} + patchOpts.ApplyOptions(opts) + res, err := resInt.Patch(ctx, metadata.Name, patch.Type(), data, *patchOpts.AsPatchOptions()) if err != nil { return err