Implement completion for "kops upgrade cluster"

This commit is contained in:
John Gardiner Myers 2021-07-05 11:01:57 -07:00
parent dedf53fd16
commit abf8e268cd
5 changed files with 64 additions and 67 deletions

View File

@ -154,6 +154,7 @@ func NewCmdRoot(f *util.Factory, out io.Writer) *cobra.Command {
cmd.AddCommand(NewCmdSet(f, out)) cmd.AddCommand(NewCmdSet(f, out))
cmd.AddCommand(NewCmdToolbox(f, out)) cmd.AddCommand(NewCmdToolbox(f, out))
cmd.AddCommand(NewCmdUnset(f, out)) cmd.AddCommand(NewCmdUnset(f, out))
cmd.AddCommand(NewCmdUpgrade(f, out))
cmd.AddCommand(NewCmdValidate(f, out)) cmd.AddCommand(NewCmdValidate(f, out))
cmd.AddCommand(NewCmdVersion(f, out)) cmd.AddCommand(NewCmdVersion(f, out))

View File

@ -17,34 +17,22 @@ limitations under the License.
package main package main
import ( import (
"io"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"k8s.io/kubectl/pkg/util/i18n" "k8s.io/kops/cmd/kops/util"
"k8s.io/kubectl/pkg/util/templates"
)
var (
upgradeLong = templates.LongDesc(i18n.T(`
Automates checking for and applying Kubernetes updates. This upgrades a cluster to the latest recommended
production ready k8s version. After this command is run, use kops update cluster and kops rolling-update cluster
to finish a cluster upgrade.
`))
upgradeExample = templates.Examples(i18n.T(`
# Upgrade a cluster's Kubernetes version.
kops upgrade cluster k8s-cluster.example.com --yes --state=s3://my-state-store
`))
upgradeShort = i18n.T("Upgrade a kubernetes cluster.")
) )
// upgradeCmd represents the upgrade command // upgradeCmd represents the upgrade command
var upgradeCmd = &cobra.Command{
Use: "upgrade",
Short: upgradeShort,
Long: upgradeLong,
Example: upgradeExample,
}
func init() { func NewCmdUpgrade(f *util.Factory, out io.Writer) *cobra.Command {
rootCommand.AddCommand(upgradeCmd) cmd := &cobra.Command{
Use: "upgrade",
Short: upgradeClusterShort,
}
// create subcommands
cmd.AddCommand(NewCmdUpgradeCluster(f, out))
return cmd
} }

View File

@ -19,6 +19,7 @@ package main
import ( import (
"context" "context"
"fmt" "fmt"
"io"
"os" "os"
"github.com/blang/semver/v4" "github.com/blang/semver/v4"
@ -26,42 +27,65 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog/v2" "k8s.io/klog/v2"
"k8s.io/kops" "k8s.io/kops"
"k8s.io/kops/cmd/kops/util"
kopsapi "k8s.io/kops/pkg/apis/kops" kopsapi "k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/apis/kops/util" kopsutil "k8s.io/kops/pkg/apis/kops/util"
"k8s.io/kops/pkg/commands" "k8s.io/kops/pkg/commands"
"k8s.io/kops/pkg/commands/commandutils"
"k8s.io/kops/pkg/pretty"
"k8s.io/kops/upup/pkg/fi" "k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup" "k8s.io/kops/upup/pkg/fi/cloudup"
"k8s.io/kops/util/pkg/tables" "k8s.io/kops/util/pkg/tables"
"k8s.io/kubectl/pkg/util/i18n"
"k8s.io/kubectl/pkg/util/templates"
) )
type UpgradeClusterCmd struct { var (
Yes bool upgradeClusterLong = templates.LongDesc(i18n.T(`
Automates checking for and applying Kubernetes updates. This upgrades a cluster to the latest recommended
production ready Kubernetes version. After this command is run, use ` + pretty.Bash("kops update cluster") + ` and ` + pretty.Bash("kops rolling-update cluster") + `
to finish a cluster upgrade.
`))
upgradeClusterExample = templates.Examples(i18n.T(`
# Upgrade a cluster's Kubernetes version.
kops upgrade cluster k8s-cluster.example.com --yes --state=s3://my-state-store
`))
upgradeClusterShort = i18n.T("Upgrade a kubernetes cluster.")
)
type UpgradeClusterOptions struct {
ClusterName string
Yes bool
Channel string Channel string
} }
var upgradeCluster UpgradeClusterCmd func NewCmdUpgradeCluster(f *util.Factory, out io.Writer) *cobra.Command {
options := &UpgradeClusterOptions{}
func init() {
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "cluster", Use: "cluster [CLUSTER]",
Short: upgradeShort, Short: upgradeClusterShort,
Long: upgradeLong, Long: upgradeClusterLong,
Example: upgradeExample, Example: upgradeClusterExample,
Run: func(cmd *cobra.Command, args []string) { Args: rootCommand.clusterNameArgs(&options.ClusterName),
ValidArgsFunction: commandutils.CompleteClusterName(&rootCommand, true),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.TODO() ctx := context.TODO()
err := upgradeCluster.Run(ctx, args) return RunUpgradeCluster(ctx, f, out, options)
if err != nil {
exitWithError(err)
}
}, },
} }
cmd.Flags().BoolVarP(&upgradeCluster.Yes, "yes", "y", false, "Apply update") cmd.Flags().BoolVarP(&options.Yes, "yes", "y", false, "Apply update")
cmd.Flags().StringVar(&upgradeCluster.Channel, "channel", "", "Channel to use for upgrade") cmd.Flags().StringVar(&options.Channel, "channel", "", "Channel to use for upgrade")
cmd.RegisterFlagCompletionFunc("channel", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
// TODO implement completion against VFS
return []string{"alpha", "stable"}, cobra.ShellCompDirectiveNoFileComp
})
upgradeCmd.AddCommand(cmd) return cmd
} }
type upgradeAction struct { type upgradeAction struct {
@ -73,13 +97,8 @@ type upgradeAction struct {
apply func() apply func()
} }
func (c *UpgradeClusterCmd) Run(ctx context.Context, args []string) error { func RunUpgradeCluster(ctx context.Context, f *util.Factory, out io.Writer, options *UpgradeClusterOptions) error {
err := rootCommand.ProcessArgs(args) cluster, err := GetCluster(ctx, f, options.ClusterName)
if err != nil {
return err
}
cluster, err := rootCommand.Cluster(ctx)
if err != nil { if err != nil {
return err return err
} }
@ -98,7 +117,7 @@ func (c *UpgradeClusterCmd) Run(ctx context.Context, args []string) error {
return fmt.Errorf("upgrade is not for use with imported clusters.)") return fmt.Errorf("upgrade is not for use with imported clusters.)")
} }
channelLocation := c.Channel channelLocation := options.Channel
if channelLocation == "" { if channelLocation == "" {
channelLocation = cluster.Spec.Channel channelLocation = cluster.Spec.Channel
} }
@ -132,7 +151,7 @@ func (c *UpgradeClusterCmd) Run(ctx context.Context, args []string) error {
var currentKubernetesVersion *semver.Version var currentKubernetesVersion *semver.Version
{ {
sv, err := util.ParseKubernetesVersion(cluster.Spec.KubernetesVersion) sv, err := kopsutil.ParseKubernetesVersion(cluster.Spec.KubernetesVersion)
if err != nil { if err != nil {
klog.Warningf("error parsing KubernetesVersion %q", cluster.Spec.KubernetesVersion) klog.Warningf("error parsing KubernetesVersion %q", cluster.Spec.KubernetesVersion)
} else { } else {
@ -247,13 +266,13 @@ func (c *UpgradeClusterCmd) Run(ctx context.Context, args []string) error {
return a.New return a.New
}) })
err := t.Render(actions, os.Stdout, "ITEM", "PROPERTY", "OLD", "NEW") err := t.Render(actions, out, "ITEM", "PROPERTY", "OLD", "NEW")
if err != nil { if err != nil {
return err return err
} }
} }
if !c.Yes { if !options.Yes {
fmt.Printf("\nMust specify --yes to perform upgrade\n") fmt.Printf("\nMust specify --yes to perform upgrade\n")
return nil return nil
} }

View File

@ -5,17 +5,6 @@
Upgrade a kubernetes cluster. Upgrade a kubernetes cluster.
### Synopsis
Automates checking for and applying Kubernetes updates. This upgrades a cluster to the latest recommended production ready k8s version. After this command is run, use kops update cluster and kops rolling-update cluster to finish a cluster upgrade.
### Examples
```
# Upgrade a cluster's Kubernetes version.
kops upgrade cluster k8s-cluster.example.com --yes --state=s3://my-state-store
```
### Options ### Options
``` ```

View File

@ -7,10 +7,10 @@ Upgrade a kubernetes cluster.
### Synopsis ### Synopsis
Automates checking for and applying Kubernetes updates. This upgrades a cluster to the latest recommended production ready k8s version. After this command is run, use kops update cluster and kops rolling-update cluster to finish a cluster upgrade. Automates checking for and applying Kubernetes updates. This upgrades a cluster to the latest recommended production ready Kubernetes version. After this command is run, usekops update cluster andkops rolling-update cluster to finish a cluster upgrade.
``` ```
kops upgrade cluster [flags] kops upgrade cluster [CLUSTER] [flags]
``` ```
### Examples ### Examples