Skip to content

Commit 7683eff

Browse files
committed
feat: Accept context to be passed to all client functions
1 parent eb48b18 commit 7683eff

File tree

1 file changed

+23
-27
lines changed

1 file changed

+23
-27
lines changed

client.go

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ type Client struct {
2828
client dynamic.Interface
2929
discoveryClient *discovery.DiscoveryClient
3030
groupResourceMapper meta.RESTMapper
31-
32-
schemaCache map[string]schema.GroupVersionKind
3331
}
3432

3533
// typedClients holds kubernetes clients for different API groups.
@@ -58,9 +56,7 @@ func NewClientUsingContext(path, context string) (*Client, error) {
5856
config *restclient.Config
5957
)
6058

61-
k8sClient := Client{
62-
schemaCache: make(map[string]schema.GroupVersionKind),
63-
}
59+
k8sClient := Client{}
6460

6561
if path == "" {
6662
// In cluster client if path is empty
@@ -127,9 +123,9 @@ func GetContextsFromConfig(path string) ([]string, error) {
127123
}
128124

129125
// GetNamedObject returns a specific kubernetes object
130-
func (k8s *Client) GetNamedObject(resource schema.GroupVersionResource, name string) (NamedObject, error) {
126+
func (k8s *Client) GetNamedObject(resource schema.GroupVersionResource, name string, ctx context.Context) (NamedObject, error) {
131127
resourceHandle := k8s.client.Resource(resource)
132-
rawObject, err := resourceHandle.Get(context.Background(), name, metav1.GetOptions{})
128+
rawObject, err := resourceHandle.Get(ctx, name, metav1.GetOptions{})
133129
if err != nil {
134130
return nil, err
135131
}
@@ -138,9 +134,9 @@ func (k8s *Client) GetNamedObject(resource schema.GroupVersionResource, name str
138134
}
139135

140136
// GetNamespacedObject returns a specific kubernetes object from a specific namespace
141-
func (k8s *Client) GetNamespacedObject(resource schema.GroupVersionResource, name, namespace string) (NamedObject, error) {
137+
func (k8s *Client) GetNamespacedObject(resource schema.GroupVersionResource, name, namespace string, ctx context.Context) (NamedObject, error) {
142138
resourceHandle := k8s.client.Resource(resource).Namespace(namespace)
143-
rawObject, err := resourceHandle.Get(context.Background(), name, metav1.GetOptions{})
139+
rawObject, err := resourceHandle.Get(ctx, name, metav1.GetOptions{})
144140
if err != nil {
145141
return nil, err
146142
}
@@ -149,36 +145,36 @@ func (k8s *Client) GetNamespacedObject(resource schema.GroupVersionResource, nam
149145
}
150146

151147
// ListAllObjects returns a list of all objects for a given type that is assumed to be global.
152-
func (k8s *Client) ListAllObjects(resource schema.GroupVersionResource, labelSelector, fieldSelector string) ([]NamedObject, error) {
153-
return k8s.list(resource, "", labelSelector, fieldSelector)
148+
func (k8s *Client) ListAllObjects(resource schema.GroupVersionResource, labelSelector, fieldSelector string, ctx context.Context) ([]NamedObject, error) {
149+
return k8s.list(resource, "", labelSelector, fieldSelector, ctx)
154150
}
155151

156152
// ListAllObjectsInNamespace returns a list of all objects for a given type in a given namespace.
157-
func (k8s *Client) ListAllObjectsInNamespace(resource schema.GroupVersionResource, namespace, labelSelector, fieldSelector string) ([]NamedObject, error) {
158-
return k8s.list(resource, namespace, labelSelector, fieldSelector)
153+
func (k8s *Client) ListAllObjectsInNamespace(resource schema.GroupVersionResource, namespace, labelSelector, fieldSelector string, ctx context.Context) ([]NamedObject, error) {
154+
return k8s.list(resource, namespace, labelSelector, fieldSelector, ctx)
159155
}
160156

161157
// ListAllObjectsInNamespaceMatching returns a list of all objects matching a given selector struct.
162158
// This struct is used in varios API objects like namespaceSelector or objectSelector.
163159
// Use ParseLabelSelector to create this struct from an existing object.
164-
func (k8s *Client) ListAllObjectsInNamespaceMatching(resource schema.GroupVersionResource, namespace string, labelMatchExpression metav1.LabelSelector, fieldSelector string) ([]NamedObject, error) {
160+
func (k8s *Client) ListAllObjectsInNamespaceMatching(resource schema.GroupVersionResource, namespace string, labelMatchExpression metav1.LabelSelector, fieldSelector string, ctx context.Context) ([]NamedObject, error) {
165161
labelSelector := metav1.FormatLabelSelector(&labelMatchExpression)
166-
return k8s.list(resource, namespace, labelSelector, fieldSelector)
162+
return k8s.list(resource, namespace, labelSelector, fieldSelector, ctx)
167163
}
168164

169165
// ListAllObjectsMatching returns a list of all objects matching a given selector struct.
170166
// This struct is used in varios API objects like namespaceSelector or objectSelector.
171167
// Use ParseLabelSelector to create this struct from an existing object.
172-
func (k8s *Client) ListAllObjectsMatching(resource schema.GroupVersionResource, labelMatchExpression metav1.LabelSelector, fieldSelector string) ([]NamedObject, error) {
168+
func (k8s *Client) ListAllObjectsMatching(resource schema.GroupVersionResource, labelMatchExpression metav1.LabelSelector, fieldSelector string, ctx context.Context) ([]NamedObject, error) {
173169
labelSelector := metav1.FormatLabelSelector(&labelMatchExpression)
174-
return k8s.list(resource, "", labelSelector, fieldSelector)
170+
return k8s.list(resource, "", labelSelector, fieldSelector, ctx)
175171
}
176172

177173
// list returns a list of objects for a given type.
178174
// Namespace, labelSelector and fieldSelector are optional arguments. If namespace is left empty,
179175
// a global resource is expected. If selector is left empty, all objects will
180176
// be returned.
181-
func (k8s *Client) list(resource schema.GroupVersionResource, namespace, labelSelector, fieldSelector string) ([]NamedObject, error) {
177+
func (k8s *Client) list(resource schema.GroupVersionResource, namespace, labelSelector, fieldSelector string, ctx context.Context) ([]NamedObject, error) {
182178
options := metav1.ListOptions{
183179
LabelSelector: labelSelector,
184180
FieldSelector: fieldSelector,
@@ -192,7 +188,7 @@ func (k8s *Client) list(resource schema.GroupVersionResource, namespace, labelSe
192188
resourceHandle = k8s.client.Resource(resource)
193189
}
194190

195-
list, err := resourceHandle.List(context.Background(), options)
191+
list, err := resourceHandle.List(ctx, options)
196192
if err != nil {
197193
return []NamedObject{}, err
198194
}
@@ -215,7 +211,7 @@ func (k8s *Client) list(resource schema.GroupVersionResource, namespace, labelSe
215211

216212
// Apply creates or updates a given kubernetes object.
217213
// If a namespace is set, the object will be created in that namespace.
218-
func (k8s *Client) Apply(resource schema.GroupVersionResource, object NamedObject, options metav1.ApplyOptions) error {
214+
func (k8s *Client) Apply(resource schema.GroupVersionResource, object NamedObject, options metav1.ApplyOptions, ctx context.Context) error {
219215
var (
220216
resourceHandle dynamic.ResourceInterface
221217
identifier string
@@ -233,7 +229,7 @@ func (k8s *Client) Apply(resource schema.GroupVersionResource, object NamedObjec
233229
Object: object,
234230
}
235231

236-
if _, err := resourceHandle.Apply(context.Background(), object.GetName(), unstructuredObject, options); err != nil {
232+
if _, err := resourceHandle.Apply(ctx, object.GetName(), unstructuredObject, options); err != nil {
237233
return errors.Wrapf(err, "failed to trigger apply for %s", identifier)
238234
}
239235

@@ -242,7 +238,7 @@ func (k8s *Client) Apply(resource schema.GroupVersionResource, object NamedObjec
242238

243239
// DeleteNamespaced removes a specific kubernetes object from a specific namespace.
244240
// If an empty namespace is given, the object will be treated as a cluster-wide resource.
245-
func (k8s *Client) DeleteNamespaced(resource schema.GroupVersionResource, name, namespace string) error {
241+
func (k8s *Client) DeleteNamespaced(resource schema.GroupVersionResource, name, namespace string, ctx context.Context) error {
246242
var (
247243
resourceHandle dynamic.ResourceInterface
248244
identifier string
@@ -256,7 +252,7 @@ func (k8s *Client) DeleteNamespaced(resource schema.GroupVersionResource, name,
256252
identifier = name
257253
}
258254

259-
if err := resourceHandle.Delete(context.Background(), name, metav1.DeleteOptions{}); err != nil {
255+
if err := resourceHandle.Delete(ctx, name, metav1.DeleteOptions{}); err != nil {
260256
return errors.Wrapf(err, "failed to trigger delete for %s", identifier)
261257
}
262258

@@ -265,7 +261,7 @@ func (k8s *Client) DeleteNamespaced(resource schema.GroupVersionResource, name,
265261

266262
// Patch applies a set of patches on a given kubernetes object.
267263
// The patches are applied as json patches.
268-
func (k8s *Client) Patch(resource schema.GroupVersionResource, object NamedObject, patches []PatchOperation, options metav1.PatchOptions) error {
264+
func (k8s *Client) Patch(resource schema.GroupVersionResource, object NamedObject, patches []PatchOperation, options metav1.PatchOptions, ctx context.Context) error {
269265
var (
270266
resourceHandle dynamic.ResourceInterface
271267
identifier string
@@ -284,7 +280,7 @@ func (k8s *Client) Patch(resource schema.GroupVersionResource, object NamedObjec
284280
return errors.Wrapf(err, "failed to marshal patch data for %s", identifier)
285281
}
286282

287-
if _, err := resourceHandle.Patch(context.Background(), object.GetName(), types.JSONPatchType, patchData, metav1.PatchOptions{}); err != nil {
283+
if _, err := resourceHandle.Patch(ctx, object.GetName(), types.JSONPatchType, patchData, metav1.PatchOptions{}); err != nil {
288284
return errors.Wrapf(err, "failed to apply patch for %s", identifier)
289285
}
290286

@@ -307,7 +303,7 @@ func (k8s *Client) GetServiceAccountToken(serviceAccountName, namespace string,
307303
}
308304

309305
if strings.ToLower(boundPodRef.Kind) != "pod" {
310-
return "", fmt.Errorf("bound object reference must be a pod or nil")
306+
return "", ErrInvalidBoundObjectRef{}
311307
}
312308
}
313309

@@ -324,7 +320,7 @@ func (k8s *Client) GetServiceAccountToken(serviceAccountName, namespace string,
324320
return "", err
325321
}
326322
if len(response.Status.Token) == 0 {
327-
return "", fmt.Errorf("no token in server response")
323+
return "", ErrNoToken{}
328324
}
329325

330326
return response.Status.Token, nil

0 commit comments

Comments
 (0)