mirror of
https://github.com/kubenetworks/kubevpn.git
synced 2025-12-24 11:51:13 +08:00
refactor: change temp kubeconfig to ~/.kubevpn/tmp
This commit is contained in:
@@ -21,6 +21,8 @@ const (
|
|||||||
LogFile = "daemon.log"
|
LogFile = "daemon.log"
|
||||||
|
|
||||||
ConfigFile = "config.yaml"
|
ConfigFile = "config.yaml"
|
||||||
|
|
||||||
|
TmpDir = "tmp"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed config.yaml
|
//go:embed config.yaml
|
||||||
@@ -51,6 +53,14 @@ func init() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
err = os.MkdirAll(GetTempPath(), 0755)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
err = os.Chmod(GetTempPath(), 0755)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
path := filepath.Join(HomePath, ConfigFile)
|
path := filepath.Join(HomePath, ConfigFile)
|
||||||
_, err = os.Stat(path)
|
_, err = os.Stat(path)
|
||||||
@@ -85,3 +95,7 @@ func GetSyncthingPath() string {
|
|||||||
func GetConfigFilePath() string {
|
func GetConfigFilePath() string {
|
||||||
return filepath.Join(HomePath, ConfigFile)
|
return filepath.Join(HomePath, ConfigFile)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetTempPath() string {
|
||||||
|
return filepath.Join(HomePath, TmpDir)
|
||||||
|
}
|
||||||
|
|||||||
@@ -333,9 +333,9 @@ func SshJump(ctx context.Context, conf *SshConfig, flags *pflag.FlagSet, print b
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if print {
|
if print {
|
||||||
plog.G(ctx).Infof("Use temporary kubeconfig: %s", path)
|
plog.G(ctx).Infof("Use temp kubeconfig: %s", path)
|
||||||
} else {
|
} else {
|
||||||
plog.G(ctx).Debugf("Use temporary kubeconfig: %s", path)
|
plog.G(ctx).Debugf("Use temp kubeconfig: %s", path)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package util
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
@@ -128,7 +129,12 @@ func GetAPIServerFromKubeConfigBytes(kubeconfigBytes []byte) *net.IPNet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ConvertToTempKubeconfigFile(kubeconfigBytes []byte) (string, error) {
|
func ConvertToTempKubeconfigFile(kubeconfigBytes []byte) (string, error) {
|
||||||
temp, err := os.CreateTemp("", "*.kubeconfig")
|
pattern := "*.kubeconfig"
|
||||||
|
cluster, ns, _ := GetCluster(kubeconfigBytes)
|
||||||
|
if cluster != "" {
|
||||||
|
pattern = fmt.Sprintf("%s_%s_%s", cluster, ns, pattern)
|
||||||
|
}
|
||||||
|
temp, err := os.CreateTemp(config.GetTempPath(), pattern)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
@@ -147,6 +153,32 @@ func ConvertToTempKubeconfigFile(kubeconfigBytes []byte) (string, error) {
|
|||||||
return temp.Name(), nil
|
return temp.Name(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetCluster(kubeConfigBytes []byte) (cluster string, ns string, err error) {
|
||||||
|
var clientConfig clientcmd.ClientConfig
|
||||||
|
clientConfig, err = clientcmd.NewClientConfigFromBytes(kubeConfigBytes)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var rawConfig api.Config
|
||||||
|
rawConfig, err = clientConfig.RawConfig()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err = api.FlattenConfig(&rawConfig); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if rawConfig.Contexts == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
kubeContext := rawConfig.Contexts[rawConfig.CurrentContext]
|
||||||
|
if kubeContext == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
cluster = kubeContext.Cluster
|
||||||
|
ns = kubeContext.Namespace
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func InitFactory(kubeconfigBytes string, ns string) cmdutil.Factory {
|
func InitFactory(kubeconfigBytes string, ns string) cmdutil.Factory {
|
||||||
configFlags := genericclioptions.NewConfigFlags(true)
|
configFlags := genericclioptions.NewConfigFlags(true)
|
||||||
configFlags.WrapConfigFn = func(c *rest.Config) *rest.Config {
|
configFlags.WrapConfigFn = func(c *rest.Config) *rest.Config {
|
||||||
|
|||||||
Reference in New Issue
Block a user