Skip to content

Commit 7821503

Browse files
committed
Reconcile machine before creation deadline
1 parent 5b9a383 commit 7821503

File tree

3 files changed

+97
-7
lines changed

3 files changed

+97
-7
lines changed

pkg/util/provider/machinecontroller/machine.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,15 @@ func (c *controller) reconcileClusterMachine(ctx context.Context, machine *v1alp
174174
}
175175
}
176176
if machine.Spec.ProviderID == "" || machine.Status.CurrentStatus.Phase == "" || machine.Status.CurrentStatus.Phase == v1alpha1.MachineCrashLoopBackOff {
177-
return c.triggerCreationFlow(
177+
retry, err = c.triggerCreationFlow(
178178
ctx,
179179
&driver.CreateMachineRequest{
180180
Machine: machine,
181181
MachineClass: machineClass,
182182
Secret: &corev1.Secret{Data: secretData},
183183
},
184184
)
185+
return c.adjustCreateRetryRequired(machine, retry), err
185186
}
186187

187188
return machineutils.LongRetry, nil
@@ -353,7 +354,9 @@ func (c *controller) triggerCreationFlow(ctx context.Context, createMachineReque
353354
// If node label is not present
354355
klog.V(2).Infof("Creating a VM for machine %q, please wait!", machine.Name)
355356
klog.V(2).Infof("The machine creation is triggered with timeout of %s", c.getEffectiveCreationTimeout(createMachineRequest.Machine).Duration)
356-
createMachineResponse, err := c.driver.CreateMachine(ctx, createMachineRequest)
357+
createMCCtx, cancelFunc := c.getCreationContext(ctx, machine)
358+
defer cancelFunc()
359+
createMachineResponse, err := c.driver.CreateMachine(createMCCtx, createMachineRequest)
357360
if err != nil {
358361
// Create call returned an error.
359362
klog.Errorf("Error while creating machine %s: %s", machine.Name, err.Error())

pkg/util/provider/machinecontroller/machine_util.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ func SyncMachineTaints(
473473
return toBeUpdated
474474
}
475475

476-
// machineCreateErrorHandler TODO
476+
// machineCreateErrorHandler handles errors when machine creation does not succeed
477477
func (c *controller) machineCreateErrorHandler(ctx context.Context, machine *v1alpha1.Machine, createMachineResponse *driver.CreateMachineResponse, err error) (machineutils.RetryPeriod, error) {
478478
var (
479479
retryRequired = machineutils.MediumRetry
@@ -491,7 +491,7 @@ func (c *controller) machineCreateErrorHandler(ctx context.Context, machine *v1a
491491
lastKnownState = createMachineResponse.LastKnownState
492492
}
493493

494-
c.machineStatusUpdate(
494+
statusUpdErr := c.machineStatusUpdate(
495495
ctx,
496496
machine,
497497
v1alpha1.LastOperation{
@@ -507,8 +507,23 @@ func (c *controller) machineCreateErrorHandler(ctx context.Context, machine *v1a
507507
},
508508
lastKnownState,
509509
)
510+
return retryRequired, statusUpdErr
511+
}
512+
513+
// adjustCreateRetryRequired adjusts the retry period if needed so that we don't have a case where the machine is reconciled after machineCreationTimeout
514+
// if the retry period is too large so that, it is adjusted so that it causes a reconcile at the machineCreationTimeout
515+
// if the machineCreationTimeout has already passed, return `ShortRetry` so that the machine is immediately reconciled
516+
func (c *controller) adjustCreateRetryRequired(machine *v1alpha1.Machine, retryRequired machineutils.RetryPeriod) machineutils.RetryPeriod {
517+
adjustedRetry := retryRequired
518+
machineCreationDeadline := machine.CreationTimestamp.Time.Add(c.getEffectiveCreationTimeout(machine).Duration)
519+
if time.Now().After(machineCreationDeadline) {
520+
adjustedRetry = machineutils.ShortRetry
521+
} else if time.Now().Add(time.Duration(retryRequired)).After(machineCreationDeadline) {
522+
// Machine will reconcile after create deadline. Adapt RetryPeriod to reconcile machine at deadline
523+
adjustedRetry = machineutils.RetryPeriod(machineCreationDeadline.Sub(time.Now()))
524+
}
510525

511-
return retryRequired, nil
526+
return adjustedRetry
512527
}
513528

514529
func (c *controller) machineStatusUpdate(
@@ -974,7 +989,7 @@ func isConditionEmpty(condition v1.NodeCondition) bool {
974989
return condition == v1.NodeCondition{}
975990
}
976991

977-
// initializes err and description with the passed string message
992+
// printLogInitError initializes err and description with the passed string message
978993
func printLogInitError(s string, err *error, description *string, machine *v1alpha1.Machine) {
979994
klog.Warningf(s+" machine: %q ", machine.Name)
980995
*err = fmt.Errorf(s+" %s", machineutils.InitiateVMDeletion)
@@ -1304,7 +1319,7 @@ func (c *controller) getEffectiveHealthTimeout(machine *v1alpha1.Machine) *metav
13041319
return effectiveHealthTimeout
13051320
}
13061321

1307-
// getEffectiveHealthTimeout returns the creationTimeout set on the machine-object, otherwise returns the timeout set using the global-flag.
1322+
// getEffectiveCreationTimeout returns the creationTimeout set on the machine-object, otherwise returns the timeout set using the global-flag.
13081323
func (c *controller) getEffectiveCreationTimeout(machine *v1alpha1.Machine) *metav1.Duration {
13091324
var effectiveCreationTimeout *metav1.Duration
13101325
if machine.Spec.MachineConfiguration != nil && machine.Spec.MachineConfiguration.MachineCreationTimeout != nil {
@@ -1326,6 +1341,11 @@ func (c *controller) getEffectiveNodeConditions(machine *v1alpha1.Machine) *stri
13261341
return effectiveNodeConditions
13271342
}
13281343

1344+
func (c *controller) getCreationContext(parentCtx context.Context, machine *v1alpha1.Machine) (context.Context, context.CancelFunc) {
1345+
timeOutDuration := c.getEffectiveCreationTimeout(machine).Duration
1346+
return context.WithDeadline(parentCtx, machine.CreationTimestamp.Time.Add(timeOutDuration))
1347+
}
1348+
13291349
// UpdateNodeTerminationCondition updates termination condition on the node object
13301350
func (c *controller) UpdateNodeTerminationCondition(ctx context.Context, machine *v1alpha1.Machine) error {
13311351
if machine.Status.CurrentStatus.Phase == "" || machine.Status.CurrentStatus.Phase == v1alpha1.MachineCrashLoopBackOff {

pkg/util/provider/machinecontroller/machine_util_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2725,4 +2725,71 @@ var _ = Describe("machine_util", func() {
27252725
}),
27262726
)
27272727
})
2728+
2729+
Describe("#Adjust Machine Create Retry Timeout", func() {
2730+
type setup struct {
2731+
machine *machinev1.Machine
2732+
mcCreateTimeout metav1.Duration
2733+
createOpRetry machineutils.RetryPeriod
2734+
}
2735+
2736+
type expect struct {
2737+
retryAdjusted bool
2738+
}
2739+
2740+
type data struct {
2741+
setup setup
2742+
expect expect
2743+
}
2744+
2745+
DescribeTable("##table",
2746+
func(data *data) {
2747+
stop := make(chan struct{})
2748+
defer close(stop)
2749+
2750+
c, trackers := createController(stop, testNamespace, nil, nil, nil, nil)
2751+
defer trackers.Stop()
2752+
2753+
c.safetyOptions.MachineCreationTimeout = data.setup.mcCreateTimeout
2754+
2755+
adjustedRetry := c.adjustCreateRetryRequired(data.setup.machine, machineutils.RetryPeriod(data.setup.createOpRetry))
2756+
2757+
if data.expect.retryAdjusted {
2758+
Expect(adjustedRetry).Should(BeNumerically("<=", machineutils.RetryPeriod(c.getEffectiveCreationTimeout(data.setup.machine).Duration)))
2759+
} else {
2760+
Expect(adjustedRetry).To(Equal(data.setup.createOpRetry))
2761+
}
2762+
},
2763+
Entry("Should not adjust machine create retry period if retry is set to occur before machine create deadline", &data{
2764+
setup: setup{
2765+
createOpRetry: machineutils.RetryPeriod(3 * time.Minute),
2766+
mcCreateTimeout: metav1.Duration{Duration: 20 * time.Minute},
2767+
machine: newMachine(
2768+
&machinev1.MachineTemplateSpec{
2769+
Spec: machinev1.MachineSpec{},
2770+
},
2771+
&machinev1.MachineStatus{},
2772+
nil, nil, map[string]string{v1alpha1.NodeLabelKey: "test-node-0"}, true, metav1.Now()),
2773+
},
2774+
expect: expect{
2775+
retryAdjusted: false,
2776+
},
2777+
}),
2778+
Entry("Should adjust machine create retry period to machine create deadline if retry is set to occur after machine create deadline", &data{
2779+
setup: setup{
2780+
createOpRetry: machineutils.RetryPeriod(4 * time.Minute),
2781+
mcCreateTimeout: metav1.Duration{Duration: 3 * time.Minute},
2782+
machine: newMachine(
2783+
&machinev1.MachineTemplateSpec{
2784+
Spec: machinev1.MachineSpec{},
2785+
},
2786+
&machinev1.MachineStatus{},
2787+
nil, nil, map[string]string{v1alpha1.NodeLabelKey: "test-node-0"}, true, metav1.Now()),
2788+
},
2789+
expect: expect{
2790+
retryAdjusted: true,
2791+
},
2792+
}),
2793+
)
2794+
})
27282795
})

0 commit comments

Comments
 (0)