diff --git a/cmd/cluster/upgrade_operator.go b/cmd/cluster/upgrade_operator.go index e5a7a0f..6a93cf1 100644 --- a/cmd/cluster/upgrade_operator.go +++ b/cmd/cluster/upgrade_operator.go @@ -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 { diff --git a/cmd/cluster/utils.go b/cmd/cluster/utils.go index 447f304..0e1299a 100644 --- a/cmd/cluster/utils.go +++ b/cmd/cluster/utils.go @@ -17,6 +17,7 @@ package cluster import ( "bytes" "context" + "errors" "fmt" "os" "sort" @@ -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 +}