diff --git a/cmd/upgrade/cluster/cmd.go b/cmd/upgrade/cluster/cmd.go index 9291c26000..fa484115f1 100644 --- a/cmd/upgrade/cluster/cmd.go +++ b/cmd/upgrade/cluster/cmd.go @@ -174,11 +174,20 @@ func run(cmd *cobra.Command, _ []string) { os.Exit(1) } } - + clusterVersion := cluster.OpenshiftVersion() + if clusterVersion == "" { + clusterVersion = cluster.Version().RawID() + } // Check that the version is valid validVersion := false for _, v := range availableUpgrades { - if v == version { + + isValidVersion, err := ocm.IsValidVersion(version, v, clusterVersion) + if err != nil { + r.Reporter.Errorf("Error validating the version") + os.Exit(1) + } + if isValidVersion { validVersion = true break } @@ -187,6 +196,7 @@ func run(cmd *cobra.Command, _ []string) { r.Reporter.Errorf("Expected a valid version to upgrade to") os.Exit(1) } + if scheduleDate == "" || scheduleTime == "" { interactive.Enable() } @@ -232,6 +242,15 @@ func run(cmd *cobra.Command, _ []string) { r.Reporter.Infof("Account and operator roles for cluster '%s' are compatible with upgrade", clusterKey) } + version, err = ocm.CheckAndParseVersion(availableUpgrades, version) + if err != nil { + r.Reporter.Errorf("Error parsing version to upgrade to") + os.Exit(1) + } + if !confirm.Confirm("upgrade cluster to version '%s'", version) { + os.Exit(0) + } + upgradePolicyBuilder := cmv1.NewUpgradePolicy(). ScheduleType("manual"). Version(version) diff --git a/pkg/ocm/versions.go b/pkg/ocm/versions.go index 38fe623352..8b783cc91c 100644 --- a/pkg/ocm/versions.go +++ b/pkg/ocm/versions.go @@ -220,3 +220,80 @@ func (c *Client) GetDefaultVersion() (version string, err error) { } return "", fmt.Errorf("There are no openShift versions available") } + +func IsValidVersion(userRequestedVersion string, supportedVersion string, clusterVersion string) (bool, error) { + + a, err := ver.NewVersion(userRequestedVersion) + if err != nil { + return false, err + } + + b, err := ver.NewVersion(supportedVersion) + if err != nil { + return false, err + } + versionSplit := a.Segments64() + //If user has specified patch we check directly and return the result + if len(versionSplit) > 2 && versionSplit[2] > 0 { + return a.Equal(b), err + } + + //If the user has specified only major and minor we check + //if the major minor is greater than cluster major minor if so return true + //else return false. + userRequestedMajorMinorVersion := fmt.Sprintf("%d.%d", + versionSplit[0], versionSplit[1]) + + c, err := ver.NewVersion(clusterVersion) + if err != nil { + return false, err + } + cSplit := c.Segments64() + + clusterMajorMinorVersion := fmt.Sprintf("%d.%d", + cSplit[0], cSplit[1]) + + userRequestedParsedVersion, err := ver.NewVersion(userRequestedMajorMinorVersion) + if err != nil { + return false, err + } + + clusterParsedVersion, err := ver.NewVersion(clusterMajorMinorVersion) + if err != nil { + return false, err + } + + //If major minor is only specified and it is equal to cluster current version + //return error + if userRequestedParsedVersion.Equal(clusterParsedVersion) { + return false, nil + } + + v := b.Segments64() + supportedMajorMinorVersion := fmt.Sprintf("%d.%d", + v[0], v[1]) + + parsedSupportedVersion, err := ver.NewVersion(supportedMajorMinorVersion) + if err != nil { + return false, err + } + + //If major minor specified is not equal to cluster version and is not + //in the supported version then return error + if userRequestedParsedVersion.Equal(parsedSupportedVersion) { + return true, nil + } + return false, nil +} + +func CheckAndParseVersion(availableUpgrades []string, version string) (string, error) { + a, err := ver.NewVersion(version) + if err != nil { + return "", err + } + versionSplit := a.Segments64() + if versionSplit[2] > 0 { + return version, nil + } + return availableUpgrades[0], nil +}