connect.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. package cmd
  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. var connectCmd = &cobra.Command{
  17. Use: "connect",
  18. Short: "Commands that connect to external clusters and providers",
  19. }
  20. var connectKubeconfigCmd = &cobra.Command{
  21. Use: "kubeconfig",
  22. Short: "Uses the local kubeconfig to add a cluster",
  23. Run: func(cmd *cobra.Command, args []string) {
  24. err := checkLoginAndRun(cmd.Context(), args, runConnectKubeconfig)
  25. if err != nil {
  26. os.Exit(1)
  27. }
  28. },
  29. }
  30. var connectECRCmd = &cobra.Command{
  31. Use: "ecr",
  32. Short: "Adds an ECR instance to a project",
  33. Run: func(cmd *cobra.Command, args []string) {
  34. err := checkLoginAndRun(cmd.Context(), args, runConnectECR)
  35. if err != nil {
  36. os.Exit(1)
  37. }
  38. },
  39. }
  40. var connectDockerhubCmd = &cobra.Command{
  41. Use: "dockerhub",
  42. Short: "Adds a Docker Hub registry integration to a project",
  43. Run: func(cmd *cobra.Command, args []string) {
  44. err := checkLoginAndRun(cmd.Context(), args, runConnectDockerhub)
  45. if err != nil {
  46. os.Exit(1)
  47. }
  48. },
  49. }
  50. var connectRegistryCmd = &cobra.Command{
  51. Use: "registry",
  52. Short: "Adds a custom image registry to a project",
  53. Run: func(cmd *cobra.Command, args []string) {
  54. err := checkLoginAndRun(cmd.Context(), args, runConnectRegistry)
  55. if err != nil {
  56. os.Exit(1)
  57. }
  58. },
  59. }
  60. var connectHelmRepoCmd = &cobra.Command{
  61. Use: "helm",
  62. Short: "Adds a custom Helm registry to a project",
  63. Run: func(cmd *cobra.Command, args []string) {
  64. err := checkLoginAndRun(cmd.Context(), args, runConnectHelmRepo)
  65. if err != nil {
  66. os.Exit(1)
  67. }
  68. },
  69. }
  70. var connectGCRCmd = &cobra.Command{
  71. Use: "gcr",
  72. Short: "Adds a GCR instance to a project",
  73. Run: func(cmd *cobra.Command, args []string) {
  74. err := checkLoginAndRun(cmd.Context(), args, runConnectGCR)
  75. if err != nil {
  76. os.Exit(1)
  77. }
  78. },
  79. }
  80. var connectGARCmd = &cobra.Command{
  81. Use: "gar",
  82. Short: "Adds a GAR instance to a project",
  83. Run: func(cmd *cobra.Command, args []string) {
  84. err := checkLoginAndRun(cmd.Context(), args, runConnectGAR)
  85. if err != nil {
  86. os.Exit(1)
  87. }
  88. cmd.Context()
  89. },
  90. }
  91. var connectDOCRCmd = &cobra.Command{
  92. Use: "docr",
  93. Short: "Adds a DOCR instance to a project",
  94. Run: func(cmd *cobra.Command, args []string) {
  95. err := checkLoginAndRun(cmd.Context(), args, runConnectDOCR)
  96. if err != nil {
  97. os.Exit(1)
  98. }
  99. },
  100. }
  101. func init() {
  102. rootCmd.AddCommand(connectCmd)
  103. connectCmd.AddCommand(connectKubeconfigCmd)
  104. connectKubeconfigCmd.PersistentFlags().StringVarP(
  105. &kubeconfigPath,
  106. "kubeconfig",
  107. "k",
  108. "",
  109. "path to kubeconfig",
  110. )
  111. contexts = connectKubeconfigCmd.PersistentFlags().StringArray(
  112. "context",
  113. nil,
  114. "the context to connect (defaults to the current context)",
  115. )
  116. connectCmd.AddCommand(connectECRCmd)
  117. connectCmd.AddCommand(connectRegistryCmd)
  118. connectCmd.AddCommand(connectDockerhubCmd)
  119. connectCmd.AddCommand(connectGCRCmd)
  120. connectCmd.AddCommand(connectGARCmd)
  121. connectCmd.AddCommand(connectDOCRCmd)
  122. connectCmd.AddCommand(connectHelmRepoCmd)
  123. }
  124. func runConnectKubeconfig(ctx context.Context, _ *types.GetAuthenticatedUserResponse, client api.Client, cliConf config.CLIConfig, _ []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, _ []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, _ []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, _ []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, _ []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, _ []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, _ []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, _ []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. }