Skip to content

Commit ff636e7

Browse files
committed
Switch functional options with no args to vars
This switches functional arguments with no variables to not taking arguments, meaning that they can be called like ```go r.Update(ctx, obj, client.UpdateDryRunAll) ``` instead of ```go r.Update(ctx, obj, client.UpdateDryRunAll()) ``` Options with arguments still get called as functions.
1 parent 012fe9f commit ff636e7

File tree

2 files changed

+22
-28
lines changed

2 files changed

+22
-28
lines changed

pkg/client/client_test.go

Lines changed: 7 additions & 7 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,7 +2157,7 @@ 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
})

pkg/client/options.go

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,8 @@ type CreateOptionFunc func(*CreateOptions)
6767

6868
// CreateDryRunAll is a functional option that sets the DryRun
6969
// field of a CreateOptions struct to metav1.DryRunAll.
70-
func CreateDryRunAll() CreateOptionFunc {
71-
return func(opts *CreateOptions) {
72-
opts.DryRun = []string{metav1.DryRunAll}
73-
}
70+
var CreateDryRunAll CreateOptionFunc = func(opts *CreateOptions) {
71+
opts.DryRun = []string{metav1.DryRunAll}
7472
}
7573

7674
// DeleteOptions contains options for delete requests. It's generally a subset
@@ -339,10 +337,8 @@ type UpdateOptionFunc func(*UpdateOptions)
339337

340338
// UpdateDryRunAll is a functional option that sets the DryRun
341339
// field of a UpdateOptions struct to metav1.DryRunAll.
342-
func UpdateDryRunAll() UpdateOptionFunc {
343-
return func(opts *UpdateOptions) {
344-
opts.DryRun = []string{metav1.DryRunAll}
345-
}
340+
var UpdateDryRunAll UpdateOptionFunc = func(opts *UpdateOptions) {
341+
opts.DryRun = []string{metav1.DryRunAll}
346342
}
347343

348344
// PatchOptions contains options for patch requests.
@@ -398,21 +394,19 @@ func (o *PatchOptions) AsPatchOptions() *metav1.PatchOptions {
398394
// https://github.com/tmrts/go-patterns/blob/master/idiom/functional-options.md.
399395
type PatchOptionFunc func(*PatchOptions)
400396

401-
// PatchDryRunAll is a functional option that sets the DryRun
402-
// field of a PatchOptions struct to metav1.DryRunAll.
403-
func PatchDryRunAll() PatchOptionFunc {
404-
return func(opts *PatchOptions) {
405-
opts.DryRun = []string{metav1.DryRunAll}
406-
}
397+
// ForceOwnership sets the Force option, indicating that
398+
// in case of conflicts with server-side apply, the client should
399+
// acquire ownership of the conflicting field. Most controllers
400+
// should use this.
401+
var ForceOwnership PatchOptionFunc = func(opts *PatchOptions) {
402+
definitelyTrue := true
403+
opts.Force = &definitelyTrue
407404
}
408405

409-
// ForceOwnership is a functional option that sets the Force
410-
// field of a PatchOptions struct to true.
411-
func ForceOwnership() PatchOptionFunc {
412-
force := true
413-
return func(opts *PatchOptions) {
414-
opts.Force = &force
415-
}
406+
// PatchDryRunAll is a functional option that sets the DryRun
407+
// field of a PatchOptions struct to metav1.DryRunAll.
408+
var PatchDryRunAll PatchOptionFunc = func(opts *PatchOptions) {
409+
opts.DryRun = []string{metav1.DryRunAll}
416410
}
417411

418412
// FieldOwner set the field manager name for the given server-side apply patch.

0 commit comments

Comments
 (0)