cluster.go 3.9 KB

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