Skip to content

Commit c5f7dbe

Browse files
committed
Address review
1 parent 5b28f0b commit c5f7dbe

File tree

4 files changed

+87
-105
lines changed

4 files changed

+87
-105
lines changed

pkg/util/provider/machinecontroller/machine.go

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -300,56 +300,6 @@ func (c *controller) reconcileClusterMachineTermination(key string) error {
300300
}
301301
}
302302

303-
/*
304-
SECTION
305-
NodeToMachine operations
306-
*/
307-
308-
func inPlaceUpdateLabelsChanged(oldNode, node *corev1.Node) bool {
309-
if oldNode == nil || node == nil {
310-
return false
311-
}
312-
313-
labelKeys := []string{
314-
v1alpha1.LabelKeyNodeCandidateForUpdate,
315-
v1alpha1.LabelKeyNodeSelectedForUpdate,
316-
v1alpha1.LabelKeyNodeUpdateResult,
317-
}
318-
319-
for _, key := range labelKeys {
320-
oldVal, oldOk := oldNode.Labels[key]
321-
newVal, newOk := node.Labels[key]
322-
if (!oldOk && newOk) || (key == v1alpha1.LabelKeyNodeUpdateResult && oldVal != newVal) {
323-
return true
324-
}
325-
}
326-
327-
return false
328-
}
329-
330-
func addedOrRemovedEssentialTaints(oldNode, node *corev1.Node, taintKeys []string) bool {
331-
mapOldNodeTaintKeys := make(map[string]bool)
332-
mapNodeTaintKeys := make(map[string]bool)
333-
334-
for _, t := range oldNode.Spec.Taints {
335-
mapOldNodeTaintKeys[t.Key] = true
336-
}
337-
338-
for _, t := range node.Spec.Taints {
339-
mapNodeTaintKeys[t.Key] = true
340-
}
341-
342-
for _, tk := range taintKeys {
343-
_, oldNodeHasTaint := mapOldNodeTaintKeys[tk]
344-
_, newNodeHasTaint := mapNodeTaintKeys[tk]
345-
if oldNodeHasTaint != newNodeHasTaint {
346-
klog.V(2).Infof("Taint with key %q has been added/removed from the node %q", tk, node.Name)
347-
return true
348-
}
349-
}
350-
return false
351-
}
352-
353303
/*
354304
SECTION
355305
Machine operations - Create, Delete

pkg/util/provider/machinecontroller/machine_safety.go

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package controller
77

88
import (
99
"context"
10-
"fmt"
1110
"strings"
1211
"time"
1312

@@ -21,7 +20,6 @@ import (
2120
apierrors "k8s.io/apimachinery/pkg/api/errors"
2221
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2322
"k8s.io/apimachinery/pkg/labels"
24-
"k8s.io/apimachinery/pkg/util/sets"
2523
"k8s.io/klog/v2"
2624
)
2725

@@ -172,34 +170,30 @@ func (c *controller) AnnotateNodesUnmanagedByMCM(ctx context.Context) (machineut
172170
klog.Errorf("Couldn't fetch machine, Error: %s", err)
173171
} else if err == errNoMachineMatch {
174172

175-
if !node.CreationTimestamp.Time.Before(time.Now().Add(c.safetyOptions.MachineCreationTimeout.Duration * -1)) {
173+
if time.Since(node.CreationTimestamp.Time) < c.safetyOptions.MachineCreationTimeout.Duration {
176174
// node creationTimestamp is NOT before now() - machineCreationTime
177175
// meaning creationTimeout has not occurred since node creation
178176
// hence don't tag such nodes
179177
klog.V(3).Infof("Node %q is still too young to be tagged with NotManagedByMCM", node.Name)
180178
continue
181-
} else if _, annotationPresent := node.ObjectMeta.Annotations[machineutils.NotManagedByMCM]; annotationPresent {
182-
// annotation already exists, ignore this node
183-
continue
184-
}
185-
186-
// if no backing machine object for a node, annotate it
187-
nodeCopy := node.DeepCopy()
188-
annotations := map[string]string{
189-
machineutils.NotManagedByMCM: "1",
190179
}
191-
192-
klog.V(3).Infof("Adding NotManagedByMCM annotation to Node %q", node.Name)
193-
// err is returned only when node update fails
194-
if err := c.updateNodeWithAnnotations(ctx, nodeCopy, annotations); err != nil {
180+
// Remove MCM finalizer from orphan nodes to allow deletion
181+
if finalizerPresent, err := c.removeNodeFinalizers(ctx, node); err != nil {
182+
klog.Errorf("Failed to remove finalizer from orphan node %q: %v", node.Name, err)
195183
return machineutils.MediumRetry, err
184+
} else if finalizerPresent {
185+
klog.Infof("Removed MCM finalizer from orphan node %q to allow deletion", node.Name)
196186
}
197-
// Remove MCM finalizer from orphan nodes to allow deletion
198-
if sets.NewString(node.Finalizers...).Has(NodeFinalizerName) {
199-
if _, err := c.removeNodeFinalizers(ctx, node); err != nil {
200-
klog.Errorf("Failed to remove finalizer from orphan node %q: %v", node.Name, err)
201-
} else {
202-
klog.Infof("Removed MCM finalizer from orphan node %q to allow deletion", node.Name)
187+
if _, annotationPresent := node.ObjectMeta.Annotations[machineutils.NotManagedByMCM]; !annotationPresent {
188+
// if no backing machine object for a node, annotate it
189+
nodeCopy := node.DeepCopy()
190+
annotations := map[string]string{
191+
machineutils.NotManagedByMCM: "1",
192+
}
193+
klog.V(3).Infof("Adding NotManagedByMCM annotation to Node %q", node.Name)
194+
// err is returned only when node update fails
195+
if err := c.updateNodeWithAnnotations(ctx, nodeCopy, annotations); err != nil {
196+
return machineutils.MediumRetry, err
203197
}
204198
}
205199
}
@@ -214,8 +208,6 @@ func (c *controller) AnnotateNodesUnmanagedByMCM(ctx context.Context) (machineut
214208
if err := c.updateNodeWithAnnotations(ctx, nodeCopy, nil); err != nil {
215209
return machineutils.MediumRetry, err
216210
}
217-
// Queue node for reconciliation to add finalizer back
218-
c.enqueueNodeAfter(node, time.Duration(machineutils.ShortRetry), fmt.Sprintf("Node %q is managed by MCM, reconciling", node.Name))
219211
}
220212
}
221213

pkg/util/provider/machinecontroller/machine_util.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1882,7 +1882,6 @@ func (c *controller) deleteNodeFinalizers(ctx context.Context, machine *v1alpha1
18821882
var (
18831883
err error
18841884
description string
1885-
retryPeriod = machineutils.ShortRetry
18861885
state v1alpha1.MachineState
18871886
)
18881887

@@ -1903,7 +1902,7 @@ func (c *controller) deleteNodeFinalizers(ctx context.Context, machine *v1alpha1
19031902
state = v1alpha1.MachineStateFailed
19041903
}
19051904
} else {
1906-
retryPeriod, err = c.removeNodeFinalizers(ctx, node)
1905+
_, err = c.removeNodeFinalizers(ctx, node)
19071906
if err != nil {
19081907
description = fmt.Sprintf("Removal of finalizers from Node Object %q failed due to error: %s, Retrying node finalizer removal. %s", nodeName, err, machineutils.RemoveNodeFinalizers)
19091908
klog.Errorf(description)
@@ -1938,7 +1937,7 @@ func (c *controller) deleteNodeFinalizers(ctx context.Context, machine *v1alpha1
19381937
if updateErr != nil {
19391938
return updateRetryPeriod, updateErr
19401939
}
1941-
return retryPeriod, err
1940+
return machineutils.ShortRetry, err
19421941
}
19431942

19441943
// deleteNodeObject attempts to delete the node object backed by the machine object

pkg/util/provider/machinecontroller/node.go

Lines changed: 69 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"k8s.io/apimachinery/pkg/selection"
2626
"k8s.io/apimachinery/pkg/types"
2727
"k8s.io/apimachinery/pkg/util/sets"
28-
"k8s.io/apimachinery/pkg/util/strategicpatch"
2928
)
3029

3130
var (
@@ -42,6 +41,7 @@ func (c *controller) addNode(obj any) {
4241

4342
// If NotManagedByMCM annotation is present on node, don't process this node object
4443
if _, annotationPresent := node.ObjectMeta.Annotations[machineutils.NotManagedByMCM]; annotationPresent {
44+
klog.Infof("NotManagedByMCM annotation present on node %q, skipping ADD event processing", node.Name)
4545
return
4646
}
4747
c.enqueueNode(node, "handling ADD event for node")
@@ -57,6 +57,7 @@ func (c *controller) updateNode(oldObj, newObj any) {
5757

5858
// If NotManagedByMCM annotation is present on node, don't process this node object
5959
if _, annotationPresent := node.ObjectMeta.Annotations[machineutils.NotManagedByMCM]; annotationPresent {
60+
klog.Infof("NotManagedByMCM annotation present on node %q, skipping UPDATE event processing", node.Name)
6061
return
6162
}
6263

@@ -76,7 +77,7 @@ func (c *controller) updateNode(oldObj, newObj any) {
7677

7778
machine, err := c.getMachineFromNode(node.Name)
7879
if err != nil {
79-
klog.Errorf("unable to handle update event for node %s, couldn't fetch associated machine. Error: %s", node.Name, err)
80+
klog.Errorf("unable to handle update event for node %q, couldn't fetch associated machine. Error: %v", node.Name, err)
8081
return
8182
}
8283

@@ -116,6 +117,7 @@ func (c *controller) deleteNode(obj any) {
116117

117118
// If NotManagedByMCM annotation is present on node, don't process this node object
118119
if _, annotationPresent := node.ObjectMeta.Annotations[machineutils.NotManagedByMCM]; annotationPresent {
120+
klog.Infof("NotManagedByMCM annotation present on node %q, skipping DELETE event processing", node.Name)
119121
return
120122
}
121123

@@ -139,6 +141,7 @@ func (c *controller) reconcileClusterNodeKey(key string) error {
139141

140142
// If NotManagedByMCM annotation is present on node, don't process this node object
141143
if _, annotationPresent := node.ObjectMeta.Annotations[machineutils.NotManagedByMCM]; annotationPresent {
144+
klog.Infof("ClusterNode %q: NotManagedByMCM annotation present, skipping reconciliation", key)
142145
return nil
143146
}
144147

@@ -155,7 +158,7 @@ func (c *controller) reconcileClusterNodeKey(key string) error {
155158
}
156159

157160
//Add finalizers to node object if not present
158-
_, err = c.addNodeFinalizers(ctx, node)
161+
err = c.addNodeFinalizers(ctx, node)
159162
if err != nil {
160163
klog.Errorf("ClusterNode %q: error adding finalizers to node: %v", key, err)
161164
c.enqueueNodeAfter(node, time.Duration(machineutils.ShortRetry), err.Error())
@@ -173,9 +176,9 @@ func (c *controller) triggerMachineDeletion(ctx context.Context, nodeName string
173176
}
174177

175178
if machine.DeletionTimestamp == nil {
176-
klog.Infof("Node %s for machine %s has been deleted. Triggering machine deletion flow.", nodeName, machine.Name)
179+
klog.Infof("Node %q for machine %q has been deleted. Triggering machine deletion flow.", nodeName, machine.Name)
177180
if err := c.controlMachineClient.Machines(c.namespace).Delete(ctx, machine.Name, metav1.DeleteOptions{}); err != nil {
178-
klog.Errorf("machine object %s backing the deleted node %s could not be marked for deletion. Error: %s", machine.Name, nodeName, err)
181+
klog.Errorf("machine object %q backing the deleted node %q could not be marked for deletion. Error: %s", machine.Name, nodeName, err)
179182
return err
180183
}
181184
}
@@ -220,51 +223,44 @@ func (c *controller) getMachineFromNode(nodeName string) (*v1alpha1.Machine, err
220223
return machines[0], nil
221224
}
222225

223-
func (c *controller) addNodeFinalizers(ctx context.Context, node *corev1.Node) (machineutils.RetryPeriod, error) {
226+
func (c *controller) addNodeFinalizers(ctx context.Context, node *corev1.Node) error {
224227
if finalizers := sets.NewString(node.Finalizers...); !finalizers.Has(NodeFinalizerName) {
225228
finalizers.Insert(NodeFinalizerName)
226229
if err := c.updateNodeFinalizers(ctx, node, finalizers.List()); err != nil {
227-
return machineutils.ShortRetry, err
230+
return err
228231
}
229232
klog.Infof("Added finalizer to node %q", node.Name)
230-
return machineutils.LongRetry, nil
233+
return nil
231234
}
232235
// Do not treat case where finalizer is already present as an error
233-
return machineutils.LongRetry, nil
236+
return nil
234237
}
235238

236-
func (c *controller) removeNodeFinalizers(ctx context.Context, node *corev1.Node) (machineutils.RetryPeriod, error) {
239+
func (c *controller) removeNodeFinalizers(ctx context.Context, node *corev1.Node) (bool, error) {
237240
if finalizers := sets.NewString(node.Finalizers...); finalizers.Has(NodeFinalizerName) {
238241
finalizers.Delete(NodeFinalizerName)
239242
if err := c.updateNodeFinalizers(ctx, node, finalizers.List()); err != nil {
240-
return machineutils.ShortRetry, err
243+
return true, err
241244
}
242245
klog.Infof("Removed finalizer from node %q", node.Name)
243-
return machineutils.ShortRetry, nil
246+
return true, nil
244247
}
245-
return machineutils.ShortRetry, fmt.Errorf("node finalizer not found on node %q", node.Name)
248+
return false, nil
246249
}
247250

248-
// updateNodeFinalizers updates the node finalizers using strategic merge patch
251+
// updateNodeFinalizers updates the node finalizers using merge patch
249252
func (c *controller) updateNodeFinalizers(ctx context.Context, node *corev1.Node, finalizers []string) error {
250-
oldData, err := json.Marshal(node)
251-
if err != nil {
252-
return fmt.Errorf("failed to marshal old node %#v for node %q: %v", node, node.Name, err)
253+
patch := map[string]any{
254+
"metadata": map[string]any{
255+
"finalizers": finalizers,
256+
},
253257
}
254-
255-
newNode := node.DeepCopy()
256-
newNode.Finalizers = finalizers
257-
newData, err := json.Marshal(newNode)
258+
patchBytes, err := json.Marshal(patch)
258259
if err != nil {
259-
return fmt.Errorf("failed to marshal new node %#v for node %q: %v", newNode, node.Name, err)
260+
return fmt.Errorf("failed to marshal patch for node %q: %v", node.Name, err)
260261
}
261262

262-
// Create the strategic merge patch
263-
patchBytes, err := strategicpatch.CreateTwoWayMergePatch(oldData, newData, corev1.Node{})
264-
if err != nil {
265-
return fmt.Errorf("failed to create patch for node %q: %v", node.Name, err)
266-
}
267-
_, err = c.targetCoreClient.CoreV1().Nodes().Patch(ctx, node.Name, types.StrategicMergePatchType, patchBytes, metav1.PatchOptions{})
263+
_, err = c.targetCoreClient.CoreV1().Nodes().Patch(ctx, node.Name, types.MergePatchType, patchBytes, metav1.PatchOptions{})
268264
if err != nil {
269265
klog.Errorf("failed to patch finalizers for node %q: %s", node.Name, err)
270266
return err
@@ -278,3 +274,48 @@ func (c *controller) hasNodeFinalizerBeenRemoved(oldNode, newNode *corev1.Node,
278274
newFinalizers := sets.NewString(newNode.Finalizers...)
279275
return oldFinalizers.Has(finalizerName) && !newFinalizers.Has(finalizerName)
280276
}
277+
278+
func inPlaceUpdateLabelsChanged(oldNode, node *corev1.Node) bool {
279+
if oldNode == nil || node == nil {
280+
return false
281+
}
282+
283+
labelKeys := []string{
284+
v1alpha1.LabelKeyNodeCandidateForUpdate,
285+
v1alpha1.LabelKeyNodeSelectedForUpdate,
286+
v1alpha1.LabelKeyNodeUpdateResult,
287+
}
288+
289+
for _, key := range labelKeys {
290+
oldVal, oldOk := oldNode.Labels[key]
291+
newVal, newOk := node.Labels[key]
292+
if (!oldOk && newOk) || (key == v1alpha1.LabelKeyNodeUpdateResult && oldVal != newVal) {
293+
return true
294+
}
295+
}
296+
297+
return false
298+
}
299+
300+
func addedOrRemovedEssentialTaints(oldNode, node *corev1.Node, taintKeys []string) bool {
301+
mapOldNodeTaintKeys := make(map[string]bool)
302+
mapNodeTaintKeys := make(map[string]bool)
303+
304+
for _, t := range oldNode.Spec.Taints {
305+
mapOldNodeTaintKeys[t.Key] = true
306+
}
307+
308+
for _, t := range node.Spec.Taints {
309+
mapNodeTaintKeys[t.Key] = true
310+
}
311+
312+
for _, tk := range taintKeys {
313+
_, oldNodeHasTaint := mapOldNodeTaintKeys[tk]
314+
_, newNodeHasTaint := mapNodeTaintKeys[tk]
315+
if oldNodeHasTaint != newNodeHasTaint {
316+
klog.V(2).Infof("Taint with key %q has been added/removed from the node %q", tk, node.Name)
317+
return true
318+
}
319+
}
320+
return false
321+
}

0 commit comments

Comments
 (0)