mirror of
https://github.com/kubenetworks/kubevpn.git
synced 2025-12-24 11:51:13 +08:00
feat: add APIServer ip to route table if ssh info is not empty (#361)
This commit is contained in:
@@ -110,6 +110,11 @@ func CmdClone(f cmdutil.Factory) *cobra.Command {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !sshConf.IsEmpty() {
|
||||
if ip := util.GetAPIServerFromKubeConfigBytes(bytes); ip != nil {
|
||||
extraRoute.ExtraCIDR = append(extraRoute.ExtraCIDR, ip.String())
|
||||
}
|
||||
}
|
||||
logLevel := log.InfoLevel
|
||||
if config.Debug {
|
||||
logLevel = log.DebugLevel
|
||||
|
||||
@@ -79,6 +79,11 @@ func CmdConnect(f cmdutil.Factory) *cobra.Command {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !sshConf.IsEmpty() {
|
||||
if ip := util.GetAPIServerFromKubeConfigBytes(bytes); ip != nil {
|
||||
extraRoute.ExtraCIDR = append(extraRoute.ExtraCIDR, ip.String())
|
||||
}
|
||||
}
|
||||
logLevel := log.InfoLevel
|
||||
if config.Debug {
|
||||
logLevel = log.DebugLevel
|
||||
|
||||
@@ -112,6 +112,11 @@ func CmdProxy(f cmdutil.Factory) *cobra.Command {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !sshConf.IsEmpty() {
|
||||
if ip := util.GetAPIServerFromKubeConfigBytes(bytes); ip != nil {
|
||||
extraRoute.ExtraCIDR = append(extraRoute.ExtraCIDR, ip.String())
|
||||
}
|
||||
}
|
||||
// todo 将 doConnect 方法封装?内部使用 client 发送到daemon?
|
||||
cli := daemon.GetClient(false)
|
||||
logLevel := log.InfoLevel
|
||||
|
||||
@@ -130,6 +130,11 @@ func (option *Options) Connect(ctx context.Context, sshConfig *pkgssh.SshConfig,
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !sshConfig.IsEmpty() {
|
||||
if ip := util.GetAPIServerFromKubeConfigBytes(kubeConfigBytes); ip != nil {
|
||||
option.ExtraRouteInfo.ExtraCIDR = append(option.ExtraRouteInfo.ExtraCIDR, ip.String())
|
||||
}
|
||||
}
|
||||
logLevel := log.InfoLevel
|
||||
if config.Debug {
|
||||
logLevel = log.DebugLevel
|
||||
|
||||
@@ -99,6 +99,10 @@ func (config *SshConfig) ToRPC() *rpc.SshJump {
|
||||
}
|
||||
}
|
||||
|
||||
func (config *SshConfig) IsEmpty() bool {
|
||||
return config.ConfigAlias == "" && config.Addr == "" && config.Jump == ""
|
||||
}
|
||||
|
||||
func AddSshFlags(flags *pflag.FlagSet, sshConf *SshConfig) {
|
||||
// for ssh jumper host
|
||||
flags.StringVar(&sshConf.Addr, "ssh-addr", "", "Optional ssh jump server address to dial as <hostname>:<port>, eg: 127.0.0.1:22")
|
||||
|
||||
@@ -3,6 +3,8 @@ package util
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net"
|
||||
"net/url"
|
||||
"os"
|
||||
"reflect"
|
||||
"unsafe"
|
||||
@@ -96,6 +98,36 @@ func ConvertToKubeConfigBytes(factory cmdutil.Factory) ([]byte, string, error) {
|
||||
return marshal, namespace, nil
|
||||
}
|
||||
|
||||
func GetAPIServerFromKubeConfigBytes(kubeconfigBytes []byte) *net.IPNet {
|
||||
kubeConfig, err := clientcmd.RESTConfigFromKubeConfig(kubeconfigBytes)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
var host string
|
||||
host, _, err = net.SplitHostPort(kubeConfig.Host)
|
||||
if err != nil {
|
||||
u, err2 := url.Parse(kubeConfig.Host)
|
||||
if err2 != nil {
|
||||
return nil
|
||||
}
|
||||
host, _, err = net.SplitHostPort(u.Host)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
ip := net.ParseIP(host)
|
||||
if ip == nil {
|
||||
return nil
|
||||
}
|
||||
var mask net.IPMask
|
||||
if ip.To4() != nil {
|
||||
mask = net.CIDRMask(32, 32)
|
||||
} else {
|
||||
mask = net.CIDRMask(128, 128)
|
||||
}
|
||||
return &net.IPNet{IP: ip, Mask: mask}
|
||||
}
|
||||
|
||||
func ConvertToTempKubeconfigFile(kubeconfigBytes []byte) (string, error) {
|
||||
temp, err := os.CreateTemp("", "*.kubeconfig")
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user