2
0

connect.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 connectDOCRCmd = &cobra.Command{
  79. Use: "docr",
  80. Short: "Adds a DOCR instance to a project",
  81. Run: func(cmd *cobra.Command, args []string) {
  82. err := checkLoginAndRun(args, runConnectDOCR)
  83. if err != nil {
  84. os.Exit(1)
  85. }
  86. },
  87. }
  88. func init() {
  89. rootCmd.AddCommand(connectCmd)
  90. connectCmd.AddCommand(connectKubeconfigCmd)
  91. connectKubeconfigCmd.PersistentFlags().StringVarP(
  92. &kubeconfigPath,
  93. "kubeconfig",
  94. "k",
  95. "",
  96. "path to kubeconfig",
  97. )
  98. contexts = connectKubeconfigCmd.PersistentFlags().StringArray(
  99. "context",
  100. nil,
  101. "the context to connect (defaults to the current context)",
  102. )
  103. connectCmd.AddCommand(connectECRCmd)
  104. connectCmd.AddCommand(connectRegistryCmd)
  105. connectCmd.AddCommand(connectDockerhubCmd)
  106. connectCmd.AddCommand(connectGCRCmd)
  107. connectCmd.AddCommand(connectDOCRCmd)
  108. connectCmd.AddCommand(connectHelmRepoCmd)
  109. }
  110. func runConnectKubeconfig(_ *types.GetAuthenticatedUserResponse, client *api.Client, _ []string) error {
  111. isLocal := false
  112. if cliConf.Driver == "local" {
  113. isLocal = true
  114. }
  115. id, err := connect.Kubeconfig(
  116. client,
  117. kubeconfigPath,
  118. *contexts,
  119. cliConf.Project,
  120. isLocal,
  121. )
  122. if err != nil {
  123. return err
  124. }
  125. return cliConf.SetCluster(id)
  126. }
  127. func runConnectECR(_ *types.GetAuthenticatedUserResponse, client *api.Client, _ []string) error {
  128. regID, err := connect.ECR(
  129. client,
  130. cliConf.Project,
  131. )
  132. if err != nil {
  133. return err
  134. }
  135. return cliConf.SetRegistry(regID)
  136. }
  137. func runConnectGCR(_ *types.GetAuthenticatedUserResponse, client *api.Client, _ []string) error {
  138. regID, err := connect.GCR(
  139. client,
  140. cliConf.Project,
  141. )
  142. if err != nil {
  143. return err
  144. }
  145. return cliConf.SetRegistry(regID)
  146. }
  147. func runConnectDOCR(_ *types.GetAuthenticatedUserResponse, client *api.Client, _ []string) error {
  148. regID, err := connect.DOCR(
  149. client,
  150. cliConf.Project,
  151. )
  152. if err != nil {
  153. return err
  154. }
  155. return cliConf.SetRegistry(regID)
  156. }
  157. func runConnectDockerhub(_ *types.GetAuthenticatedUserResponse, client *api.Client, _ []string) error {
  158. regID, err := connect.Dockerhub(
  159. client,
  160. cliConf.Project,
  161. )
  162. if err != nil {
  163. return err
  164. }
  165. return cliConf.SetRegistry(regID)
  166. }
  167. func runConnectRegistry(_ *types.GetAuthenticatedUserResponse, client *api.Client, _ []string) error {
  168. regID, err := connect.Registry(
  169. client,
  170. cliConf.Project,
  171. )
  172. if err != nil {
  173. return err
  174. }
  175. return cliConf.SetRegistry(regID)
  176. }
  177. func runConnectHelmRepo(_ *types.GetAuthenticatedUserResponse, client *api.Client, _ []string) error {
  178. hrID, err := connect.HelmRepo(
  179. client,
  180. cliConf.Project,
  181. )
  182. if err != nil {
  183. return err
  184. }
  185. return cliConf.SetHelmRepo(hrID)
  186. }