...
1
16
17 package config
18
19 import (
20 "fmt"
21 "io"
22
23 "github.com/spf13/cobra"
24 "k8s.io/client-go/tools/clientcmd"
25 cmdutil "k8s.io/kubectl/pkg/cmd/util"
26 "k8s.io/kubectl/pkg/util/i18n"
27 "k8s.io/kubectl/pkg/util/templates"
28 )
29
30 var (
31 getClustersExample = templates.Examples(`
32 # List the clusters that kubectl knows about
33 kubectl config get-clusters`)
34 )
35
36
37
38 func NewCmdConfigGetClusters(out io.Writer, configAccess clientcmd.ConfigAccess) *cobra.Command {
39 cmd := &cobra.Command{
40 Use: "get-clusters",
41 Short: i18n.T("Display clusters defined in the kubeconfig"),
42 Long: i18n.T("Display clusters defined in the kubeconfig."),
43 Example: getClustersExample,
44 Run: func(cmd *cobra.Command, args []string) {
45 cmdutil.CheckErr(runGetClusters(out, configAccess))
46 },
47 }
48
49 return cmd
50 }
51
52 func runGetClusters(out io.Writer, configAccess clientcmd.ConfigAccess) error {
53 config, err := configAccess.GetStartingConfig()
54 if err != nil {
55 return err
56 }
57
58 fmt.Fprintf(out, "NAME\n")
59 for name := range config.Clusters {
60 fmt.Fprintf(out, "%s\n", name)
61 }
62
63 return nil
64 }
65
View as plain text