Skip to content

Commit

Permalink
Add minor precautions
Browse files Browse the repository at this point in the history
  • Loading branch information
HomayoonAlimohammadi committed Nov 6, 2024
1 parent 7efd9e6 commit efce977
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions controllers/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,12 @@ func (r *MicroK8sControlPlaneReconciler) reconcileMachines(ctx context.Context,
var oldVersion, newVersion string

if numMachines > 0 {
var err error
sort.Sort(SortByCreationTimestamp(machines))
oldVersion = getOldestVersion(machines)
oldVersion, err = getOldestVersion(machines)
if err != nil {
return ctrl.Result{}, fmt.Errorf("failed to get oldest version: %w", err)
}
newVersion = semver.MajorMinor(mcp.Spec.Version)
}

Expand Down Expand Up @@ -787,7 +791,8 @@ func waitForPodDeletion(ctx context.Context, kubeclient *kubernetesClient, podNa
}

// getOldestVersion returns the oldest version of the machines.
func getOldestVersion(machines []clusterv1.Machine) (v string) {
func getOldestVersion(machines []clusterv1.Machine) (string, error) {
var v string
for _, m := range machines {
if m.Spec.Version == nil {
// weird!
Expand All @@ -804,13 +809,17 @@ func getOldestVersion(machines []clusterv1.Machine) (v string) {
}
}

return
if v == "" {
return "", fmt.Errorf("no version found")
}
return v, nil
}

func isMachineUpgraded(m clusterv1.Machine, newVersion string) bool {
if m.Spec.Version == nil {
return false
}
machineVersion := semver.MajorMinor(*m.Spec.Version)
newVersion = semver.MajorMinor(newVersion) // just being extra careful
return semver.Compare(machineVersion, newVersion) == 0
}

0 comments on commit efce977

Please sign in to comment.