/
lifecycle.go
317 lines (277 loc) · 12.2 KB
/
lifecycle.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
package synthesis
import (
"context"
"fmt"
"time"
"github.com/go-logr/logr"
"github.com/google/uuid"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/util/flowcontrol"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/handler"
apiv1 "github.com/Azure/eno/api/v1"
"github.com/Azure/eno/internal/manager"
)
type Config struct {
SliceCreationQPS float64
PodNamespace string
PodServiceAccount string
}
type podLifecycleController struct {
config *Config
client client.Client
noCacheReader client.Reader
createSliceLimit flowcontrol.RateLimiter
}
// NewPodLifecycleController is responsible for creating and deleting pods as needed to synthesize compositions.
func NewPodLifecycleController(mgr ctrl.Manager, cfg *Config) error {
c := &podLifecycleController{
config: cfg,
client: mgr.GetClient(),
noCacheReader: mgr.GetAPIReader(),
createSliceLimit: flowcontrol.NewTokenBucketRateLimiter(float32(cfg.SliceCreationQPS), 1),
}
return ctrl.NewControllerManagedBy(mgr).
For(&apiv1.Composition{}).
Watches(&corev1.Pod{}, handler.EnqueueRequestsFromMapFunc(manager.PodToCompMapFunc)).
WithLogConstructor(manager.NewLogConstructor(mgr, "podLifecycleController")).
Complete(c)
}
func (c *podLifecycleController) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
logger := logr.FromContextOrDiscard(ctx)
comp := &apiv1.Composition{}
err := c.client.Get(ctx, req.NamespacedName, comp)
if err != nil {
return ctrl.Result{}, client.IgnoreNotFound(fmt.Errorf("getting composition resource: %w", err))
}
logger = logger.WithValues("compositionName", comp.Name, "compositionNamespace", comp.Namespace, "compositionGeneration", comp.Generation)
// It isn't safe to delete compositions until their resource slices have been cleaned up,
// since reconciling resources necessarily requires the composition.
if comp.DeletionTimestamp == nil && controllerutil.AddFinalizer(comp, "eno.azure.io/cleanup") {
err = c.client.Update(ctx, comp)
if err != nil {
return ctrl.Result{}, fmt.Errorf("updating composition: %w", err)
}
logger.V(1).Info("added cleanup finalizer to composition")
return ctrl.Result{}, nil
}
// Delete any unnecessary pods
pods := &corev1.PodList{}
err = c.client.List(ctx, pods, client.InNamespace(c.config.PodNamespace), client.MatchingFields{
manager.IdxPodsByComposition: comp.Name,
})
if err != nil {
return ctrl.Result{}, fmt.Errorf("listing pods: %w", err)
}
syn := &apiv1.Synthesizer{}
syn.Name = comp.Spec.Synthesizer.Name
err = c.client.Get(ctx, client.ObjectKeyFromObject(syn), syn)
if err != nil {
return ctrl.Result{}, fmt.Errorf("getting synthesizer: %w", err)
}
logger = logger.WithValues("synthesizerName", syn.Name, "synthesizerGeneration", syn.Generation)
logger, toDelete, exists := shouldDeletePod(logger, comp, syn, pods)
if toDelete != nil {
if err := c.client.Delete(ctx, toDelete); err != nil {
return ctrl.Result{}, client.IgnoreNotFound(fmt.Errorf("deleting pod: %w", err))
}
logger.V(0).Info("deleted synthesizer pod", "podName", toDelete.Name)
return ctrl.Result{}, nil
}
if exists {
// The pod is still running.
// Poll periodically to check if has timed out.
if syn.Spec.PodTimeout == nil {
return ctrl.Result{}, nil
}
return ctrl.Result{RequeueAfter: syn.Spec.PodTimeout.Duration}, nil
}
if comp.DeletionTimestamp != nil {
ctx = logr.NewContext(ctx, logger)
return c.reconcileDeletedComposition(ctx, comp, pods)
}
// Swap the state to prepare for resynthesis if needed
if comp.Status.CurrentSynthesis == nil || comp.Status.CurrentSynthesis.ObservedCompositionGeneration != comp.Generation {
swapStates(comp, syn)
if err := c.client.Status().Update(ctx, comp); err != nil {
return ctrl.Result{}, fmt.Errorf("swapping compisition state: %w", err)
}
logger.V(0).Info("start to synthesize")
return ctrl.Result{}, nil
}
// No need to create a pod if everything is in sync
if comp.Status.CurrentSynthesis.Synthesized != nil || comp.DeletionTimestamp != nil {
return ctrl.Result{}, nil
}
// Don't attempt to synthesize a composition that doesn't reference a synthesizer.
if comp.Spec.Synthesizer.Name == "" {
return ctrl.Result{}, nil
}
// Before it's safe to create a pod, we need to write _something_ back to the composition.
// This will fail if another write has already hit the resource i.e. synthesis completion.
// Otherwise it's possible for pod deletion event to land before the synthesis complete event.
// In that case a new pod would be created even though the synthesis just completed.
if comp.Status.CurrentSynthesis.UUID == "" {
comp.Status.CurrentSynthesis.UUID = uuid.NewString()
if err := c.client.Status().Update(ctx, comp); err != nil {
return ctrl.Result{}, fmt.Errorf("writing started timestamp to status: %w", err)
}
return ctrl.Result{}, nil
}
// Back off to avoid constantly re-synthesizing impossible compositions (unlikely but possible)
if shouldBackOffPodCreation(comp) {
const base = time.Millisecond * 250
wait := base * time.Duration(comp.Status.CurrentSynthesis.Attempts)
nextAttempt := comp.Status.CurrentSynthesis.PodCreation.Time.Add(wait)
if time.Since(nextAttempt) < 0 { // positive when past the nextAttempt
logger.V(1).Info("backing off pod creation", "latency", wait.Milliseconds())
return ctrl.Result{RequeueAfter: wait}, nil
}
}
// Confirm that a pod doesn't already exist for this synthesis without trusting informers.
// This protects against cases where synthesis has recently started and something causes
// another tick of this loop before the pod write hits the informer.
err = c.noCacheReader.List(ctx, pods, client.MatchingLabels{
"eno.azure.io/synthesis-uuid": comp.Status.CurrentSynthesis.UUID,
})
if err != nil {
return ctrl.Result{}, fmt.Errorf("checking for existing pod: %w", err)
}
for _, pod := range pods.Items {
if pod.DeletionTimestamp == nil {
logger.V(0).Info(fmt.Sprintf("refusing to create new synthesizer pod because the pod %q already exists and has not been deleted", pod.Name))
return ctrl.Result{}, nil
}
}
// If we made it this far it's safe to create a pod
pod := newPod(c.config, comp, syn)
err = c.client.Create(ctx, pod)
if err != nil {
return ctrl.Result{}, fmt.Errorf("creating pod: %w", err)
}
logger.V(0).Info("created synthesizer pod", "podName", pod.Name)
sytheses.Inc()
return ctrl.Result{}, nil
}
func (c *podLifecycleController) reconcileDeletedComposition(ctx context.Context, comp *apiv1.Composition, pods *corev1.PodList) (ctrl.Result, error) {
logger := logr.FromContextOrDiscard(ctx)
// If the composition was being synthesized at the time of deletion we need to swap the previous
// state back to current. Otherwise we'll get stuck waiting for a synthesis that can't happen.
if shouldRevertStateSwap(comp) {
comp.Status.CurrentSynthesis = comp.Status.PreviousSynthesis
comp.Status.PreviousSynthesis = nil
err := c.client.Status().Update(ctx, comp)
if err != nil {
return ctrl.Result{}, fmt.Errorf("reverting swapped status for deletion: %w", err)
}
logger.V(0).Info("reverted swapped status for deletion")
return ctrl.Result{}, nil
}
// Deletion increments the composition's generation, but the reconstitution cache is only invalidated
// when the synthesized generation (from the status) changes, which will never happen because synthesis
// is righly disabled for deleted compositions. We break out of this deadlock condition by updating
// the status without actually synthesizing.
if shouldUpdateDeletedCompositionStatus(comp) {
comp.Status.CurrentSynthesis.ObservedCompositionGeneration = comp.Generation
comp.Status.CurrentSynthesis.Ready = nil
comp.Status.CurrentSynthesis.Reconciled = nil
now := metav1.Now()
comp.Status.CurrentSynthesis.Synthesized = &now
err := c.client.Status().Update(ctx, comp)
if err != nil {
return ctrl.Result{}, fmt.Errorf("updating current composition generation: %w", err)
}
logger.V(0).Info("updated composition status to reflect deletion")
return ctrl.Result{}, nil
}
// Remove the finalizer when all pods and slices have been deleted
if isReconciling(comp) {
logger.V(1).Info("refusing to remove composition finalizer because it is still being reconciled")
return ctrl.Result{}, nil
}
if controllerutil.RemoveFinalizer(comp, "eno.azure.io/cleanup") {
err := c.client.Update(ctx, comp)
if err != nil {
return ctrl.Result{}, fmt.Errorf("removing finalizer: %w", err)
}
logger.V(0).Info("removed finalizer from composition")
}
return ctrl.Result{}, nil
}
func shouldDeletePod(logger logr.Logger, comp *apiv1.Composition, syn *apiv1.Synthesizer, pods *corev1.PodList) (logr.Logger, *corev1.Pod, bool /* exists */) {
if len(pods.Items) == 0 {
return logger, nil, false
}
// Only create pods when the previous one is deleting or non-existant
var onePodDeleting bool
for _, pod := range pods.Items {
pod := pod
// Allow a single extra pod to be created while the previous one is terminating
// in order to break potential deadlocks while avoiding a thundering herd of pods
if pod.DeletionTimestamp != nil {
if onePodDeleting {
return logger, nil, true
}
onePodDeleting = true
continue
}
if comp.DeletionTimestamp != nil {
logger = logger.WithValues("reason", "CompositionDeleted")
return logger, &pod, true
}
isCurrent := podDerivedFrom(comp, &pod)
if !isCurrent {
logger = logger.WithValues("reason", "Superseded")
return logger, &pod, true
}
// Synthesis is done
if comp.Status.CurrentSynthesis != nil && comp.Status.CurrentSynthesis.Synthesized != nil {
if comp.Status.CurrentSynthesis.PodCreation != nil {
logger = logger.WithValues("latency", time.Since(comp.Status.CurrentSynthesis.PodCreation.Time).Milliseconds())
}
logger = logger.WithValues("reason", "Success")
return logger, &pod, true
}
// Pod is too old
// We timeout eventually in case it landed on a node that for whatever reason isn't capable of running the pod
if time.Since(pod.CreationTimestamp.Time) > syn.Spec.PodTimeout.Duration {
logger = logger.WithValues("reason", "Timeout")
synthesPodRecreations.Inc()
return logger, &pod, true
}
// At this point the pod should still be running - no need to check other pods
return logger, nil, true
}
return logger, nil, false
}
func swapStates(comp *apiv1.Composition, syn *apiv1.Synthesizer) {
// Reset the current attempts counter when the composition or synthesizer have changed or synthesis was successful
attempts := 0
if comp.Status.CurrentSynthesis != nil && comp.Status.CurrentSynthesis.Synthesized == nil && comp.Status.CurrentSynthesis.ObservedCompositionGeneration == comp.Generation && comp.Status.CurrentSynthesis.ObservedSynthesizerGeneration == syn.Generation {
attempts = comp.Status.CurrentSynthesis.Attempts
}
// If the previous state has been synthesized but not the current, keep the previous to avoid orphaning deleted resources
if comp.Status.CurrentSynthesis != nil && comp.Status.CurrentSynthesis.Synthesized != nil {
comp.Status.PreviousSynthesis = comp.Status.CurrentSynthesis
}
comp.Status.CurrentSynthesis = &apiv1.Synthesis{
ObservedCompositionGeneration: comp.Generation,
Attempts: attempts,
}
}
func shouldBackOffPodCreation(comp *apiv1.Composition) bool {
current := comp.Status.CurrentSynthesis
return current != nil && current.Attempts > 0 && current.PodCreation != nil
}
func shouldRevertStateSwap(comp *apiv1.Composition) bool {
return comp.Status.PreviousSynthesis != nil && (comp.Status.CurrentSynthesis == nil || comp.Status.CurrentSynthesis.Synthesized == nil)
}
func shouldUpdateDeletedCompositionStatus(comp *apiv1.Composition) bool {
return comp.Status.CurrentSynthesis != nil && (comp.Status.CurrentSynthesis.ObservedCompositionGeneration != comp.Generation || comp.Status.CurrentSynthesis.Synthesized == nil)
}
func isReconciling(comp *apiv1.Composition) bool {
return comp.Status.CurrentSynthesis != nil && (comp.Status.CurrentSynthesis.Reconciled == nil) || comp.Status.CurrentSynthesis.ObservedCompositionGeneration != comp.Generation
}