@@ -377,7 +377,6 @@ func (c *controller) triggerCreationFlow(ctx context.Context, createMachineReque
377
377
Secret : createMachineRequest .Secret ,
378
378
},
379
379
)
380
-
381
380
if err != nil {
382
381
// VM with required name is not found.
383
382
machineErr , ok := status .FromError (err )
@@ -387,7 +386,6 @@ func (c *controller) triggerCreationFlow(ctx context.Context, createMachineReque
387
386
return machineutils .MediumRetry , err
388
387
}
389
388
klog .Warningf ("For machine %q, obtained VM error status as: %s" , machineName , machineErr )
390
-
391
389
// Decoding machine error code
392
390
switch machineErr .Code () {
393
391
case codes .NotFound , codes .Unimplemented :
@@ -404,13 +402,10 @@ func (c *controller) triggerCreationFlow(ctx context.Context, createMachineReque
404
402
klog .Errorf ("Error while creating machine %s: %s" , machine .Name , err .Error ())
405
403
return c .machineCreateErrorHandler (ctx , machine , createMachineResponse , err )
406
404
}
407
-
408
405
nodeName = createMachineResponse .NodeName
409
406
providerID = createMachineResponse .ProviderID
410
-
411
407
// Creation was successful
412
408
klog .V (2 ).Infof ("Created new VM for machine: %q with ProviderID: %q and backing node: %q" , machine .Name , providerID , getNodeName (machine ))
413
-
414
409
// If a node obj already exists by the same nodeName, treat it as a stale node and trigger machine deletion.
415
410
// TODO: there is a case with Azure where the VM may join the cluster before the CreateMachine call is completed,
416
411
// and that would make an otherwise healthy node, be marked as stale.
@@ -497,6 +492,10 @@ func (c *controller) triggerCreationFlow(ctx context.Context, createMachineReque
497
492
case codes .Uninitialized :
498
493
uninitializedMachine = true
499
494
klog .Infof ("VM instance associated with machine %s was created but not initialized." , machine .Name )
495
+ //clean me up. I'm dirty.
496
+ //TODO@thiyyakat add a pointer to a boolean variable indicating whether initialization has happened successfully.
497
+ nodeName = getMachineStatusResponse .NodeName
498
+ providerID = getMachineStatusResponse .ProviderID
500
499
501
500
default :
502
501
updateRetryPeriod , updateErr := c .machineStatusUpdate (
@@ -527,43 +526,23 @@ func (c *controller) triggerCreationFlow(ctx context.Context, createMachineReque
527
526
nodeName = getMachineStatusResponse .NodeName
528
527
providerID = getMachineStatusResponse .ProviderID
529
528
}
530
-
529
+ //Update labels, providerID
530
+ var clone * v1alpha1.Machine
531
+ clone , err = c .updateLabels (ctx , createMachineRequest .Machine , nodeName , providerID )
532
+ //initialize VM if not initialized
531
533
if uninitializedMachine {
532
- retryPeriod , err := c .initializeMachine (ctx , createMachineRequest .Machine , createMachineRequest .MachineClass , createMachineRequest .Secret )
534
+ var retryPeriod machineutils.RetryPeriod
535
+ retryPeriod , err = c .initializeMachine (ctx , clone , createMachineRequest .MachineClass , createMachineRequest .Secret )
533
536
if err != nil {
534
537
return retryPeriod , err
535
538
}
539
+ // Return error even when machine object is updated
540
+ err = fmt .Errorf ("machine creation in process. Machine initialization (if required) is successful" )
541
+ return machineutils .ShortRetry , err
536
542
}
537
-
538
- _ , machineNodeLabelPresent := createMachineRequest .Machine .Labels [v1alpha1 .NodeLabelKey ]
539
- _ , machinePriorityAnnotationPresent := createMachineRequest .Machine .Annotations [machineutils .MachinePriority ]
540
-
541
- if ! machineNodeLabelPresent || ! machinePriorityAnnotationPresent || machine .Spec .ProviderID == "" {
542
- clone := machine .DeepCopy ()
543
- if clone .Labels == nil {
544
- clone .Labels = make (map [string ]string )
545
- }
546
- clone .Labels [v1alpha1 .NodeLabelKey ] = nodeName
547
- if clone .Annotations == nil {
548
- clone .Annotations = make (map [string ]string )
549
- }
550
- if clone .Annotations [machineutils .MachinePriority ] == "" {
551
- clone .Annotations [machineutils .MachinePriority ] = "3"
552
- }
553
-
554
- clone .Spec .ProviderID = providerID
555
- _ , err := c .controlMachineClient .Machines (clone .Namespace ).Update (ctx , clone , metav1.UpdateOptions {})
556
- if err != nil {
557
- klog .Warningf ("Machine UPDATE failed for %q. Retrying, error: %s" , machine .Name , err )
558
- } else {
559
- klog .V (2 ).Infof ("Machine labels/annotations UPDATE for %q" , machine .Name )
560
-
561
- // Return error even when machine object is updated
562
- err = fmt .Errorf ("Machine creation in process. Machine UPDATE successful" )
563
- }
543
+ if err != nil {
564
544
return machineutils .ShortRetry , err
565
545
}
566
-
567
546
if machine .Status .CurrentStatus .Phase == "" || machine .Status .CurrentStatus .Phase == v1alpha1 .MachineCrashLoopBackOff {
568
547
clone := machine .DeepCopy ()
569
548
clone .Status .LastOperation = v1alpha1.LastOperation {
@@ -577,23 +556,49 @@ func (c *controller) triggerCreationFlow(ctx context.Context, createMachineReque
577
556
TimeoutActive : true ,
578
557
LastUpdateTime : metav1 .Now (),
579
558
}
580
-
581
559
_ , err := c .controlMachineClient .Machines (clone .Namespace ).UpdateStatus (ctx , clone , metav1.UpdateOptions {})
582
560
if err != nil {
583
561
klog .Warningf ("Machine/status UPDATE failed for %q. Retrying, error: %s" , machine .Name , err )
584
562
} else {
585
563
klog .V (2 ).Infof ("Machine/status UPDATE for %q during creation" , machine .Name )
586
-
587
564
// Return error even when machine object is updated
588
- err = fmt .Errorf ("Machine creation in process. Machine/Status UPDATE successful" )
565
+ err = fmt .Errorf ("machine creation in process. Machine/Status UPDATE successful" )
589
566
}
590
-
591
567
return machineutils .ShortRetry , err
592
568
}
593
-
594
569
return machineutils .LongRetry , nil
595
570
}
596
571
572
+ func (c * controller ) updateLabels (ctx context.Context , machine * v1alpha1.Machine , nodeName , providerID string ) (clone * v1alpha1.Machine , err error ) {
573
+ machineNodeLabelPresent := metav1 .HasLabel (machine .ObjectMeta , v1alpha1 .NodeLabelKey )
574
+ machinePriorityAnnotationPresent := metav1 .HasAnnotation (machine .ObjectMeta , machineutils .MachinePriority )
575
+ clone = machine .DeepCopy ()
576
+ if ! machineNodeLabelPresent || ! machinePriorityAnnotationPresent || machine .Spec .ProviderID == "" {
577
+ if clone .Labels == nil {
578
+ clone .Labels = make (map [string ]string )
579
+ }
580
+ clone .Labels [v1alpha1 .NodeLabelKey ] = nodeName
581
+ if clone .Annotations == nil {
582
+ clone .Annotations = make (map [string ]string )
583
+ }
584
+ if clone .Annotations [machineutils .MachinePriority ] == "" {
585
+ clone .Annotations [machineutils .MachinePriority ] = "3"
586
+ }
587
+ clone .Spec .ProviderID = providerID
588
+ var updatedMachine * v1alpha1.Machine
589
+ updatedMachine , err = c .controlMachineClient .Machines (clone .Namespace ).Update (ctx , clone , metav1.UpdateOptions {})
590
+ if err != nil {
591
+ klog .Warningf ("Machine labels/annotations UPDATE failed for %q. Will retry after VM initialization (if required), error: %s" , machine .Name , err )
592
+ clone = machine .DeepCopy ()
593
+ } else {
594
+ clone = updatedMachine
595
+ klog .V (2 ).Infof ("Machine labels/annotations UPDATE for %q" , clone .Name )
596
+ err = fmt .Errorf ("machine creation in process. Machine labels/annotations update is successful" )
597
+ }
598
+ }
599
+ return clone , err
600
+ }
601
+
597
602
func (c * controller ) initializeMachine (ctx context.Context , machine * v1alpha1.Machine , machineClass * v1alpha1.MachineClass , secret * corev1.Secret ) (machineutils.RetryPeriod , error ) {
598
603
req := & driver.InitializeMachineRequest {
599
604
Machine : machine ,
@@ -608,15 +613,11 @@ func (c *controller) initializeMachine(ctx context.Context, machine *v1alpha1.Ma
608
613
klog .Errorf ("Cannot decode Driver error for machine %q: %s. Unexpected behaviour as Driver errors are expected to be of type status.Status" , machine .Name , err )
609
614
return machineutils .LongRetry , err
610
615
}
611
- klog .Errorf ("Error occurred while initializing VM instance for machine %q: %s" , machine .Name , err )
612
- switch errStatus .Code () {
613
- case codes .Unimplemented :
616
+ if errStatus .Code () == codes .Unimplemented {
614
617
klog .V (3 ).Infof ("Provider does not support Driver.InitializeMachine - skipping VM instance initialization for %q." , machine .Name )
615
618
return 0 , nil
616
- case codes .NotFound :
617
- klog .Warningf ("No VM instance found for machine %q. Skipping VM instance initialization." , machine .Name )
618
- return 0 , nil
619
619
}
620
+ klog .Errorf ("Error occurred while initializing VM instance for machine %q: %s" , machine .Name , err )
620
621
updateRetryPeriod , updateErr := c .machineStatusUpdate (
621
622
ctx ,
622
623
machine ,
@@ -633,14 +634,11 @@ func (c *controller) initializeMachine(ctx context.Context, machine *v1alpha1.Ma
633
634
},
634
635
machine .Status .LastKnownState ,
635
636
)
636
-
637
637
if updateErr != nil {
638
638
return updateRetryPeriod , updateErr
639
639
}
640
-
641
640
return machineutils .ShortRetry , err
642
641
}
643
-
644
642
klog .V (3 ).Infof ("VM instance %q for machine %q was initialized" , resp .ProviderID , machine .Name )
645
643
return 0 , nil
646
644
}
@@ -661,7 +659,7 @@ func (c *controller) triggerDeletionFlow(ctx context.Context, deleteMachineReque
661
659
return c .setMachineTerminationStatus (ctx , deleteMachineRequest )
662
660
663
661
case strings .Contains (machine .Status .LastOperation .Description , machineutils .GetVMStatus ):
664
- return c .getVMStatus (
662
+ return c .updateMachineStatusAndNodeLabel (
665
663
ctx ,
666
664
& driver.GetMachineStatusRequest {
667
665
Machine : deleteMachineRequest .Machine ,
0 commit comments