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:
@@ -3,6 +3,7 @@ package util
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/url"
|
||||
"os"
|
||||
@@ -128,7 +129,12 @@ func GetAPIServerFromKubeConfigBytes(kubeconfigBytes []byte) *net.IPNet {
|
||||
}
|
||||
|
||||
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 {
|
||||
return "", err
|
||||
}
|
||||
@@ -147,6 +153,32 @@ func ConvertToTempKubeconfigFile(kubeconfigBytes []byte) (string, error) {
|
||||
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 {
|
||||
configFlags := genericclioptions.NewConfigFlags(true)
|
||||
configFlags.WrapConfigFn = func(c *rest.Config) *rest.Config {
|
||||
|
||||
Reference in New Issue
Block a user