Skip to content

Commit 38483b2

Browse files
authored
Merge pull request #435 from DirectXMan12/feature/minimal-apply
⚠️ Minimal Apply Support, Fix Up Client Options
2 parents 3511f3c + 36db6ad commit 38483b2

9 files changed

+533
-409
lines changed

pkg/client/client.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,19 @@ type statusWriter struct {
171171
var _ StatusWriter = &statusWriter{}
172172

173173
// Update implements client.StatusWriter
174-
func (sw *statusWriter) Update(ctx context.Context, obj runtime.Object) error {
174+
func (sw *statusWriter) Update(ctx context.Context, obj runtime.Object, opts ...UpdateOptionFunc) error {
175175
_, ok := obj.(*unstructured.Unstructured)
176176
if ok {
177-
return sw.client.unstructuredClient.UpdateStatus(ctx, obj)
177+
return sw.client.unstructuredClient.UpdateStatus(ctx, obj, opts...)
178178
}
179-
return sw.client.typedClient.UpdateStatus(ctx, obj)
179+
return sw.client.typedClient.UpdateStatus(ctx, obj, opts...)
180+
}
181+
182+
// Patch implements client.Client
183+
func (sw *statusWriter) Patch(ctx context.Context, obj runtime.Object, patch Patch, opts ...PatchOptionFunc) error {
184+
_, ok := obj.(*unstructured.Unstructured)
185+
if ok {
186+
return sw.client.unstructuredClient.PatchStatus(ctx, obj, patch, opts...)
187+
}
188+
return sw.client.typedClient.PatchStatus(ctx, obj, patch, opts...)
180189
}

pkg/client/client_test.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ var _ = Describe("Client", func() {
276276
Expect(cl).NotTo(BeNil())
277277

278278
By("creating the object (with DryRun)")
279-
err = cl.Create(context.TODO(), dep, client.CreateDryRunAll())
279+
err = cl.Create(context.TODO(), dep, client.CreateDryRunAll)
280280
Expect(err).NotTo(HaveOccurred())
281281

282282
actual, err := clientset.AppsV1().Deployments(ns).Get(dep.Name, metav1.GetOptions{})
@@ -415,7 +415,7 @@ var _ = Describe("Client", func() {
415415
})
416416

417417
By("creating the object")
418-
err = cl.Create(context.TODO(), u, client.CreateDryRunAll())
418+
err = cl.Create(context.TODO(), u, client.CreateDryRunAll)
419419
Expect(err).NotTo(HaveOccurred())
420420

421421
actual, err := clientset.AppsV1().Deployments(ns).Get(dep.Name, metav1.GetOptions{})
@@ -1074,7 +1074,7 @@ var _ = Describe("Client", func() {
10741074
Expect(err).NotTo(HaveOccurred())
10751075

10761076
By("patching the Deployment with dry-run")
1077-
err = cl.Patch(context.TODO(), dep, client.ConstantPatch(types.MergePatchType, mergePatch), client.PatchDryRunAll())
1077+
err = cl.Patch(context.TODO(), dep, client.ConstantPatch(types.MergePatchType, mergePatch), client.PatchDryRunAll)
10781078
Expect(err).NotTo(HaveOccurred())
10791079

10801080
By("validating patched Deployment doesn't have the new annotation")
@@ -1183,7 +1183,7 @@ var _ = Describe("Client", func() {
11831183
Kind: "Deployment",
11841184
Version: "v1",
11851185
})
1186-
err = cl.Patch(context.TODO(), u, client.ConstantPatch(types.MergePatchType, mergePatch), client.PatchDryRunAll())
1186+
err = cl.Patch(context.TODO(), u, client.ConstantPatch(types.MergePatchType, mergePatch), client.PatchDryRunAll)
11871187
Expect(err).NotTo(HaveOccurred())
11881188

11891189
By("validating patched Deployment does not have the new annotation")
@@ -2000,7 +2000,7 @@ var _ = Describe("Client", func() {
20002000
Describe("CreateOptions", func() {
20012001
It("should allow setting DryRun to 'all'", func() {
20022002
co := &client.CreateOptions{}
2003-
client.CreateDryRunAll()(co)
2003+
client.CreateDryRunAll(co)
20042004
all := []string{metav1.DryRunAll}
20052005
Expect(co.AsCreateOptions().DryRun).To(Equal(all))
20062006
})
@@ -2141,7 +2141,7 @@ var _ = Describe("Client", func() {
21412141
Describe("UpdateOptions", func() {
21422142
It("should allow setting DryRun to 'all'", func() {
21432143
uo := &client.UpdateOptions{}
2144-
client.UpdateDryRunAll()(uo)
2144+
client.UpdateDryRunAll(uo)
21452145
all := []string{metav1.DryRunAll}
21462146
Expect(uo.AsUpdateOptions().DryRun).To(Equal(all))
21472147
})
@@ -2157,19 +2157,25 @@ var _ = Describe("Client", func() {
21572157
Describe("PatchOptions", func() {
21582158
It("should allow setting DryRun to 'all'", func() {
21592159
po := &client.PatchOptions{}
2160-
client.PatchDryRunAll()(po)
2160+
client.PatchDryRunAll(po)
21612161
all := []string{metav1.DryRunAll}
21622162
Expect(po.AsPatchOptions().DryRun).To(Equal(all))
21632163
})
21642164

21652165
It("should allow setting Force to 'true'", func() {
21662166
po := &client.PatchOptions{}
2167-
client.PatchWithForce()(po)
2167+
client.ForceOwnership(po)
21682168
mpo := po.AsPatchOptions()
21692169
Expect(mpo.Force).NotTo(BeNil())
21702170
Expect(*mpo.Force).To(BeTrue())
21712171
})
21722172

2173+
It("should allow setting the field manager", func() {
2174+
po := &client.PatchOptions{}
2175+
client.FieldOwner("some-owner")(po)
2176+
Expect(po.AsPatchOptions().FieldManager).To(Equal("some-owner"))
2177+
})
2178+
21732179
It("should produce empty metav1.PatchOptions if nil", func() {
21742180
var po *client.PatchOptions
21752181
Expect(po.AsPatchOptions()).To(Equal(&metav1.PatchOptions{}))

pkg/client/fake/client.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,14 @@ type fakeStatusWriter struct {
244244
client *fakeClient
245245
}
246246

247-
func (sw *fakeStatusWriter) Update(ctx context.Context, obj runtime.Object) error {
247+
func (sw *fakeStatusWriter) Update(ctx context.Context, obj runtime.Object, opts ...client.UpdateOptionFunc) error {
248248
// TODO(droot): This results in full update of the obj (spec + status). Need
249249
// a way to update status field only.
250-
return sw.client.Update(ctx, obj)
250+
return sw.client.Update(ctx, obj, opts...)
251+
}
252+
253+
func (sw *fakeStatusWriter) Patch(ctx context.Context, obj runtime.Object, patch client.Patch, opts ...client.PatchOptionFunc) error {
254+
// TODO(droot): This results in full update of the obj (spec + status). Need
255+
// a way to update status field only.
256+
return sw.client.Patch(ctx, obj, patch, opts...)
251257
}

pkg/client/fake/client_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ var _ = Describe("Fake client", func() {
167167
Namespace: "ns2",
168168
},
169169
}
170-
err := cl.Create(nil, newcm, client.CreateDryRunAll())
170+
err := cl.Create(nil, newcm, client.CreateDryRunAll)
171171
Expect(err).To(BeNil())
172172

173173
By("Getting the new configmap")
@@ -193,7 +193,7 @@ var _ = Describe("Fake client", func() {
193193
"test-key": "new-value",
194194
},
195195
}
196-
err := cl.Update(nil, newcm, client.UpdateDryRunAll())
196+
err := cl.Update(nil, newcm, client.UpdateDryRunAll)
197197
Expect(err).To(BeNil())
198198

199199
By("Getting the new configmap")

0 commit comments

Comments
 (0)