Skip to content

Commit

Permalink
Merge pull request openshift#745 from pvasant/acceptmajorminorforclus…
Browse files Browse the repository at this point in the history
…terupgrade

accept major minor version for cluster upgrade
  • Loading branch information
openshift-ci[bot] authored Jul 8, 2022
2 parents 2ed1dbc + e6607aa commit 8fd25d0
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 2 deletions.
23 changes: 21 additions & 2 deletions cmd/upgrade/cluster/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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()
}
Expand Down Expand Up @@ -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)
Expand Down
88 changes: 88 additions & 0 deletions pkg/ocm/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,91 @@ 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])

userRequestedParsedVersion, err := ver.NewVersion(userRequestedMajorMinorVersion)
if err != nil {
return false, err
}

isValid, err := checkClusterVersion(clusterVersion, userRequestedParsedVersion)
if err != nil {
return false, err
}
if !isValid {
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 checkClusterVersion(clusterVersion string, userRequestedParsedVersion *ver.Version) (bool, error) {
c, err := ver.NewVersion(clusterVersion)
if err != nil {
return false, err
}
cSplit := c.Segments64()

clusterMajorMinorVersion := fmt.Sprintf("%d.%d",
cSplit[0], cSplit[1])

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
}
return true, 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
}

0 comments on commit 8fd25d0

Please sign in to comment.