@@ -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
3130var (
@@ -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
249252func (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