-
Notifications
You must be signed in to change notification settings - Fork 0
/
reconciler.go
101 lines (85 loc) · 2.95 KB
/
reconciler.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
package kopper
import (
gocontext "context"
"fmt"
"time"
"github.com/flanksource/commons/logger"
"github.com/flanksource/duty/context"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
func SetupReconciler[T any, PT interface {
*T
client.Object
}](ctx context.Context, mgr ctrl.Manager, OnUpsertFunc func(context.Context, PT) error, OnDeleteFunc func(context.Context, string) error, finalizer string) error {
if finalizer == "" {
return fmt.Errorf("field Finalizer cannot be empty")
}
r := Reconciler[T, PT]{
DutyContext: ctx,
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
OnUpsertFunc: OnUpsertFunc,
OnDeleteFunc: OnDeleteFunc,
Finalizer: finalizer,
}
return r.SetupWithManager(mgr)
}
type Reconciler[T any, PT interface {
*T
client.Object
}] struct {
client.Client
DutyContext context.Context
Scheme *runtime.Scheme
OnUpsertFunc func(context.Context, PT) error
OnDeleteFunc func(context.Context, string) error
Finalizer string
}
func (r *Reconciler[T, PT]) Reconcile(ctx gocontext.Context, req ctrl.Request) (ctrl.Result, error) {
obj := PT(new(T))
err := r.Get(ctx, req.NamespacedName, obj)
if err != nil {
if errors.IsNotFound(err) {
return ctrl.Result{}, nil
}
return ctrl.Result{}, err
}
resourceName := fmt.Sprintf("%s[%s]", obj.GetObjectKind().GroupVersionKind().Kind, obj.GetUID())
if !obj.GetDeletionTimestamp().IsZero() {
logger.Infof("[kopper] deleting %s", resourceName)
if err := r.OnDeleteFunc(r.DutyContext, string(obj.GetUID())); err != nil {
logger.Errorf("[kopper] failed to delete %s: %v", resourceName, err)
return ctrl.Result{Requeue: true, RequeueAfter: 2 * time.Minute}, err
}
controllerutil.RemoveFinalizer(obj, r.Finalizer)
return ctrl.Result{}, r.Update(ctx, obj)
}
if !controllerutil.ContainsFinalizer(obj, r.Finalizer) {
controllerutil.AddFinalizer(obj, r.Finalizer)
if err := r.Update(ctx, obj); err != nil {
logger.Errorf("[kopper] failed to update finalizers %s: %v", resourceName, err)
return ctrl.Result{Requeue: true, RequeueAfter: 2 * time.Minute}, err
}
}
if err := r.OnUpsertFunc(r.DutyContext, obj); err != nil {
logger.Errorf("[kopper] failed to upsert %s: %v", resourceName, err)
return ctrl.Result{Requeue: true, RequeueAfter: 2 * time.Minute}, err
}
if err := r.Status().Update(r.DutyContext, obj); err != nil {
logger.Errorf("[kopper] failed to update status %s: %v", resourceName, err)
return ctrl.Result{Requeue: true, RequeueAfter: 2 * time.Minute}, err
}
logger.Infof("[kopper] upserted %s", resourceName)
return ctrl.Result{}, nil
}
// SetupWithManager sets up the controller with the Manager.
func (r *Reconciler[T, PT]) SetupWithManager(mgr ctrl.Manager) error {
pObj := PT(new(T))
return ctrl.NewControllerManagedBy(mgr).
For(pObj).
Complete(r)
}