-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathobjects.go
More file actions
669 lines (563 loc) · 18.2 KB
/
objects.go
File metadata and controls
669 lines (563 loc) · 18.2 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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
package machinery
import (
"context"
"fmt"
"strconv"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
machinerytypes "k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/util/csaupgrade"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"pkg.package-operator.run/boxcutter/machinery/types"
)
// ObjectEngine reconciles individual objects.
type ObjectEngine struct {
scheme *runtime.Scheme
cache client.Reader
writer client.Writer
comparator comparator
fieldOwner string
systemPrefix string
}
// NewObjectEngine returns a new Engine instance.
func NewObjectEngine(
scheme *runtime.Scheme,
cache client.Reader,
writer client.Writer,
comparator comparator,
fieldOwner string,
systemPrefix string,
) *ObjectEngine {
return &ObjectEngine{
scheme: scheme,
cache: cache,
writer: writer,
comparator: comparator,
fieldOwner: fieldOwner,
systemPrefix: systemPrefix,
}
}
// Object interface combining client.Object and runtime.Object.
type Object interface {
client.Object
runtime.Object
}
type objectEngineOwnerStrategy interface {
SetControllerReference(owner, obj metav1.Object) error
GetController(obj metav1.Object) (metav1.OwnerReference, bool)
IsController(owner, obj metav1.Object) bool
CopyOwnerReferences(objA, objB metav1.Object)
ReleaseController(obj metav1.Object)
RemoveOwner(owner, obj metav1.Object)
}
type comparator interface {
Compare(
desiredObject, actualObject Object,
opts ...types.ComparatorOption,
) (res CompareResult, err error)
}
const (
managedByLabel string = "app.kubernetes.io/managed-by"
managedByLabelValue string = "boxcutter"
boxcutterManagedLabel string = "boxcutter-managed"
)
// Teardown ensures the given object is safely removed from the cluster.
func (e *ObjectEngine) Teardown(
ctx context.Context,
revision int64, // Revision number, must start at 1.
desiredObject Object,
opts ...types.ObjectTeardownOption,
) (objectGone bool, err error) {
var options types.ObjectTeardownOptions
for _, opt := range opts {
opt.ApplyToObjectTeardownOptions(&options)
}
options.Default()
// Sanity checks.
if revision == 0 {
panic("owner revision must be set and start at 1")
}
// The "orphan" finalizer on the owner object indicates that the Owner
// is being deleted and orphaning its dependents. This finalizer is
// managed by KCM's gc controller. If we observe it, we are racing with
// the gc controller, and should not delete dependent objects.
if options.Orphan || (options.Owner != nil && controllerutil.ContainsFinalizer(options.Owner, "orphan")) {
err := e.removeBoxcutterManagedLabelsAndAnnotations(ctx, e.writer, desiredObject)
if err != nil {
return false, err
}
return true, nil
}
// Lookup actual object state on cluster.
actualObject := desiredObject.DeepCopyObject().(Object)
err = e.cache.Get(
ctx, client.ObjectKeyFromObject(desiredObject), actualObject,
)
if meta.IsNoMatchError(err) {
// API no longer registered.
// Consider the object deleted.
return true, nil
}
if errors.IsNotFound(err) {
// Object is gone, yay!
return true, nil
}
if err != nil {
return false, fmt.Errorf("getting object before deletion: %w", err)
}
// Check revision matches.
actualRevision, err := e.getObjectRevision(actualObject)
if err != nil {
return false, fmt.Errorf("getting object revision: %w", err)
}
// Object is not owned by this revision
if actualRevision != revision {
if options.Owner == nil {
// No Owner to remove from the object, return.
return true, nil
}
ctrlSit, _ := e.detectOwner(options.Owner, options.OwnerStrategy, actualObject, nil)
if ctrlSit != ctrlSituationIsController {
// Remove us from owners list:
patch := actualObject.DeepCopyObject().(Object)
options.OwnerStrategy.RemoveOwner(options.Owner, patch)
return true, e.writer.Patch(ctx, patch, client.MergeFrom(actualObject))
}
}
// Actually delete the object.
writer := e.writer
if options.TeardownWriter != nil {
writer = options.TeardownWriter
}
err = writer.Delete(ctx, desiredObject, client.Preconditions{
UID: ptr.To(actualObject.GetUID()),
ResourceVersion: ptr.To(actualObject.GetResourceVersion()),
})
if errors.IsNotFound(err) {
return true, nil
}
// TODO: Catch Precondition errors?
if err != nil {
return false, fmt.Errorf("deleting object: %w", err)
}
// need to wait for Not Found Error to ensure finalizers have been progressed.
return false, nil
}
// Reconcile runs actions to bring actual state closer to desired.
func (e *ObjectEngine) Reconcile(
ctx context.Context,
revision int64, // Revision number, must start at 1.
desiredObject Object,
opts ...types.ObjectReconcileOption,
) (ObjectResult, error) {
var options types.ObjectReconcileOptions
for _, opt := range opts {
opt.ApplyToObjectReconcileOptions(&options)
}
labels := desiredObject.GetLabels()
if labels == nil {
labels = map[string]string{}
}
labels[managedByLabel] = managedByLabelValue
desiredObject.SetLabels(labels)
options.Default()
// Sanity checks.
if revision == 0 {
panic("owner revision must be set and start at 1")
}
if err := ensureGVKIsSet(desiredObject, e.scheme); err != nil {
return nil, err
}
// Copy because some client actions will modify the object.
desiredObject = desiredObject.DeepCopyObject().(Object)
e.setObjectRevision(desiredObject, revision)
if options.Owner != nil {
if err := options.OwnerStrategy.SetControllerReference(
options.Owner, desiredObject,
); err != nil {
return nil, fmt.Errorf("set controller reference: %w", err)
}
}
// Lookup actual object state on cluster.
actualObject := desiredObject.DeepCopyObject().(Object)
err := e.cache.Get(
ctx, client.ObjectKeyFromObject(desiredObject), actualObject,
)
switch {
case errors.IsNotFound(err):
// Object might still already exist on the cluster,
// either because of slow caches or because
// label selectors exclude it from the cache.
//
// To be on the safe-side do a normal POST call.
// Using SSA might patch an already existing object,
// violating collision protection settings.
err := e.create(
ctx, desiredObject, options, client.FieldOwner(e.fieldOwner))
if errors.IsAlreadyExists(err) {
// Might be a slow cache or an object created by a different actor
// but excluded by the cache selector.
return nil, NewCreateCollisionError(desiredObject, err.Error())
}
if err != nil {
return nil, fmt.Errorf("creating resource: %w", err)
}
if err := e.migrateFieldManagersToSSA(ctx, desiredObject); err != nil {
return nil, fmt.Errorf("migrating to SSA after create: %w", err)
}
return newObjectResultCreated(
desiredObject, options), nil
case err != nil:
return nil, fmt.Errorf("getting object: %w", err)
}
return e.objectUpdateHandling(
ctx, revision, desiredObject,
actualObject, options,
)
}
func (e *ObjectEngine) checkSituation(
desiredObject Object,
actualObject Object,
options types.ObjectReconcileOptions,
) (
ctrlSit ctrlSituation,
compareRes CompareResult,
actualOwner *metav1.OwnerReference,
err error,
) {
var compareOpts []types.ComparatorOption
if options.Owner != nil {
ctrlSit, actualOwner = e.detectOwner(
options.Owner, options.OwnerStrategy, actualObject, options.PreviousOwners)
compareOpts = append(compareOpts, types.WithOwner(options.Owner, options.OwnerStrategy))
} else {
if e.isBoxcutterManaged(actualObject) {
ctrlSit = ctrlSituationIsController
} else {
ctrlSit = ctrlSituationNoController
}
}
// An object already exists on the cluster.
// Before doing anything else, we have to figure out
// who owns and controls the object.
compareRes, err = e.comparator.Compare(desiredObject, actualObject, compareOpts...)
if err != nil {
err = fmt.Errorf("diverge check: %w", err)
return ctrlSit,
compareRes,
actualOwner,
err
}
return ctrlSit,
compareRes,
actualOwner,
err
}
func (e *ObjectEngine) objectUpdateHandling(
ctx context.Context,
revision int64,
desiredObject Object,
actualObject Object,
options types.ObjectReconcileOptions,
) (ObjectResult, error) {
ctrlSit, compareRes, actualOwner, err := e.checkSituation(
desiredObject, actualObject, options)
if err != nil {
return nil, err
}
// Ensure revision linearity.
actualObjectRevision, err := e.getObjectRevision(actualObject)
if err != nil {
return nil, fmt.Errorf("getting revision of object: %w", err)
}
if actualObjectRevision > revision {
// Leave object alone.
// It's already owned by a later revision.
return newObjectResultProgressed(
actualObject, compareRes, options,
), nil
}
// Use optimistic locking to ensure that object will only
// be overridden when previous state is known to us.
// This prevents re-adoption of orphaned objects where we
// haven't observed the orphaning yet.
desiredObject.SetResourceVersion(actualObject.GetResourceVersion())
switch ctrlSit {
case ctrlSituationIsController:
modified := compareRes.Comparison != nil &&
(!compareRes.Comparison.Modified.Empty() ||
!compareRes.Comparison.Removed.Empty())
if !compareRes.IsConflict() && !modified {
// No conflict with another field manager
// and no modification needed.
return newObjectResultIdle(
actualObject, compareRes, options,
), nil
}
if !compareRes.IsConflict() && modified {
// No conflict with another controller, but modifications needed.
err := e.apply(
ctx, desiredObject,
options,
)
if err != nil {
// Might be a Conflict if object already exists.
return nil, fmt.Errorf("patching (modified): %w", err)
}
return newObjectResultUpdated(
desiredObject, compareRes, options,
), nil
}
// This is not supposed to happen.
// Some other entity changed fields under our control,
// while not contesting to be object controller!
//
// Let's try to force those fields back to their intended values.
// If this change is being done by another controller tightly operating
// on this resource, this may lead to a ownership fight.
//
// Note "Collision Protection":
// We don't care about collision protection settings here,
// because we are already controlling the object.
//
// Note "Concurrent Reconciles":
// It's safe because this patch operation will fail if another reconciler
// claimed controlling ownership in the background.
// The failure is caused by this patch operation
// adding this revision as controller and another controller existing.
// Having two ownerRefs set to controller is rejected by the kube-apiserver.
// Even though we force FIELD-level ownership in the call below.
err := e.apply(
ctx, desiredObject,
options,
client.ForceOwnership,
)
if err != nil {
return nil, fmt.Errorf("patching (conflict): %w", err)
}
if options.Paused {
return newObjectResultRecovered(
actualObject, compareRes, options,
), nil
}
return newObjectResultRecovered(
desiredObject, compareRes, options,
), nil
// Taking control checklist:
// - current controlling owner MUST be in PreviousOwners list
// - OR object has _no_ controlling owner and CollisionProtection set to IfNoController or None
// - OR object has another controlling owner and Collision Protection is set to None
//
// If any of the above points is not true, refuse.
case ctrlSituationUnknownController:
if options.CollisionProtection != types.CollisionProtectionNone {
return newObjectResultConflict(
actualObject, compareRes,
actualOwner, options,
), nil
}
case ctrlSituationNoController:
// If the object has no controller, but there are system annotations or labels present,
// the object might have been just orphaned, if we re-adopt it now, it would get deleted
// by the kubernetes garbage collector.
if options.CollisionProtection == types.CollisionProtectionPrevent ||
e.isBoxcutterManaged(actualObject) {
return newObjectResultConflict(
actualObject, compareRes,
actualOwner, options,
), nil
}
case ctrlSituationPreviousIsController:
// no extra operation
break
}
// A previous revision is current controller.
// This means we want to take control, but
// retain older revisions ownerReferences,
// so they can still react to events.
// TODO:
// ObjectResult ModifiedFields does not contain ownerReference changes
// introduced here, this may lead to Updated Actions without modifications.
e.setObjectRevision(desiredObject, revision)
if options.Owner != nil {
options.OwnerStrategy.CopyOwnerReferences(actualObject, desiredObject)
options.OwnerStrategy.ReleaseController(desiredObject)
if err := options.OwnerStrategy.SetControllerReference(
options.Owner, desiredObject,
); err != nil {
return nil, fmt.Errorf("set controller reference: %w", err)
}
}
// Write changes.
err = e.apply(
ctx, desiredObject,
options,
client.ForceOwnership,
)
if err != nil {
// Might be a Conflict if object already exists.
return nil, fmt.Errorf("patching (owner change): %w", err)
}
if options.Paused {
return newObjectResultUpdated(
actualObject, compareRes, options,
), nil
}
return newObjectResultUpdated(
desiredObject, compareRes, options,
), nil
}
// isBoxcutterManaged is used to detect if we have managed this object at some point.
// It's only purpose is to prevent boxcutter immediately re-adopting objects when
// resources get orphaned by the GC.
func (e *ObjectEngine) isBoxcutterManaged(obj client.Object) bool {
labels := obj.GetLabels()
annotations := obj.GetAnnotations()
_, hasRevisionAnnotation := annotations[e.revisionAnnotation()]
if labels[managedByLabel] == managedByLabelValue && hasRevisionAnnotation {
return true
}
return false
}
func (e *ObjectEngine) create(
ctx context.Context, obj client.Object,
options types.ObjectReconcileOptions, opts ...client.CreateOption,
) error {
if options.Paused {
return nil
}
return e.writer.Create(ctx, obj, opts...)
}
func (e *ObjectEngine) apply(
ctx context.Context,
obj Object,
options types.ObjectReconcileOptions,
opts ...client.ApplyOption,
) error {
if options.Paused {
return nil
}
if err := e.migrateFieldManagersToSSA(ctx, obj); err != nil {
return err
}
o := make([]client.ApplyOption, 0, len(opts)+1)
o = append(o, client.FieldOwner(e.fieldOwner))
o = append(o, opts...)
var ac runtime.ApplyConfiguration
switch v := obj.(type) {
case runtime.ApplyConfiguration:
ac = v
case *unstructured.Unstructured:
ac = client.ApplyConfigurationFromUnstructured(v)
default:
return NewUnsupportedApplyConfigurationError(obj)
}
return e.writer.Apply(ctx, ac, o...)
}
type ctrlSituation string
const (
// Owner is already controller.
ctrlSituationIsController ctrlSituation = "IsController"
// Previous revision/previous owner is controller.
ctrlSituationPreviousIsController ctrlSituation = "PreviousIsController"
// Someone else is controller of this object.
// This includes the "next" revision, as it's not in "previousOwners".
ctrlSituationUnknownController ctrlSituation = "UnknownController"
// No controller found.
ctrlSituationNoController ctrlSituation = "NoController"
)
func (e *ObjectEngine) detectOwner(
owner client.Object,
ownerStrategy objectEngineOwnerStrategy,
actualObject Object,
previousOwners []client.Object,
) (ctrlSituation, *metav1.OwnerReference) {
// e.ownerStrategy may either work on .metadata.ownerReferences or
// on an annotation to allow cross-namespace and cross-cluster refs.
ownerRef, ok := ownerStrategy.GetController(actualObject)
if !ok {
return ctrlSituationNoController, nil
}
// Are we already controller?
if ownerStrategy.IsController(owner, actualObject) {
return ctrlSituationIsController, &ownerRef
}
// Check if previous owner is controller.
for _, previousOwner := range previousOwners {
if ownerStrategy.IsController(previousOwner, actualObject) {
return ctrlSituationPreviousIsController, &ownerRef
}
}
// Anyone else controller?
// This statement can only resolve to true if annotations
// are used for owner reference tracking.
return ctrlSituationUnknownController, &ownerRef
}
// Stores the revision number in a well-known annotation on the given object.
func (e *ObjectEngine) setObjectRevision(obj client.Object, revision int64) {
a := obj.GetAnnotations()
if a == nil {
a = map[string]string{}
}
a[e.revisionAnnotation()] = strconv.FormatInt(revision, 10)
obj.SetAnnotations(a)
}
// Retrieves the revision number from a well-known annotation on the given object.
func (e *ObjectEngine) getObjectRevision(obj client.Object) (int64, error) {
a := obj.GetAnnotations()
if a == nil {
return 0, nil
}
if len(a[e.revisionAnnotation()]) == 0 {
return 0, nil
}
return strconv.ParseInt(a[e.revisionAnnotation()], 10, 64)
}
// Migrate field ownerships to be compatible with server-side apply.
// SSA really is complicated: https://github.com/kubernetes/kubernetes/issues/99003
func (e *ObjectEngine) migrateFieldManagersToSSA(
ctx context.Context, object Object,
) error {
patch, err := csaupgrade.UpgradeManagedFieldsPatch(
object, sets.New(e.fieldOwner), e.fieldOwner)
switch {
case err != nil:
return err
case len(patch) == 0:
// csaupgrade.UpgradeManagedFieldsPatch returns nil, nil when no work is to be done.
// Empty patch cannot be applied so exit early.
return nil
}
if err := e.writer.Patch(ctx, object, client.RawPatch(
machinerytypes.JSONPatchType, patch)); err != nil {
return fmt.Errorf("update field managers: %w", err)
}
return nil
}
func (e *ObjectEngine) revisionAnnotation() string {
return e.systemPrefix + "/revision"
}
func (e *ObjectEngine) removeBoxcutterManagedLabelsAndAnnotations(
ctx context.Context, w client.Writer, obj Object,
) error {
updated := obj.DeepCopyObject().(Object)
annotations := obj.GetAnnotations()
delete(annotations, e.revisionAnnotation())
obj.SetAnnotations(annotations)
labels := updated.GetLabels()
if l, ok := labels[managedByLabel]; ok && l == managedByLabelValue {
delete(labels, managedByLabel)
}
delete(labels, boxcutterManagedLabel)
updated.SetLabels(labels)
if err := w.Patch(ctx, updated, client.MergeFrom(obj)); err != nil {
return fmt.Errorf("patching object labels: %w", err)
}
return nil
}