-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #193 from vkareh/SDA-3107/cluster-upgrades
upgrades: Allow scheduling, listing, canceling cluster upgrades
- Loading branch information
Showing
18 changed files
with
1,023 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
/* | ||
Copyright (c) 2020 Red Hat, Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package upgrade | ||
|
||
import ( | ||
"os" | ||
|
||
cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/openshift/moactl/pkg/aws" | ||
c "github.com/openshift/moactl/pkg/cluster" | ||
"github.com/openshift/moactl/pkg/confirm" | ||
"github.com/openshift/moactl/pkg/logging" | ||
"github.com/openshift/moactl/pkg/ocm" | ||
"github.com/openshift/moactl/pkg/ocm/upgrades" | ||
rprtr "github.com/openshift/moactl/pkg/reporter" | ||
) | ||
|
||
var args struct { | ||
clusterKey string | ||
} | ||
|
||
var Cmd = &cobra.Command{ | ||
Use: "upgrade", | ||
Aliases: []string{"upgrades"}, | ||
Short: "Cancel cluster upgrade", | ||
Long: "Cancel scheduled cluster upgrade", | ||
Run: run, | ||
} | ||
|
||
func init() { | ||
flags := Cmd.Flags() | ||
flags.SortFlags = false | ||
|
||
flags.StringVarP( | ||
&args.clusterKey, | ||
"cluster", | ||
"c", | ||
"", | ||
"Name or ID of the cluster to cancel the upgrade for (required)", | ||
) | ||
Cmd.MarkFlagRequired("cluster") | ||
} | ||
|
||
func run(cmd *cobra.Command, _ []string) { | ||
reporter := rprtr.CreateReporterOrExit() | ||
logger := logging.CreateLoggerOrExit(reporter) | ||
|
||
// Check that the cluster key (name, identifier or external identifier) given by the user | ||
// is reasonably safe so that there is no risk of SQL injection: | ||
clusterKey := args.clusterKey | ||
if !c.IsValidClusterKey(clusterKey) { | ||
reporter.Errorf( | ||
"Cluster name, identifier or external identifier '%s' isn't valid: it "+ | ||
"must contain only letters, digits, dashes and underscores", | ||
clusterKey, | ||
) | ||
os.Exit(1) | ||
} | ||
|
||
// Create the AWS client: | ||
var err error | ||
awsClient, err := aws.NewClient(). | ||
Logger(logger). | ||
Build() | ||
if err != nil { | ||
reporter.Errorf("Failed to create AWS client: %v", err) | ||
os.Exit(1) | ||
} | ||
|
||
awsCreator, err := awsClient.GetCreator() | ||
if err != nil { | ||
reporter.Errorf("Failed to get AWS creator: %v", err) | ||
os.Exit(1) | ||
} | ||
|
||
// Create the client for the OCM API: | ||
ocmConnection, err := ocm.NewConnection(). | ||
Logger(logger). | ||
Build() | ||
if err != nil { | ||
reporter.Errorf("Failed to create OCM connection: %v", err) | ||
os.Exit(1) | ||
} | ||
defer func() { | ||
err = ocmConnection.Close() | ||
if err != nil { | ||
reporter.Errorf("Failed to close OCM connection: %v", err) | ||
} | ||
}() | ||
|
||
// Get the client for the OCM collection of clusters: | ||
ocmClient := ocmConnection.ClustersMgmt().V1() | ||
|
||
// Try to find the cluster: | ||
reporter.Debugf("Loading cluster '%s'", clusterKey) | ||
cluster, err := ocm.GetCluster(ocmClient.Clusters(), clusterKey, awsCreator.ARN) | ||
if err != nil { | ||
reporter.Errorf("Failed to get cluster '%s': %v", clusterKey, err) | ||
os.Exit(1) | ||
} | ||
|
||
if cluster.State() != cmv1.ClusterStateReady { | ||
reporter.Errorf("Cluster '%s' is not yet ready", clusterKey) | ||
os.Exit(1) | ||
} | ||
|
||
scheduledUpgrade, err := upgrades.GetScheduledUpgrade(ocmClient, cluster.ID()) | ||
if err != nil { | ||
reporter.Errorf("Failed to get scheduled upgrades for cluster '%s': %v", clusterKey, err) | ||
os.Exit(1) | ||
} | ||
if scheduledUpgrade == nil { | ||
reporter.Warnf("There are no scheduled upgrades on cluster '%s'", clusterKey) | ||
os.Exit(0) | ||
} | ||
|
||
if confirm.Confirm("cancel scheduled upgrade on cluster %s", clusterKey) { | ||
reporter.Debugf("Deleting scheduled upgrade for cluster '%s'", clusterKey) | ||
canceled, err := upgrades.CancelUpgrade(ocmClient, cluster.ID()) | ||
if err != nil { | ||
reporter.Errorf("Failed to cancel scheduled upgrade on cluster '%s': %v", clusterKey, err) | ||
os.Exit(1) | ||
} | ||
|
||
if !canceled { | ||
reporter.Warnf("There were no scheduled upgrades on cluster '%s'", clusterKey) | ||
os.Exit(0) | ||
} | ||
|
||
reporter.Infof("Successfully canceled scheduled upgrade on cluster '%s'", clusterKey) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
/* | ||
Copyright (c) 2020 Red Hat, Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package upgrade | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
"text/tabwriter" | ||
|
||
cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/openshift/moactl/pkg/aws" | ||
"github.com/openshift/moactl/pkg/logging" | ||
"github.com/openshift/moactl/pkg/ocm" | ||
"github.com/openshift/moactl/pkg/ocm/upgrades" | ||
"github.com/openshift/moactl/pkg/ocm/versions" | ||
rprtr "github.com/openshift/moactl/pkg/reporter" | ||
) | ||
|
||
var args struct { | ||
clusterKey string | ||
} | ||
|
||
var Cmd = &cobra.Command{ | ||
Use: "upgrades", | ||
Aliases: []string{"upgrade"}, | ||
Short: "List available cluster upgrades", | ||
Long: "List available and scheduled cluster version upgrades", | ||
Run: run, | ||
} | ||
|
||
func init() { | ||
flags := Cmd.Flags() | ||
|
||
flags.StringVarP( | ||
&args.clusterKey, | ||
"cluster", | ||
"c", | ||
"", | ||
"Name or ID of the cluster to list the upgrades of (required).", | ||
) | ||
Cmd.MarkFlagRequired("cluster") | ||
} | ||
|
||
func run(_ *cobra.Command, _ []string) { | ||
reporter := rprtr.CreateReporterOrExit() | ||
logger := logging.CreateLoggerOrExit(reporter) | ||
|
||
// Check that the cluster key (name, identifier or external identifier) given by the user | ||
// is reasonably safe so that there is no risk of SQL injection: | ||
clusterKey := args.clusterKey | ||
if !ocm.IsValidClusterKey(clusterKey) { | ||
reporter.Errorf( | ||
"Cluster name, identifier or external identifier '%s' isn't valid: it "+ | ||
"must contain only letters, digits, dashes and underscores", | ||
clusterKey, | ||
) | ||
os.Exit(1) | ||
} | ||
|
||
// Create the client for the OCM API: | ||
ocmConnection, err := ocm.NewConnection(). | ||
Logger(logger). | ||
Build() | ||
if err != nil { | ||
reporter.Errorf("Failed to create OCM connection: %v", err) | ||
os.Exit(1) | ||
} | ||
defer func() { | ||
err = ocmConnection.Close() | ||
if err != nil { | ||
reporter.Errorf("Failed to close OCM connection: %v", err) | ||
} | ||
}() | ||
ocmClient := ocmConnection.ClustersMgmt().V1() | ||
|
||
// Create the AWS client: | ||
awsClient, err := aws.NewClient(). | ||
Logger(logger). | ||
Build() | ||
if err != nil { | ||
reporter.Errorf("Failed to create AWS client: %v", err) | ||
os.Exit(1) | ||
} | ||
|
||
awsCreator, err := awsClient.GetCreator() | ||
if err != nil { | ||
reporter.Errorf("Failed to get AWS creator: %v", err) | ||
os.Exit(1) | ||
} | ||
|
||
// Try to find the cluster: | ||
reporter.Debugf("Loading cluster '%s'", clusterKey) | ||
cluster, err := ocm.GetCluster(ocmClient.Clusters(), clusterKey, awsCreator.ARN) | ||
if err != nil { | ||
reporter.Errorf("Failed to get cluster '%s': %v", clusterKey, err) | ||
os.Exit(1) | ||
} | ||
|
||
if cluster.State() != cmv1.ClusterStateReady { | ||
reporter.Errorf("Cluster '%s' is not yet ready", clusterKey) | ||
os.Exit(1) | ||
} | ||
|
||
// Load available upgrades for this cluster | ||
reporter.Debugf("Loading available upgrades for cluster '%s'", clusterKey) | ||
availableUpgrades, err := versions.GetAvailableUpgrades(ocmClient, versions.GetVersionID(cluster)) | ||
if err != nil { | ||
reporter.Errorf("Failed to get available upgrades for cluster '%s': %v", clusterKey, err) | ||
os.Exit(1) | ||
} | ||
|
||
if len(availableUpgrades) == 0 { | ||
reporter.Infof("There are no available upgrades for cluster '%s'", clusterKey) | ||
os.Exit(0) | ||
} | ||
|
||
latestRev := latestInCurrentMinor(versions.GetVersionID(cluster), availableUpgrades) | ||
|
||
reporter.Debugf("Loading scheduled upgrades for cluster '%s'", clusterKey) | ||
scheduledUpgrade, err := upgrades.GetScheduledUpgrade(ocmClient, cluster.ID()) | ||
if err != nil { | ||
reporter.Errorf("Failed to get scheduled upgrades for cluster '%s': %v", clusterKey, err) | ||
os.Exit(1) | ||
} | ||
|
||
// Create the writer that will be used to print the tabulated results: | ||
writer := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0) | ||
fmt.Fprintf(writer, "VERSION\tNOTES\n") | ||
for i, availableUpgrade := range availableUpgrades { | ||
notes := "" | ||
if notes == "" && (i == 0 || availableUpgrade == latestRev) { | ||
notes = "recommended" | ||
} | ||
if availableUpgrade == scheduledUpgrade.Version() { | ||
notes = fmt.Sprintf("scheduled for %s", scheduledUpgrade.NextRun().Format("2006-01-02 15:04 MST")) | ||
} | ||
fmt.Fprintf(writer, "%s\t%s\n", availableUpgrade, notes) | ||
} | ||
writer.Flush() | ||
} | ||
|
||
func latestInCurrentMinor(current string, versions []string) string { | ||
currentParts := strings.Split(current, ".") | ||
currentRev := currentParts[2] | ||
latestVersion := current | ||
latestRev := currentRev | ||
for _, version := range versions { | ||
versionParts := strings.Split(version, ".") | ||
if currentParts[1] != versionParts[1] { | ||
continue | ||
} | ||
if versionParts[2] > latestRev { | ||
latestRev = versionParts[2] | ||
latestVersion = version | ||
} | ||
} | ||
return latestVersion | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.