cluster.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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.AddCommand(clusterNamespaceCmd)
  60. clusterCmd.AddCommand(clusterListCmd)
  61. clusterCmd.AddCommand(clusterDeleteCmd)
  62. clusterNamespaceCmd.AddCommand(clusterNamespaceListCmd)
  63. }
  64. func listClusters(user *api.AuthCheckResponse, client *api.Client, args []string) error {
  65. clusters, err := client.ListProjectClusters(context.Background(), config.Project)
  66. if err != nil {
  67. return err
  68. }
  69. w := new(tabwriter.Writer)
  70. w.Init(os.Stdout, 3, 8, 0, '\t', tabwriter.AlignRight)
  71. fmt.Fprintf(w, "%s\t%s\t%s\n", "ID", "NAME", "SERVER")
  72. currClusterID := config.Cluster
  73. for _, cluster := range clusters {
  74. if currClusterID == cluster.ID {
  75. color.New(color.FgGreen).Fprintf(w, "%d\t%s\t%s (current cluster)\n", cluster.ID, cluster.Name, cluster.Server)
  76. } else {
  77. fmt.Fprintf(w, "%d\t%s\t%s\n", cluster.ID, cluster.Name, cluster.Server)
  78. }
  79. }
  80. w.Flush()
  81. return nil
  82. }
  83. func deleteCluster(user *api.AuthCheckResponse, client *api.Client, args []string) error {
  84. userResp, err := utils.PromptPlaintext(
  85. fmt.Sprintf(
  86. `Are you sure you'd like to delete the cluster with id %s? %s `,
  87. args[0],
  88. color.New(color.FgCyan).Sprintf("[y/n]"),
  89. ),
  90. )
  91. if err != nil {
  92. return err
  93. }
  94. if userResp := strings.ToLower(userResp); userResp == "y" || userResp == "yes" {
  95. id, err := strconv.ParseUint(args[0], 10, 64)
  96. if err != nil {
  97. return err
  98. }
  99. err = client.DeleteProjectCluster(context.Background(), config.Project, uint(id))
  100. if err != nil {
  101. return err
  102. }
  103. color.New(color.FgGreen).Printf("Deleted cluster with id %d\n", id)
  104. }
  105. return nil
  106. }
  107. func listNamespaces(user *api.AuthCheckResponse, client *api.Client, args []string) error {
  108. pID := config.Project
  109. // get the service account based on the cluster id
  110. cID := config.Cluster
  111. // get the list of namespaces
  112. namespaces, err := client.GetK8sNamespaces(
  113. context.Background(),
  114. pID,
  115. cID,
  116. )
  117. if err != nil {
  118. return err
  119. }
  120. w := new(tabwriter.Writer)
  121. w.Init(os.Stdout, 3, 8, 0, '\t', tabwriter.AlignRight)
  122. fmt.Fprintf(w, "%s\t%s\n", "NAME", "STATUS")
  123. for _, namespace := range namespaces.Items {
  124. fmt.Fprintf(w, "%s\t%s\n", namespace.Name, namespace.Status.Phase)
  125. }
  126. w.Flush()
  127. return nil
  128. }