forked from ironcore-dev/controller-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclientutils.go
More file actions
636 lines (559 loc) · 19.7 KB
/
clientutils.go
File metadata and controls
636 lines (559 loc) · 19.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and IronCore contributors
// SPDX-License-Identifier: Apache-2.0
// Package clientutils provides utilities for working with the client package of
// controller-runtime.
package clientutils
import (
"context"
"encoding/json"
"fmt"
"reflect"
"github.com/ironcore-dev/controller-utils/metautils"
"github.com/ironcore-dev/controller-utils/unstructuredutils"
"k8s.io/apimachinery/pkg/api/equality"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/conversion"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
type clientMeta interface {
Scheme() *runtime.Scheme
RESTMapper() meta.RESTMapper
GroupVersionKindFor(obj runtime.Object) (schema.GroupVersionKind, error)
IsObjectNamespaced(obj runtime.Object) (bool, error)
}
type nonReaderClient interface {
client.Writer
client.StatusClient
client.SubResourceClientConstructor
clientMeta
}
type readerClient struct {
client.Reader
nonReaderClient
}
// ReaderClient returns a client.Client that uses the given client.Reader for all read operations and the
// client.Client for the remaining operations.
func ReaderClient(r client.Reader, c client.Client) client.Client {
return readerClient{r, c}
}
// IgnoreAlreadyExists returns nil if the given error matches apierrors.IsAlreadyExists.
func IgnoreAlreadyExists(err error) error {
if apierrors.IsAlreadyExists(err) {
return nil
}
return err
}
// CreateMultipleFromFile creates multiple objects by reading the given file as unstructured objects and then creating
// the read objects using the given client and options.
func CreateMultipleFromFile(ctx context.Context, c client.Client, filename string, opts ...client.CreateOption) ([]unstructured.Unstructured, error) {
objs, err := unstructuredutils.ReadFile(filename)
if err != nil {
return nil, err
}
if err := CreateMultiple(ctx, c, unstructuredutils.UnstructuredSliceToObjectSliceNoCopy(objs), opts...); err != nil {
return nil, err
}
return objs, nil
}
// CreateMultiple creates multiple objects using the given client and options.
func CreateMultiple(ctx context.Context, c client.Client, objs []client.Object, opts ...client.CreateOption) error {
for _, obj := range objs {
if err := c.Create(ctx, obj, opts...); err != nil {
return fmt.Errorf("error creating object %s: %w",
client.ObjectKeyFromObject(obj), err)
}
}
return nil
}
// GetRequest is a request to get an object with the given key and object (that is later used to write the result into).
type GetRequest struct {
Key client.ObjectKey
Object client.Object
}
// GetRequestFromObject converts the given client.Object to a GetRequest. Namespace and name should be present on
// the object.
func GetRequestFromObject(obj client.Object) GetRequest {
return GetRequest{
Key: client.ObjectKeyFromObject(obj),
Object: obj,
}
}
// GetRequestsFromObjects converts each client.Object into a GetRequest using GetRequestFromObject.
func GetRequestsFromObjects(objs []client.Object) []GetRequest {
if objs == nil {
return nil
}
res := make([]GetRequest, 0, len(objs))
for _, obj := range objs {
res = append(res, GetRequestFromObject(obj))
}
return res
}
// ObjectsFromGetRequests retrieves all client.Object objects from the given slice of GetRequest.
func ObjectsFromGetRequests(reqs []GetRequest) []client.Object {
if reqs == nil {
return nil
}
objs := make([]client.Object, 0, len(reqs))
for _, req := range reqs {
objs = append(objs, req.Object)
}
return objs
}
type getRequestTypedKey struct {
typ reflect.Type
objectKey client.ObjectKey
}
type getRequestUnstructuredKey struct {
gvk schema.GroupVersionKind
objectKey client.ObjectKey
}
// GetRequestSet is a set of GetRequest.
//
// Internally, the objects are differentiated by either being typed or unstructured.
// For unstructured objects, the group version kind they supply alongside their client.ObjectKey is used as identity.
// For typed objects, their element type (all typed objects *have* to be pointers to structs) alongside their
// client.ObjectKey is used as identity.
// If a typed object is *not* a pointer to a struct, a panic will happen.
type GetRequestSet struct {
typed map[getRequestTypedKey]client.Object
unstructured map[getRequestUnstructuredKey]client.Object
}
func (s *GetRequestSet) unstructuredKey(req GetRequest) getRequestUnstructuredKey {
u := req.Object.(*unstructured.Unstructured)
return getRequestUnstructuredKey{
gvk: u.GroupVersionKind(),
objectKey: req.Key,
}
}
func (s *GetRequestSet) typedKey(req GetRequest) getRequestTypedKey {
t := reflect.TypeOf(req.Object)
// Taken from runtime.Scheme.AddKnownTypes.
// In this case it's fine to panic as we distinguish between typed and unstructured
// objects beforehand.
if t.Kind() != reflect.Ptr {
panic("All types must be pointers to structs")
}
t = t.Elem()
if t.Kind() != reflect.Struct {
panic("All types must be pointers to struct")
}
return getRequestTypedKey{
typ: t,
objectKey: req.Key,
}
}
// Insert inserts the given items into the set.
func (s *GetRequestSet) Insert(items ...GetRequest) {
for _, item := range items {
switch item.Object.(type) {
case *unstructured.Unstructured:
s.unstructured[s.unstructuredKey(item)] = item.Object
default:
s.typed[s.typedKey(item)] = item.Object
}
}
}
// Len returns the length of the set.
func (s *GetRequestSet) Len() int {
return len(s.typed) + len(s.unstructured)
}
// Has checks if the given item is present in the set.
func (s *GetRequestSet) Has(item GetRequest) bool {
var ok bool
switch item.Object.(type) {
case *unstructured.Unstructured:
_, ok = s.unstructured[s.unstructuredKey(item)]
default:
_, ok = s.typed[s.typedKey(item)]
}
return ok
}
// Delete deletes the given items from the set, if they were present.
func (s *GetRequestSet) Delete(items ...GetRequest) {
for _, item := range items {
switch item.Object.(type) {
case *unstructured.Unstructured:
delete(s.unstructured, s.unstructuredKey(item))
default:
delete(s.typed, s.typedKey(item))
}
}
}
// Iterate iterates through the get requests of this set using the given function.
// If the function returns true (i.e. stop), the iteration is canceled.
func (s *GetRequestSet) Iterate(f func(GetRequest) (cont bool)) {
for k, v := range s.typed {
if cont := f(GetRequest{Key: k.objectKey, Object: v}); !cont {
return
}
}
for k, v := range s.unstructured {
if cont := f(GetRequest{Key: k.objectKey, Object: v}); !cont {
return
}
}
}
// List returns all GetRequests of this set.
func (s *GetRequestSet) List() []GetRequest {
res := make([]GetRequest, 0, s.Len())
s.Iterate(func(request GetRequest) (cont bool) {
res = append(res, request)
return true
})
return res
}
// NewGetRequestSet creates a new set of GetRequest.
//
// Internally, the objects are differentiated by either being typed or unstructured.
// For unstructured objects, the group version kind they supply alongside their client.ObjectKey is used as identity.
// For typed objects, their element type (all typed objects *have* to be pointers to structs) alongside their
// client.ObjectKey is used as identity.
// If a typed object is *not* a pointer to a struct, a panic will happen.
func NewGetRequestSet(items ...GetRequest) *GetRequestSet {
s := &GetRequestSet{
typed: make(map[getRequestTypedKey]client.Object),
unstructured: make(map[getRequestUnstructuredKey]client.Object),
}
s.Insert(items...)
return s
}
// GetMultipleFromFile creates multiple objects by reading the given file as unstructured objects and then creating
// the read objects using the given client and options.
func GetMultipleFromFile(ctx context.Context, c client.Client, filename string) ([]unstructured.Unstructured, error) {
objs, err := unstructuredutils.ReadFile(filename)
if err != nil {
return nil, err
}
reqs := make([]GetRequest, 0, len(objs))
for i := range objs {
obj := &objs[i]
reqs = append(reqs, GetRequest{
Key: client.ObjectKeyFromObject(obj),
Object: obj,
})
}
if err := GetMultiple(ctx, c, reqs); err != nil {
return nil, err
}
return objs, nil
}
// GetMultiple gets multiple objects using the given client. The results are written back into the given GetRequest.
func GetMultiple(ctx context.Context, c client.Client, reqs []GetRequest) error {
for _, req := range reqs {
if err := c.Get(ctx, req.Key, req.Object); err != nil {
return fmt.Errorf("error getting object %s: %w", req.Key, err)
}
}
return nil
}
// apply is a PatchProvider always providing a server-side apply patch.
type apply struct{}
// PatchFor implements PatchProvider.
func (a apply) PatchFor(obj client.Object) client.Patch {
return applyPatch{}
}
// ApplyAll provides a server-side apply patch for any given object.
var ApplyAll = apply{}
// PatchProvider retrieves a patch for any given object.
type PatchProvider interface {
PatchFor(obj client.Object) client.Patch
}
// PatchRequest is the request to patch an object with a patch.
type PatchRequest struct {
Object client.Object
Patch client.Patch
}
// applyPatch uses server-side apply semantics without relying on the deprecated client.Apply constant.
type applyPatch struct{}
func (applyPatch) Type() types.PatchType {
return types.ApplyPatchType
}
func (applyPatch) Data(obj client.Object) ([]byte, error) {
return json.Marshal(obj)
}
// PatchRequestFromObjectAndProvider is a shorthand to create a PatchRequest using a client.Object and PatchProvider.
func PatchRequestFromObjectAndProvider(obj client.Object, provider PatchProvider) PatchRequest {
return PatchRequest{
Object: obj,
Patch: provider.PatchFor(obj),
}
}
// PatchRequestsFromObjectsAndProvider converts all client.Object objects to PatchRequest using
// PatchRequestFromObjectAndProvider.
func PatchRequestsFromObjectsAndProvider(objs []client.Object, provider PatchProvider) []PatchRequest {
if objs == nil {
return nil
}
res := make([]PatchRequest, 0, len(objs))
for _, obj := range objs {
res = append(res, PatchRequestFromObjectAndProvider(obj, provider))
}
return res
}
// ObjectsFromPatchRequests extracts all client.Object objects from the given slice of PatchRequest.
func ObjectsFromPatchRequests(reqs []PatchRequest) []client.Object {
if reqs == nil {
return nil
}
objs := make([]client.Object, 0, len(reqs))
for _, req := range reqs {
objs = append(objs, req.Object)
}
return objs
}
// PatchMultiple executes multiple PatchRequest with the given client.PatchOption.
func PatchMultiple(ctx context.Context, c client.Client, reqs []PatchRequest, opts ...client.PatchOption) error {
for _, req := range reqs {
if err := c.Patch(ctx, req.Object, req.Patch, opts...); err != nil {
return fmt.Errorf("error patching object %s: %w",
client.ObjectKeyFromObject(req.Object),
err,
)
}
}
return nil
}
// PatchMultipleFromFile patches all objects from the given filename using the patchFor function.
// The returned unstructured.Unstructured objects contain the result of applying them.
func PatchMultipleFromFile(
ctx context.Context,
c client.Client,
filename string,
patchProvider PatchProvider,
opts ...client.PatchOption,
) ([]unstructured.Unstructured, error) {
objs, err := unstructuredutils.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("error reading file: %w", err)
}
reqs := make([]PatchRequest, 0, len(objs))
for i := range objs {
obj := &objs[i]
reqs = append(reqs, PatchRequest{obj, patchProvider.PatchFor(obj)})
}
if err := PatchMultiple(ctx, c, reqs, opts...); err != nil {
return nil, err
}
return objs, nil
}
// DeleteMultipleFromFile deletes all client.Object objects from the given file with the given
// client.DeleteOption options.
func DeleteMultipleFromFile(ctx context.Context, c client.Client, filename string, opts ...client.DeleteOption) error {
us, err := unstructuredutils.ReadFile(filename)
if err != nil {
return fmt.Errorf("error reading file: %w", err)
}
objs := unstructuredutils.UnstructuredSliceToObjectSliceNoCopy(us)
return DeleteMultiple(ctx, c, objs, opts...)
}
// DeleteMultiple deletes multiple given client.Object objects using the given client.DeleteOption options.
func DeleteMultiple(ctx context.Context, c client.Client, objs []client.Object, opts ...client.DeleteOption) error {
for _, obj := range objs {
if err := c.Delete(ctx, obj, opts...); err != nil {
return fmt.Errorf("error deleting object %s: %w",
client.ObjectKeyFromObject(obj),
err,
)
}
}
return nil
}
// ListAndFilter is a shorthand for doing a client.Client.List followed by filtering the list's elements
// with the given function.
func ListAndFilter(ctx context.Context, c client.Client, list client.ObjectList, filterFunc func(object client.Object) (bool, error), opts ...client.ListOption) error {
if err := c.List(ctx, list, opts...); err != nil {
return err
}
items, err := metautils.ExtractList(list)
if err != nil {
return fmt.Errorf("error extracting list: %w", err)
}
var filtered []client.Object
for _, item := range items {
ok, err := filterFunc(item)
if err != nil {
return err
}
if ok {
item := item
filtered = append(filtered, item)
}
}
if err := metautils.SetList(list, filtered); err != nil {
return fmt.Errorf("error setting list: %w", err)
}
return nil
}
// ListAndFilterControlledBy is a shorthand for doing a client.List followed by filtering the list's elements
// using metautils.IsControlledBy.
func ListAndFilterControlledBy(ctx context.Context, c client.Client, owner client.Object, list client.ObjectList, opts ...client.ListOption) error {
scheme := c.Scheme()
return ListAndFilter(ctx, c, list, func(object client.Object) (bool, error) {
return metautils.IsControlledBy(scheme, owner, object)
}, opts...)
}
func setObject(dst, src client.Object) error {
dstV, err := conversion.EnforcePtr(dst)
if err != nil {
return err
}
srcV, err := conversion.EnforcePtr(src)
if err != nil {
return err
}
if !srcV.Type().AssignableTo(dstV.Type()) {
return fmt.Errorf("cannot assign %T to %T", src, dst)
}
dstV.Set(srcV.Convert(dstV.Type()))
return nil
}
// IsOlderThan returns a function that determines whether an object is older than another.
func IsOlderThan(obj client.Object) func(other client.Object) (bool, error) {
return func(other client.Object) (bool, error) {
return obj.GetCreationTimestamp().After(other.GetCreationTimestamp().Time), nil
}
}
// CreateOrUseAndPatch traverses through a slice of objects and tries to find a matching object using matchFunc.
// If it does, the matching object is set to the object, optionally patched and returned.
// If multiple objects match, the winning object is the oldest.
// If no object matches, initFunc is called and the new object is created.
// mutateFunc is optional, if none is specified no mutation will happen.
func CreateOrUseAndPatch(
ctx context.Context,
c client.Client,
objects []client.Object,
obj client.Object,
matchFunc func() (bool, error),
lessFunc func(other client.Object) (bool, error),
mutateFunc func() error,
) (controllerutil.OperationResult, []client.Object, error) {
var (
base = obj.DeepCopyObject().(client.Object)
best client.Object
other = make([]client.Object, 0, len(objects))
)
for _, object := range objects {
if err := setObject(obj, object); err != nil {
return controllerutil.OperationResultNone, nil, err
}
match, err := matchFunc()
if err != nil {
return controllerutil.OperationResultNone, nil, err
}
if match {
if best == nil {
best = object
continue
}
less, err := lessFunc(best)
if err != nil {
return controllerutil.OperationResultNone, nil, err
}
if !less {
other = append(other, best)
best = object
continue
}
}
other = append(other, object)
}
if best != nil {
if err := setObject(obj, best); err != nil {
return controllerutil.OperationResultNone, nil, err
}
baseObj := obj.DeepCopyObject().(client.Object)
if mutateFunc != nil {
if err := mutateFunc(); err != nil {
return controllerutil.OperationResultNone, nil, err
}
}
if equality.Semantic.DeepEqual(baseObj, obj) {
return controllerutil.OperationResultNone, other, nil
}
if err := c.Patch(ctx, obj, client.MergeFrom(baseObj)); err != nil {
return controllerutil.OperationResultNone, nil, err
}
return controllerutil.OperationResultUpdated, other, nil
}
if err := setObject(obj, base); err != nil {
return controllerutil.OperationResultNone, nil, err
}
if mutateFunc != nil {
if err := mutateFunc(); err != nil {
return controllerutil.OperationResultNone, nil, err
}
}
if err := c.Create(ctx, obj); err != nil {
return controllerutil.OperationResultNone, nil, err
}
return controllerutil.OperationResultCreated, other, nil
}
// DeleteIfExists deletes the given object, if it exists. It returns any non apierrors.IsNotFound error
// and whether the object actually existed or not.
func DeleteIfExists(ctx context.Context, c client.Client, obj client.Object, opts ...client.DeleteOption) (existed bool, err error) {
if err := c.Delete(ctx, obj, opts...); err != nil {
if !apierrors.IsNotFound(err) {
return false, err
}
return false, nil
}
return true, nil
}
// DeleteMultipleIfExist deletes the given objects, if they exist. It returns any non apierrors.IsNotFound error
// and any object that existed before issuing the delete request.
func DeleteMultipleIfExist(ctx context.Context, c client.Client, objs []client.Object, opts ...client.DeleteOption) (existed []client.Object, err error) {
for i, obj := range objs {
ok, err := DeleteIfExists(ctx, c, obj, opts...)
if err != nil {
return existed, fmt.Errorf("[object %d]: error deleting %v: %w", i, obj, err)
}
if ok {
obj := obj
existed = append(existed, obj)
}
}
return existed, nil
}
// PatchAddFinalizer issues a patch to add the given finalizer to the given object.
// The client.Patch method will be called regardless whether the finalizer was already present or not.
func PatchAddFinalizer(ctx context.Context, c client.Client, obj client.Object, finalizer string) error {
baseObj := obj.DeepCopyObject().(client.Object)
controllerutil.AddFinalizer(obj, finalizer)
return c.Patch(ctx, obj, client.MergeFrom(baseObj))
}
// PatchRemoveFinalizer issues a patch to remove the given finalizer from the given object.
// The client.Patch method will be called regardless whether the finalizer was already gone or not.
func PatchRemoveFinalizer(ctx context.Context, c client.Client, obj client.Object, finalizer string) error {
baseObj := obj.DeepCopyObject().(client.Object)
controllerutil.RemoveFinalizer(obj, finalizer)
return c.Patch(ctx, obj, client.MergeFrom(baseObj))
}
// PatchEnsureFinalizer checks if the given object has the given finalizer and, if not, issues a patch request
// to add it. The modified result reports whether the object had to be modified.
func PatchEnsureFinalizer(ctx context.Context, c client.Client, obj client.Object, finalizer string) (modified bool, err error) {
if controllerutil.ContainsFinalizer(obj, finalizer) {
return false, nil
}
if err := PatchAddFinalizer(ctx, c, obj, finalizer); err != nil {
return false, err
}
return true, nil
}
// PatchEnsureNoFinalizer checks if the given object has the given finalizer and, if yes, issues a patch request
// to remove it. The modified result reports whether the object had to be modified.
func PatchEnsureNoFinalizer(ctx context.Context, c client.Client, obj client.Object, finalizer string) (modified bool, err error) {
if !controllerutil.ContainsFinalizer(obj, finalizer) {
return false, nil
}
if err := PatchRemoveFinalizer(ctx, c, obj, finalizer); err != nil {
return false, err
}
return true, nil
}