Skip to content

Commit

Permalink
Merge pull request #193 from vkareh/SDA-3107/cluster-upgrades
Browse files Browse the repository at this point in the history
upgrades: Allow scheduling, listing, canceling cluster upgrades
  • Loading branch information
vkareh authored Dec 4, 2020
2 parents 0666587 + f4e5fde commit 36a8d95
Show file tree
Hide file tree
Showing 18 changed files with 1,023 additions and 6 deletions.
2 changes: 2 additions & 0 deletions cmd/dlt/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/openshift/moactl/cmd/dlt/idp"
"github.com/openshift/moactl/cmd/dlt/ingress"
"github.com/openshift/moactl/cmd/dlt/machinepool"
"github.com/openshift/moactl/cmd/dlt/upgrade"
"github.com/openshift/moactl/pkg/confirm"
)

Expand All @@ -43,4 +44,5 @@ func init() {
Cmd.AddCommand(idp.Cmd)
Cmd.AddCommand(ingress.Cmd)
Cmd.AddCommand(machinepool.Cmd)
Cmd.AddCommand(upgrade.Cmd)
}
148 changes: 148 additions & 0 deletions cmd/dlt/upgrade/cmd.go
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)
}
}
2 changes: 2 additions & 0 deletions cmd/list/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/openshift/moactl/cmd/list/ingress"
"github.com/openshift/moactl/cmd/list/machinepool"
"github.com/openshift/moactl/cmd/list/region"
"github.com/openshift/moactl/cmd/list/upgrade"
"github.com/openshift/moactl/cmd/list/user"
"github.com/openshift/moactl/cmd/list/version"
)
Expand All @@ -42,6 +43,7 @@ func init() {
Cmd.AddCommand(ingress.Cmd)
Cmd.AddCommand(machinepool.Cmd)
Cmd.AddCommand(region.Cmd)
Cmd.AddCommand(upgrade.Cmd)
Cmd.AddCommand(user.Cmd)
Cmd.AddCommand(version.Cmd)
}
175 changes: 175 additions & 0 deletions cmd/list/upgrade/cmd.go
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
}
2 changes: 2 additions & 0 deletions cmd/rosa/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"github.com/openshift/moactl/cmd/logout"
"github.com/openshift/moactl/cmd/logs"
"github.com/openshift/moactl/cmd/revoke"
"github.com/openshift/moactl/cmd/upgrade"
"github.com/openshift/moactl/cmd/verify"
"github.com/openshift/moactl/cmd/version"
"github.com/openshift/moactl/cmd/whoami"
Expand Down Expand Up @@ -76,6 +77,7 @@ func init() {
root.AddCommand(logout.Cmd)
root.AddCommand(logs.Cmd)
root.AddCommand(revoke.Cmd)
root.AddCommand(upgrade.Cmd)
root.AddCommand(verify.Cmd)
root.AddCommand(version.Cmd)
root.AddCommand(whoami.Cmd)
Expand Down
Loading

0 comments on commit 36a8d95

Please sign in to comment.