mirror of
https://github.com/kubenetworks/kubevpn.git
synced 2025-10-04 23:12:42 +08:00
feat: add option extra-cidr
This commit is contained in:
@@ -64,6 +64,7 @@ func CmdConnect(f cmdutil.Factory) *cobra.Command {
|
|||||||
}
|
}
|
||||||
cmd.Flags().BoolVar(&config.Debug, "debug", false, "enable debug mode or not, true or false")
|
cmd.Flags().BoolVar(&config.Debug, "debug", false, "enable debug mode or not, true or false")
|
||||||
cmd.Flags().StringVar(&config.Image, "image", config.Image, "use this image to startup container")
|
cmd.Flags().StringVar(&config.Image, "image", config.Image, "use this image to startup container")
|
||||||
|
cmd.Flags().StringArrayVar(&connect.ExtraCIDR, "extra-cidr", []string{}, "Extra cidr string, eg: --extra-cidr 192.168.0.159/24 --extra-cidr 192.168.1.160/32")
|
||||||
|
|
||||||
addSshFlag(cmd, sshConf)
|
addSshFlag(cmd, sshConf)
|
||||||
return cmd
|
return cmd
|
||||||
|
@@ -33,6 +33,7 @@ func CmdDev(f cmdutil.Factory) *cobra.Command {
|
|||||||
Volumes: opts.NewListOpts(nil),
|
Volumes: opts.NewListOpts(nil),
|
||||||
ExtraHosts: opts.NewListOpts(nil),
|
ExtraHosts: opts.NewListOpts(nil),
|
||||||
NoProxy: false,
|
NoProxy: false,
|
||||||
|
ExtraCIDR: []string{},
|
||||||
}
|
}
|
||||||
var sshConf = &util.SshConfig{}
|
var sshConf = &util.SshConfig{}
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
@@ -74,6 +75,7 @@ func CmdDev(f cmdutil.Factory) *cobra.Command {
|
|||||||
connect := handler.ConnectOptions{
|
connect := handler.ConnectOptions{
|
||||||
Headers: devOptions.Headers,
|
Headers: devOptions.Headers,
|
||||||
Workloads: args,
|
Workloads: args,
|
||||||
|
ExtraCIDR: devOptions.ExtraCIDR,
|
||||||
}
|
}
|
||||||
|
|
||||||
if devOptions.ParentContainer != "" {
|
if devOptions.ParentContainer != "" {
|
||||||
@@ -97,9 +99,9 @@ func CmdDev(f cmdutil.Factory) *cobra.Command {
|
|||||||
if err := connect.InitClient(f); err != nil {
|
if err := connect.InitClient(f); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err2 := connect.PreCheckResource()
|
err := connect.PreCheckResource()
|
||||||
if err2 != nil {
|
if err != nil {
|
||||||
return err2
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(connect.Workloads) > 1 {
|
if len(connect.Workloads) > 1 {
|
||||||
@@ -121,12 +123,12 @@ func CmdDev(f cmdutil.Factory) *cobra.Command {
|
|||||||
handler.Cleanup(syscall.SIGQUIT)
|
handler.Cleanup(syscall.SIGQUIT)
|
||||||
select {}
|
select {}
|
||||||
}()
|
}()
|
||||||
if err := connect.DoConnect(); err != nil {
|
if err = connect.DoConnect(); err != nil {
|
||||||
log.Errorln(err)
|
log.Errorln(err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
devOptions.Namespace = connect.Namespace
|
devOptions.Namespace = connect.Namespace
|
||||||
err := devOptions.Main(context.Background())
|
err = devOptions.Main(context.Background())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorln(err)
|
log.Errorln(err)
|
||||||
}
|
}
|
||||||
@@ -139,6 +141,7 @@ func CmdDev(f cmdutil.Factory) *cobra.Command {
|
|||||||
cmd.Flags().BoolVar(&devOptions.NoProxy, "no-proxy", false, "Whether proxy remote workloads traffic into local or not, true: just startup container on local without inject containers to intercept traffic, false: intercept traffic and forward to local")
|
cmd.Flags().BoolVar(&devOptions.NoProxy, "no-proxy", false, "Whether proxy remote workloads traffic into local or not, true: just startup container on local without inject containers to intercept traffic, false: intercept traffic and forward to local")
|
||||||
cmdutil.AddContainerVarFlags(cmd, &devOptions.ContainerName, devOptions.ContainerName)
|
cmdutil.AddContainerVarFlags(cmd, &devOptions.ContainerName, devOptions.ContainerName)
|
||||||
cmdutil.CheckErr(cmd.RegisterFlagCompletionFunc("container", completion.ContainerCompletionFunc(f)))
|
cmdutil.CheckErr(cmd.RegisterFlagCompletionFunc("container", completion.ContainerCompletionFunc(f)))
|
||||||
|
cmd.Flags().StringArrayVar(&devOptions.ExtraCIDR, "extra-cidr", []string{}, "Extra cidr string, eg: --extra-cidr 192.168.0.159/24 --extra-cidr 192.168.1.160/32")
|
||||||
|
|
||||||
// docker options
|
// docker options
|
||||||
cmd.Flags().Var(&devOptions.ExtraHosts, "add-host", "Add a custom host-to-IP mapping (host:ip)")
|
cmd.Flags().Var(&devOptions.ExtraHosts, "add-host", "Add a custom host-to-IP mapping (host:ip)")
|
||||||
|
@@ -80,6 +80,7 @@ func CmdDuplicate(f cmdutil.Factory) *cobra.Command {
|
|||||||
connectOptions := handler.ConnectOptions{
|
connectOptions := handler.ConnectOptions{
|
||||||
Namespace: duplicateOptions.Namespace,
|
Namespace: duplicateOptions.Namespace,
|
||||||
Workloads: args,
|
Workloads: args,
|
||||||
|
ExtraCIDR: duplicateOptions.ExtraCIDR,
|
||||||
}
|
}
|
||||||
if err := connectOptions.InitClient(f); err != nil {
|
if err := connectOptions.InitClient(f); err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -110,6 +111,8 @@ func CmdDuplicate(f cmdutil.Factory) *cobra.Command {
|
|||||||
cmd.Flags().StringToStringVarP(&duplicateOptions.Headers, "headers", "H", map[string]string{}, "Traffic with special headers with reverse it to duplicate workloads, you should startup your service after reverse workloads successfully, If not special, redirect all traffic to duplicate workloads, format is k=v, like: k1=v1,k2=v2")
|
cmd.Flags().StringToStringVarP(&duplicateOptions.Headers, "headers", "H", map[string]string{}, "Traffic with special headers with reverse it to duplicate workloads, you should startup your service after reverse workloads successfully, If not special, redirect all traffic to duplicate workloads, format is k=v, like: k1=v1,k2=v2")
|
||||||
cmd.Flags().BoolVar(&config.Debug, "debug", false, "Enable debug mode or not, true or false")
|
cmd.Flags().BoolVar(&config.Debug, "debug", false, "Enable debug mode or not, true or false")
|
||||||
cmd.Flags().StringVar(&config.Image, "image", config.Image, "Use this image to startup container")
|
cmd.Flags().StringVar(&config.Image, "image", config.Image, "Use this image to startup container")
|
||||||
|
cmd.Flags().StringArrayVar(&duplicateOptions.ExtraCIDR, "extra-cidr", []string{}, "Extra cidr string, eg: --extra-cidr 192.168.0.159/24 --extra-cidr 192.168.1.160/32")
|
||||||
|
|
||||||
cmd.Flags().StringVar(&duplicateOptions.TargetImage, "target-image", "", "Duplicate container use this image to startup container, if not special, use origin origin image")
|
cmd.Flags().StringVar(&duplicateOptions.TargetImage, "target-image", "", "Duplicate container use this image to startup container, if not special, use origin origin image")
|
||||||
cmd.Flags().StringVar(&duplicateOptions.TargetContainer, "target-container", "", "Duplicate container use special image to startup this container, if not special, use origin origin image")
|
cmd.Flags().StringVar(&duplicateOptions.TargetContainer, "target-container", "", "Duplicate container use special image to startup this container, if not special, use origin origin image")
|
||||||
cmd.Flags().StringVar(&duplicateOptions.TargetNamespace, "target-namespace", "", "Duplicate workloads in this namespace, if not special, use origin namespace")
|
cmd.Flags().StringVar(&duplicateOptions.TargetNamespace, "target-namespace", "", "Duplicate workloads in this namespace, if not special, use origin namespace")
|
||||||
|
@@ -93,6 +93,7 @@ func CmdProxy(f cmdutil.Factory) *cobra.Command {
|
|||||||
cmd.Flags().StringToStringVarP(&connect.Headers, "headers", "H", map[string]string{}, "Traffic with special headers with reverse it to local PC, you should startup your service after reverse workloads successfully, If not special, redirect all traffic to local PC, format is k=v, like: k1=v1,k2=v2")
|
cmd.Flags().StringToStringVarP(&connect.Headers, "headers", "H", map[string]string{}, "Traffic with special headers with reverse it to local PC, you should startup your service after reverse workloads successfully, If not special, redirect all traffic to local PC, format is k=v, like: k1=v1,k2=v2")
|
||||||
cmd.Flags().BoolVar(&config.Debug, "debug", false, "Enable debug mode or not, true or false")
|
cmd.Flags().BoolVar(&config.Debug, "debug", false, "Enable debug mode or not, true or false")
|
||||||
cmd.Flags().StringVar(&config.Image, "image", config.Image, "Use this image to startup container")
|
cmd.Flags().StringVar(&config.Image, "image", config.Image, "Use this image to startup container")
|
||||||
|
cmd.Flags().StringArrayVar(&connect.ExtraCIDR, "extra-cidr", []string{}, "Extra cidr string, eg: --extra-cidr 192.168.0.159/24 --extra-cidr 192.168.1.160/32")
|
||||||
|
|
||||||
addSshFlag(cmd, sshConf)
|
addSshFlag(cmd, sshConf)
|
||||||
cmd.ValidArgsFunction = utilcomp.ResourceTypeAndNameCompletionFunc(f)
|
cmd.ValidArgsFunction = utilcomp.ResourceTypeAndNameCompletionFunc(f)
|
||||||
|
@@ -38,6 +38,7 @@ type Options struct {
|
|||||||
Factory cmdutil.Factory
|
Factory cmdutil.Factory
|
||||||
ContainerName string
|
ContainerName string
|
||||||
NoProxy bool
|
NoProxy bool
|
||||||
|
ExtraCIDR []string
|
||||||
|
|
||||||
// docker options
|
// docker options
|
||||||
Platform string
|
Platform string
|
||||||
|
@@ -50,6 +50,8 @@ type ConnectOptions struct {
|
|||||||
Namespace string
|
Namespace string
|
||||||
Headers map[string]string
|
Headers map[string]string
|
||||||
Workloads []string
|
Workloads []string
|
||||||
|
ExtraCIDR []string
|
||||||
|
|
||||||
clientset *kubernetes.Clientset
|
clientset *kubernetes.Clientset
|
||||||
restclient *rest.RESTClient
|
restclient *rest.RESTClient
|
||||||
config *rest.Config
|
config *rest.Config
|
||||||
@@ -255,6 +257,14 @@ func (c *ConnectOptions) startLocalTunServe(ctx context.Context, forwardAddress
|
|||||||
for _, ipNet := range c.cidrs {
|
for _, ipNet := range c.cidrs {
|
||||||
list.Insert(ipNet.String())
|
list.Insert(ipNet.String())
|
||||||
}
|
}
|
||||||
|
// add extra-cidr
|
||||||
|
for _, s := range c.ExtraCIDR {
|
||||||
|
_, _, err = net.ParseCIDR(s)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("invalid extra-cidr %s, err: %v", s, err)
|
||||||
|
}
|
||||||
|
list.Insert(s)
|
||||||
|
}
|
||||||
r := core.Route{
|
r := core.Route{
|
||||||
ServeNodes: []string{
|
ServeNodes: []string{
|
||||||
fmt.Sprintf("tun:/127.0.0.1:8422?net=%s&route=%s", c.localTunIP.String(), strings.Join(list.UnsortedList(), ",")),
|
fmt.Sprintf("tun:/127.0.0.1:8422?net=%s&route=%s", c.localTunIP.String(), strings.Join(list.UnsortedList(), ",")),
|
||||||
|
@@ -41,6 +41,7 @@ type DuplicateOptions struct {
|
|||||||
Namespace string
|
Namespace string
|
||||||
Headers map[string]string
|
Headers map[string]string
|
||||||
Workloads []string
|
Workloads []string
|
||||||
|
ExtraCIDR []string
|
||||||
|
|
||||||
TargetKubeconfig string
|
TargetKubeconfig string
|
||||||
TargetNamespace string
|
TargetNamespace string
|
||||||
@@ -646,8 +647,31 @@ func (d *DuplicateOptions) setEnv(u *unstructured.Unstructured) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// todo replace origin registry with special registry for pulling image
|
// todo replace origin registry with special registry for pulling image
|
||||||
func (d *DuplicateOptions) replaceRegistry(u *unstructured.Unstructured) {
|
func (d *DuplicateOptions) replaceRegistry(u *unstructured.Unstructured) error {
|
||||||
if d.TargetRegistry != "" {
|
if d.TargetRegistry == "" {
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
temp, path, err := util.GetPodTemplateSpecPath(u)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
//for i, container := range temp.Spec.InitContainers {
|
||||||
|
// if container.Image
|
||||||
|
//}
|
||||||
|
|
||||||
|
var marshal []byte
|
||||||
|
if marshal, err = json.Marshal(temp.Spec); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
var content map[string]interface{}
|
||||||
|
if err = json.Unmarshal(marshal, &content); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err = unstructured.SetNestedField(u.Object, content, append(path, "spec")...); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
@@ -13,6 +13,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/docker/distribution/reference"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
corev1 "k8s.io/api/core/v1"
|
corev1 "k8s.io/api/core/v1"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
@@ -299,3 +300,19 @@ func init() {
|
|||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestName(t *testing.T) {
|
||||||
|
name := "alpine@sha256:b733d4a32c4da6a00a84df2ca32791bb03df95400243648d8c539e7b4cce329c"
|
||||||
|
named, err := reference.ParseNormalizedNamed(name)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
named = reference.TagNameOnly(named)
|
||||||
|
domain := reference.Domain(named)
|
||||||
|
path := reference.Path(named)
|
||||||
|
tagged, ok := named.(reference.Tagged)
|
||||||
|
if !ok {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
fmt.Println(domain, path, tagged)
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user