cluster.go 3.9 KB

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