connect.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. package commands
  2. import (
  3. "context"
  4. "os"
  5. api "github.com/porter-dev/porter/api/client"
  6. "github.com/porter-dev/porter/api/types"
  7. "github.com/porter-dev/porter/cli/cmd/config"
  8. "github.com/porter-dev/porter/cli/cmd/connect"
  9. "github.com/spf13/cobra"
  10. )
  11. var (
  12. kubeconfigPath string
  13. print *bool
  14. contexts *[]string
  15. )
  16. func registerCommand_Connect(cliConf config.CLIConfig) *cobra.Command {
  17. connectCmd := &cobra.Command{
  18. Use: "connect",
  19. Short: "Commands that connect to external clusters and providers",
  20. }
  21. connectKubeconfigCmd := &cobra.Command{
  22. Use: "kubeconfig",
  23. Short: "Uses the local kubeconfig to add a cluster",
  24. Run: func(cmd *cobra.Command, args []string) {
  25. err := checkLoginAndRunWithConfig(cmd, cliConf, args, runConnectKubeconfig)
  26. if err != nil {
  27. os.Exit(1)
  28. }
  29. },
  30. }
  31. connectECRCmd := &cobra.Command{
  32. Use: "ecr",
  33. Short: "Adds an ECR instance to a project",
  34. Run: func(cmd *cobra.Command, args []string) {
  35. err := checkLoginAndRunWithConfig(cmd, cliConf, args, runConnectECR)
  36. if err != nil {
  37. os.Exit(1)
  38. }
  39. },
  40. }
  41. connectDockerhubCmd := &cobra.Command{
  42. Use: "dockerhub",
  43. Short: "Adds a Docker Hub registry integration to a project",
  44. Run: func(cmd *cobra.Command, args []string) {
  45. err := checkLoginAndRunWithConfig(cmd, cliConf, args, runConnectDockerhub)
  46. if err != nil {
  47. os.Exit(1)
  48. }
  49. },
  50. }
  51. connectRegistryCmd := &cobra.Command{
  52. Use: "registry",
  53. Short: "Adds a custom image registry to a project",
  54. Run: func(cmd *cobra.Command, args []string) {
  55. err := checkLoginAndRunWithConfig(cmd, cliConf, args, runConnectRegistry)
  56. if err != nil {
  57. os.Exit(1)
  58. }
  59. },
  60. }
  61. connectHelmRepoCmd := &cobra.Command{
  62. Use: "helm",
  63. Short: "Adds a custom Helm registry to a project",
  64. Run: func(cmd *cobra.Command, args []string) {
  65. err := checkLoginAndRunWithConfig(cmd, cliConf, args, runConnectHelmRepo)
  66. if err != nil {
  67. os.Exit(1)
  68. }
  69. },
  70. }
  71. connectGCRCmd := &cobra.Command{
  72. Use: "gcr",
  73. Short: "Adds a GCR instance to a project",
  74. Run: func(cmd *cobra.Command, args []string) {
  75. err := checkLoginAndRunWithConfig(cmd, cliConf, args, runConnectGCR)
  76. if err != nil {
  77. os.Exit(1)
  78. }
  79. },
  80. }
  81. connectGARCmd := &cobra.Command{
  82. Use: "gar",
  83. Short: "Adds a GAR instance to a project",
  84. Run: func(cmd *cobra.Command, args []string) {
  85. err := checkLoginAndRunWithConfig(cmd, cliConf, args, runConnectGAR)
  86. if err != nil {
  87. os.Exit(1)
  88. }
  89. cmd.Context()
  90. },
  91. }
  92. connectDOCRCmd := &cobra.Command{
  93. Use: "docr",
  94. Short: "Adds a DOCR instance to a project",
  95. Run: func(cmd *cobra.Command, args []string) {
  96. err := checkLoginAndRunWithConfig(cmd, cliConf, args, runConnectDOCR)
  97. if err != nil {
  98. os.Exit(1)
  99. }
  100. },
  101. }
  102. connectCmd.AddCommand(connectKubeconfigCmd)
  103. connectKubeconfigCmd.PersistentFlags().StringVarP(
  104. &kubeconfigPath,
  105. "kubeconfig",
  106. "k",
  107. "",
  108. "path to kubeconfig",
  109. )
  110. contexts = connectKubeconfigCmd.PersistentFlags().StringArray(
  111. "context",
  112. nil,
  113. "the context to connect (defaults to the current context)",
  114. )
  115. connectCmd.AddCommand(connectECRCmd)
  116. connectCmd.AddCommand(connectRegistryCmd)
  117. connectCmd.AddCommand(connectDockerhubCmd)
  118. connectCmd.AddCommand(connectGCRCmd)
  119. connectCmd.AddCommand(connectGARCmd)
  120. connectCmd.AddCommand(connectDOCRCmd)
  121. connectCmd.AddCommand(connectHelmRepoCmd)
  122. return connectCmd
  123. }
  124. func runConnectKubeconfig(ctx context.Context, _ *types.GetAuthenticatedUserResponse, client api.Client, cliConf config.CLIConfig, _ config.FeatureFlags, _ *cobra.Command, _ []string) error {
  125. isLocal := false
  126. if cliConf.Driver == "local" {
  127. isLocal = true
  128. }
  129. id, err := connect.Kubeconfig(
  130. ctx,
  131. client,
  132. kubeconfigPath,
  133. *contexts,
  134. cliConf.Project,
  135. isLocal,
  136. )
  137. if err != nil {
  138. return err
  139. }
  140. return cliConf.SetCluster(id)
  141. }
  142. func runConnectECR(ctx context.Context, _ *types.GetAuthenticatedUserResponse, client api.Client, cliConf config.CLIConfig, _ config.FeatureFlags, _ *cobra.Command, _ []string) error {
  143. regID, err := connect.ECR(
  144. ctx,
  145. client,
  146. cliConf.Project,
  147. )
  148. if err != nil {
  149. return err
  150. }
  151. return cliConf.SetRegistry(regID)
  152. }
  153. func runConnectGCR(ctx context.Context, _ *types.GetAuthenticatedUserResponse, client api.Client, cliConf config.CLIConfig, _ config.FeatureFlags, _ *cobra.Command, _ []string) error {
  154. regID, err := connect.GCR(
  155. ctx,
  156. client,
  157. cliConf.Project,
  158. )
  159. if err != nil {
  160. return err
  161. }
  162. return cliConf.SetRegistry(regID)
  163. }
  164. func runConnectGAR(ctx context.Context, _ *types.GetAuthenticatedUserResponse, client api.Client, cliConf config.CLIConfig, _ config.FeatureFlags, _ *cobra.Command, _ []string) error {
  165. regID, err := connect.GAR(
  166. ctx,
  167. client,
  168. cliConf.Project,
  169. )
  170. if err != nil {
  171. return err
  172. }
  173. return cliConf.SetRegistry(regID)
  174. }
  175. func runConnectDOCR(ctx context.Context, _ *types.GetAuthenticatedUserResponse, client api.Client, cliConf config.CLIConfig, _ config.FeatureFlags, _ *cobra.Command, _ []string) error {
  176. regID, err := connect.DOCR(
  177. ctx,
  178. client,
  179. cliConf.Project,
  180. )
  181. if err != nil {
  182. return err
  183. }
  184. return cliConf.SetRegistry(regID)
  185. }
  186. func runConnectDockerhub(ctx context.Context, _ *types.GetAuthenticatedUserResponse, client api.Client, cliConf config.CLIConfig, _ config.FeatureFlags, _ *cobra.Command, _ []string) error {
  187. regID, err := connect.Dockerhub(
  188. ctx,
  189. client,
  190. cliConf.Project,
  191. )
  192. if err != nil {
  193. return err
  194. }
  195. return cliConf.SetRegistry(regID)
  196. }
  197. func runConnectRegistry(ctx context.Context, _ *types.GetAuthenticatedUserResponse, client api.Client, cliConf config.CLIConfig, _ config.FeatureFlags, _ *cobra.Command, _ []string) error {
  198. regID, err := connect.Registry(
  199. ctx,
  200. client,
  201. cliConf.Project,
  202. )
  203. if err != nil {
  204. return err
  205. }
  206. return cliConf.SetRegistry(regID)
  207. }
  208. func runConnectHelmRepo(ctx context.Context, _ *types.GetAuthenticatedUserResponse, client api.Client, cliConf config.CLIConfig, _ config.FeatureFlags, _ *cobra.Command, _ []string) error {
  209. hrID, err := connect.HelmRepo(
  210. ctx,
  211. client,
  212. cliConf.Project,
  213. )
  214. if err != nil {
  215. return err
  216. }
  217. return cliConf.SetHelmRepo(hrID)
  218. }