-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneric.go
236 lines (206 loc) · 7.71 KB
/
generic.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
package framework
import (
"context"
"fmt"
"github.com/go-logr/logr"
"halkyon.io/api/v1beta1"
"halkyon.io/operator-framework/util"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
"strings"
"sync"
)
// GenericReconciler implements Reconciler in a generic way as it pertains to reconciling a Resource
type GenericReconciler struct {
resource Resource
}
// blank assignment to make sure we implement Reconciler
var _ reconcile.Reconciler = &GenericReconciler{}
// NewGenericReconciler creates a new GenericReconciler that can handle resources represented by the specified Resource, which
// acts as a prototype standing in for instances that will be reconciled.
func NewGenericReconciler(resource Resource) *GenericReconciler {
return &GenericReconciler{resource: resource}
}
func (b *GenericReconciler) logger() logr.Logger {
return LoggerFor(b.resource.GetUnderlyingAPIResource())
}
func (b *GenericReconciler) Reconcile(request reconcile.Request) (reconcile.Result, error) {
b.logger().WithValues("namespace", request.Namespace)
typeName := util.GetObjectName(b.resource)
// Get a new empty instance from the prototype
resource := b.resource.NewEmpty()
// Initialize it from the cluster state, using the name / namespace from the reconcile request
resource.SetName(request.Name)
resource.SetNamespace(request.Namespace)
_, err := Helper.Fetch(request.Name, request.Namespace, resource.GetUnderlyingAPIResource())
if err != nil {
if errors.IsNotFound(err) {
// Return and don't create
b.logger().Info("'" + request.Name + "' " + typeName + " is marked for deletion. Running clean-up.")
err := resource.Delete()
return reconcile.Result{}, err
}
// Error reading the object - create the request.
b.logger().Error(err, "failed to initialize '"+request.Name+"' "+typeName)
if resource != nil {
err = UpdateStatusIfNeeded(resource, err)
return reconcile.Result{}, nil
}
return reconcile.Result{}, err
}
// Initialize with default values if needed
if resource.ProvideDefaultValues() {
if e := Helper.Client.Update(context.Background(), resource.GetUnderlyingAPIResource()); e != nil {
b.logger().Error(e, fmt.Sprintf("failed to update '%s' %s", resource.GetName(), typeName))
}
return reconcile.Result{}, nil
}
// Check the validity of the resource
if err := resource.CheckValidity(); err != nil {
err = UpdateStatusIfNeeded(resource, fmt.Errorf("validation error(s): %v", err))
return reconcile.Result{}, err
}
// Initialize dependents
dependents, err := resource.InitDependentResources()
if err != nil {
return reconcile.Result{}, err
}
// Initialize status if needed
status := resource.GetStatus()
if len(status.Conditions) == 0 {
status.Conditions = make([]v1beta1.DependentCondition, 0, len(dependents))
}
for _, dependent := range dependents {
// add watch if needed
config := dependent.GetConfig()
if config.Watched {
if err := getCallbackFor(b.resource)(dependent.Owner(), config.GroupVersionKind); err != nil {
return reconcile.Result{}, err
}
}
}
resource.SetStatus(status)
initialStatus := status.Reason
b.logger().Info("-> "+typeName, "name", resource.GetName(), "status", initialStatus)
err = resource.CreateOrUpdate()
// always check status for updates
if err = UpdateStatusIfNeeded(resource, err); err != nil {
return reconcile.Result{}, err
}
requeue := resource.NeedsRequeue()
// only log exit if status changed to avoid being too verbose
if status.Reason != initialStatus {
msg := "<- " + typeName
if requeue {
msg += " (requeued)"
}
b.logger().Info(msg, "name", resource.GetName(), "status", status.Reason)
}
return reconcile.Result{Requeue: requeue}, nil
}
// UpdateStatusIfNeeded updates the status of the specified Resource, computing its status or handling the specified error
// if it's not nil
func UpdateStatusIfNeeded(instance Resource, err error) error {
// update the resource if the status has changed
object := instance.GetUnderlyingAPIResource()
logger := LoggerFor(object)
updateStatus := false
if err == nil {
updateStatus = instance.ComputeStatus()
} else {
status := instance.GetStatus()
updateStatus, status = instance.Handle(err)
if updateStatus {
instance.SetStatus(status)
}
logger.Error(err, fmt.Sprintf("'%s' %s has an error", instance.GetName(), util.GetObjectName(instance.GetUnderlyingAPIResource())))
}
if updateStatus {
if e := Helper.Client.Status().Update(context.Background(), object); e != nil {
logger.Error(e, fmt.Sprintf("failed to update status for '%s' %s", instance.GetName(), util.GetObjectName(object)))
return e
}
}
return nil
}
// RegisterNewReconciler creates a new GenericReconciler for the specified Resource and register it with the specified Manager,
// setting up watches as needed depending on the Resource and its DependentResources configuration
func RegisterNewReconciler(resource Resource, mgr manager.Manager) error {
resourceType := resource.GetUnderlyingAPIResource()
// Create a new controller
controllerName := controllerNameFor(resourceType)
reconciler := NewGenericReconciler(resource)
c, err := controller.New(controllerName, mgr, controller.Options{Reconciler: reconciler})
if err != nil {
return err
}
// Register logger
registerLogger(controllerName)
// Watch for changes to primary resource
if err = c.Watch(&source.Kind{Type: resourceType}, &handler.EnqueueRequestForObject{}); err != nil {
return err
}
// Create callback for dependent resources to add themselves as watched resources
callbacks[controllerName] = createCallbackFor(c)
return nil
}
type WatchCallback func(owner SerializableResource, dependentGVK schema.GroupVersionKind) error
var (
callbacks = make(map[string]WatchCallback, 7)
// record which gvks we're already watching to not register another watch again
watched = make(map[schema.GroupVersionKind]bool, 21)
mutex = &sync.Mutex{}
)
func createCallbackFor(c controller.Controller) WatchCallback {
return func(resource SerializableResource, dependentGVK schema.GroupVersionKind) error {
// if we're not already watching this secondary resource
mutex.Lock()
notAlreadyWatched := !watched[dependentGVK]
mutex.Unlock()
if notAlreadyWatched {
// watch it
owner := &handler.EnqueueRequestForOwner{
IsController: true,
OwnerType: CreateEmptyUnstructured(resource.GetGroupVersionKind()),
}
if err := c.Watch(createSourceForGVK(dependentGVK), owner); err != nil {
return err
}
mutex.Lock()
watched[dependentGVK] = true
mutex.Unlock()
}
return nil
}
}
func getCallbackFor(resource Resource) WatchCallback {
return callbacks[controllerNameFor(resource.GetUnderlyingAPIResource())]
}
func controllerNameFor(resource SerializableResource) string {
return strings.ToLower(util.GetObjectName(resource)) + "-controller"
}
func CreateUnstructuredObject(from runtime.Object, gvk schema.GroupVersionKind) (runtime.Object, error) {
u, err := runtime.DefaultUnstructuredConverter.ToUnstructured(from)
if err != nil {
return nil, err
}
obj := &unstructured.Unstructured{Object: u}
obj.SetGroupVersionKind(gvk)
return obj, nil
}
// createSourceForGVK creates a *source.Kind for the given gvk.
func createSourceForGVK(gvk schema.GroupVersionKind) *source.Kind {
return &source.Kind{Type: CreateEmptyUnstructured(gvk)}
}
func CreateEmptyUnstructured(gvk schema.GroupVersionKind) *unstructured.Unstructured {
u := &unstructured.Unstructured{}
u.SetGroupVersionKind(gvk)
return u
}