-
Notifications
You must be signed in to change notification settings - Fork 9
/
controller.go
453 lines (393 loc) · 13.6 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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
package main
import (
"context"
"crypto/sha1"
"encoding/hex"
"time"
log "github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
pv1 "k8s.io/api/policy/v1"
"k8s.io/apimachinery/pkg/api/equality"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/util/retry"
)
const (
heritageLabel = "heritage"
pdbController = "pdb-controller"
nonReadyTTLAnnotationName = "pdb-controller.zalando.org/non-ready-ttl"
nonReadySinceAnnotationName = "pdb-controller.zalando.org/non-ready-since"
parentResourceHashLabel = "parent-resource-hash"
)
var (
ownerLabels = map[string]string{heritageLabel: pdbController}
)
// PDBController creates PodDisruptionBudgets for deployments and StatefulSets
// if missing.
type PDBController struct {
kubernetes.Interface
interval time.Duration
pdbNameSuffix string
nonReadyTTL time.Duration
parentResourceHash bool
maxUnavailable intstr.IntOrString
}
// NewPDBController initializes a new PDBController.
func NewPDBController(interval time.Duration, client kubernetes.Interface, pdbNameSuffix string, nonReadyTTL time.Duration, parentResourceHash bool, maxUnavailable intstr.IntOrString) *PDBController {
return &PDBController{
Interface: client,
interval: interval,
pdbNameSuffix: pdbNameSuffix,
nonReadyTTL: nonReadyTTL,
parentResourceHash: parentResourceHash,
maxUnavailable: maxUnavailable,
}
}
// Run runs the controller loop until it receives a stop signal over the stop
// channel.
func (n *PDBController) Run(ctx context.Context) {
for {
log.Debug("Running main control loop.")
err := n.runOnce(ctx)
if err != nil {
log.Error(err)
}
select {
case <-time.After(n.interval):
case <-ctx.Done():
log.Info("Terminating main controller loop.")
return
}
}
}
// runOnce runs the main reconcilation loop of the controller.
func (n *PDBController) runOnce(ctx context.Context) error {
allPDBs, err := n.PolicyV1().PodDisruptionBudgets(v1.NamespaceAll).List(ctx, metav1.ListOptions{})
if err != nil {
return err
}
managedPDBs, unmanagedPDBs := filterPDBs(allPDBs.Items)
deployments, err := n.AppsV1().Deployments(v1.NamespaceAll).List(ctx, metav1.ListOptions{})
if err != nil {
return err
}
statefulSets, err := n.AppsV1().StatefulSets(v1.NamespaceAll).List(ctx, metav1.ListOptions{})
if err != nil {
return err
}
resources := make([]kubeResource, 0, len(deployments.Items)+len(statefulSets.Items))
for _, d := range deployments.Items {
// manually set Kind and APIVersion because of a bug in
// client-go
// https://github.com/kubernetes/client-go/issues/308
d.Kind = "Deployment"
d.APIVersion = "apps/v1"
resources = append(resources, deployment{d})
}
for _, s := range statefulSets.Items {
// manually set Kind and APIVersion because of a bug in
// client-go
// https://github.com/kubernetes/client-go/issues/308
s.Kind = "StatefulSet"
s.APIVersion = "apps/v1"
resources = append(resources, statefulSet{s})
}
desiredPDBs := n.generateDesiredPDBs(resources, managedPDBs, unmanagedPDBs)
n.reconcilePDBs(ctx, desiredPDBs, managedPDBs)
return nil
}
func (n *PDBController) generateDesiredPDBs(resources []kubeResource, managedPDBs, unmanagedPDBs map[string]pv1.PodDisruptionBudget) map[string]pv1.PodDisruptionBudget {
desiredPDBs := make(map[string]pv1.PodDisruptionBudget, len(managedPDBs))
nonReadyTTL := time.Time{}
if n.nonReadyTTL > 0 {
nonReadyTTL = time.Now().UTC().Add(-n.nonReadyTTL)
}
for _, resource := range resources {
matchedPDBs := getMatchedPDBs(resource.TemplateLabels(), unmanagedPDBs)
// don't create managed PDB if there is already unmanaged and
// matched PDBs
if len(matchedPDBs) > 0 {
continue
}
// don't create PDB if the resource has 1 or less replicas
if resource.Replicas() <= 1 {
continue
}
// ensure PDB if the resource has more than one replica and all
// of them are ready
if resource.StatusReadyReplicas() >= resource.Replicas() {
pdb := n.generatePDB(resource, time.Time{})
desiredPDBs[pdb.Namespace+"/"+pdb.Name] = pdb
continue
}
ownedPDBs := getOwnedPDBs(managedPDBs, resource)
validPDBs := make([]pv1.PodDisruptionBudget, 0, len(ownedPDBs))
// only consider valid PDBs. If they're invalid they'll be
// recreated on the next iteration
for _, pdb := range ownedPDBs {
if pdbSpecValid(pdb) {
validPDBs = append(validPDBs, pdb)
}
}
if len(validPDBs) > 0 {
// it's unlikely that we will have more than a single
// valid owned PDB. If we do simply pick the first one
// and check if it's still valid. Any other PDBs will
// automatically get dropped.
pdb := validPDBs[0]
if pdb.Annotations == nil {
pdb.Annotations = make(map[string]string)
}
ttl, err := overrideNonReadyTTL(resource.Annotations(), nonReadyTTL)
if err != nil {
log.Errorf("Failed to override PDB Delete TTL: %s", err)
}
var nonReadySince time.Time
if nonReadySinceStr, ok := pdb.Annotations[nonReadySinceAnnotationName]; ok {
nonReadySince, err = time.Parse(time.RFC3339, nonReadySinceStr)
if err != nil {
log.Errorf("Failed to parse non-ready-since annotation '%s': %v", nonReadySinceStr, err)
}
}
if !nonReadySince.IsZero() {
if !ttl.IsZero() && nonReadySince.Before(ttl) {
continue
}
} else {
nonReadySince = time.Now().UTC()
}
generatedPDB := n.generatePDB(resource, nonReadySince)
desiredPDBs[generatedPDB.Namespace+"/"+generatedPDB.Name] = generatedPDB
}
}
return desiredPDBs
}
// mergeActualAndDesiredPDB takes the current definition of a PDB as it is in cluster and a PDB
// with our desired configurations and does a merge between them. We also return a boolean to tell the
// caller if any change actually had to be made to achieve our desired state or not
func mergeActualAndDesiredPDB(managedPDB, desiredPDB pv1.PodDisruptionBudget) (pv1.PodDisruptionBudget, bool) {
needsUpdate := false
// check if PDBs are equal an only update if not
if !equality.Semantic.DeepEqual(managedPDB.Spec, desiredPDB.Spec) ||
!equality.Semantic.DeepEqual(managedPDB.Labels, desiredPDB.Labels) ||
!equality.Semantic.DeepEqual(managedPDB.Annotations, desiredPDB.Annotations) {
managedPDB.Annotations = desiredPDB.Annotations
managedPDB.Labels = desiredPDB.Labels
managedPDB.Spec = desiredPDB.Spec
needsUpdate = true
}
return managedPDB, needsUpdate
}
func (n *PDBController) reconcilePDBs(ctx context.Context, desiredPDBs, managedPDBs map[string]pv1.PodDisruptionBudget) {
for key, managedPDB := range managedPDBs {
desiredPDB, ok := desiredPDBs[key]
if !ok {
err := n.PolicyV1().PodDisruptionBudgets(managedPDB.Namespace).Delete(ctx, managedPDB.Name, metav1.DeleteOptions{})
if err != nil {
log.Errorf("Failed to delete PDB: %v", err)
continue
}
log.WithFields(log.Fields{
"action": "removed",
"pdb": managedPDB.Name,
"namespace": managedPDB.Namespace,
"selector": managedPDB.Spec.Selector.String(),
}).Info("")
// If we delete a PDB then we don't want to attempt to update it later since this will
// result in a `StorageError` since we can't find the PDB to make an update to it.
continue
}
// check if PDBs are equal an only update if not
updatedPDB, needsUpdate := mergeActualAndDesiredPDB(managedPDB, desiredPDB)
if needsUpdate {
err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
// Technically the updatedPDB and managedPDB namespace should never be different
// but just to be **certain** we're updating the correct namespace we'll just use
// the one that was given to us and not potentially modified
_, err := n.PolicyV1().PodDisruptionBudgets(managedPDB.Namespace).Update(ctx, &updatedPDB, metav1.UpdateOptions{})
// If the update failed that likely means that our definition of what was on the cluster
// has become out of date. To resolve this we'll need to get a more up to date copy of
// the object we're attempting to modify
if err != nil {
currentPDB, err := n.PolicyV1().PodDisruptionBudgets(managedPDB.Namespace).Get(ctx, managedPDB.Name, metav1.GetOptions{})
// This err is locally scoped to this if block and will not cause our `RetryOnConflict`
// to pass if it is nil. If we're in this block then we will get another Retry
if err != nil {
return err
}
updatedPDB, _ = mergeActualAndDesiredPDB(
*currentPDB,
desiredPDB,
)
}
// If this err != nil then the current block will be re-run by `RetryOnConflict`
// on an exponential backoff schedule to see if we can fix the problem by trying again
return err
})
if err != nil {
log.Errorf("Failed to update PDB: %v", err)
continue
}
log.WithFields(log.Fields{
"action": "updated",
"pdb": desiredPDB.Name,
"namespace": desiredPDB.Namespace,
"selector": desiredPDB.Spec.Selector.String(),
}).Info("")
}
}
for key, desiredPDB := range desiredPDBs {
if _, ok := managedPDBs[key]; !ok {
_, err := n.PolicyV1().PodDisruptionBudgets(desiredPDB.Namespace).Create(ctx, &desiredPDB, metav1.CreateOptions{})
if err != nil {
log.Errorf("Failed to create PDB: %v", err)
continue
}
log.WithFields(log.Fields{
"action": "added",
"pdb": desiredPDB.Name,
"namespace": desiredPDB.Namespace,
"selector": desiredPDB.Spec.Selector.String(),
}).Info("")
}
}
}
func overrideNonReadyTTL(annotations map[string]string, nonReadyTTL time.Time) (time.Time, error) {
if ttlVal, ok := annotations[nonReadyTTLAnnotationName]; ok {
duration, err := time.ParseDuration(ttlVal)
if err != nil {
return time.Time{}, err
}
return time.Now().UTC().Add(-duration), nil
}
return nonReadyTTL, nil
}
// pdbSpecValid returns true if the PDB spec is up-to-date
func pdbSpecValid(pdb pv1.PodDisruptionBudget) bool {
return pdb.Spec.MinAvailable == nil
}
// getMatchedPDBs gets matching PodDisruptionBudgets.
func getMatchedPDBs(labels map[string]string, pdbs map[string]pv1.PodDisruptionBudget) []pv1.PodDisruptionBudget {
matchedPDBs := make([]pv1.PodDisruptionBudget, 0)
for _, pdb := range pdbs {
if labelsIntersect(labels, pdb.Spec.Selector.MatchLabels) {
matchedPDBs = append(matchedPDBs, pdb)
}
}
return matchedPDBs
}
func getOwnedPDBs(pdbs map[string]pv1.PodDisruptionBudget, owner kubeResource) []pv1.PodDisruptionBudget {
ownedPDBs := make([]pv1.PodDisruptionBudget, 0, len(pdbs))
for _, pdb := range pdbs {
if isOwnedReference(owner, pdb.ObjectMeta) {
ownedPDBs = append(ownedPDBs, pdb)
}
}
return ownedPDBs
}
// isOwnedReference returns true if the dependent object is owned by the owner
// object.
func isOwnedReference(owner kubeResource, dependent metav1.ObjectMeta) bool {
for _, ref := range dependent.OwnerReferences {
if ref.APIVersion == owner.APIVersion() &&
ref.Kind == owner.Kind() &&
ref.UID == owner.UID() &&
ref.Name == owner.Name() {
return true
}
}
return false
}
// containLabels reports whether expectedLabels are in labels.
func containLabels(labels, expectedLabels map[string]string) bool {
for key, val := range expectedLabels {
if v, ok := labels[key]; !ok || v != val {
return false
}
}
return true
}
// labelsIntersect checks whether two maps a and b intersects. Intersection is
// defined as at least one identical key value pair must exist in both maps and
// there must be no keys which match where the values doesn't match.
func labelsIntersect(a, b map[string]string) bool {
intersect := false
for key, val := range a {
v, ok := b[key]
if ok {
if v == val {
intersect = true
} else { // if the key exists but the values doesn't match, don't consider it an intersection
return false
}
}
}
return intersect
}
func filterPDBs(pdbs []pv1.PodDisruptionBudget) (map[string]pv1.PodDisruptionBudget, map[string]pv1.PodDisruptionBudget) {
managed := make(map[string]pv1.PodDisruptionBudget, len(pdbs))
unmanaged := make(map[string]pv1.PodDisruptionBudget, len(pdbs))
for _, pdb := range pdbs {
if containLabels(pdb.Labels, ownerLabels) {
managed[pdb.Namespace+"/"+pdb.Name] = pdb
continue
}
unmanaged[pdb.Namespace+"/"+pdb.Name] = pdb
}
return managed, unmanaged
}
func (n *PDBController) generatePDB(owner kubeResource, ttl time.Time) pv1.PodDisruptionBudget {
var suffix string
if n.pdbNameSuffix != "" {
suffix = "-" + n.pdbNameSuffix
}
pdb := pv1.PodDisruptionBudget{
ObjectMeta: metav1.ObjectMeta{
Name: owner.Name() + suffix,
Namespace: owner.Namespace(),
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: owner.APIVersion(),
Kind: owner.Kind(),
Name: owner.Name(),
UID: owner.UID(),
},
},
Labels: owner.Labels(),
Annotations: make(map[string]string),
},
Spec: pv1.PodDisruptionBudgetSpec{
MaxUnavailable: &n.maxUnavailable,
Selector: owner.Selector(),
},
}
if n.parentResourceHash {
// if we fail to generate the hash simply fall back to using
// the existing selector
hash, err := resourceHash(owner.Kind(), owner.Name())
if err == nil {
pdb.Spec.Selector = &metav1.LabelSelector{
MatchLabels: map[string]string{
parentResourceHashLabel: hash,
},
}
}
}
if pdb.Labels == nil {
pdb.Labels = make(map[string]string)
}
pdb.Labels[heritageLabel] = pdbController
if !ttl.IsZero() {
pdb.Annotations[nonReadySinceAnnotationName] = ttl.Format(time.RFC3339)
}
return pdb
}
func resourceHash(kind, name string) (string, error) {
h := sha1.New()
_, err := h.Write([]byte(kind + "-" + name))
if err != nil {
return "", err
}
return hex.EncodeToString(h.Sum(nil)), nil
}