Forráskód Böngészése

add ability to locally set path to kubeconfig

Mohammed Nafees 4 éve
szülő
commit
4cb359ad76
4 módosított fájl, 87 hozzáadás és 12 törlés
  1. 15 0
      cli/cmd/config.go
  2. 29 2
      cli/cmd/config/config.go
  3. 22 5
      cli/cmd/portforward.go
  4. 21 5
      cli/cmd/run.go

+ 15 - 0
cli/cmd/config.go

@@ -154,6 +154,20 @@ var configSetHostCmd = &cobra.Command{
 	},
 }
 
+var configSetKubeconfigCmd = &cobra.Command{
+	Use:   "set-kubeconfig [host]",
+	Args:  cobra.ExactArgs(1),
+	Short: "Saves the path to kubeconfig in the default configuration",
+	Run: func(cmd *cobra.Command, args []string) {
+		err := cliConf.SetKubeconfig(args[0])
+
+		if err != nil {
+			color.New(color.FgRed).Printf("An error occurred: %v\n", err)
+			os.Exit(1)
+		}
+	},
+}
+
 func init() {
 	rootCmd.AddCommand(configCmd)
 
@@ -162,6 +176,7 @@ func init() {
 	configCmd.AddCommand(configSetHostCmd)
 	configCmd.AddCommand(configSetRegistryCmd)
 	configCmd.AddCommand(configSetHelmRepoCmd)
+	configCmd.AddCommand(configSetKubeconfigCmd)
 }
 
 func printConfig() error {

+ 29 - 2
cli/cmd/config/config.go

@@ -1,6 +1,8 @@
 package config
 
 import (
+	"errors"
+	"fmt"
 	"io/ioutil"
 	"os"
 	"path/filepath"
@@ -32,8 +34,9 @@ type CLIConfig struct {
 
 	Token string `yaml:"token"`
 
-	Registry uint `yaml:"registry"`
-	HelmRepo uint `yaml:"helm_repo"`
+	Registry   uint   `yaml:"registry"`
+	HelmRepo   uint   `yaml:"helm_repo"`
+	Kubeconfig string `yaml:"kubeconfig"`
 }
 
 // InitAndLoadConfig populates the config object with the following precedence rules:
@@ -276,3 +279,27 @@ func (c *CLIConfig) SetHelmRepo(helmRepoID uint) error {
 
 	return nil
 }
+
+func (c *CLIConfig) SetKubeconfig(kubeconfig string) error {
+	path, err := filepath.Abs(kubeconfig)
+
+	if err != nil {
+		return err
+	}
+
+	if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
+		return fmt.Errorf("%s does not exist", path)
+	}
+
+	viper.Set("kubeconfig", kubeconfig)
+	color.New(color.FgGreen).Printf("Set the path to kubeconfig as %s\n", kubeconfig)
+	err = viper.WriteConfig()
+
+	if err != nil {
+		return err
+	}
+
+	config.Kubeconfig = kubeconfig
+
+	return nil
+}

+ 22 - 5
cli/cmd/portforward.go

@@ -3,6 +3,7 @@ package cmd
 import (
 	"context"
 	"fmt"
+	"io"
 	"net/http"
 	"net/url"
 	"os"
@@ -145,13 +146,29 @@ func portForward(user *types.GetAuthenticatedUserResponse, client *api.Client, a
 		pod = pods[0]
 	}
 
-	kubeResp, err := client.GetKubeconfig(context.Background(), cliConf.Project, cliConf.Cluster)
+	var kubeBytes []byte
 
-	if err != nil {
-		return err
-	}
+	if cliConf.Kubeconfig == "" {
+		kubeResp, err := client.GetKubeconfig(context.TODO(), cliConf.Project, cliConf.Cluster)
+
+		if err != nil {
+			return err
+		}
+
+		kubeBytes = kubeResp.Kubeconfig
+	} else {
+		file, err := os.Open(cliConf.Kubeconfig)
 
-	kubeBytes := kubeResp.Kubeconfig
+		if err != nil {
+			return err
+		}
+
+		kubeBytes, err = io.ReadAll(file)
+
+		if err != nil {
+			return err
+		}
+	}
 
 	cmdConf, err := clientcmd.NewClientConfigFromBytes(kubeBytes)
 

+ 21 - 5
cli/cmd/run.go

@@ -264,13 +264,29 @@ func (p *PorterRunSharedConfig) setSharedConfig() error {
 	pID := cliConf.Project
 	cID := cliConf.Cluster
 
-	kubeResp, err := p.Client.GetKubeconfig(context.TODO(), pID, cID)
+	var kubeBytes []byte
 
-	if err != nil {
-		return err
-	}
+	if cliConf.Kubeconfig == "" {
+		kubeResp, err := p.Client.GetKubeconfig(context.TODO(), pID, cID)
+
+		if err != nil {
+			return err
+		}
+
+		kubeBytes = kubeResp.Kubeconfig
+	} else {
+		file, err := os.Open(cliConf.Kubeconfig)
 
-	kubeBytes := kubeResp.Kubeconfig
+		if err != nil {
+			return err
+		}
+
+		kubeBytes, err = io.ReadAll(file)
+
+		if err != nil {
+			return err
+		}
+	}
 
 	cmdConf, err := clientcmd.NewClientConfigFromBytes(kubeBytes)