mirror of
https://github.com/kubenetworks/kubevpn.git
synced 2025-12-24 11:51:13 +08:00
refactor: return error if get nil daemon client (#553)
This commit is contained in:
@@ -30,7 +30,10 @@ func (svr *Server) Clone(req *rpc.CloneRequest, resp rpc.Daemon_CloneServer) (er
|
||||
Level: req.Level,
|
||||
OriginKubeconfigPath: req.OriginKubeconfigPath,
|
||||
}
|
||||
cli := svr.GetClient(false)
|
||||
cli, err := svr.GetClient(false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
connResp, err := cli.Connect(resp.Context(), connReq)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -2,9 +2,9 @@ package action
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/spf13/pflag"
|
||||
|
||||
@@ -84,9 +84,9 @@ func (svr *Server) ConnectFork(req *rpc.ConnectRequest, resp rpc.Daemon_ConnectF
|
||||
}
|
||||
|
||||
func (svr *Server) redirectConnectForkToSudoDaemon(req *rpc.ConnectRequest, resp rpc.Daemon_ConnectServer, logger *log.Logger) (err error) {
|
||||
cli := svr.GetClient(true)
|
||||
if cli == nil {
|
||||
return fmt.Errorf("sudo daemon not start")
|
||||
cli, err := svr.GetClient(true)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "sudo daemon not start")
|
||||
}
|
||||
var sshConf = ssh.ParseSshFromRPC(req.SshJump)
|
||||
file, err := util.ConvertToTempKubeconfigFile([]byte(req.KubeconfigBytes))
|
||||
|
||||
@@ -2,10 +2,10 @@ package action
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/spf13/pflag"
|
||||
"google.golang.org/grpc/codes"
|
||||
@@ -99,9 +99,9 @@ func (svr *Server) Connect(req *rpc.ConnectRequest, resp rpc.Daemon_ConnectServe
|
||||
}
|
||||
|
||||
func (svr *Server) redirectToSudoDaemon(req *rpc.ConnectRequest, resp rpc.Daemon_ConnectServer, logger *log.Logger) (e error) {
|
||||
cli := svr.GetClient(true)
|
||||
if cli == nil {
|
||||
return fmt.Errorf("sudo daemon not start")
|
||||
cli, err := svr.GetClient(true)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "sudo daemon not start")
|
||||
}
|
||||
var sshConf = ssh.ParseSshFromRPC(req.SshJump)
|
||||
file, err := util.ConvertToTempKubeconfigFile([]byte(req.KubeconfigBytes))
|
||||
|
||||
@@ -2,10 +2,10 @@ package action
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/spf13/pflag"
|
||||
"k8s.io/apimachinery/pkg/util/sets"
|
||||
@@ -104,9 +104,9 @@ func (svr *Server) Disconnect(req *rpc.DisconnectRequest, resp rpc.Daemon_Discon
|
||||
}
|
||||
|
||||
if !svr.IsSudo {
|
||||
cli := svr.GetClient(true)
|
||||
if cli == nil {
|
||||
return fmt.Errorf("sudo daemon not start")
|
||||
cli, err := svr.GetClient(true)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "sudo daemon not start")
|
||||
}
|
||||
connResp, err := cli.Disconnect(resp.Context(), req)
|
||||
if err != nil {
|
||||
@@ -149,8 +149,8 @@ func disconnectByKubeConfig(ctx context.Context, svr *Server, kubeconfigBytes st
|
||||
}
|
||||
|
||||
func disconnect(ctx context.Context, svr *Server, connect *handler.ConnectOptions) {
|
||||
client := svr.GetClient(false)
|
||||
if client == nil {
|
||||
_, err := svr.GetClient(false)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if svr.connect != nil {
|
||||
|
||||
@@ -2,9 +2,9 @@ package action
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/spf13/pflag"
|
||||
"k8s.io/utils/ptr"
|
||||
@@ -67,9 +67,9 @@ func (svr *Server) Proxy(req *rpc.ProxyRequest, resp rpc.Daemon_ProxyServer) (e
|
||||
}
|
||||
}()
|
||||
|
||||
daemonClient := svr.GetClient(false)
|
||||
if daemonClient == nil {
|
||||
return fmt.Errorf("daemon is not avaliable")
|
||||
cli, err := svr.GetClient(false)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "daemon is not available")
|
||||
}
|
||||
|
||||
connectNs, err := util.DetectConnectNamespace(ctx, connect.GetFactory(), req.ConnectNamespace)
|
||||
@@ -92,7 +92,7 @@ func (svr *Server) Proxy(req *rpc.ProxyRequest, resp rpc.Daemon_ProxyServer) (e
|
||||
} else {
|
||||
plog.G(ctx).Infof("Disconnecting from another cluster...")
|
||||
var disconnectResp rpc.Daemon_DisconnectClient
|
||||
disconnectResp, err = daemonClient.Disconnect(ctx, &rpc.DisconnectRequest{
|
||||
disconnectResp, err = cli.Disconnect(ctx, &rpc.DisconnectRequest{
|
||||
ID: ptr.To[int32](0),
|
||||
})
|
||||
if err != nil {
|
||||
@@ -114,7 +114,7 @@ func (svr *Server) Proxy(req *rpc.ProxyRequest, resp rpc.Daemon_ProxyServer) (e
|
||||
if svr.connect == nil {
|
||||
plog.G(ctx).Debugf("Connectting to cluster")
|
||||
var connResp rpc.Daemon_ConnectClient
|
||||
connResp, err = daemonClient.Connect(ctx, convert(req))
|
||||
connResp, err = cli.Connect(ctx, convert(req))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ type Server struct {
|
||||
rpc.UnimplementedDaemonServer
|
||||
|
||||
Cancel func()
|
||||
GetClient func(isSudo bool) rpc.DaemonClient
|
||||
GetClient func(isSudo bool) (rpc.DaemonClient, error)
|
||||
IsSudo bool
|
||||
LogFile *lumberjack.Logger
|
||||
Lock sync.Mutex
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"os"
|
||||
"time"
|
||||
|
||||
pkgerr "github.com/pkg/errors"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
"google.golang.org/grpc/health/grpc_health_v1"
|
||||
@@ -23,22 +24,22 @@ import (
|
||||
|
||||
var daemonClient, sudoDaemonClient rpc.DaemonClient
|
||||
|
||||
func GetClient(isSudo bool) (cli rpc.DaemonClient) {
|
||||
func GetClient(isSudo bool) (cli rpc.DaemonClient, err error) {
|
||||
sockPath := config.GetSockPath(isSudo)
|
||||
if _, err := os.Stat(sockPath); errors.Is(err, os.ErrNotExist) {
|
||||
return nil
|
||||
if _, err = os.Stat(sockPath); errors.Is(err, os.ErrNotExist) {
|
||||
return nil, err
|
||||
}
|
||||
if isSudo && sudoDaemonClient != nil {
|
||||
return sudoDaemonClient
|
||||
return sudoDaemonClient, nil
|
||||
}
|
||||
if !isSudo && daemonClient != nil {
|
||||
return daemonClient
|
||||
return daemonClient, nil
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
conn, err := grpc.DialContext(ctx, "unix:"+sockPath, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
||||
conn, err := grpc.NewClient("unix:"+sockPath, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
||||
if err != nil {
|
||||
return nil
|
||||
return nil, err
|
||||
}
|
||||
defer func() {
|
||||
if cli == nil {
|
||||
@@ -50,10 +51,10 @@ func GetClient(isSudo bool) (cli rpc.DaemonClient) {
|
||||
var response *grpc_health_v1.HealthCheckResponse
|
||||
response, err = healthClient.Check(ctx, &grpc_health_v1.HealthCheckRequest{})
|
||||
if err != nil {
|
||||
return nil
|
||||
return nil, err
|
||||
}
|
||||
if response.Status != grpc_health_v1.HealthCheckResponse_SERVING {
|
||||
return nil
|
||||
return nil, fmt.Errorf("health check failed: %v", response.Status)
|
||||
}
|
||||
|
||||
cli = rpc.NewDaemonClient(conn)
|
||||
@@ -66,7 +67,7 @@ func GetClient(isSudo bool) (cli rpc.DaemonClient) {
|
||||
var quitStream rpc.Daemon_QuitClient
|
||||
quitStream, err = cli.Quit(ctx, &rpc.QuitRequest{})
|
||||
if err != nil {
|
||||
return nil
|
||||
return nil, err
|
||||
}
|
||||
err = util.PrintGRPCStream[rpc.QuitResponse](quitStream, nil)
|
||||
return
|
||||
@@ -77,7 +78,7 @@ func GetClient(isSudo bool) (cli rpc.DaemonClient) {
|
||||
} else {
|
||||
daemonClient = cli
|
||||
}
|
||||
return cli
|
||||
return cli, nil
|
||||
}
|
||||
|
||||
func GetClientWithoutCache(ctx context.Context, isSudo bool) (cli rpc.DaemonClient, conn *grpc.ClientConn, err error) {
|
||||
@@ -86,7 +87,7 @@ func GetClientWithoutCache(ctx context.Context, isSudo bool) (cli rpc.DaemonClie
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return
|
||||
}
|
||||
conn, err = grpc.DialContext(ctx, "unix:"+sockPath, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
||||
conn, err = grpc.NewClient("unix:"+sockPath, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -102,6 +103,7 @@ func GetClientWithoutCache(ctx context.Context, isSudo bool) (cli rpc.DaemonClie
|
||||
return
|
||||
}
|
||||
if response.Status != grpc_health_v1.HealthCheckResponse_SERVING {
|
||||
err = fmt.Errorf("health check failed: %v", response.Status)
|
||||
return
|
||||
}
|
||||
cli = rpc.NewDaemonClient(conn)
|
||||
@@ -120,14 +122,14 @@ func StartupDaemon(ctx context.Context, path ...string) error {
|
||||
return err
|
||||
}
|
||||
// normal daemon
|
||||
if daemonClient = GetClient(false); daemonClient == nil {
|
||||
if daemonClient, err = GetClient(false); daemonClient == nil {
|
||||
if err = runDaemon(ctx, exe, false); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// sudo daemon
|
||||
if sudoDaemonClient = GetClient(true); sudoDaemonClient == nil {
|
||||
if sudoDaemonClient, err = GetClient(true); sudoDaemonClient == nil {
|
||||
if err = runDaemon(ctx, exe, true); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -136,13 +138,13 @@ func StartupDaemon(ctx context.Context, path ...string) error {
|
||||
}
|
||||
|
||||
func runDaemon(ctx context.Context, exe string, isSudo bool) error {
|
||||
cli := GetClient(isSudo)
|
||||
if cli != nil {
|
||||
_, err := GetClient(isSudo)
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
pidPath := config.GetPidPath(isSudo)
|
||||
err := os.Remove(pidPath)
|
||||
err = os.Remove(pidPath)
|
||||
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||
return err
|
||||
}
|
||||
@@ -166,9 +168,9 @@ func runDaemon(ctx context.Context, exe string, isSudo bool) error {
|
||||
}
|
||||
}
|
||||
|
||||
client := GetClient(isSudo)
|
||||
if client == nil {
|
||||
return fmt.Errorf("failed to get daemon server client")
|
||||
_, err = GetClient(isSudo)
|
||||
if err != nil {
|
||||
return pkgerr.Wrap(err, "failed to get daemon server client")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
typescontainer "github.com/docker/docker/api/types/container"
|
||||
"github.com/docker/go-connections/nat"
|
||||
"github.com/google/uuid"
|
||||
pkgerr "github.com/pkg/errors"
|
||||
log "github.com/sirupsen/logrus"
|
||||
v1 "k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
@@ -91,9 +92,9 @@ func (option *Options) Main(ctx context.Context, sshConfig *pkgssh.SshConfig, co
|
||||
// Connect to cluster network on docker container or host
|
||||
func (option *Options) Connect(ctx context.Context, sshConfig *pkgssh.SshConfig, imagePullSecretName string, portBindings nat.PortMap, connectNamespace string) error {
|
||||
if option.ConnectMode == ConnectModeHost {
|
||||
daemonCli := daemon.GetClient(false)
|
||||
if daemonCli == nil {
|
||||
return fmt.Errorf("get nil daemon client")
|
||||
cli, err := daemon.GetClient(false)
|
||||
if err != nil {
|
||||
return pkgerr.Wrap(err, "get nil daemon client")
|
||||
}
|
||||
kubeConfigBytes, ns, err := util.ConvertToKubeConfigBytes(option.factory)
|
||||
if err != nil {
|
||||
@@ -121,7 +122,7 @@ func (option *Options) Connect(ctx context.Context, sshConfig *pkgssh.SshConfig,
|
||||
ConnectNamespace: connectNamespace,
|
||||
}
|
||||
option.AddRollbackFunc(func() error {
|
||||
resp, err := daemonCli.Disconnect(ctx, &rpc.DisconnectRequest{
|
||||
resp, err := cli.Disconnect(ctx, &rpc.DisconnectRequest{
|
||||
KubeconfigBytes: ptr.To(string(kubeConfigBytes)),
|
||||
Namespace: ptr.To(ns),
|
||||
SshJump: sshConfig.ToRPC(),
|
||||
@@ -133,7 +134,7 @@ func (option *Options) Connect(ctx context.Context, sshConfig *pkgssh.SshConfig,
|
||||
return nil
|
||||
})
|
||||
var resp rpc.Daemon_ProxyClient
|
||||
resp, err = daemonCli.Proxy(ctx, req)
|
||||
resp, err = cli.Proxy(ctx, req)
|
||||
if err != nil {
|
||||
plog.G(ctx).Errorf("Connect to cluster error: %s", err.Error())
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user