-
Notifications
You must be signed in to change notification settings - Fork 1
/
builders_reconciler.go
306 lines (263 loc) · 8.79 KB
/
builders_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
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
// Licensed to Alexandre VILAIN under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Alexandre VILAIN licenses this file to you 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 reconciler
import (
"context"
"errors"
"fmt"
"reflect"
"sort"
"time"
"github.com/alexandrevilain/controller-tools/pkg/discovery"
"github.com/alexandrevilain/controller-tools/pkg/resource"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/tools/record"
kstatus "sigs.k8s.io/cli-utils/pkg/kstatus/status"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/log"
)
type BuildersReconciler struct {
client.Client
Scheme *runtime.Scheme
Recorder record.EventRecorder
Discovery discovery.Manager
}
type Resource struct {
builder resource.Builder
current client.Object
found bool
}
func (r *BuildersReconciler) Reconcile(ctx context.Context, owner client.Object, builders []resource.Builder) ([]*resource.Status, time.Duration, error) {
logger := log.FromContext(ctx)
resources, err := r.getResourcesFromBuilders(ctx, builders)
if err != nil {
return nil, 0, err
}
// Sort resources by their dependencies. The objective is to run builders without dependencies first
// as they could be dependent for another builders.
// We may have the need for a graph, but looks like it would work as expected for now.
sort.Slice(resources, func(i, j int) bool {
iDependentBuilder, iHasDependent := resources[i].builder.(resource.DependentBuilder)
jDependentBuilder, jHasDependent := resources[j].builder.(resource.DependentBuilder)
// If both have dependences, sort by dependencies length.
if iHasDependent && jHasDependent {
return len(iDependentBuilder.Dependencies()) < len(jDependentBuilder.Dependencies())
}
// If i has dependencies but not j, i should be after j.
if iHasDependent && !jHasDependent {
return false
}
// Otherwise i could be before j, it doesn't matter.
return true
})
logger.Info("Reconciling resources", "count", len(resources))
statuses := []*resource.Status{}
for _, res := range resources {
// If the builder isn't enabled, check if it needs to be deleted, then skip iteration.
if !res.builder.Enabled() {
if res.found {
err := r.Client.Delete(ctx, res.current)
r.logAndRecordOperationResult(ctx, owner, res.current, controllerutil.OperationResult("deleted"), err)
if err != nil {
return nil, 0, fmt.Errorf("can't delete resource: %w", err)
}
}
continue
}
// The build can provide a custom compare function, ensure equality.Semantic knowns it.
if comparer, ok := res.builder.(resource.Comparer); ok {
err := equality.Semantic.AddFunc(comparer.Equal)
if err != nil {
return nil, 0, err
}
}
if dependent, ok := res.builder.(resource.DependentBuilder); ok {
for _, dependency := range dependent.Dependencies() {
dependency := dependency
objectKey := client.ObjectKey{
Name: dependency.Name,
Namespace: dependency.Namespace,
}
logger.Info("Checking builder dependency",
"builder", fmt.Sprintf("%T", res.builder),
"dependency", objectKey,
)
err := r.Client.Get(ctx, objectKey, dependency.Object)
if err != nil {
if apierrors.IsNotFound(err) {
return nil, 5 * time.Second, nil
}
return nil, 0, err
}
status, err := r.getResourceStatus(dependency.Object)
if err != nil {
return nil, 0, err
}
if !status.Ready {
return nil, 5 * time.Second, nil
}
}
}
// Create case
if !res.found && res.builder.Enabled() {
res.current = res.builder.Build()
err := res.builder.Update(res.current)
if err != nil {
return nil, 0, err
}
err = r.Client.Create(ctx, res.current)
r.logAndRecordOperationResult(ctx, owner, res.current, controllerutil.OperationResultCreated, err)
if err != nil {
return nil, 0, err
}
}
// Update case
if res.found && res.builder.Enabled() {
before := res.current.DeepCopyObject()
err := res.builder.Update(res.current)
if err != nil {
return nil, 0, err
}
if !equality.Semantic.DeepEqual(before, res.current) {
err = r.Client.Update(ctx, res.current)
r.logAndRecordOperationResult(ctx, owner, res.current, controllerutil.OperationResultUpdated, err)
if err != nil {
return nil, 0, err
}
}
}
// Report status
status, err := r.getResourceStatus(res.current)
if err != nil {
return nil, 0, err
}
statuses = append(statuses, status)
}
return statuses, 0, nil
}
func (r *BuildersReconciler) getResourceStatus(res client.Object) (*resource.Status, error) {
udeploy, err := runtime.DefaultUnstructuredConverter.ToUnstructured(res)
if err != nil {
return nil, err
}
u := &unstructured.Unstructured{}
u.SetUnstructuredContent(udeploy)
status, err := kstatus.Compute(u)
if err != nil {
return nil, err
}
return &resource.Status{
GVK: res.GetObjectKind().GroupVersionKind(),
Name: res.GetName(),
Namespace: res.GetNamespace(),
Labels: res.GetLabels(),
Ready: status.Status == kstatus.CurrentStatus,
}, nil
}
func (r *BuildersReconciler) getType(gvk schema.GroupVersionKind) reflect.Type {
for typename, reflectType := range r.Scheme.KnownTypes(gvk.GroupVersion()) {
if typename == gvk.Kind {
return reflectType
}
}
return nil
}
func (r *BuildersReconciler) getResourcesFromBuilders(ctx context.Context, builders []resource.Builder) ([]*Resource, error) {
logger := log.FromContext(ctx)
result := []*Resource{}
for _, builder := range builders {
res := builder.Build()
gvk, err := apiutil.GVKForObject(res, r.Scheme)
if err != nil {
return nil, err
}
objectType := r.getType(gvk)
if objectType == nil {
return nil, fmt.Errorf("can't get type for %s", gvk)
}
object, ok := reflect.New(objectType).Interface().(client.Object)
if !ok {
return nil, errors.New("can't create a new client.Object instance from known type")
}
supported, err := r.Discovery.IsGVKSupported(gvk)
if err != nil {
return nil, fmt.Errorf("can't determine if GVK \"%s\" is supported: %w", gvk.String(), err)
}
if !supported {
logger.V(2).Info("Skipping resource due to unsupported by apiserver", "kind", gvk.Kind)
continue
}
found := true
err = r.Client.Get(ctx, client.ObjectKeyFromObject(res), object, &client.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
found = false
} else {
return nil, err
}
}
result = append(result, &Resource{
builder: builder,
current: object,
found: found,
})
}
return result, nil
}
// logAndRecordOperationResult logs and records an event for the provided object operation result.
func (r *BuildersReconciler) logAndRecordOperationResult(ctx context.Context, owner, resource runtime.Object, operationResult controllerutil.OperationResult, err error) {
logger := log.FromContext(ctx)
var (
action string
reason string
)
switch operationResult {
case controllerutil.OperationResultCreated:
action = "create"
reason = "RessourceCreate"
case controllerutil.OperationResultUpdated, controllerutil.OperationResultUpdatedStatus, controllerutil.OperationResultUpdatedStatusOnly:
action = "update"
reason = "ResourceUpdate"
case controllerutil.OperationResult("deleted"):
action = "delete"
reason = "ResourceDelete"
case controllerutil.OperationResultNone:
fallthrough
default:
return
}
if err == nil {
msg := fmt.Sprintf("%sd resource %s of type %T", action, resource.(metav1.Object).GetName(), resource.(metav1.Object))
reason := fmt.Sprintf("%sSuccess", reason)
logger.Info(msg)
r.Recorder.Event(owner, corev1.EventTypeNormal, reason, msg)
}
if err != nil {
msg := fmt.Sprintf("failed to %s resource %s of Type %T", action, resource.(metav1.Object).GetName(), resource.(metav1.Object))
reason := fmt.Sprintf("%sError", reason)
logger.Error(err, msg)
r.Recorder.Event(owner, corev1.EventTypeWarning, reason, msg)
}
}