-
Notifications
You must be signed in to change notification settings - Fork 97
/
reconciler.go
122 lines (100 loc) · 3.83 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package controller
import (
"context"
"fmt"
"reflect"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"github.com/nginxinc/nginx-gateway-fabric/internal/framework/events"
)
// NamespacedNameFilterFunc is a function that returns true if the resource should be processed by the reconciler.
// If the function returns false, the reconciler will log the returned string.
type NamespacedNameFilterFunc func(nsname types.NamespacedName) (shouldProcess bool, msg string)
// ReconcilerConfig is the configuration for the reconciler.
type ReconcilerConfig struct {
// Getter gets a resource from the k8s API.
Getter Getter
// ObjectType is the type of the resource that the reconciler will reconcile.
ObjectType client.Object
// EventCh is the channel where the reconciler will send events.
EventCh chan<- interface{}
// NamespacedNameFilter filters resources the controller will process. Can be nil.
NamespacedNameFilter NamespacedNameFilterFunc
// OnlyMetadata indicates that this controller for this resource is only caching metadata for the resource.
OnlyMetadata bool
}
// Reconciler reconciles Kubernetes resources of a specific type.
// It implements the reconcile.Reconciler interface.
// A successful reconciliation of a resource has the two possible outcomes:
// (1) If the resource is deleted, the Implementation will send a DeleteEvent to the event channel.
// (2) If the resource is upserted (created or updated), the Implementation will send an UpsertEvent
// to the event channel.
type Reconciler struct {
cfg ReconcilerConfig
}
var _ reconcile.Reconciler = &Reconciler{}
// NewReconciler creates a new reconciler.
func NewReconciler(cfg ReconcilerConfig) *Reconciler {
return &Reconciler{
cfg: cfg,
}
}
func (r *Reconciler) newObject(objectType client.Object) client.Object {
if r.cfg.OnlyMetadata {
partialObj := &metav1.PartialObjectMetadata{}
partialObj.SetGroupVersionKind(objectType.GetObjectKind().GroupVersionKind())
return partialObj
}
// without Elem(), t will be a pointer to the type. For example, *v1.Gateway, not v1.Gateway
t := reflect.TypeOf(objectType).Elem()
// We could've used objectType.DeepCopyObject() here, but it's a bit slower confirmed by benchmarks.
return reflect.New(t).Interface().(client.Object)
}
// Reconcile implements the reconcile.Reconciler Reconcile method.
func (r *Reconciler) Reconcile(ctx context.Context, req reconcile.Request) (reconcile.Result, error) {
logger := log.FromContext(ctx)
// The controller runtime has set the logger with the group, kind, namespace and name of the resource,
// and a few other key/value pairs. So we don't need to set them here.
logger.Info("Reconciling the resource")
if r.cfg.NamespacedNameFilter != nil {
if shouldProcess, msg := r.cfg.NamespacedNameFilter(req.NamespacedName); !shouldProcess {
logger.Info(msg)
return reconcile.Result{}, nil
}
}
obj := r.newObject(r.cfg.ObjectType)
if err := r.cfg.Getter.Get(ctx, req.NamespacedName, obj); err != nil {
if !apierrors.IsNotFound(err) {
logger.Error(err, "Failed to get the resource")
return reconcile.Result{}, err
}
// The resource does not exist (was deleted).
obj = nil
}
var e interface{}
var op string
if obj == nil {
e = &events.DeleteEvent{
Type: r.cfg.ObjectType,
NamespacedName: req.NamespacedName,
}
op = "Deleted"
} else {
e = &events.UpsertEvent{
Resource: obj,
}
op = "Upserted"
}
select {
case <-ctx.Done():
logger.Info("Did not process the resource because the context was canceled")
return reconcile.Result{}, nil
case r.cfg.EventCh <- e:
}
logger.Info(fmt.Sprintf("%s the resource", op))
return reconcile.Result{}, nil
}