From 76d8c93e33e142a6d54aea612b1cc14fec60908c Mon Sep 17 00:00:00 2001 From: sufuf3 Date: Tue, 24 Jul 2018 14:59:18 +0800 Subject: [PATCH] Add add route feature into client cc linkernetworks/vortex#154 --- client/main.go | 43 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/client/main.go b/client/main.go index 8db8e3a..9affef1 100644 --- a/client/main.go +++ b/client/main.go @@ -21,11 +21,14 @@ type podOptions struct { type interfaceOptions struct { CIDR string `short:"i" long:"ip" description:"The ip address of the interface, should be a valid v4 CIDR Address"` - //FIXME we will support the static route in the furture - //Gateway string `short:"g" long:"gw" description:"The gateway of the inteface subnet"` VLANTag *int32 `short:"v" long:"vlan" description:"The Vlan Tag of the interface"` } +type routeOptions struct { + DstCIDR string `long:"net" description:"The destination network for add IP routing table, like '-net target'"` + Gateway string `short:"g" long:"gateway" description:"The gateway of the interface subnet"` +} + type connectOptions struct { Bridge string `short:"b" long:"bridge" description:"Target bridge name" required:"true"` Interface string `short:"n" long:"nic" description:"The interface name in the container" required:"true"` @@ -35,6 +38,7 @@ type clientOptions struct { Server string `short:"s" long:"server " description:"target server address, [ip:port] for TCP or unix://[path] for UNIX" required:"true"` Connect connectOptions `group:"connectOptions"` Interface interfaceOptions `group:"interfaceOptions" ` + Route routeOptions `group:"routeOptions" ` Pod podOptions `group:"podOptions" ` } @@ -44,12 +48,13 @@ var parser = flags.NewParser(&options, flags.Default) func main() { var setCIDR bool var setVLANAccessLink bool + var setRoute bool if _, err := parser.Parse(); err != nil { parser.WriteHelp(os.Stderr) os.Exit(1) } - // Verify CIDR address + // Verify CIDR address and setCIDR bool if options.Interface.CIDR != "" { setCIDR = true } else { @@ -62,6 +67,7 @@ func main() { } } + // setVLANAccessLink bool if options.Interface.VLANTag != nil { setVLANAccessLink = true } @@ -72,6 +78,17 @@ func main() { } } + // setRoute bool + if options.Route.DstCIDR != "" { + setRoute = true + } + + if setRoute { + if !utils.IsValidCIDR(options.Route.DstCIDR) { + log.Fatalf("Route destination netIP is not correct: %s", options.Route.DstCIDR) + } + } + log.Println("Start to connect to ", options.Server) // Set up a connection to the server. conn, err := grpc.Dial(options.Server, grpc.WithInsecure()) @@ -167,5 +184,25 @@ func main() { "Set Port with VLAN", ) } + + if setRoute { + addRouteResp, err := ncClient.AddRoute(ctx, + &pb.AddRouteRequest{ + Path: findNetworkNamespacePathResp.Path, + DstCIDR: options.Route.DstCIDR, + GwIP: options.Route.Gateway, + ContainerVethName: options.Connect.Interface, + }, + ) + if err != nil { + log.Fatalf("There is something wrong with adding route: %v", err) + } + common.CheckFatal( + addRouteResp.Success, + addRouteResp.Reason, + "Add Route", + ) + } + log.Printf("network-controller client has completed all tasks") }