Просмотр исходного кода

add cli command for connecting helm repo

Alexander Belanger 4 лет назад
Родитель
Сommit
c413e9855f
3 измененных файлов с 140 добавлено и 0 удалено
  1. 20 0
      api/client/registry.go
  2. 26 0
      cli/cmd/connect.go
  3. 94 0
      cli/cmd/connect/helmrepo.go

+ 20 - 0
api/client/registry.go

@@ -27,6 +27,26 @@ func (c *Client) CreateRegistry(
 	return resp, err
 }
 
+// CreateRegistry creates a new registry integration
+func (c *Client) CreateHelmRepo(
+	ctx context.Context,
+	projectID uint,
+	req *types.CreateHelmRepoRequest,
+) (*types.HelmRepo, error) {
+	resp := &types.HelmRepo{}
+
+	err := c.postRequest(
+		fmt.Sprintf(
+			"/projects/%d/helmrepos",
+			projectID,
+		),
+		req,
+		resp,
+	)
+
+	return resp, err
+}
+
 // ListRegistries returns a list of registries for a project
 func (c *Client) ListRegistries(
 	ctx context.Context,

+ 26 - 0
cli/cmd/connect.go

@@ -68,6 +68,18 @@ var connectRegistryCmd = &cobra.Command{
 	},
 }
 
+var connectHelmRepoCmd = &cobra.Command{
+	Use:   "helm",
+	Short: "Adds a custom Helm registry to a project",
+	Run: func(cmd *cobra.Command, args []string) {
+		err := checkLoginAndRun(args, runConnectHelmRepo)
+
+		if err != nil {
+			os.Exit(1)
+		}
+	},
+}
+
 var connectGCRCmd = &cobra.Command{
 	Use:   "gcr",
 	Short: "Adds a GCR instance to a project",
@@ -116,6 +128,7 @@ func init() {
 	connectCmd.AddCommand(connectDockerhubCmd)
 	connectCmd.AddCommand(connectGCRCmd)
 	connectCmd.AddCommand(connectDOCRCmd)
+	connectCmd.AddCommand(connectHelmRepoCmd)
 }
 
 func runConnectKubeconfig(_ *types.GetAuthenticatedUserResponse, client *api.Client, _ []string) error {
@@ -204,3 +217,16 @@ func runConnectRegistry(_ *types.GetAuthenticatedUserResponse, client *api.Clien
 
 	return config.SetRegistry(regID)
 }
+
+func runConnectHelmRepo(_ *types.GetAuthenticatedUserResponse, client *api.Client, _ []string) error {
+	hrID, err := connect.HelmRepo(
+		client,
+		config.Project,
+	)
+
+	if err != nil {
+		return err
+	}
+
+	return config.SetHelmRepo(hrID)
+}

+ 94 - 0
cli/cmd/connect/helmrepo.go

@@ -0,0 +1,94 @@
+package connect
+
+import (
+	"context"
+	"fmt"
+	"net/url"
+
+	"github.com/porter-dev/porter/api/types"
+
+	"github.com/fatih/color"
+	api "github.com/porter-dev/porter/api/client"
+	"github.com/porter-dev/porter/cli/cmd/utils"
+)
+
+func HelmRepo(
+	client *api.Client,
+	projectID uint,
+) (uint, error) {
+	// if project ID is 0, ask the user to set the project ID or create a project
+	if projectID == 0 {
+		return 0, fmt.Errorf("no project set, please run porter project set [id]")
+	}
+
+	repoName, err := utils.PromptPlaintext(fmt.Sprintf(`Provide the name that you would like to give this Helm registry. 
+Name: `))
+
+	if err != nil {
+		return 0, err
+	}
+
+	repoURL, err := utils.PromptPlaintext(fmt.Sprintf(`Provide the Helm registry URL, make sure to include the protocol. For example, https://charts.bitnami.com/bitnami.
+Registry URL: `))
+
+	if err != nil {
+		return 0, err
+	}
+
+	if _, err := url.Parse(repoURL); err != nil {
+		return 0, fmt.Errorf("not a valid url: %s", err)
+	}
+
+	username, err := utils.PromptPlaintext(fmt.Sprintf(`Helm repo username (press enter for a public registry):`))
+
+	if err != nil {
+		return 0, err
+	}
+
+	password, err := utils.PromptPassword(`Helm registry password (press enter for a public registry).
+Password:`)
+
+	if err != nil {
+		return 0, err
+	}
+
+	var basicIntegrationID uint = 0
+
+	if username != "" && password != "" {
+		// create the basic auth integration
+		integration, err := client.CreateBasicAuthIntegration(
+			context.Background(),
+			projectID,
+			&types.CreateBasicRequest{
+				Username: username,
+				Password: password,
+			},
+		)
+
+		if err != nil {
+			return 0, err
+		}
+
+		color.New(color.FgGreen).Printf("created basic auth integration with id %d\n", integration.ID)
+
+		basicIntegrationID = integration.ID
+	}
+
+	reg, err := client.CreateHelmRepo(
+		context.Background(),
+		projectID,
+		&types.CreateHelmRepoRequest{
+			URL:                repoURL,
+			Name:               repoName,
+			BasicIntegrationID: basicIntegrationID,
+		},
+	)
+
+	if err != nil {
+		return 0, err
+	}
+
+	color.New(color.FgGreen).Printf("created helm registry integration with id %d and name %s\n", reg.ID, reg.Name)
+
+	return reg.ID, nil
+}