@@ -25,14 +25,16 @@ package controller
25
25
import (
26
26
"context"
27
27
"fmt"
28
+ "github.com/gardener/machine-controller-manager/pkg/util/annotations"
29
+ "github.com/gardener/machine-controller-manager/pkg/util/provider/machineutils"
30
+ apierrors "k8s.io/apimachinery/pkg/api/errors"
28
31
"reflect"
29
32
"sync"
30
33
"time"
31
34
32
35
"k8s.io/klog/v2"
33
36
34
37
v1 "k8s.io/api/core/v1"
35
- "k8s.io/apimachinery/pkg/api/errors"
36
38
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
37
39
"k8s.io/apimachinery/pkg/labels"
38
40
"k8s.io/apimachinery/pkg/types"
@@ -422,7 +424,7 @@ func (dc *controller) reconcileClusterMachineDeployment(key string) error {
422
424
return err
423
425
}
424
426
deployment , err := dc .controlMachineClient .MachineDeployments (dc .namespace ).Get (ctx , name , metav1.GetOptions {})
425
- if errors .IsNotFound (err ) {
427
+ if apierrors .IsNotFound (err ) {
426
428
klog .V (4 ).Infof ("Deployment %v has been deleted" , key )
427
429
return nil
428
430
}
@@ -512,6 +514,11 @@ func (dc *controller) reconcileClusterMachineDeployment(key string) error {
512
514
return err
513
515
}
514
516
517
+ err = dc .setMachinePriorityAnnotationAndUpdateTriggeredForDeletion (ctx , d )
518
+ if err != nil {
519
+ return err
520
+ }
521
+
515
522
if d .Spec .Paused {
516
523
klog .V (3 ).Infof ("Scaling detected for machineDeployment %s which is paused" , d .Name )
517
524
return dc .sync (ctx , d , machineSets , machineMap )
@@ -604,3 +611,58 @@ func (dc *controller) updateMachineDeploymentFinalizers(ctx context.Context, mac
604
611
dc .updateMachineDeploymentFinalizers (ctx , machineDeployment , finalizers )
605
612
}
606
613
}
614
+
615
+ func (dc * controller ) setMachinePriorityAnnotationAndUpdateTriggeredForDeletion (ctx context.Context , mcd * v1alpha1.MachineDeployment ) error {
616
+ var triggerForDeletionMachineNames , skipTriggerForDeletionMachineNames []string
617
+ triggerForDeletionMachineNames = annotations .GetMachineNamesTriggeredForDeletion (mcd )
618
+ if len (triggerForDeletionMachineNames ) == 0 {
619
+ return nil
620
+ }
621
+ klog .V (3 ).Infof ("MachineDeployment %q has #%d machine(s) marked for deletion, triggerForDeletionMachineNames=%v" , mcd .Name , len (triggerForDeletionMachineNames ), triggerForDeletionMachineNames )
622
+ for _ , machineName := range triggerForDeletionMachineNames {
623
+ mc , err := dc .machineLister .Machines (dc .namespace ).Get (machineName )
624
+ if apierrors .IsNotFound (err ) {
625
+ klog .V (4 ).Infof ("Machine %q is not found in MachineDeployment %q - skip setting MachinePriority=1 annotation" , machineName , mcd .Name )
626
+ skipTriggerForDeletionMachineNames = append (skipTriggerForDeletionMachineNames , machineName )
627
+ continue
628
+ }
629
+ if machineutils .IsMachineFailedOrTerminating (mc ) {
630
+ klog .V (4 ).Infof ("Machine %q of MachineDeployment %q is in Failed/Terminating state - skip setting MachinePriority=1 annotation" , machineName , mcd .Name )
631
+ skipTriggerForDeletionMachineNames = append (skipTriggerForDeletionMachineNames , machineName )
632
+ continue
633
+ }
634
+ if mc .Annotations [machineutils .MachinePriority ] == "1" {
635
+ klog .V (4 ).Infof ("Machine %q of MachineDeployment %q already marked with MachinePriority=1 annotation" , machineName , mcd .Name )
636
+ continue
637
+ }
638
+ mcAdjust := mc .DeepCopy ()
639
+ mcAdjust .Annotations [machineutils .MachinePriority ] = "1"
640
+ _ , err = dc .controlMachineClient .Machines (mcAdjust .Namespace ).Update (ctx , mcAdjust , metav1.UpdateOptions {})
641
+ if err != nil {
642
+ klog .Errorf ("Failed to set MachinePriority=1 annotation on Machine %q of MachineDeployment %q: %v" , machineName , mcd .Name , err )
643
+ return err
644
+ }
645
+ klog .V (3 ).Infof ("Machine %q of MachineDeployment %q marked with MachinePriority=1 annotation successfully" , machineName , mcd .Name )
646
+ }
647
+
648
+ if len (skipTriggerForDeletionMachineNames ) == 0 {
649
+ return nil
650
+ }
651
+
652
+ triggerForDeletionMachineNames = sets .NewString (triggerForDeletionMachineNames ... ).Delete (skipTriggerForDeletionMachineNames ... ).List ()
653
+ triggerDeletionAnnotValue := annotations .CreateMachinesTriggeredForDeletionAnnotValue (triggerForDeletionMachineNames )
654
+
655
+ mcdAdjust := mcd .DeepCopy ()
656
+ if triggerDeletionAnnotValue != "" {
657
+ mcdAdjust .Annotations [machineutils .TriggerDeletionByMCM ] = triggerDeletionAnnotValue
658
+ } else {
659
+ delete (mcdAdjust .Annotations , machineutils .TriggerDeletionByMCM )
660
+ }
661
+ _ , err := dc .controlMachineClient .MachineDeployments (mcd .Namespace ).Update (ctx , mcdAdjust , metav1.UpdateOptions {})
662
+ if err != nil {
663
+ klog .Errorf ("Failed to update MachineDeployment %q with #%d machine names still pending deletion, triggerDeletionAnnotValue=%q" , mcd .Name , len (triggerForDeletionMachineNames ), triggerDeletionAnnotValue )
664
+ return err
665
+ }
666
+ klog .V (3 ).Infof ("Updated MachineDeployment %q with #%d machine names still pending deletion, triggerDeletionAnnotValue=%q" , mcd .Name , len (triggerForDeletionMachineNames ), triggerDeletionAnnotValue )
667
+ return nil
668
+ }
0 commit comments