Skip to content

Commit

Permalink
Merge pull request #12039 from johngmyers/complete-get
Browse files Browse the repository at this point in the history
Implement completion for "kops get", part two
  • Loading branch information
k8s-ci-robot committed Jul 22, 2021
2 parents a8aa6a9 + 5670d56 commit e014fce
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 80 deletions.
3 changes: 1 addition & 2 deletions cmd/kops/get_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"context"
"fmt"
"io"
"os"
"strings"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -109,7 +108,7 @@ func NewCmdGetCluster(f *util.Factory, out io.Writer, getOptions *GetOptions) *c
},
ValidArgsFunction: commandutils.CompleteClusterName(&rootCommand, false, true),
RunE: func(cmd *cobra.Command, args []string) error {
return RunGetClusters(context.TODO(), &rootCommand, os.Stdout, &options)
return RunGetClusters(context.TODO(), &rootCommand, out, &options)
},
}

Expand Down
60 changes: 31 additions & 29 deletions cmd/kops/get_instancegroups.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ import (
"context"
"fmt"
"io"
"os"
"strconv"

"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/kops/cmd/kops/util"
api "k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/commands/commandutils"
"k8s.io/kops/pkg/formatter"
"k8s.io/kops/util/pkg/tables"
"k8s.io/kubectl/pkg/util/i18n"
Expand All @@ -36,24 +36,25 @@ import (

var (
getInstancegroupsLong = templates.LongDesc(i18n.T(`
Display one or many instancegroup resources.`))
Display one or many instance group resources.`))

getInstancegroupsExample = templates.Examples(i18n.T(`
# Get all instancegroups in a state store
kops get ig
# Get all instance groups in a state store
kops get instancegroups
# Get a cluster's instancegroup
kops get ig --name k8s-cluster.example.com nodes
kops get instancegroups --name k8s-cluster.example.com nodes
# Save a cluster's instancegroups desired configuration to YAML file
kops get ig --name k8s-cluster.example.com -o yaml > instancegroups-desired-config.yaml
kops get instancegroups --name k8s-cluster.example.com -o yaml > instancegroups-desired-config.yaml
`))

getInstancegroupsShort = i18n.T(`Get one or many instancegroups`)
getInstancegroupsShort = i18n.T(`Get one or many instance groups.`)
)

type GetInstanceGroupsOptions struct {
*GetOptions
InstanceGroupNames []string
}

func NewCmdGetInstanceGroups(f *util.Factory, out io.Writer, getOptions *GetOptions) *cobra.Command {
Expand All @@ -62,57 +63,58 @@ func NewCmdGetInstanceGroups(f *util.Factory, out io.Writer, getOptions *GetOpti
}

cmd := &cobra.Command{
Use: "instancegroups",
Use: "instancegroups [INSTANCE_GROUP]...",
Aliases: []string{"instancegroup", "ig"},
Short: getInstancegroupsShort,
Long: getInstancegroupsLong,
Example: getInstancegroupsExample,
Run: func(cmd *cobra.Command, args []string) {
ctx := context.TODO()
err := RunGetInstanceGroups(ctx, &options, args)
if err != nil {
exitWithError(err)
Args: func(cmd *cobra.Command, args []string) error {
options.ClusterName = rootCommand.ClusterName(true)
if options.ClusterName == "" {
return fmt.Errorf("--name is required")
}

options.InstanceGroupNames = args
return nil
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return completeInstanceGroup(&args, nil)(cmd, nil, toComplete)
},
RunE: func(cmd *cobra.Command, args []string) error {
return RunGetInstanceGroups(context.TODO(), &rootCommand, out, &options)
},
}

return cmd
}

func RunGetInstanceGroups(ctx context.Context, options *GetInstanceGroupsOptions, args []string) error {
out := os.Stdout

clusterName := rootCommand.ClusterName(true)
if clusterName == "" {
return fmt.Errorf("--name is required")
}

clientset, err := rootCommand.Clientset()
func RunGetInstanceGroups(ctx context.Context, f commandutils.Factory, out io.Writer, options *GetInstanceGroupsOptions) error {
clientset, err := f.Clientset()
if err != nil {
return err
}

cluster, err := clientset.GetCluster(ctx, clusterName)
cluster, err := clientset.GetCluster(ctx, options.ClusterName)
if err != nil {
return fmt.Errorf("error fetching cluster %q: %v", clusterName, err)
return fmt.Errorf("error fetching cluster %q: %v", options.ClusterName, err)
}

if cluster == nil {
return fmt.Errorf("cluster %q was not found", clusterName)
return fmt.Errorf("cluster %q was not found", options.ClusterName)
}

list, err := clientset.InstanceGroupsFor(cluster).List(ctx, metav1.ListOptions{})
if err != nil {
return err
}

instancegroups, err := filterInstanceGroupsByName(args, list.Items)
instancegroups, err := filterInstanceGroupsByName(options.InstanceGroupNames, list.Items)
if err != nil {
return err
}

if len(instancegroups) == 0 {
return fmt.Errorf("No InstanceGroup objects found")
return fmt.Errorf("no InstanceGroup objects found")
}

var obj []runtime.Object
Expand All @@ -130,7 +132,7 @@ func RunGetInstanceGroups(ctx context.Context, options *GetInstanceGroupsOptions
case OutputJSON:
return fullOutputJSON(out, obj...)
default:
return fmt.Errorf("Unknown output format: %q", options.Output)
return fmt.Errorf("unknown output format: %q", options.Output)
}
}

Expand Down Expand Up @@ -181,7 +183,7 @@ func igOutputTable(cluster *api.Cluster, instancegroups []*api.InstanceGroup, ou
return int32PointerToString(c.Spec.MaxSize)
})
// SUBNETS is not selected by default - not as useful as ZONES
return t.Render(instancegroups, os.Stdout, "NAME", "ROLE", "MACHINETYPE", "MIN", "MAX", "ZONES")
return t.Render(instancegroups, out, "NAME", "ROLE", "MACHINETYPE", "MIN", "MAX", "ZONES")
}

func int32PointerToString(v *int32) string {
Expand Down
43 changes: 14 additions & 29 deletions cmd/kops/get_instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"strings"

"k8s.io/kops/pkg/cloudinstances"
"k8s.io/kops/pkg/commands/commandutils"
"k8s.io/kubectl/pkg/util/i18n"
"k8s.io/kubectl/pkg/util/templates"

Expand All @@ -43,52 +44,36 @@ import (
"k8s.io/kops/upup/pkg/fi/cloudup"
)

func NewCmdGetInstances(f *util.Factory, out io.Writer, options *GetOptions) *cobra.Command {
getInstancesShort := i18n.T(`Display cluster instances.`)

getInstancesLong := templates.LongDesc(i18n.T(`
Display cluster instances.`))

getInstancesExample := templates.Examples(i18n.T(`
var (
getInstancesExample = templates.Examples(i18n.T(`
# Display all instances.
kops get instances
`))

getInstancesShort = i18n.T(`Display cluster instances.`)
)

func NewCmdGetInstances(f *util.Factory, out io.Writer, options *GetOptions) *cobra.Command {
cmd := &cobra.Command{
Use: "instances",
Short: getInstancesShort,
Long: getInstancesLong,
Example: getInstancesExample,
Run: func(cmd *cobra.Command, args []string) {
ctx := context.TODO()

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

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

return cmd
}

func RunGetInstances(ctx context.Context, f *util.Factory, out io.Writer, options *GetOptions) error {

clientset, err := f.Clientset()
if err != nil {
return err
}

clusterName := rootCommand.ClusterName(true)
options.ClusterName = clusterName
if clusterName == "" {
return fmt.Errorf("--name is required")
}

cluster, err := clientset.GetCluster(ctx, options.ClusterName)
if err != nil {
return err
Expand Down
23 changes: 16 additions & 7 deletions cmd/kops/get_keypairs.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ var (

type GetKeypairsOptions struct {
*GetOptions
Distrusted bool
KeysetNames []string
Distrusted bool
}

func NewCmdGetKeypairs(f *util.Factory, out io.Writer, getOptions *GetOptions) *cobra.Command {
Expand All @@ -58,12 +59,20 @@ func NewCmdGetKeypairs(f *util.Factory, out io.Writer, getOptions *GetOptions) *
Aliases: []string{"keypair"},
Short: getKeypairShort,
Example: getKeypairExample,
Args: func(cmd *cobra.Command, args []string) error {
options.ClusterName = rootCommand.ClusterName(true)
if options.ClusterName == "" {
return fmt.Errorf("--name is required")
}

options.KeysetNames = args
return nil
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return completeGetKeypairs(options, args, toComplete)
},
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.TODO()
return RunGetKeypairs(ctx, out, options, args)
return RunGetKeypairs(context.TODO(), &rootCommand, out, options)
},
}

Expand Down Expand Up @@ -120,13 +129,13 @@ func listKeypairs(keyStore fi.CAStore, names []string, includeDistrusted bool) (
return items, nil
}

func RunGetKeypairs(ctx context.Context, out io.Writer, options *GetKeypairsOptions, args []string) error {
cluster, err := rootCommand.Cluster(ctx)
func RunGetKeypairs(ctx context.Context, f commandutils.Factory, out io.Writer, options *GetKeypairsOptions) error {
clientset, err := f.Clientset()
if err != nil {
return err
}

clientset, err := rootCommand.Clientset()
cluster, err := clientset.GetCluster(ctx, options.ClusterName)
if err != nil {
return err
}
Expand All @@ -136,7 +145,7 @@ func RunGetKeypairs(ctx context.Context, out io.Writer, options *GetKeypairsOpti
return err
}

items, err := listKeypairs(keyStore, args, options.Distrusted)
items, err := listKeypairs(keyStore, options.KeysetNames, options.Distrusted)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion docs/cli/kops_get.md

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

14 changes: 7 additions & 7 deletions docs/cli/kops_get_instancegroups.md

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

6 changes: 1 addition & 5 deletions docs/cli/kops_get_instances.md

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

0 comments on commit e014fce

Please sign in to comment.