Skip to content

Commit e7f0f07

Browse files
aaronfernelankathlenkite
authored
Trigger removal of CA tainted machine without waiting for replica change (#972) (#976)
* for-deletion machines dont want for replica change * fix timing issue in additional unit test * added unit test for managereplicas * fixed ineffectual assignment&unused * added additional check in test * fixed test assertion * addressed review comments * accepted review comment --------- Co-authored-by: Tarun Ramakrishna Elankath <[email protected]> Co-authored-by: elankath <[email protected]>
1 parent 6446778 commit e7f0f07

File tree

7 files changed

+138
-124
lines changed

7 files changed

+138
-124
lines changed

pkg/controller/controller_utils.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -786,22 +786,6 @@ func (s ActiveMachines) Less(i, j int) bool {
786786
return false
787787
}
788788

789-
// IsMachineActive checks if machine was active
790-
func IsMachineActive(p *v1alpha1.Machine) bool {
791-
if p.Status.CurrentStatus.Phase == v1alpha1.MachineFailed {
792-
return false
793-
} else if p.Status.CurrentStatus.Phase == v1alpha1.MachineTerminating {
794-
return false
795-
}
796-
797-
return true
798-
}
799-
800-
// IsMachineFailed checks if machine has failed
801-
func IsMachineFailed(p *v1alpha1.Machine) bool {
802-
return p.Status.CurrentStatus.Phase == v1alpha1.MachineFailed
803-
}
804-
805789
// MachineKey is the function used to get the machine name from machine object
806790
// ToCheck : as machine-namespace does not matter
807791
func MachineKey(machine *v1alpha1.Machine) string {

pkg/controller/deployment_test.go

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ package controller
77
import (
88
"context"
99
"errors"
10+
"fmt"
11+
"github.com/gardener/machine-controller-manager/pkg/util/annotations"
12+
"github.com/gardener/machine-controller-manager/pkg/util/provider/machineutils"
1013
"time"
1114

1215
machinev1 "github.com/gardener/machine-controller-manager/pkg/apis/machine/v1alpha1"
@@ -1400,8 +1403,9 @@ var _ = Describe("machineDeployment", func() {
14001403
Labels: map[string]string{
14011404
"test-label": "test-label",
14021405
},
1403-
UID: "1234567",
1404-
Generation: 5,
1406+
Annotations: make(map[string]string),
1407+
UID: "1234567",
1408+
Generation: 5,
14051409
},
14061410
TypeMeta: metav1.TypeMeta{
14071411
Kind: "MachineDeployment",
@@ -1506,7 +1510,8 @@ var _ = Describe("machineDeployment", func() {
15061510
"test-label": "test-label",
15071511
machinev1.NodeLabelKey: "Node1-test",
15081512
},
1509-
UID: "1234567",
1513+
Annotations: map[string]string{},
1514+
UID: "1234567",
15101515
OwnerReferences: []metav1.OwnerReference{
15111516
{
15121517
Kind: "MachineSet",
@@ -1552,7 +1557,7 @@ var _ = Describe("machineDeployment", func() {
15521557

15531558
DescribeTable("This should",
15541559
func(preset func(testMachineDeployment *machinev1.MachineDeployment, testMachineSet *machinev1.MachineSet),
1555-
postCheck func(testMachineDeployment *machinev1.MachineDeployment, testMachineSet []machinev1.MachineSet, testNode *corev1.Node) error) {
1560+
postCheck func(testMachineDeployment *machinev1.MachineDeployment, testMachineSet []machinev1.MachineSet, testMachines []machinev1.Machine, testNode *corev1.Node) error) {
15561561

15571562
stop := make(chan struct{})
15581563
preset(testMachineDeployment, testMachineSet)
@@ -1575,13 +1580,15 @@ var _ = Describe("machineDeployment", func() {
15751580
waitForCacheSync(stop, c)
15761581
actualMachineDeployment, _ := c.controlMachineClient.MachineDeployments(testNamespace).Get(context.Background(), testMachineDeployment.Name, metav1.GetOptions{})
15771582
actualMachineSets, _ := c.controlMachineClient.MachineSets(testNamespace).List(context.Background(), metav1.ListOptions{})
1583+
waitForCacheSync(stop, c)
1584+
actualMachines, _ := c.controlMachineClient.Machines(testNamespace).List(context.Background(), metav1.ListOptions{})
15781585
testNode, _ := c.targetCoreClient.CoreV1().Nodes().Get(context.Background(), testNode.Name, metav1.GetOptions{})
15791586

1580-
Expect(postCheck(actualMachineDeployment, actualMachineSets.Items, testNode)).To(BeNil())
1587+
Expect(postCheck(actualMachineDeployment, actualMachineSets.Items, actualMachines.Items, testNode)).To(BeNil())
15811588
},
15821589
Entry("reconcile the machineDeployment and return nil",
15831590
func(_ *machinev1.MachineDeployment, _ *machinev1.MachineSet) {},
1584-
func(_ *machinev1.MachineDeployment, _ []machinev1.MachineSet, _ *corev1.Node) error {
1591+
func(_ *machinev1.MachineDeployment, _ []machinev1.MachineSet, _ []machinev1.Machine, _ *corev1.Node) error {
15851592
return nil
15861593
},
15871594
),
@@ -1592,7 +1599,7 @@ var _ = Describe("machineDeployment", func() {
15921599
testMachineSet.Spec = machinev1.MachineSetSpec{}
15931600
testMachineSet.Status = machinev1.MachineSetStatus{}
15941601
},
1595-
func(_ *machinev1.MachineDeployment, testMachineSets []machinev1.MachineSet, _ *corev1.Node) error {
1602+
func(_ *machinev1.MachineDeployment, testMachineSets []machinev1.MachineSet, _ []machinev1.Machine, _ *corev1.Node) error {
15961603
if len(testMachineSets) != 1 {
15971604
return errors.New("it should have created one machine set")
15981605
}
@@ -1603,18 +1610,19 @@ var _ = Describe("machineDeployment", func() {
16031610
func(testMachineDeployment *machinev1.MachineDeployment, _ *machinev1.MachineSet) {
16041611
testMachineDeployment.Spec.Selector = &metav1.LabelSelector{}
16051612
},
1606-
func(_ *machinev1.MachineDeployment, testMachineSets []machinev1.MachineSet, _ *corev1.Node) error {
1613+
func(_ *machinev1.MachineDeployment, testMachineSets []machinev1.MachineSet, _ []machinev1.Machine, _ *corev1.Node) error {
16071614
if len(testMachineSets) > 1 {
16081615
return errors.New("it should not have created one machine set")
16091616
}
1617+
16101618
return nil
16111619
},
16121620
),
16131621
Entry("should remove the finalizer from deployment when deleted and no machineSets are available.",
16141622
func(testMachineDeployment *machinev1.MachineDeployment, _ *machinev1.MachineSet) {
16151623
testMachineDeployment.DeletionTimestamp = &metav1.Time{Time: time.Now()}
16161624
},
1617-
func(testMachineDeployment *machinev1.MachineDeployment, _ []machinev1.MachineSet, _ *corev1.Node) error {
1625+
func(testMachineDeployment *machinev1.MachineDeployment, _ []machinev1.MachineSet, _ []machinev1.Machine, _ *corev1.Node) error {
16181626
if len(testMachineDeployment.Finalizers) > 0 {
16191627
return errors.New("it should have removed the finalizers")
16201628
}
@@ -1631,7 +1639,7 @@ var _ = Describe("machineDeployment", func() {
16311639
testMachineSet.Spec = machinev1.MachineSetSpec{}
16321640
testMachineSet.Status = machinev1.MachineSetStatus{}
16331641
},
1634-
func(_ *machinev1.MachineDeployment, testMachineSets []machinev1.MachineSet, _ *corev1.Node) error {
1642+
func(_ *machinev1.MachineDeployment, testMachineSets []machinev1.MachineSet, _ []machinev1.Machine, _ *corev1.Node) error {
16351643
if len(testMachineSets) != 0 {
16361644
return errors.New("it shouldn't have created machine set")
16371645
}
@@ -1648,7 +1656,7 @@ var _ = Describe("machineDeployment", func() {
16481656
testMachineSet.Spec = machinev1.MachineSetSpec{}
16491657
testMachineSet.Status = machinev1.MachineSetStatus{}
16501658
},
1651-
func(_ *machinev1.MachineDeployment, testMachineSets []machinev1.MachineSet, _ *corev1.Node) error {
1659+
func(_ *machinev1.MachineDeployment, testMachineSets []machinev1.MachineSet, _ []machinev1.Machine, _ *corev1.Node) error {
16521660
if len(testMachineSets) < 1 {
16531661
return errors.New("it should have created one machine set")
16541662
}
@@ -1662,7 +1670,7 @@ var _ = Describe("machineDeployment", func() {
16621670
}
16631671
testMachineDeployment.Spec.Template.Spec.Class.Name = "MachineClass-test-new"
16641672
},
1665-
func(_ *machinev1.MachineDeployment, testMachineSets []machinev1.MachineSet, _ *corev1.Node) error {
1673+
func(_ *machinev1.MachineDeployment, testMachineSets []machinev1.MachineSet, _ []machinev1.Machine, _ *corev1.Node) error {
16661674
// Old machine set should exist and should be scaled-down to zero.
16671675
if len(testMachineSets) == 0 || testMachineSets[0].Spec.Replicas != 0 {
16681676
return errors.New("it should have scaled-down old machineSet to zero")
@@ -1691,7 +1699,7 @@ var _ = Describe("machineDeployment", func() {
16911699
}
16921700
testMachine = &machinev1.Machine{}
16931701
},
1694-
func(testMachineDeployment *machinev1.MachineDeployment, testMachineSets []machinev1.MachineSet, _ *corev1.Node) error {
1702+
func(testMachineDeployment *machinev1.MachineDeployment, testMachineSets []machinev1.MachineSet, _ []machinev1.Machine, _ *corev1.Node) error {
16951703
if len(testMachineSets) != 2 || testMachineSets[0].Spec.Replicas != testMachineDeployment.Spec.Replicas {
16961704
return errors.New("it should have fully scaled-up the new machineSet")
16971705
}
@@ -1704,7 +1712,7 @@ var _ = Describe("machineDeployment", func() {
17041712
// This should trigger rolling-update.
17051713
testMachineDeployment.Spec.Template.Spec.Class.Name = "MachineClass-test-new"
17061714
},
1707-
func(_ *machinev1.MachineDeployment, testMachineSets []machinev1.MachineSet, _ *corev1.Node) error {
1715+
func(_ *machinev1.MachineDeployment, testMachineSets []machinev1.MachineSet, _ []machinev1.Machine, _ *corev1.Node) error {
17081716
if len(testMachineSets) != 2 {
17091717
return errors.New("it should have created a new machine set")
17101718
}
@@ -1716,7 +1724,7 @@ var _ = Describe("machineDeployment", func() {
17161724
// This should trigger rolling-update.
17171725
testMachineDeployment.Spec.Template.Spec.Class.Name = "MachineClass-test-new"
17181726
},
1719-
func(testMachineDeployment *machinev1.MachineDeployment, testMachineSets []machinev1.MachineSet, _ *corev1.Node) error {
1727+
func(testMachineDeployment *machinev1.MachineDeployment, testMachineSets []machinev1.MachineSet, _ []machinev1.Machine, _ *corev1.Node) error {
17201728
/*TODO: when we create a new RS then we assign it the replica count and
17211729
then we again see if the replica count can be increased in the same reconciliation loop
17221730
So in our case we first assign it replica count of 1(note a new machine is not created)
@@ -1748,7 +1756,7 @@ var _ = Describe("machineDeployment", func() {
17481756
}
17491757

17501758
},
1751-
func(testMachineDeployment *machinev1.MachineDeployment, _ []machinev1.MachineSet, _ *corev1.Node) error {
1759+
func(testMachineDeployment *machinev1.MachineDeployment, _ []machinev1.MachineSet, _ []machinev1.Machine, _ *corev1.Node) error {
17521760
// RollbackTo should be removed after rollback.
17531761
if testMachineDeployment.Spec.RollbackTo != nil {
17541762
return errors.New("RollbackTo field should have been removed from machine-deployment")
@@ -1768,7 +1776,7 @@ var _ = Describe("machineDeployment", func() {
17681776
testMachineDeployment.Spec.Template.Spec.Class.Name = "MachineClass-test-new"
17691777
testMachineDeployment.Spec.Paused = true
17701778
},
1771-
func(_ *machinev1.MachineDeployment, testMachineSets []machinev1.MachineSet, _ *corev1.Node) error {
1779+
func(_ *machinev1.MachineDeployment, testMachineSets []machinev1.MachineSet, _ []machinev1.Machine, _ *corev1.Node) error {
17721780

17731781
if len(testMachineSets) != 1 {
17741782
return errors.New("there should be only one old machine-set")
@@ -1781,6 +1789,17 @@ var _ = Describe("machineDeployment", func() {
17811789
return nil
17821790
},
17831791
),
1792+
Entry("should set MachinePriority=1 for the machines named in TriggerDeletionByMCM annotation in the MachineDeployment",
1793+
func(testMachineDeployment *machinev1.MachineDeployment, _ *machinev1.MachineSet) {
1794+
testMachineDeployment.Annotations[machineutils.TriggerDeletionByMCM] = annotations.CreateMachinesTriggeredForDeletionAnnotValue([]string{testMachine.Name})
1795+
},
1796+
func(_ *machinev1.MachineDeployment, _ []machinev1.MachineSet, machines []machinev1.Machine, _ *corev1.Node) error {
1797+
if machines[0].Annotations[machineutils.MachinePriority] != "1" {
1798+
return fmt.Errorf("testMachine should have its %q annotation set to 1", machineutils.MachinePriority)
1799+
}
1800+
return nil
1801+
},
1802+
),
17841803
)
17851804
})
17861805

pkg/controller/deployment_util.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ package controller
2525
import (
2626
"context"
2727
"fmt"
28+
"github.com/gardener/machine-controller-manager/pkg/util/provider/machineutils"
2829
"reflect"
2930
"sort"
3031
"strconv"
@@ -921,7 +922,7 @@ func WaitForMachinesHashPopulated(c v1alpha1listers.MachineSetLister, desiredGen
921922
func LabelMachinesWithHash(ctx context.Context, machineList *v1alpha1.MachineList, c v1alpha1client.MachineV1alpha1Interface, machineLister v1alpha1listers.MachineLister, namespace, name, hash string) error {
922923
for _, machine := range machineList.Items {
923924
// Ignore inactive Machines.
924-
if !IsMachineActive(&machine) {
925+
if !machineutils.IsMachineActive(&machine) {
925926
continue
926927
}
927928
// Only label the machine that doesn't already have the new hash

pkg/controller/machineset.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"context"
2727
"errors"
2828
"fmt"
29+
"github.com/gardener/machine-controller-manager/pkg/util/provider/machineutils"
2930
"reflect"
3031
"sort"
3132
"sync"
@@ -337,11 +338,11 @@ func (c *controller) manageReplicas(ctx context.Context, allMachines []*v1alpha1
337338
}
338339

339340
var activeMachines, staleMachines []*v1alpha1.Machine
340-
for _, machine := range allMachines {
341-
if IsMachineActive(machine) {
342-
activeMachines = append(activeMachines, machine)
343-
} else if IsMachineFailed(machine) {
344-
staleMachines = append(staleMachines, machine)
341+
for _, m := range allMachines {
342+
if machineutils.IsMachineFailed(m) || machineutils.IsMachineTriggeredForDeletion(m) {
343+
staleMachines = append(staleMachines, m)
344+
} else if machineutils.IsMachineActive(m) {
345+
activeMachines = append(activeMachines, m)
345346
}
346347
}
347348

@@ -685,7 +686,7 @@ func (c *controller) prepareMachineForDeletion(ctx context.Context, targetMachin
685686
} else {
686687
// successful delete of a Failed phase machine due to unhealthiness for too long, increments staleMachinesRemoved counter
687688
// note: call is blocking and thread safe as other worker threads might be updating the counter as well
688-
if IsMachineFailed(targetMachine) && targetMachine.Status.LastOperation.Type == v1alpha1.MachineOperationHealthCheck {
689+
if machineutils.IsMachineFailed(targetMachine) && targetMachine.Status.LastOperation.Type == v1alpha1.MachineOperationHealthCheck {
689690
staleMachinesRemoved.increment()
690691
}
691692
}

0 commit comments

Comments
 (0)