forked from gardener/machine-controller-manager
/
deployment.go
619 lines (548 loc) · 22.4 KB
/
deployment.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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
/*
Copyright 2015 The Kubernetes Authors.
Licensed 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.
This file was copied and modified from the kubernetes/kubernetes project
https://github.com/kubernetes/kubernetes/release-1.8/pkg/controller/deployment/deployment_controller.go
Modifications Copyright (c) 2017 SAP SE or an SAP affiliate company. All rights reserved.
*/
// Package controller is used to provide the core functionalities of machine-controller-manager
package controller
import (
"fmt"
"reflect"
"sync"
"time"
"github.com/golang/glog"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/tools/cache"
machine "github.com/gardener/machine-controller-manager/pkg/apis/cluster"
common "github.com/gardener/machine-controller-manager/pkg/apis/cluster/common"
"github.com/gardener/machine-controller-manager/pkg/apis/cluster/v1alpha1"
)
// controllerKind contains the schema.GroupVersionKind for this controller type.
var controllerKind = v1alpha1.SchemeGroupVersion.WithKind("MachineDeployment")
// GroupVersionKind is the version kind used to identify objects managed by machine-controller-manager
var GroupVersionKind = "machine.sapcloud.io/v1alpha1"
func (dc *controller) addMachineDeployment(obj interface{}) {
d := obj.(*v1alpha1.MachineDeployment)
glog.V(4).Infof("Adding machine deployment %s", d.Name)
dc.enqueueMachineDeployment(d)
}
func (dc *controller) updateMachineDeployment(old, cur interface{}) {
oldD := old.(*v1alpha1.MachineDeployment)
curD := cur.(*v1alpha1.MachineDeployment)
glog.V(4).Infof("Updating machine deployment %s", oldD.Name)
dc.enqueueMachineDeployment(curD)
}
func (dc *controller) deleteMachineDeployment(obj interface{}) {
d, ok := obj.(*v1alpha1.MachineDeployment)
if !ok {
tombstone, ok := obj.(cache.DeletedFinalStateUnknown)
if !ok {
//utilruntime.HandleError(fmt.Errorf("Couldn't get object from tombstone %#v", obj))
return
}
d, ok = tombstone.Obj.(*v1alpha1.MachineDeployment)
if !ok {
utilruntime.HandleError(fmt.Errorf("Tombstone contained object that is not a Machine Deployment %#v", obj))
return
}
}
glog.V(4).Infof("Deleting machine deployment %s", d.Name)
dc.enqueueMachineDeployment(d)
}
// addMachineSet enqueues the deployment that manages a MachineSet when the MachineSet is created.
func (dc *controller) addMachineSetToDeployment(obj interface{}) {
is := obj.(*v1alpha1.MachineSet)
if is.DeletionTimestamp != nil {
// On a restart of the controller manager, it's possible for an object to
// show up in a state that is already pending deletion.
dc.deleteMachineSetToDeployment(is)
return
}
// If it has a ControllerRef, that's all that matters.
if controllerRef := metav1.GetControllerOf(is); controllerRef != nil {
d := dc.resolveDeploymentControllerRef(is.Namespace, controllerRef)
if d == nil {
return
}
glog.V(4).Infof("MachineSet %s added.", is.Name)
dc.enqueueMachineDeployment(d)
return
}
// Otherwise, it's an orphan. Get a list of all matching Deployments and sync
// them to see if anyone wants to adopt it.
ds := dc.getMachineDeploymentsForMachineSet(is)
if len(ds) == 0 {
return
}
glog.V(4).Infof("Orphan MachineSet %s added.", is.Name)
for _, d := range ds {
dc.enqueueMachineDeployment(d)
}
}
// getDeploymentsForMachineSet returns a list of Deployments that potentially
// match a MachineSet.
func (dc *controller) getMachineDeploymentsForMachineSet(machineSet *v1alpha1.MachineSet) []*v1alpha1.MachineDeployment {
deployments, err := dc.GetMachineDeploymentsForMachineSet(machineSet)
if err != nil || len(deployments) == 0 {
return nil
}
// Because all MachineSet's belonging to a deployment should have a unique label key,
// there should never be more than one deployment returned by the above method.
// If that happens we should probably dynamically repair the situation by ultimately
// trying to clean up one of the controllers, for now we just return the older one
if len(deployments) > 1 {
// ControllerRef will ensure we don't do anything crazy, but more than one
// item in this list nevertheless constitutes user error.
glog.V(4).Infof("user error! more than one deployment is selecting machine set %s with labels: %#v, returning %s",
machineSet.Name, machineSet.Labels, deployments[0].Name)
}
return deployments
}
// updateMachineSet figures out what deployment(s) manage a MachineSet when the MachineSet
// is updated and wake them up. If the anything of the MachineSets have changed, we need to
// awaken both the old and new deployments. old and cur must be *extensions.MachineSet
// types.
func (dc *controller) updateMachineSetToDeployment(old, cur interface{}) {
curMachineSet := cur.(*v1alpha1.MachineSet)
oldMachineSet := old.(*v1alpha1.MachineSet)
if curMachineSet.ResourceVersion == oldMachineSet.ResourceVersion {
// Periodic resync will send update events for all known machine sets.
// Two different versions of the same machine set will always have different RVs.
return
}
curControllerRef := metav1.GetControllerOf(curMachineSet)
oldControllerRef := metav1.GetControllerOf(oldMachineSet)
controllerRefChanged := !reflect.DeepEqual(curControllerRef, oldControllerRef)
if controllerRefChanged && oldControllerRef != nil {
// The ControllerRef was changed. Sync the old controller, if any.
if d := dc.resolveDeploymentControllerRef(oldMachineSet.Namespace, oldControllerRef); d != nil {
dc.enqueueMachineDeployment(d)
}
}
// If it has a ControllerRef, that's all that matters.
if curControllerRef != nil {
d := dc.resolveDeploymentControllerRef(curMachineSet.Namespace, curControllerRef)
if d == nil {
return
}
glog.V(4).Infof("MachineSet %s updated.", curMachineSet.Name)
dc.enqueueMachineDeployment(d)
return
}
// Otherwise, it's an orphan. If anything changed, sync matching controllers
// to see if anyone wants to adopt it now.
labelChanged := !reflect.DeepEqual(curMachineSet.Labels, oldMachineSet.Labels)
if labelChanged || controllerRefChanged {
ds := dc.getMachineDeploymentsForMachineSet(curMachineSet)
if len(ds) == 0 {
return
}
glog.V(4).Infof("Orphan MachineSet %s updated.", curMachineSet.Name)
for _, d := range ds {
dc.enqueueMachineDeployment(d)
}
}
}
// deleteMachineSet enqueues the deployment that manages a MachineSet when
// the MachineSet is deleted. obj could be an *v1alpha1.MachineSet, or
// a DeletionFinalStateUnknown marker item.
func (dc *controller) deleteMachineSetToDeployment(obj interface{}) {
machineSet, ok := obj.(*v1alpha1.MachineSet)
// When a delete is dropped, the relist will notice a Machine in the store not
// in the list, leading to the insertion of a tombstone object which contains
// the deleted key/value. Note that this value might be stale. If the MachineSet
// changed labels the new deployment will not be woken up till the periodic resync.
if !ok {
tombstone, ok := obj.(cache.DeletedFinalStateUnknown)
if !ok {
utilruntime.HandleError(fmt.Errorf("Couldn't get object from tombstone %#v", obj))
return
}
machineSet, ok = tombstone.Obj.(*v1alpha1.MachineSet)
if !ok {
utilruntime.HandleError(fmt.Errorf("Tombstone contained object that is not a MachineSet %#v", obj))
return
}
}
controllerRef := metav1.GetControllerOf(machineSet)
if controllerRef == nil {
// No controller should care about orphans being deleted.
return
}
d := dc.resolveDeploymentControllerRef(machineSet.Namespace, controllerRef)
if d == nil {
return
}
glog.V(4).Infof("MachineSet %s deleted.", machineSet.Name)
dc.enqueueMachineDeployment(d)
}
// deleteMachine will enqueue a Recreate Deployment once all of its Machines have stopped running.
func (dc *controller) deleteMachineToMachineDeployment(obj interface{}) {
machine, ok := obj.(*v1alpha1.Machine)
// When a delete is dropped, the relist will notice a Machine in the store not
// in the list, leading to the insertion of a tombstone object which contains
// the deleted key/value. Note that this value might be stale. If the Machine
// changed labels the new deployment will not be woken up till the periodic resync.
if !ok {
tombstone, ok := obj.(cache.DeletedFinalStateUnknown)
if !ok {
utilruntime.HandleError(fmt.Errorf("Couldn't get object from tombstone %#v", obj))
return
}
machine, ok = tombstone.Obj.(*v1alpha1.Machine)
if !ok {
utilruntime.HandleError(fmt.Errorf("Tombstone contained object that is not a machine %#v", obj))
return
}
}
glog.V(4).Infof("Machine %s deleted.", machine.Name)
if d := dc.getMachineDeploymentForMachine(machine); d != nil && d.Spec.Strategy.Type == common.RecreateMachineDeploymentStrategyType {
// Sync if this Deployment now has no more Machines.
machineSets, err := ListMachineSets(d, IsListFromClient(dc.controlMachineClient))
if err != nil {
return
}
machineMap, err := dc.getMachineMapForMachineDeployment(d, machineSets)
if err != nil {
return
}
numMachines := 0
for _, machineList := range machineMap {
numMachines += len(machineList.Items)
}
if numMachines == 0 {
dc.enqueueMachineDeployment(d)
}
}
}
func (dc *controller) enqueueMachineDeployment(deployment *v1alpha1.MachineDeployment) {
key, err := KeyFunc(deployment)
if err != nil {
utilruntime.HandleError(fmt.Errorf("Couldn't get key for object %#v: %v", deployment, err))
return
}
dc.machineDeploymentQueue.Add(key)
}
func (dc *controller) enqueueRateLimited(deployment *v1alpha1.MachineDeployment) {
key, err := KeyFunc(deployment)
if err != nil {
utilruntime.HandleError(fmt.Errorf("Couldn't get key for object %#v: %v", deployment, err))
return
}
dc.machineDeploymentQueue.AddRateLimited(key)
}
// enqueueMachineDeploymentAfter will enqueue a deployment after the provided amount of time.
func (dc *controller) enqueueMachineDeploymentAfter(deployment *v1alpha1.MachineDeployment, after time.Duration) {
key, err := KeyFunc(deployment)
if err != nil {
utilruntime.HandleError(fmt.Errorf("Couldn't get key for object %#v: %v", deployment, err))
return
}
dc.machineDeploymentQueue.AddAfter(key, after)
}
// getDeploymentForMachine returns the deployment managing the given Machine.
func (dc *controller) getMachineDeploymentForMachine(machine *v1alpha1.Machine) *v1alpha1.MachineDeployment {
// Find the owning machine set
var is *v1alpha1.MachineSet
var err error
controllerRef := metav1.GetControllerOf(machine)
if controllerRef == nil {
// No controller owns this Machine.
return nil
}
if controllerRef.Kind != "MachineDeployment" { //TODO: Remove hardcoded string
// Not a Machine owned by a machine set.
return nil
}
is, err = dc.controlMachineClient.MachineSets(machine.Namespace).Get(controllerRef.Name, metav1.GetOptions{})
if err != nil || is.UID != controllerRef.UID {
glog.V(4).Infof("Cannot get machineset %q for machine %q: %v", controllerRef.Name, machine.Name, err)
return nil
}
// Now find the Deployment that owns that MachineSet.
controllerRef = metav1.GetControllerOf(is)
if controllerRef == nil {
return nil
}
return dc.resolveDeploymentControllerRef(is.Namespace, controllerRef)
}
// resolveControllerRef returns the controller referenced by a ControllerRef,
// or nil if the ControllerRef could not be resolved to a matching controller
// of the correct Kind.
func (dc *controller) resolveDeploymentControllerRef(namespace string, controllerRef *metav1.OwnerReference) *v1alpha1.MachineDeployment {
// We can't look up by UID, so look up by Name and then verify UID.
// Don't even try to look up by Name if it's the wrong Kind.
if controllerRef.Kind != controllerKind.Kind {
return nil
}
d, err := dc.controlMachineClient.MachineDeployments(namespace).Get(controllerRef.Name, metav1.GetOptions{})
if err != nil {
return nil
}
if d.UID != controllerRef.UID {
// The controller we found with this Name is not the same one that the
// ControllerRef points to.
return nil
}
return d
}
func (dc *controller) handleErr(err error, key interface{}) {
if err == nil {
dc.machineDeploymentQueue.Forget(key)
return
}
if dc.machineDeploymentQueue.NumRequeues(key) < maxRetries {
glog.V(2).Infof("Error syncing deployment %v: %v", key, err)
dc.machineDeploymentQueue.AddRateLimited(key)
return
}
utilruntime.HandleError(err)
glog.V(2).Infof("Dropping deployment %q out of the queue: %v", key, err)
dc.machineDeploymentQueue.Forget(key)
}
// getMachineSetsForDeployment uses ControllerRefManager to reconcile
// ControllerRef by adopting and orphaning.
// It returns the list of MachineSets that this Deployment should manage.
func (dc *controller) getMachineSetsForMachineDeployment(d *v1alpha1.MachineDeployment) ([]*v1alpha1.MachineSet, error) {
// List all MachineSets to find those we own but that no longer match our
// selector. They will be orphaned by ClaimMachineSets().
machineSets, err := dc.machineSetLister.List(labels.Everything())
if err != nil {
return nil, err
}
deploymentSelector, err := metav1.LabelSelectorAsSelector(d.Spec.Selector)
if err != nil {
return nil, fmt.Errorf("machine deployment %s has invalid label selector: %v", d.Name, err)
}
// If any adoptions are attempted, we should first recheck for deletion with
// an uncached quorum read sometime after listing MachineSets (see #42639).
canAdoptFunc := RecheckDeletionTimestamp(func() (metav1.Object, error) {
fresh, err := dc.controlMachineClient.MachineDeployments(d.Namespace).Get(d.Name, metav1.GetOptions{})
if err != nil {
return nil, err
}
if fresh.UID != d.UID {
return nil, fmt.Errorf("original Machine Deployment %v is gone: got uid %v, wanted %v", d.Name, fresh.UID, d.UID)
}
return fresh, nil
})
cm := NewMachineSetControllerRefManager(dc.machineSetControl, d, deploymentSelector, controllerKind, canAdoptFunc)
ISes, err := cm.ClaimMachineSets(machineSets)
return ISes, err
}
// getMachineMapForDeployment returns the Machines managed by a Deployment.
//
// It returns a map from MachineSet UID to a list of Machines controlled by that RS,
// according to the Machine's ControllerRef.
func (dc *controller) getMachineMapForMachineDeployment(d *v1alpha1.MachineDeployment, machineSets []*v1alpha1.MachineSet) (map[types.UID]*v1alpha1.MachineList, error) {
// Get all Machines that potentially belong to this Deployment.
selector, err := metav1.LabelSelectorAsSelector(d.Spec.Selector)
if err != nil {
return nil, err
}
machines, err := dc.machineLister.List(selector)
if err != nil {
return nil, err
}
// Group Machines by their controller (if it's in rsList).
machineMap := make(map[types.UID]*v1alpha1.MachineList, len(machineSets))
for _, is := range machineSets {
machineMap[is.UID] = &v1alpha1.MachineList{}
}
for _, machine := range machines {
// Do not ignore inactive Machines because Recreate Deployments need to verify that no
// Machines from older versions are running before spinning up new Machines.
controllerRef := metav1.GetControllerOf(machine)
if controllerRef == nil {
continue
}
// Only append if we care about this UID.
if machineList, ok := machineMap[controllerRef.UID]; ok {
machineList.Items = append(machineList.Items, *machine)
}
}
return machineMap, nil
}
// reconcileClusterMachineDeployment will sync the deployment with the given key.
// This function is not meant to be invoked concurrently with the same key.
func (dc *controller) reconcileClusterMachineDeployment(key string) error {
startTime := time.Now()
glog.V(3).Infof("Started syncing deployment %q (%v)", key, startTime)
defer func() {
glog.V(3).Infof("Finished syncing deployment %q (%v)", key, time.Since(startTime))
}()
_, name, err := cache.SplitMetaNamespaceKey(key)
if err != nil {
return err
}
deployment, err := dc.controlMachineClient.MachineDeployments(dc.namespace).Get(name, metav1.GetOptions{})
if errors.IsNotFound(err) {
glog.V(4).Infof("Deployment %v has been deleted", key)
return nil
}
if err != nil {
return err
}
// If MachineDeployment is frozen and no deletion timestamp, don't process it
if deployment.Labels["freeze"] == "True" && deployment.DeletionTimestamp == nil {
glog.V(3).Infof("MachineDeployment %q is frozen. However, it will still be processed if it there is an scale down event.", deployment.Name)
}
// Validate MachineDeployment
internalMachineDeployment := &machine.MachineDeployment{}
err = v1alpha1.Convert_v1alpha1_MachineDeployment_To_cluster_MachineDeployment(deployment, internalMachineDeployment, nil)
if err != nil {
return err
}
// validationerr := validation.ValidateMachineDeployment(internalMachineDeployment)
// if validationerr.ToAggregate() != nil && len(validationerr.ToAggregate().Errors()) > 0 {
// glog.V(2).Infof("Validation of MachineDeployment failled %s", validationerr.ToAggregate().Error())
// return nil
// }
// TODO: Enable Validation again.
// // Validate MachineClass
// _, secretRef, err := dc.validateMachineClass(&deployment.Spec.Template.Spec.Class)
// if err != nil || secretRef == nil {
// return err
// }
// Resync the MachineDeployment after 10 minutes to avoid missing out on missed out events
defer dc.enqueueMachineDeploymentAfter(deployment, 10*time.Minute)
// Deep-copy otherwise we are mutating our cache.
// TODO: Deep-copy only when needed.
d := deployment.DeepCopy()
// Manipulate finalizers
if d.DeletionTimestamp == nil {
dc.addMachineDeploymentFinalizers(d)
}
everything := metav1.LabelSelector{}
if reflect.DeepEqual(d.Spec.Selector, &everything) {
dc.recorder.Eventf(d, v1.EventTypeWarning, "SelectingAll", "This deployment is selecting all machines. A non-empty selector is required.")
if d.Status.ObservedGeneration < d.Generation {
d.Status.ObservedGeneration = d.Generation
dc.controlMachineClient.MachineDeployments(d.Namespace).Update(d)
}
return nil
}
// List MachineSets owned by this Deployment, while reconciling ControllerRef
// through adoption/orphaning.
machineSets, err := dc.getMachineSetsForMachineDeployment(d)
if err != nil {
return err
}
// List all Machines owned by this Deployment, grouped by their MachineSet.
// Current uses of the MachineMap are:
//
// * check if a Machine is labeled correctly with the Machine-template-hash label.
// * check that no old Machines are running in the middle of Recreate Deployments.
machineMap, err := dc.getMachineMapForMachineDeployment(d, machineSets)
if err != nil {
return err
}
if d.DeletionTimestamp != nil {
if finalizers := sets.NewString(d.Finalizers...); !finalizers.Has(DeleteFinalizerName) {
return nil
}
if len(machineSets) == 0 {
dc.deleteMachineDeploymentFinalizers(d)
return nil
}
glog.V(4).Infof("Deleting all child MachineSets as MachineDeployment %s has set deletionTimestamp", d.Name)
dc.terminateMachineSets(machineSets, d)
return dc.syncStatusOnly(d, machineSets, machineMap)
}
// Update deployment conditions with an Unknown condition when pausing/resuming
// a deployment. In this way, we can be sure that we won't timeout when a user
// resumes a Deployment with a set progressDeadlineSeconds.
if err = dc.checkPausedConditions(d); err != nil {
return err
}
if d.Spec.Paused {
return dc.sync(d, machineSets, machineMap)
}
// rollback is not re-entrant in case the underlying machine sets are updated with a new
// revision so we should ensure that we won't proceed to update machine sets until we
// make sure that the deployment has cleaned up its rollback spec in subsequent enqueues.
if d.Spec.RollbackTo != nil {
return dc.rollback(d, machineSets, machineMap)
}
scalingEvent, err := dc.isScalingEvent(d, machineSets, machineMap)
if err != nil {
return err
}
if scalingEvent {
return dc.sync(d, machineSets, machineMap)
}
switch d.Spec.Strategy.Type {
case common.RecreateMachineDeploymentStrategyType:
return dc.rolloutRecreate(d, machineSets, machineMap)
case common.RollingUpdateMachineDeploymentStrategyType:
return dc.rolloutRolling(d, machineSets, machineMap)
}
return fmt.Errorf("unexpected deployment strategy type: %s", d.Spec.Strategy.Type)
}
func (dc *controller) terminateMachineSets(machineSets []*v1alpha1.MachineSet, deployment *v1alpha1.MachineDeployment) {
var (
wg sync.WaitGroup
numOfMachinesets = len(machineSets)
)
wg.Add(numOfMachinesets)
for _, machineSet := range machineSets {
go func(machineSet *v1alpha1.MachineSet) {
defer wg.Done()
// Machine is already marked as 'to-be-deleted'
if machineSet.DeletionTimestamp != nil {
return
}
dc.controlMachineClient.MachineSets(machineSet.Namespace).Delete(machineSet.Name, nil)
}(machineSet)
}
wg.Wait()
}
/*
SECTION
Manipulate Finalizers
*/
func (dc *controller) addMachineDeploymentFinalizers(machineDeployment *v1alpha1.MachineDeployment) {
clone := machineDeployment.DeepCopy()
if finalizers := sets.NewString(clone.Finalizers...); !finalizers.Has(DeleteFinalizerName) {
finalizers.Insert(DeleteFinalizerName)
dc.updateMachineDeploymentFinalizers(clone, finalizers.List())
}
}
func (dc *controller) deleteMachineDeploymentFinalizers(machineDeployment *v1alpha1.MachineDeployment) {
clone := machineDeployment.DeepCopy()
if finalizers := sets.NewString(clone.Finalizers...); finalizers.Has(DeleteFinalizerName) {
finalizers.Delete(DeleteFinalizerName)
dc.updateMachineDeploymentFinalizers(clone, finalizers.List())
}
}
func (dc *controller) updateMachineDeploymentFinalizers(machineDeployment *v1alpha1.MachineDeployment, finalizers []string) {
// Get the latest version of the machineDeployment so that we can avoid conflicts
machineDeployment, err := dc.controlMachineClient.MachineDeployments(machineDeployment.Namespace).Get(machineDeployment.Name, metav1.GetOptions{})
if err != nil {
return
}
clone := machineDeployment.DeepCopy()
clone.Finalizers = finalizers
_, err = dc.controlMachineClient.MachineDeployments(machineDeployment.Namespace).Update(clone)
if err != nil {
// Keep retrying until update goes through
glog.Warning("Updated failed, retrying")
dc.updateMachineDeploymentFinalizers(machineDeployment, finalizers)
}
}