@@ -51,6 +51,8 @@ type Client[T objectWithMeta] struct {
51
51
namespace string // "" for non-namespaced clients
52
52
newObject func () T
53
53
parameterCodec runtime.ParameterCodec
54
+
55
+ prefersProtobuf bool
54
56
}
55
57
56
58
// ClientWithList represents a client with support for lists.
@@ -82,26 +84,37 @@ type alsoApplier[T objectWithMeta, C namedObject] struct {
82
84
client * Client [T ]
83
85
}
84
86
87
+ type Option [T objectWithMeta ] func (* Client [T ])
88
+
89
+ func PrefersProtobuf [T objectWithMeta ]() Option [T ] {
90
+ return func (c * Client [T ]) { c .prefersProtobuf = true }
91
+ }
92
+
85
93
// NewClient constructs a client, namespaced or not, with no support for lists or apply.
86
94
// Non-namespaced clients are constructed by passing an empty namespace ("").
87
95
func NewClient [T objectWithMeta ](
88
96
resource string , client rest.Interface , parameterCodec runtime.ParameterCodec , namespace string , emptyObjectCreator func () T ,
97
+ options ... Option [T ],
89
98
) * Client [T ] {
90
- return & Client [T ]{
99
+ c := & Client [T ]{
91
100
resource : resource ,
92
101
client : client ,
93
102
parameterCodec : parameterCodec ,
94
103
namespace : namespace ,
95
104
newObject : emptyObjectCreator ,
96
105
}
106
+ for _ , option := range options {
107
+ option (c )
108
+ }
109
+ return c
97
110
}
98
111
99
112
// NewClientWithList constructs a namespaced client with support for lists.
100
113
func NewClientWithList [T objectWithMeta , L runtime.Object ](
101
114
resource string , client rest.Interface , parameterCodec runtime.ParameterCodec , namespace string , emptyObjectCreator func () T ,
102
- emptyListCreator func () L ,
115
+ emptyListCreator func () L , options ... Option [ T ],
103
116
) * ClientWithList [T , L ] {
104
- typeClient := NewClient [T ](resource , client , parameterCodec , namespace , emptyObjectCreator )
117
+ typeClient := NewClient [T ](resource , client , parameterCodec , namespace , emptyObjectCreator , options ... )
105
118
return & ClientWithList [T , L ]{
106
119
typeClient ,
107
120
alsoLister [T , L ]{typeClient , emptyListCreator },
@@ -111,8 +124,9 @@ func NewClientWithList[T objectWithMeta, L runtime.Object](
111
124
// NewClientWithApply constructs a namespaced client with support for apply declarative configurations.
112
125
func NewClientWithApply [T objectWithMeta , C namedObject ](
113
126
resource string , client rest.Interface , parameterCodec runtime.ParameterCodec , namespace string , emptyObjectCreator func () T ,
127
+ options ... Option [T ],
114
128
) * ClientWithApply [T , C ] {
115
- typeClient := NewClient [T ](resource , client , parameterCodec , namespace , emptyObjectCreator )
129
+ typeClient := NewClient [T ](resource , client , parameterCodec , namespace , emptyObjectCreator , options ... )
116
130
return & ClientWithApply [T , C ]{
117
131
typeClient ,
118
132
alsoApplier [T , C ]{typeClient },
@@ -122,9 +136,9 @@ func NewClientWithApply[T objectWithMeta, C namedObject](
122
136
// NewClientWithListAndApply constructs a client with support for lists and applying declarative configurations.
123
137
func NewClientWithListAndApply [T objectWithMeta , L runtime.Object , C namedObject ](
124
138
resource string , client rest.Interface , parameterCodec runtime.ParameterCodec , namespace string , emptyObjectCreator func () T ,
125
- emptyListCreator func () L ,
139
+ emptyListCreator func () L , options ... Option [ T ],
126
140
) * ClientWithListAndApply [T , L , C ] {
127
- typeClient := NewClient [T ](resource , client , parameterCodec , namespace , emptyObjectCreator )
141
+ typeClient := NewClient [T ](resource , client , parameterCodec , namespace , emptyObjectCreator , options ... )
128
142
return & ClientWithListAndApply [T , L , C ]{
129
143
typeClient ,
130
144
alsoLister [T , L ]{typeClient , emptyListCreator },
@@ -146,6 +160,7 @@ func (c *Client[T]) GetNamespace() string {
146
160
func (c * Client [T ]) Get (ctx context.Context , name string , options metav1.GetOptions ) (T , error ) {
147
161
result := c .newObject ()
148
162
err := c .client .Get ().
163
+ UseProtobufAsDefaultIfPreferred (c .prefersProtobuf ).
149
164
NamespaceIfScoped (c .namespace , c .namespace != "" ).
150
165
Resource (c .resource ).
151
166
Name (name ).
@@ -181,6 +196,7 @@ func (l *alsoLister[T, L]) list(ctx context.Context, opts metav1.ListOptions) (L
181
196
timeout = time .Duration (* opts .TimeoutSeconds ) * time .Second
182
197
}
183
198
err := l .client .client .Get ().
199
+ UseProtobufAsDefaultIfPreferred (l .client .prefersProtobuf ).
184
200
NamespaceIfScoped (l .client .namespace , l .client .namespace != "" ).
185
201
Resource (l .client .resource ).
186
202
VersionedParams (& opts , l .client .parameterCodec ).
@@ -198,6 +214,7 @@ func (l *alsoLister[T, L]) watchList(ctx context.Context, opts metav1.ListOption
198
214
}
199
215
result = l .newList ()
200
216
err = l .client .client .Get ().
217
+ UseProtobufAsDefaultIfPreferred (l .client .prefersProtobuf ).
201
218
NamespaceIfScoped (l .client .namespace , l .client .namespace != "" ).
202
219
Resource (l .client .resource ).
203
220
VersionedParams (& opts , l .client .parameterCodec ).
@@ -215,6 +232,7 @@ func (c *Client[T]) Watch(ctx context.Context, opts metav1.ListOptions) (watch.I
215
232
}
216
233
opts .Watch = true
217
234
return c .client .Get ().
235
+ UseProtobufAsDefaultIfPreferred (c .prefersProtobuf ).
218
236
NamespaceIfScoped (c .namespace , c .namespace != "" ).
219
237
Resource (c .resource ).
220
238
VersionedParams (& opts , c .parameterCodec ).
@@ -226,6 +244,7 @@ func (c *Client[T]) Watch(ctx context.Context, opts metav1.ListOptions) (watch.I
226
244
func (c * Client [T ]) Create (ctx context.Context , obj T , opts metav1.CreateOptions ) (T , error ) {
227
245
result := c .newObject ()
228
246
err := c .client .Post ().
247
+ UseProtobufAsDefaultIfPreferred (c .prefersProtobuf ).
229
248
NamespaceIfScoped (c .namespace , c .namespace != "" ).
230
249
Resource (c .resource ).
231
250
VersionedParams (& opts , c .parameterCodec ).
@@ -239,6 +258,7 @@ func (c *Client[T]) Create(ctx context.Context, obj T, opts metav1.CreateOptions
239
258
func (c * Client [T ]) Update (ctx context.Context , obj T , opts metav1.UpdateOptions ) (T , error ) {
240
259
result := c .newObject ()
241
260
err := c .client .Put ().
261
+ UseProtobufAsDefaultIfPreferred (c .prefersProtobuf ).
242
262
NamespaceIfScoped (c .namespace , c .namespace != "" ).
243
263
Resource (c .resource ).
244
264
Name (obj .GetName ()).
@@ -253,6 +273,7 @@ func (c *Client[T]) Update(ctx context.Context, obj T, opts metav1.UpdateOptions
253
273
func (c * Client [T ]) UpdateStatus (ctx context.Context , obj T , opts metav1.UpdateOptions ) (T , error ) {
254
274
result := c .newObject ()
255
275
err := c .client .Put ().
276
+ UseProtobufAsDefaultIfPreferred (c .prefersProtobuf ).
256
277
NamespaceIfScoped (c .namespace , c .namespace != "" ).
257
278
Resource (c .resource ).
258
279
Name (obj .GetName ()).
@@ -267,6 +288,7 @@ func (c *Client[T]) UpdateStatus(ctx context.Context, obj T, opts metav1.UpdateO
267
288
// Delete takes name of the resource and deletes it. Returns an error if one occurs.
268
289
func (c * Client [T ]) Delete (ctx context.Context , name string , opts metav1.DeleteOptions ) error {
269
290
return c .client .Delete ().
291
+ UseProtobufAsDefaultIfPreferred (c .prefersProtobuf ).
270
292
NamespaceIfScoped (c .namespace , c .namespace != "" ).
271
293
Resource (c .resource ).
272
294
Name (name ).
@@ -282,6 +304,7 @@ func (l *alsoLister[T, L]) DeleteCollection(ctx context.Context, opts metav1.Del
282
304
timeout = time .Duration (* listOpts .TimeoutSeconds ) * time .Second
283
305
}
284
306
return l .client .client .Delete ().
307
+ UseProtobufAsDefaultIfPreferred (l .client .prefersProtobuf ).
285
308
NamespaceIfScoped (l .client .namespace , l .client .namespace != "" ).
286
309
Resource (l .client .resource ).
287
310
VersionedParams (& listOpts , l .client .parameterCodec ).
@@ -295,6 +318,7 @@ func (l *alsoLister[T, L]) DeleteCollection(ctx context.Context, opts metav1.Del
295
318
func (c * Client [T ]) Patch (ctx context.Context , name string , pt types.PatchType , data []byte , opts metav1.PatchOptions , subresources ... string ) (T , error ) {
296
319
result := c .newObject ()
297
320
err := c .client .Patch (pt ).
321
+ UseProtobufAsDefaultIfPreferred (c .prefersProtobuf ).
298
322
NamespaceIfScoped (c .namespace , c .namespace != "" ).
299
323
Resource (c .resource ).
300
324
Name (name ).
@@ -321,6 +345,7 @@ func (a *alsoApplier[T, C]) Apply(ctx context.Context, obj C, opts metav1.ApplyO
321
345
return * new (T ), fmt .Errorf ("obj.Name must be provided to Apply" )
322
346
}
323
347
err = a .client .client .Patch (types .ApplyPatchType ).
348
+ UseProtobufAsDefaultIfPreferred (a .client .prefersProtobuf ).
324
349
NamespaceIfScoped (a .client .namespace , a .client .namespace != "" ).
325
350
Resource (a .client .resource ).
326
351
Name (* obj .GetName ()).
@@ -348,6 +373,7 @@ func (a *alsoApplier[T, C]) ApplyStatus(ctx context.Context, obj C, opts metav1.
348
373
349
374
result := a .client .newObject ()
350
375
err = a .client .client .Patch (types .ApplyPatchType ).
376
+ UseProtobufAsDefaultIfPreferred (a .client .prefersProtobuf ).
351
377
NamespaceIfScoped (a .client .namespace , a .client .namespace != "" ).
352
378
Resource (a .client .resource ).
353
379
Name (* obj .GetName ()).
0 commit comments