mirror of https://github.com/kubernetes/kops.git
Merge pull request #14532 from johngmyers/kops-get
Add "kops get all" command
This commit is contained in:
commit
c592a02bb4
126
cmd/kops/get.go
126
cmd/kops/get.go
|
@ -20,36 +20,14 @@ import (
|
|||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/klog/v2"
|
||||
"k8s.io/kops/cmd/kops/util"
|
||||
api "k8s.io/kops/pkg/apis/kops"
|
||||
"k8s.io/kops/pkg/commands/commandutils"
|
||||
"k8s.io/kops/pkg/kopscodecs"
|
||||
"k8s.io/kubectl/pkg/util/i18n"
|
||||
"k8s.io/kubectl/pkg/util/templates"
|
||||
)
|
||||
|
||||
var (
|
||||
getLong = templates.LongDesc(i18n.T(`
|
||||
Display one or many resources.` + validResources))
|
||||
|
||||
getExample = templates.Examples(i18n.T(`
|
||||
# Get a cluster and its instance groups
|
||||
kops get k8s-cluster.example.com
|
||||
|
||||
# Get a cluster and its instancegroups' YAML desired configuration
|
||||
kops get k8s-cluster.example.com -o yaml
|
||||
|
||||
# Save a cluster and its instancegroups' desired configuration to YAML file
|
||||
kops get k8s-cluster.example.com -o yaml > cluster-desired-config.yaml
|
||||
`))
|
||||
|
||||
getShort = i18n.T(`Get one or many resources.`)
|
||||
)
|
||||
|
||||
type GetOptions struct {
|
||||
|
@ -69,13 +47,10 @@ func NewCmdGet(f *util.Factory, out io.Writer) *cobra.Command {
|
|||
}
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "get",
|
||||
SuggestFor: []string{"list"},
|
||||
Short: getShort,
|
||||
Long: getLong,
|
||||
Example: getExample,
|
||||
Args: rootCommand.clusterNameArgs(&options.ClusterName),
|
||||
ValidArgsFunction: commandutils.CompleteClusterName(f, true, false),
|
||||
Use: "get",
|
||||
SuggestFor: []string{"list"},
|
||||
Short: i18n.T(`Get one or many resources.`),
|
||||
Args: rootCommand.clusterNameArgs(&options.ClusterName),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return RunGet(context.TODO(), f, out, options)
|
||||
},
|
||||
|
@ -87,6 +62,7 @@ func NewCmdGet(f *util.Factory, out io.Writer) *cobra.Command {
|
|||
})
|
||||
|
||||
// create subcommands
|
||||
cmd.AddCommand(NewCmdGetAll(f, out, options))
|
||||
cmd.AddCommand(NewCmdGetAssets(f, out, options))
|
||||
cmd.AddCommand(NewCmdGetCluster(f, out, options))
|
||||
cmd.AddCommand(NewCmdGetInstanceGroups(f, out, options))
|
||||
|
@ -99,94 +75,8 @@ func NewCmdGet(f *util.Factory, out io.Writer) *cobra.Command {
|
|||
}
|
||||
|
||||
func RunGet(ctx context.Context, f commandutils.Factory, out io.Writer, options *GetOptions) error {
|
||||
client, err := f.KopsClient()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cluster, err := client.GetCluster(ctx, options.ClusterName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if cluster == nil {
|
||||
return fmt.Errorf("No cluster found")
|
||||
}
|
||||
|
||||
igList, err := client.InstanceGroupsFor(cluster).List(ctx, metav1.ListOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if igList == nil || igList.Items == nil || len(igList.Items) == 0 {
|
||||
fmt.Fprintf(os.Stderr, "No instance groups found\n")
|
||||
}
|
||||
|
||||
var instancegroups []*api.InstanceGroup
|
||||
for i := range igList.Items {
|
||||
instancegroups = append(instancegroups, &igList.Items[i])
|
||||
}
|
||||
|
||||
var addonObjects []*unstructured.Unstructured
|
||||
{
|
||||
addons, err := client.AddonsFor(cluster).List()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, addon := range addons {
|
||||
addonObjects = append(addonObjects, addon.ToUnstructured())
|
||||
}
|
||||
}
|
||||
|
||||
var allObjects []runtime.Object
|
||||
if options.Output != OutputTable {
|
||||
allObjects = append(allObjects, cluster)
|
||||
for _, group := range instancegroups {
|
||||
allObjects = append(allObjects, group)
|
||||
}
|
||||
for _, additionalObject := range addonObjects {
|
||||
allObjects = append(allObjects, additionalObject)
|
||||
}
|
||||
}
|
||||
|
||||
switch options.Output {
|
||||
case OutputYaml:
|
||||
if err := fullOutputYAML(out, allObjects...); err != nil {
|
||||
return fmt.Errorf("error writing yaml to stdout: %v", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
case OutputJSON:
|
||||
if err := fullOutputJSON(out, false, allObjects...); err != nil {
|
||||
return fmt.Errorf("error writing json to stdout: %v", err)
|
||||
}
|
||||
return nil
|
||||
|
||||
case OutputTable:
|
||||
fmt.Fprintf(out, "Cluster\n")
|
||||
err = clusterOutputTable([]*api.Cluster{cluster}, out)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(out, "\nInstance Groups\n")
|
||||
err = igOutputTable(cluster, instancegroups, out)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(addonObjects) != 0 {
|
||||
fmt.Fprintf(out, "\nAddon Objects\n")
|
||||
err = addonsOutputTable(cluster, addonObjects, out)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
default:
|
||||
return fmt.Errorf("Unknown output format: %q", options.Output)
|
||||
}
|
||||
|
||||
return nil
|
||||
klog.Warning("`kops get [CLUSTER]` is deprecated: use `kops get all [CLUSTER]`")
|
||||
return RunGetAll(ctx, f, out, &GetAllOptions{options})
|
||||
}
|
||||
|
||||
func writeYAMLSep(out io.Writer) error {
|
||||
|
|
|
@ -0,0 +1,164 @@
|
|||
/*
|
||||
Copyright 2019 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
"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/kubectl/pkg/util/i18n"
|
||||
"k8s.io/kubectl/pkg/util/templates"
|
||||
)
|
||||
|
||||
var (
|
||||
getAllLong = templates.LongDesc(i18n.T(`
|
||||
Display all resources for a cluster.`))
|
||||
|
||||
getAllExample = templates.Examples(i18n.T(`
|
||||
# Get a cluster, its instance groups, and its addons
|
||||
kops get all k8s-cluster.example.com
|
||||
|
||||
# Get a cluster, its instance groups, and its addons in YAML format
|
||||
kops get all k8s-cluster.example.com -o yaml
|
||||
`))
|
||||
|
||||
getAllShort = i18n.T(`Display all resources for a cluster.`)
|
||||
)
|
||||
|
||||
type GetAllOptions struct {
|
||||
*GetOptions
|
||||
}
|
||||
|
||||
func NewCmdGetAll(f *util.Factory, out io.Writer, getOptions *GetOptions) *cobra.Command {
|
||||
options := &GetAllOptions{
|
||||
GetOptions: getOptions,
|
||||
}
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "all [CLUSTER]",
|
||||
Short: getAllShort,
|
||||
Long: getAllLong,
|
||||
Example: getAllExample,
|
||||
Args: rootCommand.clusterNameArgs(&options.ClusterName),
|
||||
ValidArgsFunction: commandutils.CompleteClusterName(f, true, false),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return RunGetAll(context.TODO(), f, out, options)
|
||||
},
|
||||
}
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
func RunGetAll(ctx context.Context, f commandutils.Factory, out io.Writer, options *GetAllOptions) error {
|
||||
client, err := f.KopsClient()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cluster, err := client.GetCluster(ctx, options.ClusterName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if cluster == nil {
|
||||
return fmt.Errorf("No cluster found")
|
||||
}
|
||||
|
||||
igList, err := client.InstanceGroupsFor(cluster).List(ctx, metav1.ListOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if igList == nil || igList.Items == nil || len(igList.Items) == 0 {
|
||||
fmt.Fprintf(os.Stderr, "No instance groups found\n")
|
||||
}
|
||||
|
||||
var instancegroups []*api.InstanceGroup
|
||||
for i := range igList.Items {
|
||||
instancegroups = append(instancegroups, &igList.Items[i])
|
||||
}
|
||||
|
||||
var addonObjects []*unstructured.Unstructured
|
||||
{
|
||||
addons, err := client.AddonsFor(cluster).List()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, addon := range addons {
|
||||
addonObjects = append(addonObjects, addon.ToUnstructured())
|
||||
}
|
||||
}
|
||||
|
||||
var allObjects []runtime.Object
|
||||
if options.Output != OutputTable {
|
||||
allObjects = append(allObjects, cluster)
|
||||
for _, group := range instancegroups {
|
||||
allObjects = append(allObjects, group)
|
||||
}
|
||||
for _, additionalObject := range addonObjects {
|
||||
allObjects = append(allObjects, additionalObject)
|
||||
}
|
||||
}
|
||||
|
||||
switch options.Output {
|
||||
case OutputYaml:
|
||||
if err := fullOutputYAML(out, allObjects...); err != nil {
|
||||
return fmt.Errorf("error writing yaml to stdout: %v", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
case OutputJSON:
|
||||
if err := fullOutputJSON(out, false, allObjects...); err != nil {
|
||||
return fmt.Errorf("error writing json to stdout: %v", err)
|
||||
}
|
||||
return nil
|
||||
|
||||
case OutputTable:
|
||||
fmt.Fprintf(out, "Cluster\n")
|
||||
err = clusterOutputTable([]*api.Cluster{cluster}, out)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(out, "\nInstance Groups\n")
|
||||
err = igOutputTable(cluster, instancegroups, out)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(addonObjects) != 0 {
|
||||
fmt.Fprintf(out, "\nAddon Objects\n")
|
||||
err = addonsOutputTable(cluster, addonObjects, out)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
default:
|
||||
return fmt.Errorf("Unknown output format: %q", options.Output)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
|
@ -5,31 +5,10 @@
|
|||
|
||||
Get one or many resources.
|
||||
|
||||
### Synopsis
|
||||
|
||||
Display one or many resources.
|
||||
|
||||
* cluster
|
||||
* instancegroup
|
||||
* secret
|
||||
|
||||
```
|
||||
kops get [flags]
|
||||
```
|
||||
|
||||
### Examples
|
||||
|
||||
```
|
||||
# Get a cluster and its instance groups
|
||||
kops get k8s-cluster.example.com
|
||||
|
||||
# Get a cluster and its instancegroups' YAML desired configuration
|
||||
kops get k8s-cluster.example.com -o yaml
|
||||
|
||||
# Save a cluster and its instancegroups' desired configuration to YAML file
|
||||
kops get k8s-cluster.example.com -o yaml > cluster-desired-config.yaml
|
||||
```
|
||||
|
||||
### Options
|
||||
|
||||
```
|
||||
|
@ -49,6 +28,7 @@ kops get [flags]
|
|||
### SEE ALSO
|
||||
|
||||
* [kops](kops.md) - kOps is Kubernetes Operations.
|
||||
* [kops get all](kops_get_all.md) - Display all resources for a cluster.
|
||||
* [kops get assets](kops_get_assets.md) - Display assets for cluster.
|
||||
* [kops get clusters](kops_get_clusters.md) - Get one or many clusters.
|
||||
* [kops get instancegroups](kops_get_instancegroups.md) - Get one or many instance groups.
|
||||
|
|
|
@ -0,0 +1,45 @@
|
|||
|
||||
<!--- This file is automatically generated by make gen-cli-docs; changes should be made in the go CLI command code (under cmd/kops) -->
|
||||
|
||||
## kops get all
|
||||
|
||||
Display all resources for a cluster.
|
||||
|
||||
### Synopsis
|
||||
|
||||
Display all resources for a cluster.
|
||||
|
||||
```
|
||||
kops get all [CLUSTER] [flags]
|
||||
```
|
||||
|
||||
### Examples
|
||||
|
||||
```
|
||||
# Get a cluster, its instance groups, and its addons
|
||||
kops get all k8s-cluster.example.com
|
||||
|
||||
# Get a cluster, its instance groups, and its addons in YAML format
|
||||
kops get all k8s-cluster.example.com -o yaml
|
||||
```
|
||||
|
||||
### Options
|
||||
|
||||
```
|
||||
-h, --help help for all
|
||||
```
|
||||
|
||||
### Options inherited from parent commands
|
||||
|
||||
```
|
||||
--config string yaml config file (default is $HOME/.kops.yaml)
|
||||
--name string Name of cluster. Overrides KOPS_CLUSTER_NAME environment variable
|
||||
-o, --output string output format. One of: table, yaml, json (default "table")
|
||||
--state string Location of state storage (kops 'config' file). Overrides KOPS_STATE_STORE environment variable
|
||||
-v, --v Level number for the log level verbosity
|
||||
```
|
||||
|
||||
### SEE ALSO
|
||||
|
||||
* [kops get](kops_get.md) - Get one or many resources.
|
||||
|
|
@ -27,6 +27,8 @@ CNIs, use the "cni" networking option instead.
|
|||
|
||||
# Deprecations
|
||||
|
||||
* The "kops get [CLUSTER]" command is deprecated. It is replaced by "kops get all [CLUSTER]".
|
||||
|
||||
* Support for Kubernetes version 1.21 is deprecated and will be removed in kOps 1.27.
|
||||
|
||||
* Support for Kubernetes version 1.22 is deprecated and will be removed in kOps 1.28.
|
||||
|
|
Loading…
Reference in New Issue