...

Source file src/sigs.k8s.io/controller-runtime/pkg/client/interceptor/intercept.go

Documentation: sigs.k8s.io/controller-runtime/pkg/client/interceptor

     1  package interceptor
     2  
     3  import (
     4  	"context"
     5  
     6  	"k8s.io/apimachinery/pkg/api/meta"
     7  	"k8s.io/apimachinery/pkg/runtime"
     8  	"k8s.io/apimachinery/pkg/runtime/schema"
     9  	"k8s.io/apimachinery/pkg/watch"
    10  	"sigs.k8s.io/controller-runtime/pkg/client"
    11  )
    12  
    13  // Funcs contains functions that are called instead of the underlying client's methods.
    14  type Funcs struct {
    15  	Get               func(ctx context.Context, client client.WithWatch, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error
    16  	List              func(ctx context.Context, client client.WithWatch, list client.ObjectList, opts ...client.ListOption) error
    17  	Create            func(ctx context.Context, client client.WithWatch, obj client.Object, opts ...client.CreateOption) error
    18  	Delete            func(ctx context.Context, client client.WithWatch, obj client.Object, opts ...client.DeleteOption) error
    19  	DeleteAllOf       func(ctx context.Context, client client.WithWatch, obj client.Object, opts ...client.DeleteAllOfOption) error
    20  	Update            func(ctx context.Context, client client.WithWatch, obj client.Object, opts ...client.UpdateOption) error
    21  	Patch             func(ctx context.Context, client client.WithWatch, obj client.Object, patch client.Patch, opts ...client.PatchOption) error
    22  	Watch             func(ctx context.Context, client client.WithWatch, obj client.ObjectList, opts ...client.ListOption) (watch.Interface, error)
    23  	SubResource       func(client client.WithWatch, subResource string) client.SubResourceClient
    24  	SubResourceGet    func(ctx context.Context, client client.Client, subResourceName string, obj client.Object, subResource client.Object, opts ...client.SubResourceGetOption) error
    25  	SubResourceCreate func(ctx context.Context, client client.Client, subResourceName string, obj client.Object, subResource client.Object, opts ...client.SubResourceCreateOption) error
    26  	SubResourceUpdate func(ctx context.Context, client client.Client, subResourceName string, obj client.Object, opts ...client.SubResourceUpdateOption) error
    27  	SubResourcePatch  func(ctx context.Context, client client.Client, subResourceName string, obj client.Object, patch client.Patch, opts ...client.SubResourcePatchOption) error
    28  }
    29  
    30  // NewClient returns a new interceptor client that calls the functions in funcs instead of the underlying client's methods, if they are not nil.
    31  func NewClient(interceptedClient client.WithWatch, funcs Funcs) client.WithWatch {
    32  	return interceptor{
    33  		client: interceptedClient,
    34  		funcs:  funcs,
    35  	}
    36  }
    37  
    38  type interceptor struct {
    39  	client client.WithWatch
    40  	funcs  Funcs
    41  }
    42  
    43  var _ client.WithWatch = &interceptor{}
    44  
    45  func (c interceptor) GroupVersionKindFor(obj runtime.Object) (schema.GroupVersionKind, error) {
    46  	return c.client.GroupVersionKindFor(obj)
    47  }
    48  
    49  func (c interceptor) IsObjectNamespaced(obj runtime.Object) (bool, error) {
    50  	return c.client.IsObjectNamespaced(obj)
    51  }
    52  
    53  func (c interceptor) Get(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error {
    54  	if c.funcs.Get != nil {
    55  		return c.funcs.Get(ctx, c.client, key, obj, opts...)
    56  	}
    57  	return c.client.Get(ctx, key, obj, opts...)
    58  }
    59  
    60  func (c interceptor) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error {
    61  	if c.funcs.List != nil {
    62  		return c.funcs.List(ctx, c.client, list, opts...)
    63  	}
    64  	return c.client.List(ctx, list, opts...)
    65  }
    66  
    67  func (c interceptor) Create(ctx context.Context, obj client.Object, opts ...client.CreateOption) error {
    68  	if c.funcs.Create != nil {
    69  		return c.funcs.Create(ctx, c.client, obj, opts...)
    70  	}
    71  	return c.client.Create(ctx, obj, opts...)
    72  }
    73  
    74  func (c interceptor) Delete(ctx context.Context, obj client.Object, opts ...client.DeleteOption) error {
    75  	if c.funcs.Delete != nil {
    76  		return c.funcs.Delete(ctx, c.client, obj, opts...)
    77  	}
    78  	return c.client.Delete(ctx, obj, opts...)
    79  }
    80  
    81  func (c interceptor) Update(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error {
    82  	if c.funcs.Update != nil {
    83  		return c.funcs.Update(ctx, c.client, obj, opts...)
    84  	}
    85  	return c.client.Update(ctx, obj, opts...)
    86  }
    87  
    88  func (c interceptor) Patch(ctx context.Context, obj client.Object, patch client.Patch, opts ...client.PatchOption) error {
    89  	if c.funcs.Patch != nil {
    90  		return c.funcs.Patch(ctx, c.client, obj, patch, opts...)
    91  	}
    92  	return c.client.Patch(ctx, obj, patch, opts...)
    93  }
    94  
    95  func (c interceptor) DeleteAllOf(ctx context.Context, obj client.Object, opts ...client.DeleteAllOfOption) error {
    96  	if c.funcs.DeleteAllOf != nil {
    97  		return c.funcs.DeleteAllOf(ctx, c.client, obj, opts...)
    98  	}
    99  	return c.client.DeleteAllOf(ctx, obj, opts...)
   100  }
   101  
   102  func (c interceptor) Status() client.SubResourceWriter {
   103  	return c.SubResource("status")
   104  }
   105  
   106  func (c interceptor) SubResource(subResource string) client.SubResourceClient {
   107  	if c.funcs.SubResource != nil {
   108  		return c.funcs.SubResource(c.client, subResource)
   109  	}
   110  	return subResourceInterceptor{
   111  		subResourceName: subResource,
   112  		client:          c.client,
   113  		funcs:           c.funcs,
   114  	}
   115  }
   116  
   117  func (c interceptor) Scheme() *runtime.Scheme {
   118  	return c.client.Scheme()
   119  }
   120  
   121  func (c interceptor) RESTMapper() meta.RESTMapper {
   122  	return c.client.RESTMapper()
   123  }
   124  
   125  func (c interceptor) Watch(ctx context.Context, obj client.ObjectList, opts ...client.ListOption) (watch.Interface, error) {
   126  	if c.funcs.Watch != nil {
   127  		return c.funcs.Watch(ctx, c.client, obj, opts...)
   128  	}
   129  	return c.client.Watch(ctx, obj, opts...)
   130  }
   131  
   132  type subResourceInterceptor struct {
   133  	subResourceName string
   134  	client          client.Client
   135  	funcs           Funcs
   136  }
   137  
   138  var _ client.SubResourceClient = &subResourceInterceptor{}
   139  
   140  func (s subResourceInterceptor) Get(ctx context.Context, obj client.Object, subResource client.Object, opts ...client.SubResourceGetOption) error {
   141  	if s.funcs.SubResourceGet != nil {
   142  		return s.funcs.SubResourceGet(ctx, s.client, s.subResourceName, obj, subResource, opts...)
   143  	}
   144  	return s.client.SubResource(s.subResourceName).Get(ctx, obj, subResource, opts...)
   145  }
   146  
   147  func (s subResourceInterceptor) Create(ctx context.Context, obj client.Object, subResource client.Object, opts ...client.SubResourceCreateOption) error {
   148  	if s.funcs.SubResourceCreate != nil {
   149  		return s.funcs.SubResourceCreate(ctx, s.client, s.subResourceName, obj, subResource, opts...)
   150  	}
   151  	return s.client.SubResource(s.subResourceName).Create(ctx, obj, subResource, opts...)
   152  }
   153  
   154  func (s subResourceInterceptor) Update(ctx context.Context, obj client.Object, opts ...client.SubResourceUpdateOption) error {
   155  	if s.funcs.SubResourceUpdate != nil {
   156  		return s.funcs.SubResourceUpdate(ctx, s.client, s.subResourceName, obj, opts...)
   157  	}
   158  	return s.client.SubResource(s.subResourceName).Update(ctx, obj, opts...)
   159  }
   160  
   161  func (s subResourceInterceptor) Patch(ctx context.Context, obj client.Object, patch client.Patch, opts ...client.SubResourcePatchOption) error {
   162  	if s.funcs.SubResourcePatch != nil {
   163  		return s.funcs.SubResourcePatch(ctx, s.client, s.subResourceName, obj, patch, opts...)
   164  	}
   165  	return s.client.SubResource(s.subResourceName).Patch(ctx, obj, patch, opts...)
   166  }
   167  

View as plain text