connect.go 4.9 KB

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