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
package runtime
import (
"context"
"errors"
"fmt"
"log/slog"
"reflect"
"strings"
"time"
commonv1 "github.com/johnandersen777/socialweb-computer-kcp/api/common/v1alpha1"
"github.com/johnandersen777/socialweb-computer-kcp/internal/bus"
"github.com/johnandersen777/socialweb-computer-kcp/internal/kit/opt"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kruntime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/util/workqueue"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
"sigs.k8s.io/controller-runtime/pkg/cluster"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
mcbuilder "sigs.k8s.io/multicluster-runtime/pkg/builder"
mchandler "sigs.k8s.io/multicluster-runtime/pkg/handler"
"sigs.k8s.io/multicluster-runtime/pkg/multicluster"
mcreconcile "sigs.k8s.io/multicluster-runtime/pkg/reconcile"
)
const scopeMissRetries = 10
type Object interface {
metav1.Object
kruntime.Object
}
type Result struct {
RequeueAfter time.Duration
Waiting bool
Backoff bool
}
type Reconciler[T Object] interface {
Reconcile(ctx context.Context, req Request, obj T) (Result, error)
}
type ReconcilerFunc[T Object] func(ctx context.Context, req Request, obj T) (Result, error)
func (f ReconcilerFunc[T]) Reconcile(ctx context.Context, req Request, obj T) (Result, error) {
return f(ctx, req, obj)
}
type Request struct {
Cluster string
Workspace string
Namespace string
Name string
scope *scope
}
type scope struct {
client client.Client
scheme *kruntime.Scheme
fieldManager string
finalizer string
sharedStatus bool
foreignStatus []string
log *slog.Logger
}
type Option func(*options)
type options struct {
export string
fieldManager string
finalizer string
owns []client.Object
watches []watchInput
only func(client.Object) bool
inWorkspace func(context.Context, string) bool
gone func(context.Context, Request)
events []<-chan Request
sharedStatus bool
foreignStatus []string
backoffBase time.Duration
backoffMax time.Duration
workers int
err error
}
type watchInput struct {
object client.Object
export string
mapFunc func(ctx context.Context, src Request, obj client.Object) []Request
changed func(old, new client.Object) bool
observe func(obj client.Object, gone bool)
}
type WatchFilter[R Object] struct {
Changed func(old, new R) bool
Observe func(obj R, gone bool)
}
func Export(name string) Option {
return func(o *options) { o.export = strings.TrimSpace(name) }
}
func FieldManager(name string) Option {
return func(o *options) { o.fieldManager = strings.TrimSpace(name) }
}
func Finalizer(name string) Option {
return func(o *options) { o.finalizer = strings.TrimSpace(name) }
}
func Backoff(base, max time.Duration) Option {
return func(o *options) { o.backoffBase, o.backoffMax = base, max }
}
func Workers(n int) Option {
return func(o *options) { o.workers = n }
}
func SharedStatus() Option {
return func(o *options) { o.sharedStatus = true }
}
func ForeignStatus(paths ...string) Option {
return func(o *options) { o.foreignStatus = append(o.foreignStatus, paths...) }
}
func Events(ch <-chan Request) Option {
return func(o *options) {
if ch != nil {
o.events = append(o.events, ch)
}
}
}
func InWorkspaces(keep func(workspace string) bool) Option {
return func(o *options) { o.inWorkspace = inWorkspaces(keep) }
}
func InWorkspacesContext(keep func(ctx context.Context, workspace string) bool) Option {
return func(o *options) { o.inWorkspace = keep }
}
func inWorkspaces(keep func(workspace string) bool) func(context.Context, string) bool {
if keep == nil {
return nil
}
return func(_ context.Context, workspace string) bool { return keep(workspace) }
}
func Gone(fn func(ctx context.Context, req Request)) Option {
return func(o *options) { o.gone = fn }
}
func Only[T Object](keep func(T) bool) Option {
return func(o *options) {
if keep == nil {
return
}
o.only = func(obj client.Object) bool {
typed, ok := obj.(T)
return ok && keep(typed)
}
}
}
func Owns[C Object]() Option {
return func(o *options) {
obj, err := newObject[C]()
if err != nil {
o.err = errors.Join(o.err, err)
return
}
o.owns = append(o.owns, obj)
}
}
func Watches[R Object](mapFunc func(ctx context.Context, src Request, obj R) []Request) Option {
return WatchesIn("", mapFunc)
}
func WatchesIn[R Object](export string, mapFunc func(ctx context.Context, src Request, obj R) []Request) Option {
return WatchesFiltered(export, WatchFilter[R]{}, mapFunc)
}
func WatchesFiltered[R Object](export string, f WatchFilter[R], mapFunc func(ctx context.Context, src Request, obj R) []Request) Option {
return func(o *options) {
obj, err := newObject[R]()
if err != nil {
o.err = errors.Join(o.err, err)
return
}
if mapFunc == nil {
o.err = errors.Join(o.err, fmt.Errorf("runtime: the watch of %T has no map func", obj))
return
}
w := watchInput{object: obj, export: strings.TrimSpace(export), mapFunc: func(ctx context.Context, src Request, obj client.Object) []Request {
typed, ok := obj.(R)
if !ok {
return nil
}
return mapFunc(ctx, src, typed)
}}
if f.Changed != nil {
w.changed = func(old, new client.Object) bool {
o, ok1 := old.(R)
n, ok2 := new.(R)
return !ok1 || !ok2 || f.Changed(o, n)
}
}
if f.Observe != nil {
w.observe = func(obj client.Object, gone bool) {
if typed, ok := obj.(R); ok {
f.Observe(typed, gone)
}
}
}
o.watches = append(o.watches, w)
}
}
func filtered(w watchInput, inner mchandler.TypedEventHandlerFunc[client.Object, mcreconcile.Request]) mchandler.TypedEventHandlerFunc[client.Object, mcreconcile.Request] {
if w.changed == nil && w.observe == nil {
return inner
}
observe := func(obj client.Object, gone bool) {
if w.observe != nil && obj != nil {
w.observe(obj, gone)
}
}
return func(cn multicluster.ClusterName, cl cluster.Cluster) handler.TypedEventHandler[client.Object, mcreconcile.Request] {
h := inner(cn, cl)
return handler.TypedFuncs[client.Object, mcreconcile.Request]{
CreateFunc: func(ctx context.Context, e event.TypedCreateEvent[client.Object], q workqueue.TypedRateLimitingInterface[mcreconcile.Request]) {
observe(e.Object, false)
h.Create(ctx, e, q)
},
UpdateFunc: func(ctx context.Context, e event.TypedUpdateEvent[client.Object], q workqueue.TypedRateLimitingInterface[mcreconcile.Request]) {
observe(e.ObjectNew, false)
if w.changed != nil && e.ObjectOld != nil && e.ObjectNew != nil && !w.changed(e.ObjectOld, e.ObjectNew) {
return
}
h.Update(ctx, e, q)
},
DeleteFunc: func(ctx context.Context, e event.TypedDeleteEvent[client.Object], q workqueue.TypedRateLimitingInterface[mcreconcile.Request]) {
observe(e.Object, true)
h.Delete(ctx, e, q)
},
GenericFunc: func(ctx context.Context, e event.TypedGenericEvent[client.Object], q workqueue.TypedRateLimitingInterface[mcreconcile.Request]) {
h.Generic(ctx, e, q)
},
}
}
}
func For[T Object](m *Manager, name string, r Reconciler[T], opts ...Option) error {
name = strings.TrimSpace(name)
if m == nil {
return errors.New("runtime: a manager is required")
}
if name == "" {
return errors.New("runtime: a controller needs a name")
}
if r == nil {
return fmt.Errorf("runtime: the controller %s has no reconciler", name)
}
o := options{fieldManager: FieldManagerPrefix + name, backoffBase: DefaultBackoffBase, backoffMax: DefaultBackoffMax, workers: 1}
for _, opt := range opts {
if opt != nil {
opt(&o)
}
}
if o.err != nil {
return fmt.Errorf("runtime: the controller %s: %w", name, o.err)
}
if o.export == "" {
return fmt.Errorf("runtime: the controller %s names no APIExport", name)
}
o.workers = opt.Positive(o.workers, 1)
if o.inWorkspace == nil {
o.inWorkspace = inWorkspaces(m.workspaces)
}
proto, err := newObject[T]()
if err != nil {
return fmt.Errorf("runtime: the controller %s: %w", name, err)
}
m.addExport(o.export)
inExport := exportFilter(o.export)
limiter := newBackoff(o.backoffBase, o.backoffMax)
log := m.log.With("controller", name)
b := mcbuilder.ControllerManagedBy(m.mgr).Named(name).
For(proto, inExport, mcbuilder.WithPredicates(specOrDeletion())).
WithOptions(controller.TypedOptions[mcreconcile.Request]{MaxConcurrentReconciles: o.workers, RateLimiter: limiter,
SkipNameValidation: new(true)})
for _, owned := range o.owns {
b = b.Owns(owned, inExport)
}
for _, w := range o.watches {
source := inExport
if w.export != "" && w.export != o.export {
m.addExport(w.export)
source = exportFilter(w.export)
}
b = b.Watches(w.object, filtered(w, m.mapHandler(o.export, w.mapFunc)), source)
}
for _, ch := range o.events {
b = b.WatchesRawSource(m.eventSource(o.export, ch))
}
a := &adapter[T]{m: m, name: name, r: r, limiter: limiter, log: log, only: o.only, inWorkspace: o.inWorkspace, gone: o.gone,
scope: scope{scheme: m.scheme, fieldManager: o.fieldManager, finalizer: o.finalizer, sharedStatus: o.sharedStatus,
foreignStatus: o.foreignStatus, log: log}}
if err := b.Complete(a); err != nil {
return fmt.Errorf("runtime: building the controller %s: %w", name, err)
}
log.Info("the controller is registered", "kind", fmt.Sprintf("%T", proto), "export", o.export, "fieldManager", o.fieldManager)
return nil
}
func (m *Manager) mapHandler(export string, mapFunc func(ctx context.Context, src Request, obj client.Object) []Request) mchandler.TypedEventHandlerFunc[client.Object, mcreconcile.Request] {
return func(cn multicluster.ClusterName, _ cluster.Cluster) handler.TypedEventHandler[client.Object, mcreconcile.Request] {
_, lc := splitCluster(string(cn))
return handler.TypedEnqueueRequestsFromMapFunc(func(ctx context.Context, obj client.Object) []mcreconcile.Request {
workspace, err := m.path(ctx, lc)
if err != nil {
m.log.Warn("a watched object is not mapped: its workspace path does not resolve", "export", export, "cluster", lc,
"name", obj.GetName(), "err", err.Error())
return nil
}
src := Request{Cluster: lc, Workspace: workspace, Namespace: obj.GetNamespace(), Name: obj.GetName()}
var out []mcreconcile.Request
for _, r := range mapFunc(ctx, src, obj) {
cluster := lc
if r.Cluster != "" {
cluster = r.Cluster
}
target := multicluster.ClusterName(export + clusterSeparator + cluster)
out = append(out, mcreconcile.Request{ClusterName: target,
Request: reconcile.Request{NamespacedName: types.NamespacedName{Namespace: r.Namespace, Name: r.Name}}})
}
return out
})
}
}
func (m *Manager) eventSource(export string, ch <-chan Request) source.TypedSource[mcreconcile.Request] {
return source.TypedFunc[mcreconcile.Request](func(ctx context.Context, q workqueue.TypedRateLimitingInterface[mcreconcile.Request]) error {
m.sourcesWG.Go(func() {
for {
select {
case <-ctx.Done():
return
case r, ok := <-ch:
if !ok {
return
}
q.Add(mcreconcile.Request{ClusterName: multicluster.ClusterName(export + clusterSeparator + r.Cluster),
Request: reconcile.Request{NamespacedName: types.NamespacedName{Namespace: r.Namespace, Name: r.Name}}})
}
}
})
return nil
})
}
func exportFilter(export string) mcbuilder.EngageOptions {
prefix := export + clusterSeparator
return mcbuilder.WithClusterFilter(func(cn multicluster.ClusterName, _ cluster.Cluster) bool {
return strings.HasPrefix(string(cn), prefix)
})
}
func specOrDeletion() predicate.Predicate {
return predicate.Or(predicate.GenerationChangedPredicate{}, predicate.Funcs{
UpdateFunc: func(e event.UpdateEvent) bool {
if e.ObjectOld == nil || e.ObjectNew == nil {
return false
}
return e.ObjectOld.GetDeletionTimestamp().IsZero() != e.ObjectNew.GetDeletionTimestamp().IsZero()
},
})
}
type adapter[T Object] struct {
m *Manager
name string
r Reconciler[T]
limiter *backoff
log *slog.Logger
only func(client.Object) bool
inWorkspace func(context.Context, string) bool
gone func(context.Context, Request)
scope scope
}
func (a *adapter[T]) Reconcile(ctx context.Context, mcreq mcreconcile.Request) (reconcile.Result, error) {
res, err := a.reconcile(ctx, mcreq)
if err != nil && a.m.stopping(ctx) {
a.log.Debug("the reconcile ended with the shutdown", "cluster", mcreq.ClusterName, "name", mcreq.Name, "err", err.Error())
return reconcile.Result{}, nil
}
return res, err
}
func (a *adapter[T]) reconcile(ctx context.Context, mcreq mcreconcile.Request) (reconcile.Result, error) {
_, lc := splitCluster(string(mcreq.ClusterName))
cl, err := a.m.mgr.GetCluster(ctx, mcreq.ClusterName)
if err != nil {
return reconcile.Result{}, err
}
workspace, err := a.m.path(ctx, lc)
if err != nil {
return reconcile.Result{}, a.failed(ctx, mcreq, lc, mcreq.Name, err)
}
if a.inWorkspace != nil && !a.inWorkspace(ctx, workspace) {
return a.outOfScope(mcreq, workspace), nil
}
obj, err := newObject[T]()
if err != nil {
return reconcile.Result{}, reconcile.TerminalError(err)
}
s := a.scope
s.client = cl.GetClient()
req := Request{Cluster: lc, Workspace: workspace, Namespace: mcreq.Namespace, Name: mcreq.Name, scope: &s}
if err := cl.GetClient().Get(ctx, mcreq.NamespacedName, obj); err != nil {
if apierrors.IsNotFound(err) {
if a.gone != nil {
a.gone(ctx, req)
}
return reconcile.Result{}, nil
}
return reconcile.Result{}, a.failed(ctx, mcreq, lc, mcreq.Name, err)
}
if a.only != nil && !a.only(obj) {
return reconcile.Result{}, nil
}
log := a.log.With("cluster", lc, "workspace", req.Workspace, "name", req.Name)
s.log = log
if s.finalizer != "" && obj.GetDeletionTimestamp().IsZero() && !controllerutil.ContainsFinalizer(obj, s.finalizer) {
if err := req.applyFinalizer(ctx, obj, true); err != nil {
return reconcile.Result{}, a.failed(ctx, mcreq, lc, req.Name, err)
}
}
gvk, err := apiutil.GVKForObject(obj, cl.GetScheme())
if err != nil {
return reconcile.Result{}, reconcile.TerminalError(err)
}
ctx = bus.WithCallerLabels(ctx, commonv1.ObjectRef{Group: gvk.Group, Kind: gvk.Kind, Name: obj.GetName(), Workspace: req.Workspace}, obj.GetLabels())
log.Debug("reconciling")
res, err := a.r.Reconcile(ctx, req, obj)
if err != nil {
return reconcile.Result{}, a.failed(ctx, mcreq, req.Workspace, req.Name, err)
}
if s.finalizer != "" && !res.Waiting && !obj.GetDeletionTimestamp().IsZero() && controllerutil.ContainsFinalizer(obj, s.finalizer) {
if err := req.applyFinalizer(ctx, obj, false); err != nil {
return reconcile.Result{}, a.failed(ctx, mcreq, req.Workspace, req.Name, err)
}
}
if res.Backoff {
if _, next := a.limiter.peek(mcreq); res.RequeueAfter <= 0 || res.RequeueAfter > next {
return reconcile.Result{Requeue: true}, nil //nolint:staticcheck
}
}
return reconcile.Result{RequeueAfter: res.RequeueAfter}, nil
}
func (a *adapter[T]) outOfScope(item mcreconcile.Request, workspace string) reconcile.Result {
misses := a.limiter.NumRequeues(item)
if misses >= scopeMissRetries {
a.log.Debug("the workspace stays outside the controller's scope; the request is dropped", "workspace", workspace,
"name", item.Name, "misses", misses)
return reconcile.Result{}
}
a.log.Debug("the workspace is not in the controller's scope yet; it requeues with backoff", "workspace", workspace,
"name", item.Name, "misses", misses+1)
return reconcile.Result{Requeue: true} //nolint:staticcheck
}
func (a *adapter[T]) failed(ctx context.Context, item mcreconcile.Request, workspace, name string, err error) error {
if a.m.stopping(ctx) {
return err
}
failures, retryIn := a.limiter.peek(item)
a.log.Warn("the reconcile failed; it retries with backoff", "workspace", workspace, "name", name,
"failures", failures, "retryIn", retryIn.String(), "err", err.Error())
return err
}
func newObject[T Object]() (T, error) {
var zero T
t := reflect.TypeOf(zero)
if t == nil || t.Kind() != reflect.Pointer || t.Elem().Kind() != reflect.Struct {
return zero, fmt.Errorf("runtime: %v is not a pointer to a struct", t)
}
obj, ok := reflect.New(t.Elem()).Interface().(T)
if !ok {
return zero, fmt.Errorf("runtime: %v does not implement the object interface", t)
}
return obj, nil
}