feat: add options --connect-namespace to proxy and dev mode (#508)

This commit is contained in:
naison
2025-04-03 20:44:59 +08:00
committed by GitHub
parent 21087fc708
commit 804708aabe
14 changed files with 528 additions and 418 deletions

View File

@@ -62,7 +62,7 @@ type Options struct {
rollbackFuncList []func() error
}
func (option *Options) Main(ctx context.Context, sshConfig *pkgssh.SshConfig, config *Config, hostConfig *HostConfig, imagePullSecretName string) error {
func (option *Options) Main(ctx context.Context, sshConfig *pkgssh.SshConfig, config *Config, hostConfig *HostConfig, imagePullSecretName string, connectNamespace string) error {
mode := typescontainer.NetworkMode(option.ContainerOptions.netMode.NetworkMode())
if mode.IsContainer() {
plog.G(ctx).Infof("Network mode container is %s", mode.ConnectedContainer())
@@ -79,7 +79,7 @@ func (option *Options) Main(ctx context.Context, sshConfig *pkgssh.SshConfig, co
}
// Connect to cluster, in container or host
err := option.Connect(ctx, sshConfig, imagePullSecretName, hostConfig.PortBindings)
err := option.Connect(ctx, sshConfig, imagePullSecretName, hostConfig.PortBindings, connectNamespace)
if err != nil {
plog.G(ctx).Errorf("Connect to cluster failed, err: %v", err)
return err
@@ -89,7 +89,7 @@ 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) error {
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 {
@@ -118,6 +118,7 @@ func (option *Options) Connect(ctx context.Context, sshConfig *pkgssh.SshConfig,
ImagePullSecretName: imagePullSecretName,
Level: int32(util.If(config.Debug, log.DebugLevel, log.InfoLevel)),
SshJump: sshConfig.ToRPC(),
ConnectNamespace: connectNamespace,
}
option.AddRollbackFunc(func() error {
resp, err := daemonCli.Disconnect(ctx, &rpc.DisconnectRequest{
@@ -142,7 +143,7 @@ func (option *Options) Connect(ctx context.Context, sshConfig *pkgssh.SshConfig,
}
if option.ConnectMode == ConnectModeContainer {
name, err := option.CreateConnectContainer(ctx, portBindings)
name, err := option.CreateConnectContainer(ctx, portBindings, connectNamespace)
if err != nil {
return err
}
@@ -225,7 +226,7 @@ func (option *Options) Dev(ctx context.Context, config *Config, hostConfig *Host
return configList.Run(ctx)
}
func (option *Options) CreateConnectContainer(ctx context.Context, portBindings nat.PortMap) (*string, error) {
func (option *Options) CreateConnectContainer(ctx context.Context, portBindings nat.PortMap, connectNamespace string) (*string, error) {
portMap, portSet, err := option.GetExposePort(portBindings)
if err != nil {
return nil, err
@@ -243,9 +244,27 @@ func (option *Options) CreateConnectContainer(ctx context.Context, portBindings
var entrypoint []string
if option.NoProxy {
entrypoint = []string{"kubevpn", "connect", "--foreground", "-n", option.Namespace, "--kubeconfig", "/root/.kube/config", "--image", config.Image, "--netstack", string(option.Engine)}
entrypoint = []string{
"kubevpn",
"connect",
"--foreground",
"-n", option.Namespace,
"--kubeconfig", "/root/.kube/config",
"--image", config.Image,
"--netstack", string(option.Engine),
}
} else {
entrypoint = []string{"kubevpn", "proxy", option.Workload, "--foreground", "-n", option.Namespace, "--kubeconfig", "/root/.kube/config", "--image", config.Image, "--netstack", string(option.Engine)}
entrypoint = []string{
"kubevpn",
"proxy",
option.Workload,
"--foreground",
"-n", option.Namespace,
"--kubeconfig", "/root/.kube/config",
"--image", config.Image,
"--netstack", string(option.Engine),
"--connect-namespace", connectNamespace,
}
for k, v := range option.Headers {
entrypoint = append(entrypoint, "--headers", fmt.Sprintf("%s=%s", k, v))
}