Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement completion for "kops toolbox", part one #11992

Merged
merged 3 commits into from
Jul 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd/kops/BUILD.bazel

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 3 additions & 14 deletions cmd/kops/toolbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,16 @@ import (
"github.com/spf13/cobra"
"k8s.io/kops/cmd/kops/util"
"k8s.io/kubectl/pkg/util/i18n"
"k8s.io/kubectl/pkg/util/templates"
)

var (
toolboxLong = templates.LongDesc(i18n.T(`
Misc infrequently used commands.`))

toolboxExample = templates.Examples(i18n.T(`
# Dump cluster information
kops toolbox dump --name k8s-cluster.example.com
`))

toolboxShort = i18n.T(`Misc infrequently used commands.`)
toolboxShort = i18n.T(`Miscellaneous, infrequently used commands.`)
)

func NewCmdToolbox(f *util.Factory, out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "toolbox",
Short: toolboxShort,
Long: toolboxLong,
Example: toolboxExample,
Use: "toolbox",
Short: toolboxShort,
}

cmd.AddCommand(NewCmdToolboxDump(f, out))
Expand Down
50 changes: 19 additions & 31 deletions cmd/kops/toolbox_dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"k8s.io/klog/v2"
"k8s.io/kops/cmd/kops/util"
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/commands/commandutils"
"k8s.io/kops/pkg/dump"
"k8s.io/kops/pkg/resources"
resourceops "k8s.io/kops/pkg/resources/ops"
Expand Down Expand Up @@ -76,31 +77,27 @@ func NewCmdToolboxDump(f *util.Factory, out io.Writer) *cobra.Command {
options.InitDefaults()

cmd := &cobra.Command{
Use: "dump",
Short: toolboxDumpShort,
Long: toolboxDumpLong,
Example: toolboxDumpExample,
Run: func(cmd *cobra.Command, args []string) {
ctx := context.TODO()

if err := rootCommand.ProcessArgs(args); err != nil {
exitWithError(err)
}

options.ClusterName = rootCommand.ClusterName(true)

err := RunToolboxDump(ctx, f, out, options)
if err != nil {
exitWithError(err)
}
Use: "dump [CLUSTER]",
Short: toolboxDumpShort,
Long: toolboxDumpLong,
Example: toolboxDumpExample,
Args: rootCommand.clusterNameArgs(&options.ClusterName),
ValidArgsFunction: commandutils.CompleteClusterName(&rootCommand, true),
RunE: func(cmd *cobra.Command, args []string) error {
return RunToolboxDump(context.TODO(), f, out, options)
},
}

cmd.Flags().StringVarP(&options.Output, "output", "o", options.Output, "output format. One of: yaml, json")
cmd.Flags().StringVarP(&options.Output, "output", "o", options.Output, "Output format. One of json or yaml")
cmd.RegisterFlagCompletionFunc("output", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return []string{"json", "yaml"}, cobra.ShellCompDirectiveNoFileComp
})

cmd.Flags().StringVar(&options.Dir, "dir", options.Dir, "target directory; if specified will collect logs and other information.")
cmd.Flags().StringVar(&options.PrivateKey, "private-key", options.PrivateKey, "private key to use for SSH acccess to instances")
cmd.Flags().StringVar(&options.SSHUser, "ssh-user", options.SSHUser, "the remote user for SSH access to instances")
cmd.Flags().StringVar(&options.Dir, "dir", options.Dir, "Target directory; if specified will collect logs and other information.")
cmd.MarkFlagDirname("dir")
cmd.Flags().StringVar(&options.PrivateKey, "private-key", options.PrivateKey, "File containing private key to use for SSH access to instances")
cmd.Flags().StringVar(&options.SSHUser, "ssh-user", options.SSHUser, "The remote user for SSH access to instances")
cmd.RegisterFlagCompletionFunc("ssh-user", cobra.NoFileCompletions)

return cmd
}
Expand All @@ -112,10 +109,6 @@ func RunToolboxDump(ctx context.Context, f *util.Factory, out io.Writer, options
return err
}

if options.ClusterName == "" {
return fmt.Errorf("ClusterName is required")
}

cluster, err := clientset.GetCluster(ctx, options.ClusterName)
if err != nil {
return err
Expand Down Expand Up @@ -155,11 +148,6 @@ func RunToolboxDump(ctx context.Context, f *util.Factory, out io.Writer, options
return fmt.Errorf("error parsing private key %q: %v", privateKeyPath, err)
}

cluster, err := GetCluster(ctx, f, options.ClusterName)
if err != nil {
return err
}

contextName := cluster.ObjectMeta.Name
clientGetter := genericclioptions.NewConfigFlags(true)
clientGetter.Context = &contextName
Expand All @@ -168,7 +156,7 @@ func RunToolboxDump(ctx context.Context, f *util.Factory, out io.Writer, options

config, err := clientGetter.ToRESTConfig()
if err != nil {
klog.Warningf("cannot load kubecfg settings for %q: %v", contextName, err)
klog.Warningf("cannot load kubeconfig settings for %q: %v", contextName, err)
} else {
k8sClient, err := kubernetes.NewForConfig(config)
if err != nil {
Expand Down
Loading