connect.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 connectGCRCmd = &cobra.Command{
  59. Use: "gcr",
  60. Short: "Adds a GCR instance to a project",
  61. Run: func(cmd *cobra.Command, args []string) {
  62. err := checkLoginAndRun(args, runConnectGCR)
  63. if err != nil {
  64. os.Exit(1)
  65. }
  66. },
  67. }
  68. var connectDOCRCmd = &cobra.Command{
  69. Use: "docr",
  70. Short: "Adds a DOCR instance to a project",
  71. Run: func(cmd *cobra.Command, args []string) {
  72. err := checkLoginAndRun(args, runConnectDOCR)
  73. if err != nil {
  74. os.Exit(1)
  75. }
  76. },
  77. }
  78. func init() {
  79. rootCmd.AddCommand(connectCmd)
  80. connectCmd.AddCommand(connectKubeconfigCmd)
  81. connectKubeconfigCmd.PersistentFlags().StringVarP(
  82. &kubeconfigPath,
  83. "kubeconfig",
  84. "k",
  85. "",
  86. "path to kubeconfig",
  87. )
  88. contexts = connectKubeconfigCmd.PersistentFlags().StringArray(
  89. "context",
  90. nil,
  91. "the context to connect (defaults to the current context)",
  92. )
  93. connectCmd.AddCommand(connectECRCmd)
  94. connectCmd.AddCommand(connectRegistryCmd)
  95. connectCmd.AddCommand(connectDockerhubCmd)
  96. connectCmd.AddCommand(connectGCRCmd)
  97. connectCmd.AddCommand(connectDOCRCmd)
  98. }
  99. func runConnectKubeconfig(_ *types.GetAuthenticatedUserResponse, client *api.Client, _ []string) error {
  100. isLocal := false
  101. if config.Driver == "local" {
  102. isLocal = true
  103. }
  104. id, err := connect.Kubeconfig(
  105. client,
  106. kubeconfigPath,
  107. *contexts,
  108. config.Project,
  109. isLocal,
  110. )
  111. if err != nil {
  112. return err
  113. }
  114. return config.SetCluster(id)
  115. }
  116. func runConnectECR(_ *types.GetAuthenticatedUserResponse, client *api.Client, _ []string) error {
  117. regID, err := connect.ECR(
  118. client,
  119. config.Project,
  120. )
  121. if err != nil {
  122. return err
  123. }
  124. return config.SetRegistry(regID)
  125. }
  126. func runConnectGCR(_ *types.GetAuthenticatedUserResponse, client *api.Client, _ []string) error {
  127. regID, err := connect.GCR(
  128. client,
  129. config.Project,
  130. )
  131. if err != nil {
  132. return err
  133. }
  134. return config.SetRegistry(regID)
  135. }
  136. func runConnectDOCR(_ *types.GetAuthenticatedUserResponse, client *api.Client, _ []string) error {
  137. regID, err := connect.DOCR(
  138. client,
  139. config.Project,
  140. )
  141. if err != nil {
  142. return err
  143. }
  144. return config.SetRegistry(regID)
  145. }
  146. func runConnectDockerhub(_ *types.GetAuthenticatedUserResponse, client *api.Client, _ []string) error {
  147. regID, err := connect.Dockerhub(
  148. client,
  149. config.Project,
  150. )
  151. if err != nil {
  152. return err
  153. }
  154. return config.SetRegistry(regID)
  155. }
  156. func runConnectRegistry(_ *types.GetAuthenticatedUserResponse, client *api.Client, _ []string) error {
  157. regID, err := connect.Registry(
  158. client,
  159. config.Project,
  160. )
  161. if err != nil {
  162. return err
  163. }
  164. return config.SetRegistry(regID)
  165. }