Skip to content

Commit

Permalink
metadata client applies patch options
Browse files Browse the repository at this point in the history
Signed-off-by: Furkhat Kasymovgeniiuulu <vailodf@gmail.com>
  • Loading branch information
furkhat committed May 7, 2021
1 parent a17ac06 commit ff44d62
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
41 changes: 41 additions & 0 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions pkg/client/metadata_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ff44d62

Please sign in to comment.