/
controller.go
339 lines (300 loc) · 11.3 KB
/
controller.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
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
package reconciliation
import (
"context"
"encoding/json"
"fmt"
"os"
"time"
"k8s.io/apimachinery/pkg/api/meta"
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/types"
"k8s.io/apimachinery/pkg/util/jsonmergepatch"
"k8s.io/apimachinery/pkg/util/strategicpatch"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/rest"
"k8s.io/utils/ptr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
apiv1 "github.com/Azure/eno/api/v1"
"github.com/Azure/eno/internal/discovery"
"github.com/Azure/eno/internal/flowcontrol"
"github.com/Azure/eno/internal/reconstitution"
"github.com/go-logr/logr"
)
var insecureLogPatch = os.Getenv("INSECURE_LOG_PATCH") == "true"
type Options struct {
Manager ctrl.Manager
Cache *reconstitution.Cache
WriteBuffer *flowcontrol.ResourceSliceWriteBuffer
Downstream *rest.Config
DiscoveryRPS float32
RediscoverWhenNotFound bool
Timeout time.Duration
ReadinessPollInterval time.Duration
}
type Controller struct {
client client.Client
writeBuffer *flowcontrol.ResourceSliceWriteBuffer
resourceClient reconstitution.Client
timeout time.Duration
readinessPollInterval time.Duration
upstreamClient client.Client
discovery *discovery.Cache
}
func New(opts Options) (*Controller, error) {
upstreamClient, err := client.New(opts.Downstream, client.Options{
Scheme: runtime.NewScheme(), // empty scheme since we shouldn't rely on compile-time types
})
if err != nil {
return nil, err
}
disc, err := discovery.NewCache(opts.Downstream, opts.DiscoveryRPS, opts.RediscoverWhenNotFound)
if err != nil {
return nil, err
}
return &Controller{
client: opts.Manager.GetClient(),
writeBuffer: opts.WriteBuffer,
resourceClient: opts.Cache,
timeout: opts.Timeout,
readinessPollInterval: opts.ReadinessPollInterval,
upstreamClient: upstreamClient,
discovery: disc,
}, nil
}
func (c *Controller) Reconcile(ctx context.Context, req *reconstitution.Request) (ctrl.Result, error) {
ctx, cancel := context.WithTimeout(ctx, c.timeout)
defer cancel()
comp := &apiv1.Composition{}
err := c.client.Get(ctx, types.NamespacedName{Name: req.Composition.Name, Namespace: req.Composition.Namespace}, comp)
if err != nil {
return ctrl.Result{}, client.IgnoreNotFound(fmt.Errorf("getting composition: %w", err))
}
logger := logr.FromContextOrDiscard(ctx).WithValues("compositionGeneration", comp.Generation)
if comp.Status.CurrentSynthesis == nil {
return ctrl.Result{}, nil // nothing to do
}
logger = logger.WithValues("synthesizerName", comp.Spec.Synthesizer.Name, "synthesizerGeneration", comp.Status.CurrentSynthesis.ObservedSynthesizerGeneration)
ctx = logr.NewContext(ctx, logger)
// Find the current and (optionally) previous desired states in the cache
compRef := reconstitution.NewCompositionRef(comp)
resource, exists := c.resourceClient.Get(ctx, compRef, &req.Resource)
if !exists {
// It's possible for the cache to be empty because a manifest for this resource no longer exists at the requested composition generation.
// Dropping the work item is safe since filling the new version will generate a new queue message.
logger.V(1).Info("dropping work item because the corresponding manifest generation no longer exists in the cache")
return ctrl.Result{}, nil
}
var prev *reconstitution.Resource
if comp.Status.PreviousSynthesis != nil {
compRef.Generation = comp.Status.PreviousSynthesis.ObservedCompositionGeneration
prev, _ = c.resourceClient.Get(ctx, compRef, &req.Resource)
}
// Keep track of the last reconciliation time and report on it relative to the resource's reconcile interval
// This is useful for identifying cases where the loop can't keep up
if resource.Manifest.ReconcileInterval != nil {
observation := resource.ObserveReconciliation()
if observation > 0 {
delta := observation - resource.Manifest.ReconcileInterval.Duration
reconciliationScheduleDelta.Observe(delta.Seconds())
}
}
// Fetch the current resource
apiVersion, _ := resource.GVK.ToAPIVersionAndKind()
current, hasChanged, err := c.getCurrent(ctx, resource, apiVersion)
if client.IgnoreNotFound(err) != nil {
return ctrl.Result{}, fmt.Errorf("getting current state: %w", err)
}
// Nil current struct means the resource version hasn't changed since it was last observed
// Skip without logging since this is a very hot path
var modified bool
if hasChanged {
modified, err = c.reconcileResource(ctx, comp, prev, resource, current)
if err != nil {
return ctrl.Result{}, err
}
}
// Evaluate resource readiness
// - Readiness checks are skipped when this version of the resource's desired state has already become ready
// - Readiness checks are skipped when the resource hasn't changed since the last check
// - Readiness defaults to true if no checks are given
slice := &apiv1.ResourceSlice{}
err = c.client.Get(ctx, req.Manifest.Slice, slice)
if err != nil {
return ctrl.Result{}, client.IgnoreNotFound(fmt.Errorf("getting resource slice: %w", err))
}
var ready *metav1.Time
if status := req.Manifest.FindStatus(slice); status == nil || status.Ready == nil {
readiness, ok := resource.ReadinessChecks.EvalOptionally(ctx, current)
if ok {
ready = &readiness.ReadyTime
}
}
if modified {
return ctrl.Result{Requeue: true}, nil
}
// Store the results
deleted := current == nil || current.GetDeletionTimestamp() != nil
c.writeBuffer.PatchStatusAsync(ctx, &req.Manifest, patchResourceState(deleted, ready))
if ready == nil {
return ctrl.Result{RequeueAfter: wait.Jitter(c.readinessPollInterval, 0.1)}, nil
}
if resource != nil && !resource.Deleted() && resource.Manifest.ReconcileInterval != nil {
return ctrl.Result{RequeueAfter: wait.Jitter(resource.Manifest.ReconcileInterval.Duration, 0.1)}, nil
}
return ctrl.Result{}, nil
}
func (c *Controller) reconcileResource(ctx context.Context, comp *apiv1.Composition, prev, resource *reconstitution.Resource, current *unstructured.Unstructured) (bool, error) {
logger := logr.FromContextOrDiscard(ctx)
start := time.Now()
defer func() {
reconciliationLatency.Observe(time.Since(start).Seconds())
}()
if resource.Deleted() {
if current == nil || current.GetDeletionTimestamp() != nil {
return false, nil // already deleted - nothing to do
}
if comp.Annotations["eno.azure.io/deletion-strategy"] == "orphan" {
return false, nil
}
reconciliationActions.WithLabelValues("delete").Inc()
obj, err := resource.Parse()
if err != nil {
return false, fmt.Errorf("invalid resource: %w", err)
}
err = c.upstreamClient.Delete(ctx, obj)
if err != nil {
return false, client.IgnoreNotFound(fmt.Errorf("deleting resource: %w", err))
}
logger.V(0).Info("deleted resource")
return true, nil
}
// Always create the resource when it doesn't exist
if current == nil {
reconciliationActions.WithLabelValues("create").Inc()
obj, err := resource.Parse()
if err != nil {
return false, fmt.Errorf("invalid resource: %w", err)
}
err = c.upstreamClient.Create(ctx, obj)
if err != nil {
return false, fmt.Errorf("creating resource: %w", err)
}
logger.V(0).Info("created resource")
return true, nil
}
// Compute a merge patch
prevRV := current.GetResourceVersion()
patch, patchType, err := c.buildPatch(ctx, prev, resource, current)
if err != nil {
return false, fmt.Errorf("building patch: %w", err)
}
patch, err = mungePatch(patch, current.GetResourceVersion())
if err != nil {
return false, fmt.Errorf("adding resource version: %w", err)
}
if len(patch) == 0 {
logger.V(1).Info("skipping empty patch")
return false, nil
}
reconciliationActions.WithLabelValues("patch").Inc()
if insecureLogPatch {
logger.V(1).Info("INSECURE logging patch", "patch", string(patch))
}
err = c.upstreamClient.Patch(ctx, current, client.RawPatch(patchType, patch))
if err != nil {
return false, fmt.Errorf("applying patch: %w", err)
}
logger.V(0).Info("patched resource", "patchType", string(patchType), "resourceVersion", current.GetResourceVersion(), "previousResourceVersion", prevRV)
return true, nil
}
func (c *Controller) buildPatch(ctx context.Context, prev, resource *reconstitution.Resource, current *unstructured.Unstructured) ([]byte, types.PatchType, error) {
var prevManifest []byte
if prev != nil {
prevManifest = []byte(prev.Manifest.Manifest)
}
currentJS, err := current.MarshalJSON()
if err != nil {
return nil, "", reconcile.TerminalError(fmt.Errorf("building json representation of desired state: %w", err))
}
model, err := c.discovery.Get(ctx, resource.GVK)
if err != nil {
return nil, "", fmt.Errorf("getting merge metadata: %w", err)
}
if model == nil {
patch, err := jsonmergepatch.CreateThreeWayJSONMergePatch(prevManifest, []byte(resource.Manifest.Manifest), currentJS)
if err != nil {
return nil, "", reconcile.TerminalError(err)
}
return patch, types.MergePatchType, err
}
patchmeta := strategicpatch.NewPatchMetaFromOpenAPI(model)
patch, err := strategicpatch.CreateThreeWayMergePatch(prevManifest, []byte(resource.Manifest.Manifest), currentJS, patchmeta, true)
if err != nil {
return nil, "", reconcile.TerminalError(err)
}
return patch, types.StrategicMergePatchType, err
}
func (c *Controller) getCurrent(ctx context.Context, resource *reconstitution.Resource, apiVersion string) (*unstructured.Unstructured, bool, error) {
if resource.HasBeenSeen() && !resource.Deleted() {
meta := &metav1.PartialObjectMetadata{}
meta.Name = resource.Ref.Name
meta.Namespace = resource.Ref.Namespace
meta.Kind = resource.Ref.Kind
meta.APIVersion = apiVersion
err := c.upstreamClient.Get(ctx, client.ObjectKeyFromObject(meta), meta)
if err != nil {
return nil, false, err
}
if resource.MatchesLastSeen(meta.ResourceVersion) {
return nil, false, nil
}
resourceVersionChanges.Inc()
}
current := &unstructured.Unstructured{}
current.SetName(resource.Ref.Name)
current.SetNamespace(resource.Ref.Namespace)
current.SetKind(resource.Ref.Kind)
current.SetAPIVersion(apiVersion)
err := c.upstreamClient.Get(ctx, client.ObjectKeyFromObject(current), current)
if err != nil {
return nil, true, err
}
if rv := current.GetResourceVersion(); rv != "" {
resource.ObserveVersion(rv)
}
return current, true, nil
}
func mungePatch(patch []byte, rv string) ([]byte, error) {
var patchMap map[string]interface{}
err := json.Unmarshal(patch, &patchMap)
if err != nil {
return nil, reconcile.TerminalError(err)
}
u := unstructured.Unstructured{Object: patchMap}
a, err := meta.Accessor(&u)
if err != nil {
return nil, reconcile.TerminalError(err)
}
a.SetResourceVersion(rv)
a.SetCreationTimestamp(metav1.Time{})
if len(patchMap) <= 1 {
return nil, nil // resource version only == empty patch
}
return json.Marshal(patchMap)
}
func patchResourceState(deleted bool, ready *metav1.Time) flowcontrol.StatusPatchFn {
return func(rs *apiv1.ResourceState) *apiv1.ResourceState {
if rs != nil && rs.Deleted == deleted && rs.Reconciled && ptr.Deref(rs.Ready, metav1.Time{}) == ptr.Deref(ready, metav1.Time{}) {
return nil
}
return &apiv1.ResourceState{
Deleted: deleted,
Ready: ready,
Reconciled: true,
}
}
}