mirror of https://github.com/kubernetes/kops.git
Implement completion for "kops validate cluster"
This commit is contained in:
parent
a5533a058e
commit
dedf53fd16
|
@ -21,31 +21,12 @@ import (
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"k8s.io/kops/cmd/kops/util"
|
"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 {
|
func NewCmdValidate(f *util.Factory, out io.Writer) *cobra.Command {
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "validate",
|
Use: "validate",
|
||||||
Short: validateShort,
|
Short: validateClusterShort,
|
||||||
Long: validateLong,
|
|
||||||
Example: validateExample,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// create subcommands
|
// create subcommands
|
||||||
|
|
|
@ -25,6 +25,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"k8s.io/kops/pkg/commands/commandutils"
|
||||||
"k8s.io/kops/upup/pkg/fi/cloudup"
|
"k8s.io/kops/upup/pkg/fi/cloudup"
|
||||||
"k8s.io/kubectl/pkg/util/i18n"
|
"k8s.io/kubectl/pkg/util/i18n"
|
||||||
"k8s.io/kubectl/pkg/util/templates"
|
"k8s.io/kubectl/pkg/util/templates"
|
||||||
|
@ -42,11 +43,30 @@ import (
|
||||||
"sigs.k8s.io/yaml"
|
"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 {
|
type ValidateClusterOptions struct {
|
||||||
output string
|
ClusterName string
|
||||||
wait time.Duration
|
output string
|
||||||
count int
|
wait time.Duration
|
||||||
kubeconfig string
|
count int
|
||||||
|
kubeconfig string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *ValidateClusterOptions) InitDefaults() {
|
func (o *ValidateClusterOptions) InitDefaults() {
|
||||||
|
@ -57,50 +77,46 @@ func NewCmdValidateCluster(f *util.Factory, out io.Writer) *cobra.Command {
|
||||||
options := &ValidateClusterOptions{}
|
options := &ValidateClusterOptions{}
|
||||||
options.InitDefaults()
|
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{
|
cmd := &cobra.Command{
|
||||||
Use: "cluster",
|
Use: "cluster [CLUSTER]",
|
||||||
Short: validateShort,
|
Short: validateClusterShort,
|
||||||
Long: validateClusterLong,
|
Long: validateClusterLong,
|
||||||
Example: validateExample,
|
Example: validateClusterExample,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Args: rootCommand.clusterNameArgs(&options.ClusterName),
|
||||||
ctx := context.TODO()
|
ValidArgsFunction: commandutils.CompleteClusterName(&rootCommand, true),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
result, err := RunValidateCluster(ctx, f, cmd, args, os.Stdout, options)
|
result, err := RunValidateCluster(context.TODO(), f, out, options)
|
||||||
if err != nil {
|
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,
|
// 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.
|
// even if we didn't really hit an error during validation.
|
||||||
if len(result.Failures) != 0 {
|
if len(result.Failures) != 0 {
|
||||||
os.Exit(2)
|
os.Exit(2)
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.Flags().StringVarP(&options.output, "output", "o", options.output, "Output format. One of json|yaml|table.")
|
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.RegisterFlagCompletionFunc("output", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
||||||
cmd.Flags().IntVar(&options.count, "count", options.count, "If set, will validate the cluster consecutive times")
|
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")
|
cmd.Flags().StringVar(&options.kubeconfig, "kubeconfig", "", "Path to the kubeconfig file")
|
||||||
|
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func RunValidateCluster(ctx context.Context, f *util.Factory, cmd *cobra.Command, args []string, out io.Writer, options *ValidateClusterOptions) (*validation.ValidationCluster, error) {
|
func RunValidateCluster(ctx context.Context, f *util.Factory, out io.Writer, options *ValidateClusterOptions) (*validation.ValidationCluster, error) {
|
||||||
err := rootCommand.ProcessArgs(args)
|
clientSet, err := f.Clientset()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
cluster, err := rootCommand.Cluster(ctx)
|
cluster, err := GetCluster(ctx, f, options.ClusterName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -110,11 +126,6 @@ func RunValidateCluster(ctx context.Context, f *util.Factory, cmd *cobra.Command
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
clientSet, err := f.Clientset()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
list, err := clientSet.InstanceGroupsFor(cluster).List(ctx, metav1.ListOptions{})
|
list, err := clientSet.InstanceGroupsFor(cluster).List(ctx, metav1.ListOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("cannot get InstanceGroups for %q: %v", cluster.ObjectMeta.Name, err)
|
return nil, fmt.Errorf("cannot get InstanceGroups for %q: %v", cluster.ObjectMeta.Name, err)
|
||||||
|
|
|
@ -5,18 +5,6 @@
|
||||||
|
|
||||||
Validate a kOps cluster.
|
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
|
### Options
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
|
@ -15,7 +15,7 @@ This commands validates the following components:
|
||||||
4. All pods with a critical priority are running and have "Ready" status.
|
4. All pods with a critical priority are running and have "Ready" status.
|
||||||
|
|
||||||
```
|
```
|
||||||
kops validate cluster [flags]
|
kops validate cluster [CLUSTER] [flags]
|
||||||
```
|
```
|
||||||
|
|
||||||
### Examples
|
### Examples
|
||||||
|
@ -29,11 +29,11 @@ kops validate cluster [flags]
|
||||||
### Options
|
### Options
|
||||||
|
|
||||||
```
|
```
|
||||||
--count int If set, will validate the cluster consecutive times
|
--count int Number of consecutive successful validations required
|
||||||
-h, --help help for cluster
|
-h, --help help for cluster
|
||||||
--kubeconfig string Path to the kubeconfig file
|
--kubeconfig string Path to the kubeconfig file
|
||||||
-o, --output string Output format. One of json|yaml|table. (default "table")
|
-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
|
### Options inherited from parent commands
|
||||||
|
|
Loading…
Reference in New Issue