cluster.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package cmd
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "text/tabwriter"
  7. "github.com/porter-dev/porter/cli/cmd/api"
  8. "github.com/spf13/cobra"
  9. )
  10. // clusterCmd represents the "porter cluster" base command when called
  11. // without any subcommands
  12. var clusterCmd = &cobra.Command{
  13. Use: "cluster",
  14. Short: "Commands that read from a connected cluster",
  15. }
  16. var listClusterNSCmd = &cobra.Command{
  17. Use: "namespace list",
  18. Short: "Lists the namespaces in a cluster (used for testing connection)",
  19. Run: func(cmd *cobra.Command, args []string) {
  20. err := checkLoginAndRun(args, listNamespaces)
  21. if err != nil {
  22. os.Exit(1)
  23. }
  24. },
  25. }
  26. func init() {
  27. rootCmd.AddCommand(clusterCmd)
  28. clusterCmd.PersistentFlags().UintVar(
  29. &clusterID,
  30. "cluster-id",
  31. getClusterID(),
  32. "id of the cluster",
  33. )
  34. clusterCmd.AddCommand(listClusterNSCmd)
  35. }
  36. func listNamespaces(user *api.AuthCheckResponse, client *api.Client, args []string) error {
  37. pID := getProjectID()
  38. // get the service account based on the cluster id
  39. cID := getClusterID()
  40. // get the list of namespaces
  41. namespaces, err := client.GetK8sNamespaces(
  42. context.Background(),
  43. pID,
  44. cID,
  45. )
  46. if err != nil {
  47. return err
  48. }
  49. w := new(tabwriter.Writer)
  50. w.Init(os.Stdout, 3, 8, 0, '\t', tabwriter.AlignRight)
  51. fmt.Fprintf(w, "%s\t%s\n", "NAME", "STATUS")
  52. for _, namespace := range namespaces.Items {
  53. fmt.Fprintf(w, "%s\t%s\n", namespace.Name, namespace.Status.Phase)
  54. }
  55. w.Flush()
  56. return nil
  57. }