get_clusters.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package config
  14. import (
  15. "fmt"
  16. "io"
  17. "github.com/spf13/cobra"
  18. "k8s.io/kubernetes/pkg/client/unversioned/clientcmd"
  19. cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
  20. )
  21. // NewCmdConfigGetClusters creates a command object for the "get-clusters" action, which
  22. // lists all clusters defined in the kubeconfig.
  23. func NewCmdConfigGetClusters(out io.Writer, configAccess clientcmd.ConfigAccess) *cobra.Command {
  24. cmd := &cobra.Command{
  25. Use: "get-clusters",
  26. Short: "Display clusters defined in the kubeconfig",
  27. Run: func(cmd *cobra.Command, args []string) {
  28. err := runGetClusters(out, configAccess)
  29. cmdutil.CheckErr(err)
  30. },
  31. }
  32. return cmd
  33. }
  34. func runGetClusters(out io.Writer, configAccess clientcmd.ConfigAccess) error {
  35. config, err := configAccess.GetStartingConfig()
  36. if err != nil {
  37. return err
  38. }
  39. fmt.Fprintf(out, "NAME\n")
  40. for name := range config.Clusters {
  41. fmt.Fprintf(out, "%s\n", name)
  42. }
  43. return nil
  44. }