This repository has been archived by the owner on Jun 26, 2023. It is now read-only.
/
object.go
779 lines (674 loc) · 30.9 KB
/
object.go
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
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package reconcilers
import (
"context"
"fmt"
"reflect"
"strings"
"sync"
"time"
"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
"k8s.io/client-go/util/workqueue"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/source"
api "sigs.k8s.io/multi-tenancy/incubator/hnc/api/v1alpha2"
"sigs.k8s.io/multi-tenancy/incubator/hnc/internal/config"
"sigs.k8s.io/multi-tenancy/incubator/hnc/internal/forest"
"sigs.k8s.io/multi-tenancy/incubator/hnc/internal/metadata"
"sigs.k8s.io/multi-tenancy/incubator/hnc/internal/object"
"sigs.k8s.io/multi-tenancy/incubator/hnc/internal/pkg/selectors"
"sigs.k8s.io/multi-tenancy/incubator/hnc/internal/stats"
)
// syncAction is the action to take after Reconcile syncs with the in-memory forest.
// This is introduced to consolidate calls with forest lock.
type syncAction string
const (
actionUnknown syncAction = "<hnc internal error - unknown action>"
actionRemove syncAction = "remove"
actionWrite syncAction = "write"
actionNop syncAction = "no-op"
unknownSourceNamespace = "<unknown-source-namespace>"
)
// namespacedNameSet is used to keep track of existing propagated objects of
// a specific GVK in the cluster.
type namespacedNameSet map[types.NamespacedName]bool
// ObjectReconciler reconciles generic propagated objects. You must create one for each
// group/version/kind that needs to be propagated and set its `GVK` field appropriately.
type ObjectReconciler struct {
client.Client
EventRecorder record.EventRecorder
Log logr.Logger
// Forest is the in-memory forest managed by the HierarchyConfigReconciler.
Forest *forest.Forest
// GVK is the group/version/kind handled by this reconciler.
GVK schema.GroupVersionKind
// Mode describes propagation mode of objects that are handled by this reconciler.
// See more details in the comments of api.SynchronizationMode.
Mode api.SynchronizationMode
// Affected is a channel of event.GenericEvent (see "Watching Channels" in
// https://book-v1.book.kubebuilder.io/beyond_basics/controller_watches.html) that is used to
// enqueue additional objects that need updating.
Affected chan event.GenericEvent
// AffectedNamespace is a channel of events used to update namespaces.
AffectedNamespace chan event.GenericEvent
// propagatedObjectsLock is used to prevent the race condition between concurrent reconciliation threads
// trying to update propagatedObjects at the same time.
propagatedObjectsLock sync.Mutex
// propagatedObjects contains all propagated objects of the GVK handled by this reconciler.
propagatedObjects namespacedNameSet
}
// HNC doesn't actually need all these permissions, but we *do* need to have them to be able to
// propagate RoleBindings for them. These match the permissions required by the builtin
// `cluster-admin` role, as seen in issues #772 and #1311.
//
// +kubebuilder:rbac:groups=*,resources=*,verbs=*
// SyncNamespace can be called manually by the HierarchyConfigReconciler when the hierarchy changes.
// It enqueues all the current objects in the namespace and local copies of the original objects
// in the ancestors.
func (r *ObjectReconciler) SyncNamespace(ctx context.Context, log logr.Logger, ns string) error {
log = log.WithValues("gvk", r.GVK)
// Enqueue all the current objects in the namespace because some of them may have been deleted.
if err := r.enqueueLocalObjects(ctx, log, ns); err != nil {
return err
}
// Enqueue local copies of the originals in the ancestors to catch any new or changed objects.
r.enqueuePropagatedObjects(ctx, log, ns)
return nil
}
// GetGVK provides GVK that is handled by this reconciler.
func (r *ObjectReconciler) GetGVK() schema.GroupVersionKind {
return r.GVK
}
// GetMode provides the mode of objects that are handled by this reconciler.
func (r *ObjectReconciler) GetMode() api.SynchronizationMode {
return r.Mode
}
// GetValidateMode returns a valid api.SynchronizationMode based on the given mode. Please
// see the comments of api.SynchronizationMode for currently supported modes.
// If mode is not set, it will be api.Propagate by default. Any unrecognized mode is
// treated as api.Ignore.
func GetValidateMode(mode api.SynchronizationMode, log logr.Logger) api.SynchronizationMode {
switch mode {
case api.Propagate, api.Ignore, api.Remove:
return mode
case "":
log.Info("Sync mode is unset; using default 'Propagate'")
return api.Propagate
default:
log.Info("Unrecognized sync mode; will interpret as 'Ignore'", "mode", mode)
return api.Ignore
}
}
// SetMode sets the Mode field of an object reconciler and syncs objects in the cluster if needed.
// The method will return an error if syncs fail.
func (r *ObjectReconciler) SetMode(ctx context.Context, log logr.Logger, mode api.SynchronizationMode) error {
log = log.WithValues("gvk", r.GVK)
newMode := GetValidateMode(mode, log)
oldMode := r.Mode
if newMode == oldMode {
return nil
}
log.Info("Changing sync mode of the object reconciler", "oldMode", oldMode, "newMode", newMode)
r.Mode = newMode
// If the new mode is not "ignore", we need to update objects in the cluster
// (e.g., propagate or remove existing objects).
if newMode != api.Ignore {
err := r.enqueueAllObjects(ctx, r.Log)
if err != nil {
return err
}
}
return nil
}
// GetNumPropagatedObjects returns the number of propagated objects of the GVK handled by this object reconciler.
func (r *ObjectReconciler) GetNumPropagatedObjects() int {
r.propagatedObjectsLock.Lock()
defer r.propagatedObjectsLock.Unlock()
return len(r.propagatedObjects)
}
// enqueueAllObjects enqueues all the current objects in all namespaces.
func (r *ObjectReconciler) enqueueAllObjects(ctx context.Context, log logr.Logger) error {
keys := r.Forest.GetNamespaceNames()
for _, ns := range keys {
// Enqueue all the current objects in the namespace.
if err := r.enqueueLocalObjects(ctx, log, ns); err != nil {
log.Error(err, "Error while trying to enqueue local objects", "namespace", ns)
return err
}
}
return nil
}
func (r *ObjectReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
resp := ctrl.Result{}
log := loggerWithRID(r.Log).WithValues("trigger", req.NamespacedName)
if config.ExcludedNamespaces[req.Namespace] {
return resp, nil
}
if r.Mode == api.Ignore {
return resp, nil
}
stats.StartObjReconcile(r.GVK)
defer stats.StopObjReconcile(r.GVK)
// Read the object.
inst := &unstructured.Unstructured{}
inst.SetGroupVersionKind(r.GVK)
inst.SetNamespace(req.Namespace)
inst.SetName(req.Name)
if err := r.Get(ctx, req.NamespacedName, inst); err != nil {
if !errors.IsNotFound(err) {
log.Error(err, "Couldn't read")
return resp, err
}
}
// Sync with the forest and perform any required actions.
actions, srcInst := r.syncWithForest(ctx, log, inst)
return resp, r.operate(ctx, log, actions, inst, srcInst)
}
// syncWithForest syncs the object instance with the in-memory forest. It returns the action to take on
// the object (delete, write or do nothing) and a source object if the action is to write it. It can
// also update the forest if a source object is added or removed.
func (r *ObjectReconciler) syncWithForest(ctx context.Context, log logr.Logger, inst *unstructured.Unstructured) (syncAction, *unstructured.Unstructured) {
// This is the only place we should lock the forest in each Reconcile, so this fn needs to return
// everything relevant for the rest of the Reconcile. This fn shouldn't contact the apiserver since
// that's a slow operation and everything will block on the lock being held.
r.Forest.Lock()
defer r.Forest.Unlock()
// If this namespace isn't ready to be synced (or is never synced), early exit. We'll be called
// again if this changes.
if r.skipNamespace(ctx, log, inst) {
return actionNop, nil
}
// If the object's missing and we know how to handle it, return early.
if missingAction := r.syncMissingObject(ctx, log, inst); missingAction != actionUnknown {
return missingAction, nil
}
// Update the forest and get the intended action.
action, srcInst := r.syncObject(ctx, log, inst)
// If the namespace has a critical condition, we shouldn't actually take any action, regardless of
// what we'd _like_ to do. We still needed to sync the forest since we want to know when objects
// are added and removed, so we can sync them properly if the critical condition is resolved, but
// don't do anything else for now.
if ca := r.Forest.Get(inst.GetNamespace()).GetCritAncestor(); ca != "" {
log.Info("Namespace has 'ActivitiesHalted' condition; will not touch propagated object", "affectedNamespace", ca, "suppressedAction", action)
return actionNop, nil
}
return action, srcInst
}
func (r *ObjectReconciler) skipNamespace(ctx context.Context, log logr.Logger, inst *unstructured.Unstructured) bool {
// If it's about to be deleted, do nothing, just wait for it to be actually deleted.
if !inst.GetDeletionTimestamp().IsZero() {
return true
}
ns := r.Forest.Get(inst.GetNamespace())
// If the object is reconciled before the namespace is synced (when start-up), do nothing.
if !ns.Exists() {
log.V(1).Info("Containing namespace hasn't been synced yet")
return true
}
return false
}
// syncMissingObject handles the case where the object we're reconciling doesn't exist. If it can
// determine a final action to take, it returns that action, otherwise it returns actionUnknown
// which indicates that we need to call the regular syncObject method. Note that this method may
// modify `inst`.
func (r *ObjectReconciler) syncMissingObject(ctx context.Context, log logr.Logger, inst *unstructured.Unstructured) syncAction {
// If the object exists, skip.
if inst.GetCreationTimestamp() != (v1.Time{}) {
return actionUnknown
}
ns := r.Forest.Get(inst.GetNamespace())
// If it's a source, it must have been deleted. Update the forest and enqueue all its
// descendants, but there's nothing else to do.
if ns.HasSourceObject(r.GVK, inst.GetName()) {
ns.DeleteSourceObject(r.GVK, inst.GetName())
r.syncPropagation(ctx, log, inst)
return actionNop
}
// This object doesn't exist, and yet someone thinks we need to reconcile it. There are a few
// reasons why this can happen:
//
// 1. This was a source object, and for some reason we got the notification that it's been
// deleted twice.
// 2. This is a propagated object. We haven't actually created it yet, but its source exists in
// the forest so we need to make a copy.
// 3a. This *was* a propagated object that we've deleted, and we're getting a notification about
// it.
// 3b. This *should have been* a propagated object, but due to some error we were never able to
// create it.
//
// In all cases, we're going to give it the api.LabelInherited from label with a dummy value, so
// that syncObject() treats it as though it's a propagated object. This works well in all three
// cases because:
//
// 1. If this was a source object, but we treat it as a propagated object, we'll see that
// there's no source in the tree and so there will be nothing to do (which is correct).
// 2. If this really is a propagated object that needs to be created, we'll find the source in
// the tree and call write(), which will set the correct value for LabelInheritedFrom.
// 3. If this *was* a propagated object that's been deleted, then we'll see there's no source
// (like in case #1) and ignore it.
metadata.SetLabel(inst, api.LabelInheritedFrom, unknownSourceNamespace)
// Continue the regular syncing process
return actionUnknown
}
// syncObject determines if this object is a source or propagated copy and handles it accordingly.
func (r *ObjectReconciler) syncObject(ctx context.Context, log logr.Logger, inst *unstructured.Unstructured) (syncAction, *unstructured.Unstructured) {
// If for some reason this has been called on an object that isn't namespaced, let's generate some
// logspam!
if inst.GetNamespace() == "" {
for i := 0; i < 100; i++ {
log.Info("Non-namespaced object!!!")
}
return actionNop, nil
}
// If the object should be propagated, we will sync it as an propagated object.
if yes, srcInst := r.shouldSyncAsPropagated(log, inst); yes {
return r.syncPropagated(log, inst, srcInst)
}
r.syncSource(ctx, log, inst)
// No action needs to take on source objects.
return actionNop, nil
}
// shouldSyncAsPropagated returns true and the source object if this object
// should be synced as a propagated object. Otherwise, return false and nil.
// Please note 'srcInst' could still be nil even if the object should be synced
// as propagated, which indicates the source is deleted and so should this object.
func (r *ObjectReconciler) shouldSyncAsPropagated(log logr.Logger, inst *unstructured.Unstructured) (bool, *unstructured.Unstructured) {
// Get the source object if it exists.
srcInst := r.getTopSourceToPropagate(log, inst)
// This object is a propagated copy if it has "api.LabelInheritedFrom" label.
if hasPropagatedLabel(inst) {
// Return the top source object of the same name in the ancestors that
// should propagate.
return true, srcInst
}
// If there's a conflicting source in the ancestors (excluding itself) and the
// the type has 'Propagate' mode, the object will be overwritten.
mode := r.Forest.GetTypeSyncer(r.GVK).GetMode()
if mode == api.Propagate && srcInst != nil {
log.Info("Conflicting object found in ancestors namespace; will overwrite this object", "conflictingAncestor", srcInst.GetNamespace())
return true, srcInst
}
return false, nil
}
// getTopSourceToPropagate returns the top source in the ancestors (excluding
// itself) that can propagate. Otherwise, return nil.
func (r *ObjectReconciler) getTopSourceToPropagate(log logr.Logger, inst *unstructured.Unstructured) *unstructured.Unstructured {
ns := r.Forest.Get(inst.GetNamespace())
// Get all the source objects with the same name in the ancestors excluding
// itself from top down.
objs := ns.Parent().GetAncestorSourceObjects(r.GVK, inst.GetName())
for _, obj := range objs {
// If the source cannot propagate, ignore it.
// TODO: add a webhook rule to prevent e.g. removing a source finalizer that
// would cause overwriting the source objects in the descendents.
// See https://github.com/kubernetes-sigs/multi-tenancy/issues/1120
if !r.shouldPropagateSource(log, obj, inst.GetNamespace()) {
continue
}
return obj
}
return nil
}
// syncPropagated will determine whether to delete the obsolete copy or overwrite it with the source.
// Or do nothing if it remains the same as the source object.
func (r *ObjectReconciler) syncPropagated(log logr.Logger, inst, srcInst *unstructured.Unstructured) (syncAction, *unstructured.Unstructured) {
ns := r.Forest.Get(inst.GetNamespace())
// Delete this local source object from the forest if it exists. (This could
// only happen when we are trying to overwrite a conflicting source).
ns.DeleteSourceObject(r.GVK, inst.GetName())
stats.OverwriteObject(r.GVK)
// If no source object exists, delete this object. This can happen when the source was deleted by
// users or the admin decided this type should no longer be propagated.
if srcInst == nil {
return actionRemove, nil
}
// If an object doesn't exist, assume it's been deleted or not yet created.
exists := inst.GetCreationTimestamp() != v1.Time{}
// If the copy does not exist, or is different from the source, return the write action and the
// source instance. Note that DeepEqual could return `true` even if the object doesn't exist if
// the source object is trivial (e.g. a completely empty ConfigMap).
if !exists ||
!reflect.DeepEqual(object.Canonical(inst), object.Canonical(srcInst)) ||
inst.GetLabels()[api.LabelInheritedFrom] != srcInst.GetNamespace() {
metadata.SetLabel(inst, api.LabelInheritedFrom, srcInst.GetNamespace())
return actionWrite, srcInst
}
// The object already exists and doesn't need to be updated. This will typically happen when HNC
// is restarted - all the propagated objects already exist on the apiserver. Record that it exists
// for our statistics.
r.recordPropagatedObject(log, inst.GetNamespace(), inst.GetName())
// Nothing more needs to be done.
return actionNop, nil
}
// syncSource updates the copy in the forest with the current source object. We
// will enqueue all the descendants if the source can be propagated. The
// propagation exceptions will be checked when reconciling the (enqueued)
// propagated objects.
func (r *ObjectReconciler) syncSource(ctx context.Context, log logr.Logger, src *unstructured.Unstructured) {
// Update or create a copy of the source object in the forest. We now store
// all the source objects in the forests no matter if the mode is 'Propagate'
// or not, because HNCConfig webhook will also check the non-'Propagate' mode
// source objects in the forest to see if a mode change is allowed.
ns := r.Forest.Get(src.GetNamespace())
ns.SetSourceObject(cleanSource(src))
// Enqueue propagated copies for this possibly deleted source
r.syncPropagation(ctx, log, src)
}
// cleanSource creates a sanitized version of the object to store in the forest. In particular, it
// sets the standard app.kubernetes.io/managed-by label, and cleans out any unpropagated
// annotations.
func cleanSource(src *unstructured.Unstructured) *unstructured.Unstructured {
// Don't modify the original
src = src.DeepCopy()
// Clean out bad annotations.
annot := src.GetAnnotations()
if annot == nil {
annot = map[string]string{}
}
for _, unprop := range config.UnpropagatedAnnotations {
delete(annot, unprop)
}
src.SetAnnotations(annot)
// Set or replace the managed-by label.
labels := src.GetLabels()
if labels == nil {
labels = map[string]string{}
}
labels[api.LabelManagedByApps] = api.MetaGroup
src.SetLabels(labels)
return src
}
func (r *ObjectReconciler) enqueueDescendants(ctx context.Context, log logr.Logger, src *unstructured.Unstructured, reason string) {
sns := r.Forest.Get(src.GetNamespace())
if ca := sns.GetCritAncestor(); ca != "" {
// There's no point enqueuing anything if the source namespace has a crit condition since we'll
// just skip any action anyway.
log.V(1).Info("Will not enqueue descendants due to crit ancestor", "critAncestor", ca, "oldReason", reason)
return
}
log.V(1).Info("Enqueuing descendant objects", "reason", reason)
for _, ns := range sns.DescendantNames() {
dc := object.Canonical(src)
dc.SetNamespace(ns)
log.V(1).Info("... enqueuing descendant copy", "affected", ns+"/"+src.GetName(), "reason", reason)
r.Affected <- event.GenericEvent{Object: dc}
}
}
// enqueueLocalObjects enqueues all the objects (with the same GVK) in the namespace.
func (r *ObjectReconciler) enqueueLocalObjects(ctx context.Context, log logr.Logger, ns string) error {
ul := &unstructured.UnstructuredList{}
ul.SetGroupVersionKind(r.GVK)
if err := r.List(ctx, ul, client.InNamespace(ns)); err != nil {
log.Error(err, "Couldn't list objects")
return err
}
for _, inst := range ul.Items {
// We don't need the entire canonical object here but only its metadata.
// Using canonical copy is the easiest way to get an object with its metadata set.
co := object.Canonical(&inst)
co.SetNamespace(inst.GetNamespace())
log.V(1).Info("Enqueuing existing object for reconciliation", "affected", co.GetName())
r.Affected <- event.GenericEvent{Object: co}
}
return nil
}
// enqueuePropagatedObjects is only called from SyncNamespace. It's the only place a forest lock is
// needed in SyncNamespace, so we made it into a function with forest lock instead of holding the
// lock for the entire SyncNamespace.
func (r *ObjectReconciler) enqueuePropagatedObjects(ctx context.Context, log logr.Logger, ns string) {
r.Forest.Lock()
defer r.Forest.Unlock()
// Enqueue local copies of the original objects in the ancestors from forest.
o := r.Forest.Get(ns).Parent().GetAncestorSourceObjects(r.GVK, "")
for _, obj := range o {
lc := object.Canonical(obj)
lc.SetNamespace(ns)
log.V(1).Info("Enqueuing local copy of the ancestor original for reconciliation", "affected", lc.GetName())
r.Affected <- event.GenericEvent{Object: lc}
}
}
// operate operates the action generated from syncing the object with the forest.
func (r *ObjectReconciler) operate(ctx context.Context, log logr.Logger, act syncAction, inst, srcInst *unstructured.Unstructured) error {
var err error
switch act {
case actionNop:
// nop
case actionRemove:
err = r.deleteObject(ctx, log, inst)
case actionWrite:
err = r.writeObject(ctx, log, inst, srcInst)
default: // this should never, ever happen. But if it does, try to make a very obvious error message.
if act == "" {
act = actionUnknown
}
err = fmt.Errorf("HNC couldn't determine how to update this object (desired action: %s)", act)
}
r.generateEvents(srcInst, inst, act, err)
return err
}
func (r *ObjectReconciler) deleteObject(ctx context.Context, log logr.Logger, inst *unstructured.Unstructured) error {
log.Info("Deleted propagated object")
stats.WriteObject(r.GVK)
err := r.Delete(ctx, inst)
if errors.IsNotFound(err) {
log.V(1).Info("The obsolete copy doesn't exist, no more action needed")
return nil
}
if err != nil {
// Don't log the error since controller-runtime will do it for us
return err
}
// Remove the propagated object from the map because we are confident that the object was successfully deleted
// on the apiserver.
r.recordRemovedObject(log, inst.GetNamespace(), inst.GetName())
return nil
}
func (r *ObjectReconciler) writeObject(ctx context.Context, log logr.Logger, inst, srcInst *unstructured.Unstructured) error {
// The object exists if CreationTimestamp is set. This flag enables us to have only 1 API call.
exist := inst.GetCreationTimestamp() != v1.Time{}
ns := inst.GetNamespace()
inst = object.Canonical(srcInst)
inst.SetNamespace(ns)
metadata.SetLabel(inst, api.LabelInheritedFrom, srcInst.GetNamespace())
log.V(1).Info("Writing", "dst", inst.GetNamespace(), "origin", srcInst.GetNamespace())
var err error = nil
stats.WriteObject(r.GVK)
if exist {
log.Info("Updating propagated object")
err = r.Update(ctx, inst)
// RoleBindings can't have their Roles changed after they're created
// (see https://github.com/kubernetes-sigs/multi-tenancy/issues/798).
// If an RB was quickly delete and re-created in an ancestor namespace
// - fast enough that by the time that HNC notices, the new RB exists; or
// if there's a change to the RBs when HNC isn't running - HNC could see
// it as an update (not a delete + create) and attempt to update the RBs in
// all descendant namespaces, and this will fail. In order to handle this
// case, we try to delete and re-create the rolebinding here
// We don't apply this logic to other objects because if another object has an
// ownerReference pointing to the object we're deleting, it could be deleted as
// well, which is undesirable.
// The error type is 'Invalid' after I tested it out with different error types
// from https://godoc.org/k8s.io/apimachinery/pkg/api/errors
api := strings.Split(inst.GetAPIVersion(), "/")[0]
if err != nil && errors.IsInvalid(err) && inst.GetKind() == "RoleBinding" && api == "rbac.authorization.k8s.io" {
// Log this error because we're about to throw it away.
log.Error(err, "Couldn't update propagated object; will try to delete and recreate instead")
if err = r.Delete(ctx, inst); err == nil {
err = r.Create(ctx, inst)
if err != nil {
log.Info("Couldn't recreate propagated object after deleting it") // error is handles below
} else {
log.Info("Successfully recreated propagated object")
}
} else {
log.Info("Couldn't delete propagated object that we couldn't update") // error is handles below
}
}
} else {
log.Info("Propagating object")
err = r.Create(ctx, inst)
}
if err != nil {
// Don't log the error since controller-runtime will do it for us
return err
}
// Add the object to the map if it does not exist because we are confident that the object was updated/created
// successfully on the apiserver.
r.recordPropagatedObject(log, inst.GetNamespace(), inst.GetName())
return nil
}
// generateEvents is called when the reconciler has performed all necessary
// actions and knows if they've succeeded or failed. If a source should not be
// propagated or there was a failure, generate "Warning" events.
func (r *ObjectReconciler) generateEvents(srcInst, inst *unstructured.Unstructured, act syncAction, err error) {
switch {
case hasFinalizers(inst):
// Propagated objects can never have finalizers
r.EventRecorder.Event(inst, "Warning", api.EventCannotPropagate, "Objects with finalizers cannot be propagated")
case err != nil:
// There was an error updating this object; generate a warning pointing to
// the source object. Note we never take actions on a source object, so only
// propagated objects could possibly get an error.
msg := fmt.Sprintf("Could not %s from source namespace %q: %s.", act, inst.GetLabels()[api.LabelInheritedFrom], err.Error())
r.EventRecorder.Event(inst, "Warning", api.EventCannotUpdate, msg)
// Also generate a warning on the source if one exists.
if srcInst != nil {
msg = fmt.Sprintf("Could not %s to destination namespace %q: %s.", act, inst.GetNamespace(), err.Error())
r.EventRecorder.Event(srcInst, "Warning", api.EventCannotPropagate, msg)
}
}
}
// hasPropagatedLabel returns true if "api.LabelInheritedFrom" label is set.
func hasPropagatedLabel(inst *unstructured.Unstructured) bool {
labels := inst.GetLabels()
if labels == nil {
// this cannot be a copy
return false
}
_, po := labels[api.LabelInheritedFrom]
return po
}
// syncPropagation enqueues any propagated copies of the source.
//
// The method can be called when the source was deleted by users, or if it will no longer be
// propagated, e.g. because the user's changed the sync mode in the HNC Config.
func (r *ObjectReconciler) syncPropagation(ctx context.Context, log logr.Logger, inst *unstructured.Unstructured) {
// Signal the config reconciler for reconciliation because it is possible that the source object is
// deleted on the apiserver.
hnccrSingleton.requestReconcile("source object")
r.enqueueDescendants(ctx, log, inst, "source object")
}
// shouldPropagateSource returns true if the object should be propagated by the HNC. The following
// objects are not propagated:
// - Objects of a type whose mode is set to "remove" in the HNCConfiguration singleton
// - Objects with nonempty finalizer list
// - Objects have a selector that doesn't match the destination namespace
// - Service Account token secrets
func (r *ObjectReconciler) shouldPropagateSource(log logr.Logger, inst *unstructured.Unstructured, dst string) bool {
nsLabels := r.Forest.Get(dst).GetLabels()
if ok, err := selectors.ShouldPropagate(inst, nsLabels); err != nil {
log.Error(err, "Cannot propagate")
r.EventRecorder.Event(inst, "Warning", api.EventCannotParseSelector, err.Error())
return false
} else if !ok {
return false
}
switch {
// Users can set the mode of a type to "remove" to exclude objects of the type
// from being handled by HNC.
case r.Mode == api.Remove:
return false
// Object with nonempty finalizer list is not propagated
case hasFinalizers(inst):
return false
case r.GVK.Group == "" && r.GVK.Kind == "Secret":
// These are reaped by a builtin K8s controller so there's no point copying them.
// More to the point, SA tokens really aren't supposed to be copied between
// namespaces. You *could* make the argument that a parent namespace's SA should be
// shared with all its descendants, but you could also make the case that while
// administration should be inherited, identity should not. At any rate, it's moot
// as long as K8s auto deletes these tokens, and we shouldn't fight K8s.
if inst.UnstructuredContent()["type"] == "kubernetes.io/service-account-token" {
log.V(1).Info("Excluding: service account token")
return false
}
return true
default:
// Everything else is propagated
return true
}
}
// recordPropagatedObject records the fact that this object has been propagated, so we can report
// statistics in the HNC Config.
func (r *ObjectReconciler) recordPropagatedObject(log logr.Logger, namespace, name string) {
r.propagatedObjectsLock.Lock()
defer r.propagatedObjectsLock.Unlock()
nnm := types.NamespacedName{
Namespace: namespace,
Name: name,
}
if !r.propagatedObjects[nnm] {
r.propagatedObjects[nnm] = true
hnccrSingleton.requestReconcile("newly propagated object")
}
}
// recordRemovedObject records the fact that this (possibly) previously propagated object no longer
// exists.
func (r *ObjectReconciler) recordRemovedObject(log logr.Logger, namespace, name string) {
r.propagatedObjectsLock.Lock()
defer r.propagatedObjectsLock.Unlock()
nnm := types.NamespacedName{
Namespace: namespace,
Name: name,
}
if r.propagatedObjects[nnm] {
delete(r.propagatedObjects, nnm)
hnccrSingleton.requestReconcile("newly unpropagated object")
}
}
func hasFinalizers(inst *unstructured.Unstructured) bool {
return len(inst.GetFinalizers()) != 0
}
func (r *ObjectReconciler) SetupWithManager(mgr ctrl.Manager, maxReconciles int) error {
target := &unstructured.Unstructured{}
target.SetGroupVersionKind(r.GVK)
opts := controller.Options{
MaxConcurrentReconciles: maxReconciles,
// Unlike the other HNC reconcilers, the object reconciler can easily be affected by objects
// that do not directly cause reconciliations when they're modified - see, e.g.,
// https://github.com/kubernetes-sigs/multi-tenancy/issues/1154. To address this, replace the
// default exponential backoff with one with a 10s cap.
//
// I wanted to pick five seconds since I feel like that's about how much time it would take you to check
// to see if something's wrong, realize it hasn't been propagated, and then try again. However,
// the default etcd timeout is 10s, which apparently is a more realistic measure of how K8s can
// behave under heavy load, so I raised it to 10s during the PR review. The _average_ delay seen
// by users should still be about 5s though.
RateLimiter: workqueue.NewItemExponentialFailureRateLimiter(250*time.Millisecond, 10*time.Second),
}
return ctrl.NewControllerManagedBy(mgr).
For(target).
Watches(&source.Channel{Source: r.Affected}, &handler.EnqueueRequestForObject{}).
WithOptions(opts).
Complete(r)
}