connect.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package cmd
  2. import (
  3. "os"
  4. "github.com/porter-dev/porter/cli/cmd/api"
  5. "github.com/porter-dev/porter/cli/cmd/connect"
  6. "github.com/spf13/cobra"
  7. )
  8. var (
  9. kubeconfigPath string
  10. print *bool
  11. contexts *[]string
  12. )
  13. var connectCmd = &cobra.Command{
  14. Use: "connect",
  15. Short: "Commands that connect to external clusters and providers",
  16. }
  17. var connectKubeconfigCmd = &cobra.Command{
  18. Use: "kubeconfig",
  19. Short: "Uses the local kubeconfig to add a cluster",
  20. Run: func(cmd *cobra.Command, args []string) {
  21. err := checkLoginAndRun(args, runConnectKubeconfig)
  22. if err != nil {
  23. os.Exit(1)
  24. }
  25. },
  26. }
  27. var connectECRCmd = &cobra.Command{
  28. Use: "ecr",
  29. Short: "Adds an ECR instance to a project",
  30. Run: func(cmd *cobra.Command, args []string) {
  31. err := checkLoginAndRun(args, runConnectECR)
  32. if err != nil {
  33. os.Exit(1)
  34. }
  35. },
  36. }
  37. var connectGCRCmd = &cobra.Command{
  38. Use: "gcr",
  39. Short: "Adds a GCR instance to a project",
  40. Run: func(cmd *cobra.Command, args []string) {
  41. err := checkLoginAndRun(args, runConnectGCR)
  42. if err != nil {
  43. os.Exit(1)
  44. }
  45. },
  46. }
  47. var connectHRCmd = &cobra.Command{
  48. Use: "helmrepo",
  49. Aliases: []string{"helm", "helmrepos"},
  50. Short: "Adds a Helm repository to a project",
  51. Run: func(cmd *cobra.Command, args []string) {
  52. err := checkLoginAndRun(args, runConnectHelmRepoBasic)
  53. if err != nil {
  54. os.Exit(1)
  55. }
  56. },
  57. }
  58. func init() {
  59. rootCmd.AddCommand(connectCmd)
  60. connectCmd.AddCommand(connectKubeconfigCmd)
  61. connectCmd.PersistentFlags().StringVar(
  62. &host,
  63. "host",
  64. getHost(),
  65. "host url of Porter instance",
  66. )
  67. projectID = *connectCmd.PersistentFlags().UintP(
  68. "project-id",
  69. "p",
  70. getProjectID(),
  71. "project id to use",
  72. )
  73. connectKubeconfigCmd.PersistentFlags().StringVarP(
  74. &kubeconfigPath,
  75. "kubeconfig",
  76. "k",
  77. "",
  78. "path to kubeconfig",
  79. )
  80. contexts = connectKubeconfigCmd.PersistentFlags().StringArray(
  81. "context",
  82. nil,
  83. "the context to connect (defaults to the current context)",
  84. )
  85. connectCmd.AddCommand(connectECRCmd)
  86. connectCmd.AddCommand(connectGCRCmd)
  87. connectCmd.AddCommand(connectHRCmd)
  88. }
  89. func runConnectKubeconfig(_ *api.AuthCheckResponse, client *api.Client, _ []string) error {
  90. isLocal := false
  91. if getDriver() == "local" {
  92. isLocal = true
  93. }
  94. id, err := connect.Kubeconfig(
  95. client,
  96. kubeconfigPath,
  97. *contexts,
  98. getProjectID(),
  99. isLocal,
  100. )
  101. if err != nil {
  102. return err
  103. }
  104. return setCluster(id)
  105. }
  106. func runConnectECR(_ *api.AuthCheckResponse, client *api.Client, _ []string) error {
  107. regID, err := connect.ECR(
  108. client,
  109. getProjectID(),
  110. )
  111. if err != nil {
  112. return err
  113. }
  114. return setRegistry(regID)
  115. }
  116. func runConnectGCR(_ *api.AuthCheckResponse, client *api.Client, _ []string) error {
  117. regID, err := connect.GCR(
  118. client,
  119. getProjectID(),
  120. )
  121. if err != nil {
  122. return err
  123. }
  124. return setRegistry(regID)
  125. }
  126. func runConnectHelmRepoBasic(_ *api.AuthCheckResponse, client *api.Client, _ []string) error {
  127. hrID, err := connect.Helm(
  128. client,
  129. getProjectID(),
  130. )
  131. if err != nil {
  132. return err
  133. }
  134. return setHelmRepo(hrID)
  135. }