Skip to content

Commit dd6ead6

Browse files
authored
Merge pull request #589 from alvaroaleman/list-opts-as-list-option
✨ Client option types implement client option interfaces
2 parents 524b614 + b7dfffa commit dd6ead6

File tree

3 files changed

+303
-1
lines changed

3 files changed

+303
-1
lines changed

Gopkg.lock

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/client/options.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,21 @@ func (o *CreateOptions) ApplyOptions(opts []CreateOption) *CreateOptions {
142142
return o
143143
}
144144

145+
// ApplyToCreate implements CreateOption
146+
func (o *CreateOptions) ApplyToCreate(co *CreateOptions) {
147+
if o.DryRun != nil {
148+
co.DryRun = o.DryRun
149+
}
150+
if o.FieldManager != "" {
151+
co.FieldManager = o.FieldManager
152+
}
153+
if o.Raw != nil {
154+
co.Raw = o.Raw
155+
}
156+
}
157+
158+
var _ CreateOption = &CreateOptions{}
159+
145160
// CreateDryRunAll sets the "dry run" option to "all".
146161
//
147162
// Deprecated: Use DryRunAll
@@ -211,6 +226,27 @@ func (o *DeleteOptions) ApplyOptions(opts []DeleteOption) *DeleteOptions {
211226
return o
212227
}
213228

229+
var _ DeleteOption = &DeleteOptions{}
230+
231+
// ApplyToDelete implements DeleteOption
232+
func (o *DeleteOptions) ApplyToDelete(do *DeleteOptions) {
233+
if o.GracePeriodSeconds != nil {
234+
do.GracePeriodSeconds = o.GracePeriodSeconds
235+
}
236+
if o.Preconditions != nil {
237+
do.Preconditions = o.Preconditions
238+
}
239+
if o.PropagationPolicy != nil {
240+
do.PropagationPolicy = o.PropagationPolicy
241+
}
242+
if o.Raw != nil {
243+
do.Raw = o.Raw
244+
}
245+
if o.DryRun != nil {
246+
do.DryRun = o.DryRun
247+
}
248+
}
249+
214250
// GracePeriodSeconds sets the grace period for the deletion
215251
// to the given number of seconds.
216252
type GracePeriodSeconds int64
@@ -273,6 +309,24 @@ type ListOptions struct {
273309
Raw *metav1.ListOptions
274310
}
275311

312+
var _ ListOption = &ListOptions{}
313+
314+
// ApplyToList implements ListOption for ListOptions
315+
func (o *ListOptions) ApplyToList(lo *ListOptions) {
316+
if o.LabelSelector != nil {
317+
lo.LabelSelector = o.LabelSelector
318+
}
319+
if o.FieldSelector != nil {
320+
lo.FieldSelector = o.FieldSelector
321+
}
322+
if o.Namespace != "" {
323+
lo.Namespace = o.Namespace
324+
}
325+
if o.Raw != nil {
326+
lo.Raw = o.Raw
327+
}
328+
}
329+
276330
// AsListOptions returns these options as a flattened metav1.ListOptions.
277331
// This may mutate the Raw field.
278332
func (o *ListOptions) AsListOptions() *metav1.ListOptions {
@@ -422,6 +476,21 @@ func (o *UpdateOptions) ApplyOptions(opts []UpdateOption) *UpdateOptions {
422476
return o
423477
}
424478

479+
var _ UpdateOption = &UpdateOptions{}
480+
481+
// ApplyToUpdate implements UpdateOption
482+
func (o *UpdateOptions) ApplyToUpdate(uo *UpdateOptions) {
483+
if o.DryRun != nil {
484+
uo.DryRun = o.DryRun
485+
}
486+
if o.FieldManager != "" {
487+
uo.FieldManager = o.FieldManager
488+
}
489+
if o.Raw != nil {
490+
uo.Raw = o.Raw
491+
}
492+
}
493+
425494
// UpdateDryRunAll sets the "dry run" option to "all".
426495
//
427496
// Deprecated: Use DryRunAll
@@ -479,6 +548,24 @@ func (o *PatchOptions) AsPatchOptions() *metav1.PatchOptions {
479548
return o.Raw
480549
}
481550

551+
var _ PatchOption = &PatchOptions{}
552+
553+
// ApplyToPatch implements PatchOptions
554+
func (o *PatchOptions) ApplyToPatch(po *PatchOptions) {
555+
if o.DryRun != nil {
556+
po.DryRun = o.DryRun
557+
}
558+
if o.Force != nil {
559+
po.Force = o.Force
560+
}
561+
if o.FieldManager != "" {
562+
po.FieldManager = o.FieldManager
563+
}
564+
if o.Raw != nil {
565+
po.Raw = o.Raw
566+
}
567+
}
568+
482569
// ForceOwnership indicates that in case of conflicts with server-side apply,
483570
// the client should acquire ownership of the conflicting field. Most
484571
// controllers should use this.
@@ -518,4 +605,12 @@ func (o *DeleteAllOfOptions) ApplyOptions(opts []DeleteAllOfOption) *DeleteAllOf
518605
return o
519606
}
520607

608+
var _ DeleteAllOfOption = &DeleteAllOfOptions{}
609+
610+
// ApplyToDeleteAllOf implements DeleteAllOfOption
611+
func (o *DeleteAllOfOptions) ApplyToDeleteAllOf(do *DeleteAllOfOptions) {
612+
o.ApplyToList(&do.ListOptions)
613+
o.ApplyToDelete(&do.DeleteOptions)
614+
}
615+
521616
// }}}

pkg/client/options_test.go

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
/*
2+
Copyright 2018 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package client_test
18+
19+
import (
20+
. "github.com/onsi/ginkgo"
21+
. "github.com/onsi/gomega"
22+
23+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24+
"k8s.io/apimachinery/pkg/fields"
25+
"k8s.io/apimachinery/pkg/labels"
26+
utilpointer "k8s.io/utils/pointer"
27+
"sigs.k8s.io/controller-runtime/pkg/client"
28+
)
29+
30+
var _ = Describe("ListOptions", func() {
31+
It("Should set LabelSelector", func() {
32+
labelSelector, err := labels.Parse("a=b")
33+
Expect(err).NotTo(HaveOccurred())
34+
o := &client.ListOptions{LabelSelector: labelSelector}
35+
newListOpts := &client.ListOptions{}
36+
o.ApplyToList(newListOpts)
37+
Expect(newListOpts).To(Equal(o))
38+
})
39+
It("Should set FieldSelector", func() {
40+
o := &client.ListOptions{FieldSelector: fields.Nothing()}
41+
newListOpts := &client.ListOptions{}
42+
o.ApplyToList(newListOpts)
43+
Expect(newListOpts).To(Equal(o))
44+
})
45+
It("Should set Namespace", func() {
46+
o := &client.ListOptions{Namespace: "my-ns"}
47+
newListOpts := &client.ListOptions{}
48+
o.ApplyToList(newListOpts)
49+
Expect(newListOpts).To(Equal(o))
50+
})
51+
It("Should set Raw", func() {
52+
o := &client.ListOptions{Raw: &metav1.ListOptions{FieldSelector: "Hans"}}
53+
newListOpts := &client.ListOptions{}
54+
o.ApplyToList(newListOpts)
55+
Expect(newListOpts).To(Equal(o))
56+
})
57+
It("Should not set anything", func() {
58+
o := &client.ListOptions{}
59+
newListOpts := &client.ListOptions{}
60+
o.ApplyToList(newListOpts)
61+
Expect(newListOpts).To(Equal(o))
62+
})
63+
})
64+
65+
var _ = Describe("CreateOptions", func() {
66+
It("Should set DryRun", func() {
67+
o := &client.CreateOptions{DryRun: []string{"Hello", "Theodore"}}
68+
newCreatOpts := &client.CreateOptions{}
69+
o.ApplyToCreate(newCreatOpts)
70+
Expect(newCreatOpts).To(Equal(o))
71+
})
72+
It("Should set FieldManager", func() {
73+
o := &client.CreateOptions{FieldManager: "FieldManager"}
74+
newCreatOpts := &client.CreateOptions{}
75+
o.ApplyToCreate(newCreatOpts)
76+
Expect(newCreatOpts).To(Equal(o))
77+
})
78+
It("Should set Raw", func() {
79+
o := &client.CreateOptions{Raw: &metav1.CreateOptions{DryRun: []string{"Bye", "Theodore"}}}
80+
newCreatOpts := &client.CreateOptions{}
81+
o.ApplyToCreate(newCreatOpts)
82+
Expect(newCreatOpts).To(Equal(o))
83+
})
84+
It("Should not set anything", func() {
85+
o := &client.CreateOptions{}
86+
newCreatOpts := &client.CreateOptions{}
87+
o.ApplyToCreate(newCreatOpts)
88+
Expect(newCreatOpts).To(Equal(o))
89+
})
90+
})
91+
92+
var _ = Describe("DeleteOptions", func() {
93+
It("Should set GracePeriodSeconds", func() {
94+
o := &client.DeleteOptions{GracePeriodSeconds: utilpointer.Int64Ptr(42)}
95+
newDeleteOpts := &client.DeleteOptions{}
96+
o.ApplyToDelete(newDeleteOpts)
97+
Expect(newDeleteOpts).To(Equal(o))
98+
})
99+
It("Should set Preconditions", func() {
100+
o := &client.DeleteOptions{Preconditions: &metav1.Preconditions{}}
101+
newDeleteOpts := &client.DeleteOptions{}
102+
o.ApplyToDelete(newDeleteOpts)
103+
Expect(newDeleteOpts).To(Equal(o))
104+
})
105+
It("Should set PropagationPolicy", func() {
106+
policy := metav1.DeletePropagationBackground
107+
o := &client.DeleteOptions{PropagationPolicy: &policy}
108+
newDeleteOpts := &client.DeleteOptions{}
109+
o.ApplyToDelete(newDeleteOpts)
110+
Expect(newDeleteOpts).To(Equal(o))
111+
})
112+
It("Should set Raw", func() {
113+
o := &client.DeleteOptions{Raw: &metav1.DeleteOptions{}}
114+
newDeleteOpts := &client.DeleteOptions{}
115+
o.ApplyToDelete(newDeleteOpts)
116+
Expect(newDeleteOpts).To(Equal(o))
117+
})
118+
It("Should set DryRun", func() {
119+
o := &client.DeleteOptions{DryRun: []string{"Hello", "Pippa"}}
120+
newDeleteOpts := &client.DeleteOptions{}
121+
o.ApplyToDelete(newDeleteOpts)
122+
Expect(newDeleteOpts).To(Equal(o))
123+
})
124+
It("Should not set anything", func() {
125+
o := &client.DeleteOptions{}
126+
newDeleteOpts := &client.DeleteOptions{}
127+
o.ApplyToDelete(newDeleteOpts)
128+
Expect(newDeleteOpts).To(Equal(o))
129+
})
130+
})
131+
132+
var _ = Describe("UpdateOptions", func() {
133+
It("Should set DryRun", func() {
134+
o := &client.UpdateOptions{DryRun: []string{"Bye", "Pippa"}}
135+
newUpdateOpts := &client.UpdateOptions{}
136+
o.ApplyToUpdate(newUpdateOpts)
137+
Expect(newUpdateOpts).To(Equal(o))
138+
})
139+
It("Should set FieldManager", func() {
140+
o := &client.UpdateOptions{FieldManager: "Hello Boris"}
141+
newUpdateOpts := &client.UpdateOptions{}
142+
o.ApplyToUpdate(newUpdateOpts)
143+
Expect(newUpdateOpts).To(Equal(o))
144+
})
145+
It("Should set Raw", func() {
146+
o := &client.UpdateOptions{Raw: &metav1.UpdateOptions{}}
147+
newUpdateOpts := &client.UpdateOptions{}
148+
o.ApplyToUpdate(newUpdateOpts)
149+
Expect(newUpdateOpts).To(Equal(o))
150+
})
151+
It("Should not set anything", func() {
152+
o := &client.UpdateOptions{}
153+
newUpdateOpts := &client.UpdateOptions{}
154+
o.ApplyToUpdate(newUpdateOpts)
155+
Expect(newUpdateOpts).To(Equal(o))
156+
})
157+
})
158+
159+
var _ = Describe("PatchOptions", func() {
160+
It("Should set DryRun", func() {
161+
o := &client.PatchOptions{DryRun: []string{"Bye", "Boris"}}
162+
newPatchOpts := &client.PatchOptions{}
163+
o.ApplyToPatch(newPatchOpts)
164+
Expect(newPatchOpts).To(Equal(o))
165+
})
166+
It("Should set Force", func() {
167+
o := &client.PatchOptions{Force: utilpointer.BoolPtr(true)}
168+
newPatchOpts := &client.PatchOptions{}
169+
o.ApplyToPatch(newPatchOpts)
170+
Expect(newPatchOpts).To(Equal(o))
171+
})
172+
It("Should set FieldManager", func() {
173+
o := &client.PatchOptions{FieldManager: "Hello Julian"}
174+
newPatchOpts := &client.PatchOptions{}
175+
o.ApplyToPatch(newPatchOpts)
176+
Expect(newPatchOpts).To(Equal(o))
177+
})
178+
It("Should set Raw", func() {
179+
o := &client.PatchOptions{Raw: &metav1.PatchOptions{}}
180+
newPatchOpts := &client.PatchOptions{}
181+
o.ApplyToPatch(newPatchOpts)
182+
Expect(newPatchOpts).To(Equal(o))
183+
})
184+
It("Should not set anything", func() {
185+
o := &client.PatchOptions{}
186+
newPatchOpts := &client.PatchOptions{}
187+
o.ApplyToPatch(newPatchOpts)
188+
Expect(newPatchOpts).To(Equal(o))
189+
})
190+
})
191+
192+
var _ = Describe("DeleteAllOfOptions", func() {
193+
It("Should set ListOptions", func() {
194+
o := &client.DeleteAllOfOptions{ListOptions: client.ListOptions{Raw: &metav1.ListOptions{}}}
195+
newDeleteAllOfOpts := &client.DeleteAllOfOptions{}
196+
o.ApplyToDeleteAllOf(newDeleteAllOfOpts)
197+
Expect(newDeleteAllOfOpts).To(Equal(o))
198+
})
199+
It("Should set DeleleteOptions", func() {
200+
o := &client.DeleteAllOfOptions{DeleteOptions: client.DeleteOptions{GracePeriodSeconds: utilpointer.Int64Ptr(44)}}
201+
newDeleteAllOfOpts := &client.DeleteAllOfOptions{}
202+
o.ApplyToDeleteAllOf(newDeleteAllOfOpts)
203+
Expect(newDeleteAllOfOpts).To(Equal(o))
204+
})
205+
})

0 commit comments

Comments
 (0)