cluster.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package cmd
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "strconv"
  7. "strings"
  8. "text/tabwriter"
  9. "github.com/fatih/color"
  10. "github.com/porter-dev/porter/cli/cmd/api"
  11. "github.com/porter-dev/porter/cli/cmd/utils"
  12. "github.com/spf13/cobra"
  13. )
  14. // clusterCmd represents the "porter cluster" base command when called
  15. // without any subcommands
  16. var clusterCmd = &cobra.Command{
  17. Use: "cluster",
  18. Aliases: []string{"clusters"},
  19. Short: "Commands that read from a connected cluster",
  20. }
  21. var clusterListCmd = &cobra.Command{
  22. Use: "list",
  23. Short: "Lists the linked clusters in the current project",
  24. Run: func(cmd *cobra.Command, args []string) {
  25. err := checkLoginAndRun(args, listClusters)
  26. if err != nil {
  27. os.Exit(1)
  28. }
  29. },
  30. }
  31. var clusterDeleteCmd = &cobra.Command{
  32. Use: "delete [id]",
  33. Args: cobra.ExactArgs(1),
  34. Short: "Deletes the cluster with the given id",
  35. Run: func(cmd *cobra.Command, args []string) {
  36. err := checkLoginAndRun(args, deleteCluster)
  37. if err != nil {
  38. os.Exit(1)
  39. }
  40. },
  41. }
  42. var clusterNamespaceCmd = &cobra.Command{
  43. Use: "namespace",
  44. Aliases: []string{"namespaces"},
  45. Short: "Commands that perform operations on cluster namespaces",
  46. }
  47. var clusterNamespaceListCmd = &cobra.Command{
  48. Use: "list",
  49. Short: "Lists the namespaces in a cluster",
  50. Run: func(cmd *cobra.Command, args []string) {
  51. err := checkLoginAndRun(args, listNamespaces)
  52. if err != nil {
  53. os.Exit(1)
  54. }
  55. },
  56. }
  57. func init() {
  58. rootCmd.AddCommand(clusterCmd)
  59. clusterCmd.PersistentFlags().UintVar(
  60. &clusterID,
  61. "cluster-id",
  62. getClusterID(),
  63. "id of the cluster",
  64. )
  65. clusterCmd.AddCommand(clusterNamespaceCmd)
  66. clusterCmd.AddCommand(clusterListCmd)
  67. clusterCmd.AddCommand(clusterDeleteCmd)
  68. clusterNamespaceCmd.AddCommand(clusterNamespaceListCmd)
  69. }
  70. func listClusters(user *api.AuthCheckResponse, client *api.Client, args []string) error {
  71. clusters, err := client.ListProjectClusters(context.Background(), getProjectID())
  72. if err != nil {
  73. return err
  74. }
  75. w := new(tabwriter.Writer)
  76. w.Init(os.Stdout, 3, 8, 0, '\t', tabwriter.AlignRight)
  77. fmt.Fprintf(w, "%s\t%s\t%s\n", "ID", "NAME", "SERVER")
  78. currClusterID := getClusterID()
  79. for _, cluster := range clusters {
  80. if currClusterID == cluster.ID {
  81. color.New(color.FgGreen).Fprintf(w, "%d\t%s\t%s (current cluster)\n", cluster.ID, cluster.Name, cluster.Server)
  82. } else {
  83. fmt.Fprintf(w, "%d\t%s\t%s\n", cluster.ID, cluster.Name, cluster.Server)
  84. }
  85. }
  86. w.Flush()
  87. return nil
  88. }
  89. func deleteCluster(user *api.AuthCheckResponse, client *api.Client, args []string) error {
  90. userResp, err := utils.PromptPlaintext(
  91. fmt.Sprintf(
  92. `Are you sure you'd like to delete the cluster with id %s? %s `,
  93. args[0],
  94. color.New(color.FgCyan).Sprintf("[y/n]"),
  95. ),
  96. )
  97. if err != nil {
  98. return err
  99. }
  100. if userResp := strings.ToLower(userResp); userResp == "y" || userResp == "yes" {
  101. id, err := strconv.ParseUint(args[0], 10, 64)
  102. if err != nil {
  103. return err
  104. }
  105. err = client.DeleteProjectCluster(context.Background(), getProjectID(), uint(id))
  106. if err != nil {
  107. return err
  108. }
  109. color.New(color.FgGreen).Printf("Deleted cluster with id %d\n", id)
  110. }
  111. return nil
  112. }
  113. func listNamespaces(user *api.AuthCheckResponse, client *api.Client, args []string) error {
  114. pID := getProjectID()
  115. // get the service account based on the cluster id
  116. cID := getClusterID()
  117. // get the list of namespaces
  118. namespaces, err := client.GetK8sNamespaces(
  119. context.Background(),
  120. pID,
  121. cID,
  122. )
  123. if err != nil {
  124. return err
  125. }
  126. w := new(tabwriter.Writer)
  127. w.Init(os.Stdout, 3, 8, 0, '\t', tabwriter.AlignRight)
  128. fmt.Fprintf(w, "%s\t%s\n", "NAME", "STATUS")
  129. for _, namespace := range namespaces.Items {
  130. fmt.Fprintf(w, "%s\t%s\n", namespace.Name, namespace.Status.Phase)
  131. }
  132. w.Flush()
  133. return nil
  134. }