فهرست منبع

add porter kubectl and porter helm wrapper subcommands

Mohammed Nafees 3 سال پیش
والد
کامیت
df520a4ab4
2فایلهای تغییر یافته به همراه157 افزوده شده و 0 حذف شده
  1. 66 0
      cli/cmd/helm.go
  2. 91 0
      cli/cmd/kubectl.go

+ 66 - 0
cli/cmd/helm.go

@@ -0,0 +1,66 @@
+package cmd
+
+import (
+	"bytes"
+	"fmt"
+	"os"
+	"os/exec"
+
+	api "github.com/porter-dev/porter/api/client"
+	"github.com/porter-dev/porter/api/types"
+	"github.com/spf13/cobra"
+)
+
+var helmCmd = &cobra.Command{
+	Use:   "helm",
+	Short: "Use helm to interact with a Porter cluster",
+	Run: func(cmd *cobra.Command, args []string) {
+		err := checkLoginAndRun(args, runHelm)
+
+		if err != nil {
+			os.Exit(1)
+		}
+	},
+}
+
+func init() {
+	rootCmd.AddCommand(helmCmd)
+}
+
+func runHelm(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
+	_, err := exec.LookPath("helm")
+
+	if err != nil {
+		return fmt.Errorf("error finding helm: %w", err)
+	}
+
+	tmpFile, err := downloadTempKubeconfig(client)
+
+	if err != nil {
+		return err
+	}
+
+	defer func() {
+		os.Remove(tmpFile)
+	}()
+
+	os.Setenv("KUBECONFIG", tmpFile)
+
+	cmd := exec.Command("helm", args...)
+
+	var out bytes.Buffer
+	var stderr bytes.Buffer
+
+	cmd.Stdout = &out
+	cmd.Stderr = &stderr
+
+	err = cmd.Run()
+
+	if err != nil {
+		return fmt.Errorf("error running helm: %s", stderr.String())
+	}
+
+	fmt.Print(out.String())
+
+	return nil
+}

+ 91 - 0
cli/cmd/kubectl.go

@@ -0,0 +1,91 @@
+package cmd
+
+import (
+	"bytes"
+	"context"
+	"fmt"
+	"os"
+	"os/exec"
+
+	api "github.com/porter-dev/porter/api/client"
+	"github.com/porter-dev/porter/api/types"
+	"github.com/spf13/cobra"
+)
+
+var kubectlCmd = &cobra.Command{
+	Use:   "kubectl",
+	Short: "Use kubectl to interact with a Porter cluster",
+	Run: func(cmd *cobra.Command, args []string) {
+		err := checkLoginAndRun(args, runKubectl)
+
+		if err != nil {
+			os.Exit(1)
+		}
+	},
+}
+
+func init() {
+	rootCmd.AddCommand(kubectlCmd)
+}
+
+func runKubectl(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
+	_, err := exec.LookPath("kubectl")
+
+	if err != nil {
+		return fmt.Errorf("error finding kubectl: %w", err)
+	}
+
+	tmpFile, err := downloadTempKubeconfig(client)
+
+	if err != nil {
+		return err
+	}
+
+	defer func() {
+		os.Remove(tmpFile)
+	}()
+
+	os.Setenv("KUBECONFIG", tmpFile)
+
+	cmd := exec.Command("kubectl", args...)
+
+	var out bytes.Buffer
+	var stderr bytes.Buffer
+
+	cmd.Stdout = &out
+	cmd.Stderr = &stderr
+
+	err = cmd.Run()
+
+	if err != nil {
+		return fmt.Errorf("error running kubectl: %s", stderr.String())
+	}
+
+	fmt.Print(out.String())
+
+	return nil
+}
+
+func downloadTempKubeconfig(client *api.Client) (string, error) {
+	tmpFile, err := os.CreateTemp("", "porter_kubeconfig_*.yaml")
+
+	if err != nil {
+		return "", fmt.Errorf("error creating temp file for kubeconfig: %w", err)
+	}
+
+	defer tmpFile.Close()
+
+	resp, err := client.GetKubeconfig(context.Background(), cliConf.Project, cliConf.Cluster, cliConf.Kubeconfig)
+
+	if err != nil {
+		return "", fmt.Errorf("error fetching kubeconfig for cluster: %w", err)
+	}
+
+	_, err = tmpFile.Write(resp.Kubeconfig)
+
+	if err != nil {
+		return "", fmt.Errorf("error writing kubeconfig to temp file: %w", err)
+	}
+
+	return tmpFile.Name(), nil
+}