2
0

cluster.go 3.7 KB

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