Skip to content

Commit 82fd896

Browse files
elankathRishabh Patel
authored and
Rishabh Patel
committed
Remove NotManagedByMCM when not applicable (#866)
* Remove NotManagedByMCM when not applicable * enhanced unit test in case #AnnotateNodesUnmanagedByMCM * log both node and machine name when removing NotManagedByMCM annotation * review comment: leverage updateNodeWithAnnotations for removal * fixed import reordering to original in cli options
1 parent bd75d6e commit 82fd896

File tree

5 files changed

+101
-9
lines changed

5 files changed

+101
-9
lines changed

pkg/util/provider/app/options/options.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func NewMCServer() *MCServer {
7070
MaxEvictRetries: drain.DefaultMaxEvictRetries,
7171
PvDetachTimeout: metav1.Duration{Duration: 2 * time.Minute},
7272
PvReattachTimeout: metav1.Duration{Duration: 90 * time.Second},
73-
MachineSafetyOrphanVMsPeriod: metav1.Duration{Duration: 30 * time.Minute},
73+
MachineSafetyOrphanVMsPeriod: metav1.Duration{Duration: 15 * time.Minute},
7474
MachineSafetyAPIServerStatusCheckPeriod: metav1.Duration{Duration: 1 * time.Minute},
7575
MachineSafetyAPIServerStatusCheckTimeout: metav1.Duration{Duration: 30 * time.Second},
7676
},

pkg/util/provider/machinecontroller/machine_safety.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ func (c *controller) AnnotateNodesUnmanagedByMCM(ctx context.Context) (machineut
170170
return machineutils.LongRetry, err
171171
}
172172
for _, node := range nodes {
173-
_, err := c.getMachineFromNode(node.Name)
173+
machine, err := c.getMachineFromNode(node.Name)
174174
if err != nil {
175175
if err == errMultipleMachineMatch {
176176
klog.Errorf("Couldn't fetch machine, Error: %s", err)
@@ -193,11 +193,23 @@ func (c *controller) AnnotateNodesUnmanagedByMCM(ctx context.Context) (machineut
193193
machineutils.NotManagedByMCM: "1",
194194
}
195195

196+
klog.V(3).Infof("Adding NotManagedByMCM annotation to Node %q", node.Name)
196197
// err is returned only when node update fails
197-
if err := c.updateNodeWithAnnotation(ctx, nodeCopy, annotations); err != nil {
198+
if err := c.updateNodeWithAnnotations(ctx, nodeCopy, annotations); err != nil {
198199
return machineutils.MediumRetry, err
199200
}
200201
}
202+
} else {
203+
_, hasAnnot := node.Annotations[machineutils.NotManagedByMCM]
204+
if !hasAnnot {
205+
continue
206+
}
207+
klog.V(3).Infof("Removing NotManagedByMCM annotation from Node %q associated with Machine %q", node.Name, machine.Name)
208+
nodeCopy := node.DeepCopy()
209+
delete(nodeCopy.Annotations, machineutils.NotManagedByMCM)
210+
if err := c.updateNodeWithAnnotations(ctx, nodeCopy, nil); err != nil {
211+
return machineutils.MediumRetry, err
212+
}
201213
}
202214
}
203215

pkg/util/provider/machinecontroller/machine_safety_test.go

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,8 @@ var _ = Describe("safety_logic", func() {
418418
Describe("#AnnotateNodesUnmanagedByMCM", func() {
419419

420420
type setup struct {
421-
node *corev1.Node
421+
node *corev1.Node
422+
associateNodeWithMachine bool
422423
}
423424
type action struct {
424425
}
@@ -441,6 +442,25 @@ var _ = Describe("safety_logic", func() {
441442
targetCoreObjects := []runtime.Object{}
442443
controlMachineObjects := []runtime.Object{}
443444

445+
if data.setup.associateNodeWithMachine {
446+
//optional machine object for test-node-1 ie data.setup.node
447+
testMachineObject := &v1alpha1.Machine{
448+
ObjectMeta: metav1.ObjectMeta{
449+
Name: "testmachine_0",
450+
Namespace: testNamespace,
451+
Labels: map[string]string{
452+
v1alpha1.NodeLabelKey: data.setup.node.Name,
453+
},
454+
},
455+
Status: v1alpha1.MachineStatus{
456+
CurrentStatus: v1alpha1.CurrentStatus{
457+
Phase: v1alpha1.MachineRunning,
458+
},
459+
},
460+
}
461+
controlMachineObjects = append(controlMachineObjects, testMachineObject)
462+
}
463+
444464
//machine object for test-node-1
445465
testMachineObject := &v1alpha1.Machine{
446466
ObjectMeta: metav1.ObjectMeta{
@@ -726,6 +746,66 @@ var _ = Describe("safety_logic", func() {
726746
err: nil,
727747
},
728748
}),
749+
Entry("Node incorrectly assigned NotManagedByMCM annotation", &data{
750+
setup: setup{
751+
node: &corev1.Node{
752+
TypeMeta: metav1.TypeMeta{
753+
APIVersion: "v1",
754+
Kind: "Node",
755+
},
756+
ObjectMeta: metav1.ObjectMeta{
757+
Name: "test-node-0",
758+
Annotations: map[string]string{
759+
"anno1": "value1",
760+
machineutils.NotManagedByMCM: "1",
761+
},
762+
},
763+
},
764+
associateNodeWithMachine: true,
765+
},
766+
action: action{},
767+
expect: expect{
768+
node0: &corev1.Node{
769+
TypeMeta: metav1.TypeMeta{
770+
APIVersion: "v1",
771+
Kind: "Node",
772+
},
773+
ObjectMeta: metav1.ObjectMeta{
774+
Name: "test-node-0",
775+
// node0 no longer has NotManagedByMCM since it has a backing machine object.
776+
Annotations: map[string]string{
777+
"anno1": "value1",
778+
},
779+
},
780+
},
781+
node1: &corev1.Node{
782+
TypeMeta: metav1.TypeMeta{
783+
APIVersion: "v1",
784+
Kind: "Node",
785+
},
786+
ObjectMeta: metav1.ObjectMeta{
787+
Name: "test-node-1",
788+
Annotations: map[string]string{
789+
"anno1": "value1",
790+
},
791+
},
792+
},
793+
node2: &corev1.Node{
794+
TypeMeta: metav1.TypeMeta{
795+
APIVersion: "v1",
796+
Kind: "Node",
797+
},
798+
ObjectMeta: metav1.ObjectMeta{
799+
Name: "test-node-2",
800+
Annotations: map[string]string{
801+
"anno1": "value1",
802+
},
803+
},
804+
},
805+
retry: machineutils.LongRetry,
806+
err: nil,
807+
},
808+
}),
729809
)
730810
})
731811
})

pkg/util/provider/machinecontroller/machine_safety_util.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"k8s.io/klog/v2"
2323
)
2424

25-
func (c *controller) updateNodeWithAnnotation(ctx context.Context, node *v1.Node, annotations map[string]string) error {
25+
func (c *controller) updateNodeWithAnnotations(ctx context.Context, node *v1.Node, annotations map[string]string) error {
2626

2727
// Initialize node annotations if empty
2828
if node.Annotations == nil {
@@ -37,10 +37,9 @@ func (c *controller) updateNodeWithAnnotation(ctx context.Context, node *v1.Node
3737
_, err := c.targetCoreClient.CoreV1().Nodes().Update(ctx, node, metav1.UpdateOptions{})
3838

3939
if err != nil {
40-
klog.Errorf("Couldn't patch the node %q , Error: %s", node.Name, err)
40+
klog.Errorf("Failed to update annotations for Node %q due to error: %s", node.Name, err)
4141
return err
4242
}
43-
klog.V(2).Infof("Annotated node %q was annotated with NotManagedByMCM successfully", node.Name)
4443

4544
return nil
4645
}

pkg/util/provider/machinecontroller/machine_safety_util_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@ package controller
1717
import (
1818
"context"
1919

20-
"github.com/gardener/machine-controller-manager/pkg/util/provider/machineutils"
2120
. "github.com/onsi/ginkgo"
2221
. "github.com/onsi/ginkgo/extensions/table"
2322
. "github.com/onsi/gomega"
2423
corev1 "k8s.io/api/core/v1"
2524
apierrors "k8s.io/apimachinery/pkg/api/errors"
2625
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26+
27+
"github.com/gardener/machine-controller-manager/pkg/util/provider/machineutils"
2728
)
2829

2930
var _ = Describe("machine_safety_util", func() {
@@ -56,7 +57,7 @@ var _ = Describe("machine_safety_util", func() {
5657
testNode := data.action.node
5758
expectedNode := data.expect.node
5859

59-
err := c.updateNodeWithAnnotation(context.TODO(), testNode, data.action.annotations)
60+
err := c.updateNodeWithAnnotations(context.TODO(), testNode, data.action.annotations)
6061

6162
waitForCacheSync(stop, c)
6263

0 commit comments

Comments
 (0)