Parcourir la source

remove local kubeconfig when changing project or cluster

Mohammed Nafees il y a 3 ans
Parent
commit
7ed0a224b3
4 fichiers modifiés avec 51 ajouts et 43 suppressions
  1. 29 0
      api/client/k8s.go
  2. 12 0
      cli/cmd/config/config.go
  3. 5 22
      cli/cmd/portforward.go
  4. 5 21
      cli/cmd/run.go

+ 29 - 0
api/client/k8s.go

@@ -3,8 +3,12 @@ package client
 import (
 import (
 	"context"
 	"context"
 	"fmt"
 	"fmt"
+	"io"
+	"os"
 
 
+	"github.com/fatih/color"
 	"github.com/porter-dev/porter/api/types"
 	"github.com/porter-dev/porter/api/types"
+
 	v1 "k8s.io/api/batch/v1"
 	v1 "k8s.io/api/batch/v1"
 )
 )
 
 
@@ -55,9 +59,34 @@ func (c *Client) GetKubeconfig(
 	ctx context.Context,
 	ctx context.Context,
 	projectID uint,
 	projectID uint,
 	clusterID uint,
 	clusterID uint,
+	localKubeconfig string,
 ) (*types.GetTemporaryKubeconfigResponse, error) {
 ) (*types.GetTemporaryKubeconfigResponse, error) {
 	resp := &types.GetTemporaryKubeconfigResponse{}
 	resp := &types.GetTemporaryKubeconfigResponse{}
 
 
+	if localKubeconfig != "" {
+		color.New(color.FgBlue).Printf("using local kubeconfig: %s\n", localKubeconfig)
+
+		if _, err := os.Stat(localKubeconfig); !os.IsNotExist(err) {
+			file, err := os.Open(localKubeconfig)
+
+			if err != nil {
+				return nil, err
+			}
+
+			data, err := io.ReadAll(file)
+
+			if err != nil {
+				return nil, err
+			}
+
+			resp.Kubeconfig = append(resp.Kubeconfig, data...)
+
+			return resp, nil
+		}
+	}
+
+	color.New(color.FgBlue).Println("using remote kubeconfig")
+
 	err := c.getRequest(
 	err := c.getRequest(
 		fmt.Sprintf(
 		fmt.Sprintf(
 			"/projects/%d/clusters/%d/kubeconfig",
 			"/projects/%d/clusters/%d/kubeconfig",

+ 12 - 0
cli/cmd/config/config.go

@@ -212,6 +212,12 @@ func (c *CLIConfig) SetHost(host string) error {
 }
 }
 
 
 func (c *CLIConfig) SetProject(projectID uint) error {
 func (c *CLIConfig) SetProject(projectID uint) error {
+	if config.Kubeconfig != "" || viper.IsSet("kubeconfig") {
+		viper.Set("kubeconfig", "")
+		color.New(color.FgBlue).Println("Removing local kubeconfig")
+		config.Kubeconfig = ""
+	}
+
 	viper.Set("project", projectID)
 	viper.Set("project", projectID)
 	color.New(color.FgGreen).Printf("Set the current project as %d\n", projectID)
 	color.New(color.FgGreen).Printf("Set the current project as %d\n", projectID)
 	err := viper.WriteConfig()
 	err := viper.WriteConfig()
@@ -226,6 +232,12 @@ func (c *CLIConfig) SetProject(projectID uint) error {
 }
 }
 
 
 func (c *CLIConfig) SetCluster(clusterID uint) error {
 func (c *CLIConfig) SetCluster(clusterID uint) error {
+	if config.Kubeconfig != "" || viper.IsSet("kubeconfig") {
+		viper.Set("kubeconfig", "")
+		color.New(color.FgBlue).Println("Removing local kubeconfig")
+		config.Kubeconfig = ""
+	}
+
 	viper.Set("cluster", clusterID)
 	viper.Set("cluster", clusterID)
 	color.New(color.FgGreen).Printf("Set the current cluster as %d\n", clusterID)
 	color.New(color.FgGreen).Printf("Set the current cluster as %d\n", clusterID)
 	err := viper.WriteConfig()
 	err := viper.WriteConfig()

+ 5 - 22
cli/cmd/portforward.go

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

+ 5 - 21
cli/cmd/run.go

@@ -264,30 +264,14 @@ func (p *PorterRunSharedConfig) setSharedConfig() error {
 	pID := cliConf.Project
 	pID := cliConf.Project
 	cID := cliConf.Cluster
 	cID := cliConf.Cluster
 
 
-	var kubeBytes []byte
+	kubeResp, err := p.Client.GetKubeconfig(context.TODO(), pID, cID, cliConf.Kubeconfig)
 
 
-	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)
-
-		if err != nil {
-			return err
-		}
-
-		kubeBytes, err = io.ReadAll(file)
-
-		if err != nil {
-			return err
-		}
+	if err != nil {
+		return err
 	}
 	}
 
 
+	kubeBytes := kubeResp.Kubeconfig
+
 	cmdConf, err := clientcmd.NewClientConfigFromBytes(kubeBytes)
 	cmdConf, err := clientcmd.NewClientConfigFromBytes(kubeBytes)
 
 
 	if err != nil {
 	if err != nil {