Implement completion for "kops validate cluster"

This commit is contained in:
John Gardiner Myers 2021-07-05 10:30:48 -07:00
parent a5533a058e
commit dedf53fd16
4 changed files with 48 additions and 68 deletions

View File

@ -21,31 +21,12 @@ import (
"github.com/spf13/cobra"
"k8s.io/kops/cmd/kops/util"
"k8s.io/kubectl/pkg/util/i18n"
"k8s.io/kubectl/pkg/util/templates"
)
var (
validateLong = templates.LongDesc(i18n.T(`
This command validates a cluster.
See:
kops validate cluster -h
`))
validateExample = templates.Examples(i18n.T(`
# Validate the cluster set as the current context of the kube config.
# Kops will try for 10 minutes to validate the cluster 3 times.
kops validate cluster --wait 10m --count 3`))
validateShort = i18n.T(`Validate a kOps cluster.`)
)
func NewCmdValidate(f *util.Factory, out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "validate",
Short: validateShort,
Long: validateLong,
Example: validateExample,
Use: "validate",
Short: validateClusterShort,
}
// create subcommands

View File

@ -25,6 +25,7 @@ import (
"strings"
"time"
"k8s.io/kops/pkg/commands/commandutils"
"k8s.io/kops/upup/pkg/fi/cloudup"
"k8s.io/kubectl/pkg/util/i18n"
"k8s.io/kubectl/pkg/util/templates"
@ -42,11 +43,30 @@ import (
"sigs.k8s.io/yaml"
)
var (
validateClusterLong = templates.LongDesc(i18n.T(`
This commands validates the following components:
1. All control plane nodes are running and have "Ready" status.
2. All worker nodes are running and have "Ready" status.
3. All control plane nodes have the expected pods.
4. All pods with a critical priority are running and have "Ready" status.
`))
validateClusterExample = templates.Examples(i18n.T(`
# Validate the cluster set as the current context of the kube config.
# Kops will try for 10 minutes to validate the cluster 3 times.
kops validate cluster --wait 10m --count 3`))
validateClusterShort = i18n.T(`Validate a kOps cluster.`)
)
type ValidateClusterOptions struct {
output string
wait time.Duration
count int
kubeconfig string
ClusterName string
output string
wait time.Duration
count int
kubeconfig string
}
func (o *ValidateClusterOptions) InitDefaults() {
@ -57,50 +77,46 @@ func NewCmdValidateCluster(f *util.Factory, out io.Writer) *cobra.Command {
options := &ValidateClusterOptions{}
options.InitDefaults()
validateClusterLong := templates.LongDesc(i18n.T(`
This commands validates the following components:
1. All control plane nodes are running and have "Ready" status.
2. All worker nodes are running and have "Ready" status.
3. All control plane nodes have the expected pods.
4. All pods with a critical priority are running and have "Ready" status.
`))
cmd := &cobra.Command{
Use: "cluster",
Short: validateShort,
Long: validateClusterLong,
Example: validateExample,
Run: func(cmd *cobra.Command, args []string) {
ctx := context.TODO()
result, err := RunValidateCluster(ctx, f, cmd, args, os.Stdout, options)
Use: "cluster [CLUSTER]",
Short: validateClusterShort,
Long: validateClusterLong,
Example: validateClusterExample,
Args: rootCommand.clusterNameArgs(&options.ClusterName),
ValidArgsFunction: commandutils.CompleteClusterName(&rootCommand, true),
RunE: func(cmd *cobra.Command, args []string) error {
result, err := RunValidateCluster(context.TODO(), f, out, options)
if err != nil {
exitWithError(fmt.Errorf("Validation failed: %v", err))
return fmt.Errorf("Validation failed: %v", err)
}
// We want the validate command to exit non-zero if validation found a problem,
// even if we didn't really hit an error during validation.
if len(result.Failures) != 0 {
os.Exit(2)
}
return nil
},
}
cmd.Flags().StringVarP(&options.output, "output", "o", options.output, "Output format. One of json|yaml|table.")
cmd.Flags().DurationVar(&options.wait, "wait", options.wait, "If set, will wait for cluster to be ready")
cmd.Flags().IntVar(&options.count, "count", options.count, "If set, will validate the cluster consecutive times")
cmd.RegisterFlagCompletionFunc("output", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return []string{"json", "yaml", "table"}, cobra.ShellCompDirectiveNoFileComp
})
cmd.Flags().DurationVar(&options.wait, "wait", options.wait, "Amount of time to wait for the cluster to become ready")
cmd.Flags().IntVar(&options.count, "count", options.count, "Number of consecutive successful validations required")
cmd.Flags().StringVar(&options.kubeconfig, "kubeconfig", "", "Path to the kubeconfig file")
return cmd
}
func RunValidateCluster(ctx context.Context, f *util.Factory, cmd *cobra.Command, args []string, out io.Writer, options *ValidateClusterOptions) (*validation.ValidationCluster, error) {
err := rootCommand.ProcessArgs(args)
func RunValidateCluster(ctx context.Context, f *util.Factory, out io.Writer, options *ValidateClusterOptions) (*validation.ValidationCluster, error) {
clientSet, err := f.Clientset()
if err != nil {
return nil, err
}
cluster, err := rootCommand.Cluster(ctx)
cluster, err := GetCluster(ctx, f, options.ClusterName)
if err != nil {
return nil, err
}
@ -110,11 +126,6 @@ func RunValidateCluster(ctx context.Context, f *util.Factory, cmd *cobra.Command
return nil, err
}
clientSet, err := f.Clientset()
if err != nil {
return nil, err
}
list, err := clientSet.InstanceGroupsFor(cluster).List(ctx, metav1.ListOptions{})
if err != nil {
return nil, fmt.Errorf("cannot get InstanceGroups for %q: %v", cluster.ObjectMeta.Name, err)

View File

@ -5,18 +5,6 @@
Validate a kOps cluster.
### Synopsis
This command validates a cluster. See: kops validate cluster -h
### Examples
```
# Validate the cluster set as the current context of the kube config.
# Kops will try for 10 minutes to validate the cluster 3 times.
kops validate cluster --wait 10m --count 3
```
### Options
```

View File

@ -15,7 +15,7 @@ This commands validates the following components:
4. All pods with a critical priority are running and have "Ready" status.
```
kops validate cluster [flags]
kops validate cluster [CLUSTER] [flags]
```
### Examples
@ -29,11 +29,11 @@ kops validate cluster [flags]
### Options
```
--count int If set, will validate the cluster consecutive times
--count int Number of consecutive successful validations required
-h, --help help for cluster
--kubeconfig string Path to the kubeconfig file
-o, --output string Output format. One of json|yaml|table. (default "table")
--wait duration If set, will wait for cluster to be ready
--wait duration Amount of time to wait for the cluster to become ready
```
### Options inherited from parent commands