Skip to content

Commit

Permalink
Fix autopilot creation when plan is completed (k0smotron#924)
Browse files Browse the repository at this point in the history
Signed-off-by: Adrian Pedriza <[email protected]>
Co-authored-by: Adrian Pedriza <[email protected]>
  • Loading branch information
apedriza and AdrianPedriza authored Feb 18, 2025
1 parent 9fd3f58 commit c5c48db
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions internal/controller/controlplane/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,25 +419,32 @@ func (c *K0sController) createAutopilotPlan(ctx context.Context, kcp *cpv1beta1.
return fmt.Errorf("error getting autopilot plan's state: %w", err)
}
if found {
commands, found, err := unstructured.NestedSlice(existingPlan.Object, "spec", "commands")
if err != nil || !found || len(commands) == 0 {
return fmt.Errorf("error getting current autopilot plan's commands: %w", err)
}

version, found, err := unstructured.NestedString(commands[0].(map[string]interface{}), "k0supdate", "version")
if err != nil || !found {
return fmt.Errorf("error getting current autopilot plan's version: %w", err)
}
if state == "Schedulable" || state == "SchedulableWait" {
// it is necessary to check if the current autopilot process corresponds to a previous update by comparing the current
// version of the resource with the desired one. If that is the case, the state is not yet ready to proceed with a new plan.
commands, found, err := unstructured.NestedSlice(existingPlan.Object, "spec", "commands")
if err != nil || !found || len(commands) == 0 {
return fmt.Errorf("error getting current autopilot plan's commands: %w", err)
}

version, found, err := unstructured.NestedString(commands[0].(map[string]interface{}), "k0supdate", "version")
if err != nil || !found {
return fmt.Errorf("error getting current autopilot plan's version: %w", err)
}

if version != kcp.Spec.Version {
return fmt.Errorf("previous autopilot is not finished: %w", ErrNotReady)
}

return nil
}

if state == "Completed" {
// If the state is completed, it is necessary to check if the current version of the resource corresponds to the desired one.
// If that is the case, it is not necessary to proceed with a new plan.
if version == kcp.Spec.Version {
return nil
}
}
}

err = clientset.RESTClient().Delete().AbsPath("/apis/autopilot.k0sproject.io/v1beta2/plans/autopilot").Do(ctx).Error()
Expand Down

0 comments on commit c5c48db

Please sign in to comment.