connect.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package cmd
  2. import (
  3. "os"
  4. "github.com/porter-dev/porter/cli/cmd/api"
  5. "github.com/porter-dev/porter/cli/cmd/connect"
  6. "github.com/spf13/cobra"
  7. )
  8. var (
  9. kubeconfigPath string
  10. print *bool
  11. contexts *[]string
  12. )
  13. var connectCmd = &cobra.Command{
  14. Use: "connect",
  15. Short: "Commands that connect to external clusters and providers",
  16. }
  17. var connectKubeconfigCmd = &cobra.Command{
  18. Use: "kubeconfig",
  19. Short: "Uses the local kubeconfig to connect to a cluster",
  20. Run: func(cmd *cobra.Command, args []string) {
  21. err := checkLoginAndRun(args, runConnectKubeconfig)
  22. if err != nil {
  23. os.Exit(1)
  24. }
  25. },
  26. }
  27. var connectECRCmd = &cobra.Command{
  28. Use: "ecr",
  29. Short: "Connects an ECR instance to a project",
  30. Run: func(cmd *cobra.Command, args []string) {
  31. err := checkLoginAndRun(args, runConnectECR)
  32. if err != nil {
  33. os.Exit(1)
  34. }
  35. },
  36. }
  37. var connectGCRCmd = &cobra.Command{
  38. Use: "gcr",
  39. Short: "Connects a GCR instance to a project",
  40. Run: func(cmd *cobra.Command, args []string) {
  41. err := checkLoginAndRun(args, runConnectGCR)
  42. if err != nil {
  43. os.Exit(1)
  44. }
  45. },
  46. }
  47. func init() {
  48. rootCmd.AddCommand(connectCmd)
  49. connectCmd.AddCommand(connectKubeconfigCmd)
  50. connectCmd.PersistentFlags().StringVar(
  51. &host,
  52. "host",
  53. getHost(),
  54. "host url of Porter instance",
  55. )
  56. projectID = *connectCmd.PersistentFlags().UintP(
  57. "project-id",
  58. "p",
  59. getProjectID(),
  60. "project id to use",
  61. )
  62. connectKubeconfigCmd.PersistentFlags().StringVarP(
  63. &kubeconfigPath,
  64. "kubeconfig",
  65. "k",
  66. "",
  67. "path to kubeconfig",
  68. )
  69. contexts = connectKubeconfigCmd.PersistentFlags().StringArray(
  70. "contexts",
  71. nil,
  72. "the list of contexts to connect (defaults to the current context)",
  73. )
  74. connectCmd.AddCommand(connectECRCmd)
  75. connectCmd.AddCommand(connectGCRCmd)
  76. }
  77. func runConnectKubeconfig(_ *api.AuthCheckResponse, client *api.Client, _ []string) error {
  78. isLocal := false
  79. if getDriver() == "local" {
  80. isLocal = true
  81. }
  82. return connect.Kubeconfig(
  83. client,
  84. kubeconfigPath,
  85. *contexts,
  86. getProjectID(),
  87. isLocal,
  88. )
  89. }
  90. func runConnectECR(_ *api.AuthCheckResponse, client *api.Client, _ []string) error {
  91. regID, err := connect.ECR(
  92. client,
  93. getProjectID(),
  94. )
  95. if err != nil {
  96. return err
  97. }
  98. return setRegistry(regID)
  99. }
  100. func runConnectGCR(_ *api.AuthCheckResponse, client *api.Client, _ []string) error {
  101. regID, err := connect.GCR(
  102. client,
  103. getProjectID(),
  104. )
  105. if err != nil {
  106. return err
  107. }
  108. return setRegistry(regID)
  109. }