@@ -7,7 +7,6 @@ package controller
77
88import (
99 "context"
10- "errors"
1110 "fmt"
1211 "maps"
1312 "slices"
@@ -17,8 +16,6 @@ import (
1716 corev1 "k8s.io/api/core/v1"
1817 apierrors "k8s.io/apimachinery/pkg/api/errors"
1918 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
20- "k8s.io/apimachinery/pkg/labels"
21- "k8s.io/apimachinery/pkg/selection"
2219 utilruntime "k8s.io/apimachinery/pkg/util/runtime"
2320 "k8s.io/apimachinery/pkg/util/sets"
2421 "k8s.io/client-go/tools/cache"
@@ -302,182 +299,6 @@ func (c *controller) reconcileClusterMachineTermination(key string) error {
302299 return nil
303300}
304301
305- /*
306- SECTION
307- Machine controller - nodeToMachine
308- */
309- var (
310- errMultipleMachineMatch = errors .New ("multiple machines matching node" )
311- errNoMachineMatch = errors .New ("no machines matching node found" )
312- )
313-
314- func (c * controller ) addNodeToMachine (obj any ) {
315- node := obj .(* corev1.Node )
316- if node == nil {
317- klog .Errorf ("Couldn't convert to node from object" )
318- return
319- }
320-
321- // If NotManagedByMCM annotation is present on node, don't process this node object
322- if _ , annotationPresent := node .ObjectMeta .Annotations [machineutils .NotManagedByMCM ]; annotationPresent {
323- return
324- }
325-
326- key , err := cache .DeletionHandlingMetaNamespaceKeyFunc (obj )
327- if err != nil {
328- klog .Errorf ("Couldn't get key for object %+v: %v" , obj , err )
329- return
330- }
331-
332- machine , err := c .getMachineFromNode (key )
333- if err != nil {
334- if err == errNoMachineMatch {
335- // errNoMachineMatch could mean that VM is still in creation hence ignoring it
336- return
337- }
338-
339- klog .Errorf ("Couldn't fetch machine %s, Error: %s" , key , err )
340- return
341- }
342-
343- isMachineCrashLooping := machine .Status .CurrentStatus .Phase == v1alpha1 .MachineCrashLoopBackOff
344- isMachineTerminating := machine .Status .CurrentStatus .Phase == v1alpha1 .MachineTerminating
345- _ , _ , nodeConditionsHaveChanged := nodeConditionsHaveChanged (machine .Status .Conditions , node .Status .Conditions )
346-
347- if ! isMachineCrashLooping && ! isMachineTerminating && nodeConditionsHaveChanged {
348- c .enqueueMachine (machine , fmt .Sprintf ("handling node UPDATE event. conditions of backing node %q have changed" , getNodeName (machine )))
349- }
350- }
351-
352- func (c * controller ) updateNodeToMachine (oldObj , newObj any ) {
353- oldNode := oldObj .(* corev1.Node )
354- node := newObj .(* corev1.Node )
355- if node == nil {
356- klog .Errorf ("Couldn't convert to node from object" )
357- return
358- }
359-
360- machine , err := c .getMachineFromNode (node .Name )
361- if err != nil {
362- klog .Errorf ("Unable to handle update event for node %s, couldn't fetch associated machine. Error: %s" , node .Name , err )
363- return
364- }
365-
366- // check for the TriggerDeletionByMCM annotation on the node object
367- // if it is present then mark the machine object for deletion
368- if value , ok := node .Annotations [machineutils .TriggerDeletionByMCM ]; ok && value == "true" {
369- if machine .DeletionTimestamp == nil {
370- klog .Infof ("Node %s for machine %s is annotated to trigger deletion by MCM." , node .Name , machine .Name )
371- if err := c .controlMachineClient .Machines (c .namespace ).Delete (context .Background (), machine .Name , metav1.DeleteOptions {}); err != nil {
372- klog .Errorf ("Machine object %s backing the node %s could not be marked for deletion." , machine .Name , node .Name )
373- return
374- }
375- klog .Infof ("Machine object %s backing the node %s marked for deletion." , machine .Name , node .Name )
376- }
377- }
378-
379- // to reconcile on addition/removal of essential taints in machine lifecycle, example - critical component taint
380- if addedOrRemovedEssentialTaints (oldNode , node , machineutils .EssentialTaints ) {
381- c .enqueueMachine (machine , fmt .Sprintf ("handling node UPDATE event. atleast one of essential taints on backing node %q has changed" , getNodeName (machine )))
382- return
383- }
384-
385- if inPlaceUpdateLabelsChanged (oldNode , node ) {
386- c .enqueueMachine (machine , fmt .Sprintf ("handling node UPDATE event. in-place update label added or updated for node %q" , getNodeName (machine )))
387- return
388- }
389-
390- c .addNodeToMachine (newObj )
391- }
392-
393- func (c * controller ) deleteNodeToMachine (obj any ) {
394- key , err := cache .DeletionHandlingMetaNamespaceKeyFunc (obj )
395- if err != nil {
396- klog .Errorf ("Couldn't get key for object %+v: %v" , obj , err )
397- return
398- }
399-
400- machine , err := c .getMachineFromNode (key )
401- if err != nil {
402- klog .Errorf ("Couldn't fetch machine %s, Error: %s" , key , err )
403- return
404- }
405-
406- // donot respond if machine obj is already in termination flow
407- if machine .DeletionTimestamp == nil {
408- c .enqueueMachine (machine , fmt .Sprintf ("backing node obj %q got deleted" , key ))
409- }
410- }
411-
412- /*
413- SECTION
414- NodeToMachine operations
415- */
416-
417- func (c * controller ) getMachineFromNode (nodeName string ) (* v1alpha1.Machine , error ) {
418- var (
419- list = []string {nodeName }
420- selector = labels .NewSelector ()
421- req , _ = labels .NewRequirement ("node" , selection .Equals , list )
422- )
423-
424- selector = selector .Add (* req )
425- machines , _ := c .machineLister .List (selector )
426-
427- if len (machines ) > 1 {
428- return nil , errMultipleMachineMatch
429- } else if len (machines ) < 1 {
430- return nil , errNoMachineMatch
431- }
432-
433- return machines [0 ], nil
434- }
435-
436- func inPlaceUpdateLabelsChanged (oldNode , node * corev1.Node ) bool {
437- if oldNode == nil || node == nil {
438- return false
439- }
440-
441- labelKeys := []string {
442- v1alpha1 .LabelKeyNodeCandidateForUpdate ,
443- v1alpha1 .LabelKeyNodeSelectedForUpdate ,
444- v1alpha1 .LabelKeyNodeUpdateResult ,
445- }
446-
447- for _ , key := range labelKeys {
448- oldVal , oldOk := oldNode .Labels [key ]
449- newVal , newOk := node .Labels [key ]
450- if (! oldOk && newOk ) || (key == v1alpha1 .LabelKeyNodeUpdateResult && oldVal != newVal ) {
451- return true
452- }
453- }
454-
455- return false
456- }
457-
458- func addedOrRemovedEssentialTaints (oldNode , node * corev1.Node , taintKeys []string ) bool {
459- mapOldNodeTaintKeys := make (map [string ]bool )
460- mapNodeTaintKeys := make (map [string ]bool )
461-
462- for _ , t := range oldNode .Spec .Taints {
463- mapOldNodeTaintKeys [t .Key ] = true
464- }
465-
466- for _ , t := range node .Spec .Taints {
467- mapNodeTaintKeys [t .Key ] = true
468- }
469-
470- for _ , tk := range taintKeys {
471- _ , oldNodeHasTaint := mapOldNodeTaintKeys [tk ]
472- _ , newNodeHasTaint := mapNodeTaintKeys [tk ]
473- if oldNodeHasTaint != newNodeHasTaint {
474- klog .V (2 ).Infof ("Taint with key %q has been added/removed from the node %q" , tk , node .Name )
475- return true
476- }
477- }
478- return false
479- }
480-
481302/*
482303 SECTION
483304 Machine operations - Create, Delete
@@ -820,6 +641,9 @@ func (c *controller) triggerDeletionFlow(ctx context.Context, deleteMachineReque
820641 case strings .Contains (machine .Status .LastOperation .Description , machineutils .InitiateVMDeletion ):
821642 return c .deleteVM (ctx , deleteMachineRequest )
822643
644+ case strings .Contains (machine .Status .LastOperation .Description , machineutils .RemoveNodeFinalizers ):
645+ return c .deleteNodeFinalizers (ctx , machine )
646+
823647 case strings .Contains (machine .Status .LastOperation .Description , machineutils .InitiateNodeDeletion ):
824648 return c .deleteNodeObject (ctx , machine )
825649
0 commit comments