hotfix: fix --remote-kubeconfig override temp kubeconfig (#699)

* hotfix: fix --remote-kubeconfig override temp kubeconfig

* hotfix: ignore ssh set env failed

* hotfix: use unix timestamp
This commit is contained in:
naison
2025-08-10 20:05:35 +08:00
committed by GitHub
parent f59aa8aca1
commit 47b2f0006c
8 changed files with 30 additions and 21 deletions

View File

@@ -259,7 +259,7 @@ func GetConnectionIDByConfig(cmd *cobra.Command, config Config) (string, error)
var file string
defer os.Remove(file)
if !sshConf.IsEmpty() {
file, err = pkgssh.SshJump(cmd.Context(), sshConf, kubeConfigBytes, false)
file, err = pkgssh.SshJump(cmd.Context(), sshConf, kubeConfigBytes, "", false)
} else {
file, err = util.ConvertToTempKubeconfigFile(kubeConfigBytes, "")
}

View File

@@ -2,14 +2,18 @@ package action
import (
"context"
"fmt"
"io"
"os"
"path/filepath"
"time"
"github.com/golang/protobuf/proto"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc"
"github.com/wencaiwulue/kubevpn/v2/pkg/config"
"github.com/wencaiwulue/kubevpn/v2/pkg/daemon/rpc"
"github.com/wencaiwulue/kubevpn/v2/pkg/handler"
plog "github.com/wencaiwulue/kubevpn/v2/pkg/log"
@@ -113,7 +117,11 @@ func (svr *Server) redirectConnectToSudoDaemon(req *rpc.ConnectRequest, resp rpc
})
if !sshConf.IsEmpty() {
file, err = ssh.SshJump(sshCtx, sshConf, []byte(req.KubeconfigBytes), true)
var path string
if sshConf.RemoteKubeconfig != "" {
path = filepath.Join(config.GetTempPath(), fmt.Sprintf("%s_%d", sshConf.GenKubeconfigIdentify(), time.Now().Unix()))
}
file, err = ssh.SshJump(sshCtx, sshConf, []byte(req.KubeconfigBytes), path, true)
if err != nil {
return err
}

View File

@@ -116,7 +116,7 @@ func disconnectByKubeconfig(ctx context.Context, svr *Server, kubeconfigBytes st
var err error
var sshConf = ssh.ParseSshFromRPC(jump)
if !sshConf.IsEmpty() {
file, err = ssh.SshJump(ctx, sshConf, []byte(kubeconfigBytes), false)
file, err = ssh.SshJump(ctx, sshConf, []byte(kubeconfigBytes), "", false)
} else {
file, err = util.ConvertToTempKubeconfigFile([]byte(kubeconfigBytes), "")
}

View File

@@ -38,7 +38,7 @@ func (svr *Server) Proxy(resp rpc.Daemon_ProxyServer) (err error) {
var file string
defer os.Remove(file)
if !sshConf.IsEmpty() {
file, err = ssh.SshJump(ctx, sshConf, []byte(req.KubeconfigBytes), false)
file, err = ssh.SshJump(ctx, sshConf, []byte(req.KubeconfigBytes), "", false)
} else {
file, err = util.ConvertToTempKubeconfigFile([]byte(req.KubeconfigBytes), "")
}

View File

@@ -25,7 +25,7 @@ func (svr *Server) Reset(resp rpc.Daemon_ResetServer) error {
var sshConf = ssh.ParseSshFromRPC(req.SshJump)
var ctx = plog.WithLogger(resp.Context(), logger)
if !sshConf.IsEmpty() {
file, err = ssh.SshJump(ctx, sshConf, []byte(req.KubeconfigBytes), false)
file, err = ssh.SshJump(ctx, sshConf, []byte(req.KubeconfigBytes), "", false)
} else {
file, err = util.ConvertToTempKubeconfigFile([]byte(req.KubeconfigBytes), "")
}

View File

@@ -5,9 +5,12 @@ import (
"fmt"
"io"
"os"
"path/filepath"
"time"
"google.golang.org/grpc"
"github.com/wencaiwulue/kubevpn/v2/pkg/config"
"github.com/wencaiwulue/kubevpn/v2/pkg/daemon/rpc"
"github.com/wencaiwulue/kubevpn/v2/pkg/handler"
plog "github.com/wencaiwulue/kubevpn/v2/pkg/log"
@@ -102,7 +105,11 @@ func (svr *Server) Sync(resp rpc.Daemon_SyncServer) (err error) {
return nil
})
if !sshConf.IsEmpty() {
file, err = ssh.SshJump(sshCtx, sshConf, []byte(req.KubeconfigBytes), false)
var path string
if sshConf.RemoteKubeconfig != "" {
path = filepath.Join(config.GetTempPath(), fmt.Sprintf("%s_%d", sshConf.GenKubeconfigIdentify(), time.Now().Unix()))
}
file, err = ssh.SshJump(sshCtx, sshConf, []byte(req.KubeconfigBytes), path, false)
} else {
file, err = util.ConvertToTempKubeconfigFile([]byte(req.KubeconfigBytes), "")
}

View File

@@ -29,7 +29,7 @@ func (svr *Server) Uninstall(resp rpc.Daemon_UninstallServer) (err error) {
var sshConf = ssh.ParseSshFromRPC(req.SshJump)
var ctx = plog.WithLogger(resp.Context(), logger)
if !sshConf.IsEmpty() {
file, err = ssh.SshJump(ctx, sshConf, []byte(req.KubeconfigBytes), false)
file, err = ssh.SshJump(ctx, sshConf, []byte(req.KubeconfigBytes), "", false)
} else {
file, err = util.ConvertToTempKubeconfigFile([]byte(req.KubeconfigBytes), "")
}

View File

@@ -10,7 +10,6 @@ import (
"net/netip"
"net/url"
"os"
"path/filepath"
"strconv"
"sync"
"time"
@@ -69,10 +68,7 @@ func RemoteRun(client *gossh.Client, cmd string, env map[string]string) (output
for k, v := range env {
// /etc/ssh/sshd_config
// AcceptEnv DEBIAN_FRONTEND
if err = session.Setenv(k, v); err != nil {
plog.G(context.Background()).Warn(err)
err = nil
}
_ = session.Setenv(k, v)
}
var out bytes.Buffer
var er bytes.Buffer
@@ -139,13 +135,10 @@ func PortMapUntil(ctx context.Context, conf *SshConfig, remote, local netip.Addr
return nil
}
func SshJump(ctx context.Context, conf *SshConfig, kubeconfigBytes []byte, print bool) (path string, err error) {
func SshJump(ctx context.Context, conf *SshConfig, kubeconfigBytes []byte, tempPath string, print bool) (path string, err error) {
if len(conf.RemoteKubeconfig) != 0 {
var stdout []byte
var stderr []byte
if conf.RemoteKubeconfig[0] == '~' {
conf.RemoteKubeconfig = filepath.Join("/home", conf.User, conf.RemoteKubeconfig[1:])
}
// pre-check network ip connect
var cli *gossh.Client
cli, err = DialSshRemote(ctx, conf, ctx.Done())
@@ -154,9 +147,11 @@ func SshJump(ctx context.Context, conf *SshConfig, kubeconfigBytes []byte, print
}
defer cli.Close()
stdout, stderr, err = RemoteRun(cli,
fmt.Sprintf("sh -c 'kubectl config view --flatten --raw --kubeconfig %s || minikube kubectl -- config view --flatten --raw --kubeconfig %s'",
fmt.Sprintf("sh -c 'kubectl config view --flatten --raw --kubeconfig %s || minikube kubectl -- config view --flatten --raw --kubeconfig %s || cat %s'",
conf.RemoteKubeconfig,
conf.RemoteKubeconfig),
conf.RemoteKubeconfig,
conf.RemoteKubeconfig,
),
map[string]string{clientcmd.RecommendedConfigPathEnvVar: conf.RemoteKubeconfig},
)
if err != nil {
@@ -168,7 +163,6 @@ func SshJump(ctx context.Context, conf *SshConfig, kubeconfigBytes []byte, print
return
}
kubeconfigBytes = bytes.TrimSpace(stdout)
path = filepath.Join(config.GetTempPath(), conf.GenKubeconfigIdentify())
}
var clientConfig clientcmd.ClientConfig
clientConfig, err = clientcmd.NewClientConfigFromBytes(kubeconfigBytes)
@@ -284,7 +278,7 @@ func SshJump(ctx context.Context, conf *SshConfig, kubeconfigBytes []byte, print
plog.G(ctx).Errorf("failed to marshal config: %v", err)
return
}
path, err = pkgutil.ConvertToTempKubeconfigFile(marshal, path)
path, err = pkgutil.ConvertToTempKubeconfigFile(marshal, tempPath)
if err != nil {
plog.G(ctx).Errorf("failed to write kubeconfig: %v", err)
return
@@ -302,7 +296,7 @@ func SshJump(ctx context.Context, conf *SshConfig, kubeconfigBytes []byte, print
}
func SshJumpAndSetEnv(ctx context.Context, sshConf *SshConfig, kubeconfigBytes []byte, print bool) error {
path, err := SshJump(ctx, sshConf, kubeconfigBytes, print)
path, err := SshJump(ctx, sshConf, kubeconfigBytes, "", print)
if err != nil {
return err
}