Skip to content

Commit 745c7c9

Browse files
authored
Merge pull request #1508 from syseleven/metadata-client-patch-options
🐛 metadata client applies patch options
2 parents 55a329c + ff44d62 commit 745c7c9

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

pkg/client/client_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@ WAIT_LOOP:
9898
Fail(fmt.Sprintf("timed out waiting for namespace %q to be deleted", ns.Name))
9999
}
100100

101+
type mockPatchOption struct {
102+
applied bool
103+
}
104+
105+
func (o *mockPatchOption) ApplyToPatch(_ *client.PatchOptions) {
106+
o.applied = true
107+
}
108+
101109
// metaOnlyFromObj returns PartialObjectMetadata from a concrete Go struct that
102110
// returns a concrete *metav1.ObjectMeta from GetObjectMeta (yes, that plays a
103111
// bit fast and loose, but the only other options are serializing and then
@@ -715,6 +723,39 @@ var _ = Describe("Client", func() {
715723
})
716724
})
717725

726+
Describe("Patch", func() {
727+
Context("Metadata Client", func() {
728+
It("should merge patch with options", func(done Done) {
729+
cl, err := client.New(cfg, client.Options{})
730+
Expect(err).NotTo(HaveOccurred())
731+
Expect(cl).NotTo(BeNil())
732+
733+
By("initially creating a Deployment")
734+
dep, err := clientset.AppsV1().Deployments(ns).Create(ctx, dep, metav1.CreateOptions{})
735+
Expect(err).NotTo(HaveOccurred())
736+
737+
metadata := metaOnlyFromObj(dep, scheme)
738+
if metadata.Labels == nil {
739+
metadata.Labels = make(map[string]string)
740+
}
741+
metadata.Labels["foo"] = "bar"
742+
743+
testOption := &mockPatchOption{}
744+
Expect(cl.Patch(context.TODO(), metadata, client.Merge, testOption)).To(Succeed())
745+
746+
By("validating that patched metadata has new labels")
747+
actual, err := clientset.AppsV1().Deployments(ns).Get(ctx, dep.Name, metav1.GetOptions{})
748+
Expect(err).NotTo(HaveOccurred())
749+
Expect(actual).NotTo(BeNil())
750+
Expect(actual.Labels["foo"]).To(Equal("bar"))
751+
752+
By("validating patch options were applied")
753+
Expect(testOption.applied).To(Equal(true))
754+
close(done)
755+
})
756+
})
757+
})
758+
718759
Describe("StatusClient", func() {
719760
Context("with structured objects", func() {
720761
It("should update status of an existing object", func(done Done) {

pkg/client/metadata_client.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ func (mc *metadataClient) Patch(ctx context.Context, obj Object, patch Patch, op
104104
}
105105

106106
patchOpts := &PatchOptions{}
107+
patchOpts.ApplyOptions(opts)
108+
107109
res, err := resInt.Patch(ctx, metadata.Name, patch.Type(), data, *patchOpts.AsPatchOptions())
108110
if err != nil {
109111
return err

0 commit comments

Comments
 (0)