@@ -18,6 +18,7 @@ package client
18
18
19
19
import (
20
20
"context"
21
+ "errors"
21
22
"fmt"
22
23
"strings"
23
24
@@ -292,18 +293,48 @@ func (c *client) Status() SubResourceWriter {
292
293
return c .SubResource ("status" )
293
294
}
294
295
295
- func (c * client ) SubResource (subResource string ) SubResourceWriter {
296
- return & subResourceWriter {client : c , subResource : subResource }
296
+ func (c * client ) SubResource (subResource string ) SubResourceClient {
297
+ return & subResourceClient {client : c , subResource : subResource }
297
298
}
298
299
299
- // subResourceWriter is client.SubResourceWriter that writes to subresources.
300
- type subResourceWriter struct {
300
+ // subResourceClient is client.SubResourceWriter that writes to subresources.
301
+ type subResourceClient struct {
301
302
client * client
302
303
subResource string
303
304
}
304
305
305
- // ensure subResourceWriter implements client.SubResourceWriter.
306
- var _ SubResourceWriter = & subResourceWriter {}
306
+ // ensure subResourceClient implements client.SubResourceClient.
307
+ var _ SubResourceClient = & subResourceClient {}
308
+
309
+ // SubResourceGetOptions holds all the possible configuration
310
+ // for a subresource Get request.
311
+ type SubResourceGetOptions struct {
312
+ Raw * metav1.GetOptions
313
+ }
314
+
315
+ // ApplyToSubResourceGet updates the configuaration to the given get options.
316
+ func (getOpt * SubResourceGetOptions ) ApplyToSubResourceGet (o * SubResourceGetOptions ) {
317
+ if getOpt .Raw != nil {
318
+ o .Raw = getOpt .Raw
319
+ }
320
+ }
321
+
322
+ // ApplyOptions applues the given options.
323
+ func (getOpt * SubResourceGetOptions ) ApplyOptions (opts []SubResourceGetOption ) * SubResourceGetOptions {
324
+ for _ , o := range opts {
325
+ o .ApplyToSubResourceGet (getOpt )
326
+ }
327
+
328
+ return getOpt
329
+ }
330
+
331
+ // AsGetOptions returns the configured options as *metav1.GetOptions.
332
+ func (getOpt * SubResourceGetOptions ) AsGetOptions () * metav1.GetOptions {
333
+ if getOpt .Raw == nil {
334
+ return & metav1.GetOptions {}
335
+ }
336
+ return getOpt .Raw
337
+ }
307
338
308
339
// SubResourceUpdateOptions holds all the possible configuration
309
340
// for a subresource update request.
@@ -398,42 +429,54 @@ func (po *SubResourcePatchOptions) ApplyToSubResourcePatch(o *SubResourcePatchOp
398
429
}
399
430
}
400
431
401
- func (sw * subResourceWriter ) Create (ctx context.Context , obj Object , subResource Object , opts ... SubResourceCreateOption ) error {
402
- defer sw .client .resetGroupVersionKind (obj , obj .GetObjectKind ().GroupVersionKind ())
403
- defer sw .client .resetGroupVersionKind (subResource , subResource .GetObjectKind ().GroupVersionKind ())
432
+ func (sc * subResourceClient ) Get (ctx context.Context , obj Object , subResource Object , opts ... SubResourceGetOption ) error {
433
+ switch obj .(type ) {
434
+ case * unstructured.Unstructured :
435
+ return sc .client .unstructuredClient .GetSubResource (ctx , obj , subResource , sc .subResource , opts ... )
436
+ case * metav1.PartialObjectMetadata :
437
+ return errors .New ("can not get subresource using only metadata" )
438
+ default :
439
+ return sc .client .typedClient .GetSubResource (ctx , obj , subResource , sc .subResource , opts ... )
440
+ }
441
+ }
442
+
443
+ // Create implements client.SubResourceClient
444
+ func (sc * subResourceClient ) Create (ctx context.Context , obj Object , subResource Object , opts ... SubResourceCreateOption ) error {
445
+ defer sc .client .resetGroupVersionKind (obj , obj .GetObjectKind ().GroupVersionKind ())
446
+ defer sc .client .resetGroupVersionKind (subResource , subResource .GetObjectKind ().GroupVersionKind ())
404
447
405
448
switch obj .(type ) {
406
449
case * unstructured.Unstructured :
407
- return sw .client .unstructuredClient .CreateSubResource (ctx , obj , subResource , sw .subResource , opts ... )
450
+ return sc .client .unstructuredClient .CreateSubResource (ctx , obj , subResource , sc .subResource , opts ... )
408
451
case * metav1.PartialObjectMetadata :
409
452
return fmt .Errorf ("cannot update status using only metadata -- did you mean to patch?" )
410
453
default :
411
- return sw .client .typedClient .CreateSubResource (ctx , obj , subResource , sw .subResource , opts ... )
454
+ return sc .client .typedClient .CreateSubResource (ctx , obj , subResource , sc .subResource , opts ... )
412
455
}
413
456
}
414
457
415
- // Update implements client.SubResourceWriter.
416
- func (sw * subResourceWriter ) Update (ctx context.Context , obj Object , opts ... SubResourceUpdateOption ) error {
417
- defer sw .client .resetGroupVersionKind (obj , obj .GetObjectKind ().GroupVersionKind ())
458
+ // Update implements client.SubResourceClient
459
+ func (sc * subResourceClient ) Update (ctx context.Context , obj Object , opts ... SubResourceUpdateOption ) error {
460
+ defer sc .client .resetGroupVersionKind (obj , obj .GetObjectKind ().GroupVersionKind ())
418
461
switch obj .(type ) {
419
462
case * unstructured.Unstructured :
420
- return sw .client .unstructuredClient .UpdateSubResource (ctx , obj , sw .subResource , opts ... )
463
+ return sc .client .unstructuredClient .UpdateSubResource (ctx , obj , sc .subResource , opts ... )
421
464
case * metav1.PartialObjectMetadata :
422
465
return fmt .Errorf ("cannot update status using only metadata -- did you mean to patch?" )
423
466
default :
424
- return sw .client .typedClient .UpdateSubResource (ctx , obj , sw .subResource , opts ... )
467
+ return sc .client .typedClient .UpdateSubResource (ctx , obj , sc .subResource , opts ... )
425
468
}
426
469
}
427
470
428
471
// Patch implements client.SubResourceWriter.
429
- func (sw * subResourceWriter ) Patch (ctx context.Context , obj Object , patch Patch , opts ... SubResourcePatchOption ) error {
430
- defer sw .client .resetGroupVersionKind (obj , obj .GetObjectKind ().GroupVersionKind ())
472
+ func (sc * subResourceClient ) Patch (ctx context.Context , obj Object , patch Patch , opts ... SubResourcePatchOption ) error {
473
+ defer sc .client .resetGroupVersionKind (obj , obj .GetObjectKind ().GroupVersionKind ())
431
474
switch obj .(type ) {
432
475
case * unstructured.Unstructured :
433
- return sw .client .unstructuredClient .PatchSubResource (ctx , obj , sw .subResource , patch , opts ... )
476
+ return sc .client .unstructuredClient .PatchSubResource (ctx , obj , sc .subResource , patch , opts ... )
434
477
case * metav1.PartialObjectMetadata :
435
- return sw .client .metadataClient .PatchSubResource (ctx , obj , sw .subResource , patch , opts ... )
478
+ return sc .client .metadataClient .PatchSubResource (ctx , obj , sc .subResource , patch , opts ... )
436
479
default :
437
- return sw .client .typedClient .PatchSubResource (ctx , obj , sw .subResource , patch , opts ... )
480
+ return sc .client .typedClient .PatchSubResource (ctx , obj , sc .subResource , patch , opts ... )
438
481
}
439
482
}
0 commit comments