@@ -18,6 +18,7 @@ package client
1818
1919import (
2020 "context"
21+ "errors"
2122 "fmt"
2223 "strings"
2324
@@ -292,18 +293,48 @@ func (c *client) Status() SubResourceWriter {
292293 return c .SubResource ("status" )
293294}
294295
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 }
297298}
298299
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 {
301302 client * client
302303 subResource string
303304}
304305
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+ }
307338
308339// SubResourceUpdateOptions holds all the possible configuration
309340// for a subresource update request.
@@ -398,42 +429,54 @@ func (po *SubResourcePatchOptions) ApplyToSubResourcePatch(o *SubResourcePatchOp
398429 }
399430}
400431
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 ())
404447
405448 switch obj .(type ) {
406449 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 ... )
408451 case * metav1.PartialObjectMetadata :
409452 return fmt .Errorf ("cannot update status using only metadata -- did you mean to patch?" )
410453 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 ... )
412455 }
413456}
414457
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 ())
418461 switch obj .(type ) {
419462 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 ... )
421464 case * metav1.PartialObjectMetadata :
422465 return fmt .Errorf ("cannot update status using only metadata -- did you mean to patch?" )
423466 default :
424- return sw .client .typedClient .UpdateSubResource (ctx , obj , sw .subResource , opts ... )
467+ return sc .client .typedClient .UpdateSubResource (ctx , obj , sc .subResource , opts ... )
425468 }
426469}
427470
428471// 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 ())
431474 switch obj .(type ) {
432475 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 ... )
434477 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 ... )
436479 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 ... )
438481 }
439482}
0 commit comments