Skip to content

Commit

Permalink
cluster: Ensure we handle upgrades from snapshots gracefully
Browse files Browse the repository at this point in the history
Signed-off-by: Dario Freddi <dario.freddi@ispirata.com>
  • Loading branch information
drf committed Nov 29, 2019
1 parent 005099f commit b10eff9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
14 changes: 14 additions & 0 deletions cmd/cluster/upgrade_operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,20 @@ func clusterUpgradeOperatorF(command *cobra.Command, args []string) error {
fmt.Printf("You're currently running Astarte Operator version %s, no updates are available.\n", currentAstarteOperatorVersion)
return nil
}
if isUnstableVersion(currentAstarteOperatorVersion.Original()) {
baseVersion, err := getBaseVersionFromUnstable(currentAstarteOperatorVersion.Original())
if err != nil {
fmt.Println("Your cluster is currently running on snapshot - honestly, there isn't much I can do. If you're running a production cluster, I really hope you know what you're doing.")
fmt.Println("In case you didn't really mean to run on the most unstable thing you could run on, I strongly suggest running astartectl cluster uninstall-operator and astartectl cluster install-operator.")
os.Exit(1)
}
currentAstarteOperatorVersion, err = semver.NewVersion(baseVersion)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("Your cluster is currently running on an unstable snapshot - which, by the way, is a bad idea. I'm happy to reconcile you to something more stable, and I'm assuming you're upgrading from %s.\n", currentAstarteOperatorVersion)
}
fmt.Printf("Will upgrade Astarte Operator to version %s.\n", version)

if !nonInteractive {
Expand Down
21 changes: 21 additions & 0 deletions cmd/cluster/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cluster
import (
"bytes"
"context"
"errors"
"fmt"
"os"
"sort"
Expand Down Expand Up @@ -197,3 +198,23 @@ func getManagedAstarteResourceStatus(res unstructured.Unstructured) (string, tim

return operatorStatus, lastTransition, deploymentManager, deploymentProfile
}

func isUnstableVersion(version string) bool {
return strings.HasSuffix(version, "-snapshot") || version == "snapshot"
}

func getBaseVersionFromUnstable(version string) (string, error) {
if !isUnstableVersion(version) {
return "", fmt.Errorf("%v is not an unstable version", version)
}

if version == "snapshot" {
return "", errors.New("You are running on snapshot - I have no way of reconciling you from here")
}

// Get the base version, and add a .0.
baseVersion := strings.Replace(version, "-snapshot", "", 1)
baseVersion += ".0"

return baseVersion, nil
}

0 comments on commit b10eff9

Please sign in to comment.