Kaynağa Gözat

resolve relative filepaths

Alexander Belanger 5 yıl önce
ebeveyn
işleme
956f7c28b8

+ 80 - 5
cli/cmd/connect/kubeconfig.go

@@ -62,7 +62,22 @@ func Kubeconfig(
 			for _, action := range saCandidate.Actions {
 				switch action.Name {
 				case models.ClusterCADataAction:
-					resolveAction, err := resolveClusterCAAction(action.Filename)
+					absKubeconfigPath, err := local.ResolveKubeconfigPath(kubeconfigPath)
+
+					if err != nil {
+						return err
+					}
+
+					filename, err := utils.GetFileReferenceFromKubeconfig(
+						action.Filename,
+						absKubeconfigPath,
+					)
+
+					if err != nil {
+						return err
+					}
+
+					resolveAction, err := resolveClusterCAAction(filename)
 
 					if err != nil {
 						return err
@@ -70,7 +85,22 @@ func Kubeconfig(
 
 					resolvers = append(resolvers, resolveAction)
 				case models.ClientCertDataAction:
-					resolveAction, err := resolveClientCertAction(action.Filename)
+					absKubeconfigPath, err := local.ResolveKubeconfigPath(kubeconfigPath)
+
+					if err != nil {
+						return err
+					}
+
+					filename, err := utils.GetFileReferenceFromKubeconfig(
+						action.Filename,
+						absKubeconfigPath,
+					)
+
+					if err != nil {
+						return err
+					}
+
+					resolveAction, err := resolveClientCertAction(filename)
 
 					if err != nil {
 						return err
@@ -78,7 +108,22 @@ func Kubeconfig(
 
 					resolvers = append(resolvers, resolveAction)
 				case models.ClientKeyDataAction:
-					resolveAction, err := resolveClientKeyAction(action.Filename)
+					absKubeconfigPath, err := local.ResolveKubeconfigPath(kubeconfigPath)
+
+					if err != nil {
+						return err
+					}
+
+					filename, err := utils.GetFileReferenceFromKubeconfig(
+						action.Filename,
+						absKubeconfigPath,
+					)
+
+					if err != nil {
+						return err
+					}
+
+					resolveAction, err := resolveClientKeyAction(filename)
 
 					if err != nil {
 						return err
@@ -86,7 +131,22 @@ func Kubeconfig(
 
 					resolvers = append(resolvers, resolveAction)
 				case models.OIDCIssuerDataAction:
-					resolveAction, err := resolveOIDCIssuerAction(action.Filename)
+					absKubeconfigPath, err := local.ResolveKubeconfigPath(kubeconfigPath)
+
+					if err != nil {
+						return err
+					}
+
+					filename, err := utils.GetFileReferenceFromKubeconfig(
+						action.Filename,
+						absKubeconfigPath,
+					)
+
+					if err != nil {
+						return err
+					}
+
+					resolveAction, err := resolveOIDCIssuerAction(filename)
 
 					if err != nil {
 						return err
@@ -94,7 +154,22 @@ func Kubeconfig(
 
 					resolvers = append(resolvers, resolveAction)
 				case models.TokenDataAction:
-					resolveAction, err := resolveTokenDataAction(action.Filename)
+					absKubeconfigPath, err := local.ResolveKubeconfigPath(kubeconfigPath)
+
+					if err != nil {
+						return err
+					}
+
+					filename, err := utils.GetFileReferenceFromKubeconfig(
+						action.Filename,
+						absKubeconfigPath,
+					)
+
+					if err != nil {
+						return err
+					}
+
+					resolveAction, err := resolveTokenDataAction(filename)
 
 					if err != nil {
 						return err

+ 33 - 0
cli/cmd/utils/file.go

@@ -0,0 +1,33 @@
+package utils
+
+import (
+	"fmt"
+	"os"
+	"path/filepath"
+)
+
+func GetFileReferenceFromKubeconfig(
+	filename string,
+	kubeconfigPath string,
+) (string, error) {
+	if info, err := os.Stat(filename); os.IsNotExist(err) && !info.IsDir() {
+		// attempt to discover the filename relative to the kubeconfig location
+		absPath, err := filepath.Abs(kubeconfigPath)
+
+		if err != nil {
+			return "", err
+		}
+
+		fPath := filepath.Join(filepath.Dir(absPath), filename)
+
+		if info, err := os.Stat(fPath); !os.IsNotExist(err) && !info.IsDir() {
+			return fPath, nil
+		} else {
+			return "", fmt.Errorf("%s not found", filename)
+		}
+	} else if info.IsDir() {
+		return "", fmt.Errorf("%s is a directory", filename)
+	}
+
+	return filename, nil
+}

+ 28 - 12
internal/kubernetes/local/kubeconfig.go

@@ -19,19 +19,10 @@ import (
 // options set on the host, or an explicit kubeconfig path. It then strips the kubeconfig
 // of contexts not specified in the contexts array, and returns generate kubeconfig.
 func GetKubeconfigFromHost(kubeconfigPath string, contexts []string) ([]byte, error) {
-	envVarName := clientcmd.RecommendedConfigPathEnvVar
-
-	if kubeconfigPath != "" {
-		if _, err := os.Stat(kubeconfigPath); os.IsNotExist(err) {
-			// the specified kubeconfig does not exist so fallback to other options
-			kubeconfigPath = ""
-		}
-	}
+	kubeconfigPath, err := ResolveKubeconfigPath(kubeconfigPath)
 
-	if kubeconfigPath == "" && os.Getenv(envVarName) == "" {
-		if home := homedir.HomeDir(); home != "" {
-			kubeconfigPath = filepath.Join(home, ".kube", "config")
-		}
+	if err != nil {
+		return nil, err
 	}
 
 	loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
@@ -67,6 +58,31 @@ func GetKubeconfigFromHost(kubeconfigPath string, contexts []string) ([]byte, er
 	return clientcmd.Write(strippedRawConf)
 }
 
+// ResolveKubeconfigPath finds the path to a kubeconfig, first searching for the
+// passed string, then in the home directory, then as an env variable.
+func ResolveKubeconfigPath(kubeconfigPath string) (string, error) {
+	envVarName := clientcmd.RecommendedConfigPathEnvVar
+
+	if kubeconfigPath != "" {
+		if _, err := os.Stat(kubeconfigPath); os.IsNotExist(err) {
+			// the specified kubeconfig does not exist, throw error
+			return "", fmt.Errorf("kubeconfig not found: %s does not exist", kubeconfigPath)
+		}
+	}
+
+	if kubeconfigPath == "" {
+		if os.Getenv(envVarName) == "" {
+			if home := homedir.HomeDir(); home != "" {
+				kubeconfigPath = filepath.Join(home, ".kube", "config")
+			}
+		} else {
+			kubeconfigPath = os.Getenv(envVarName)
+		}
+	}
+
+	return kubeconfigPath, nil
+}
+
 // GetConfigFromHostWithCertData gets the kubeconfig using default options set on the host:
 // the kubeconfig can either be retrieved from a specified path or an environment variable.
 // This function only outputs a clientcmd that uses the allowedContexts.