Move attach() and detach() from shell script to Go
This commit is contained in:
1 parent
e3ef473cd3
commit
ac14b19db8
5 files changed
+155
-81
No files matched your search
@@ -0,0 +1,72 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net"
|
||||
"strconv"
|
||||
|
||||
"github.com/vishvananda/netns"
|
||||
|
||||
weavenet "github.com/weaveworks/weave/net"
|
||||
)
|
||||
|
||||
func attach(args []string) error {
|
||||
if len(args) < 4 {
|
||||
cmdUsage("attach-container", "[--no-multicast-route] <pid> <bridge-name> <mtu> <cidr>...")
|
||||
}
|
||||
|
||||
withMulticastRoute := true
|
||||
if args[0] == "--no-multicast-route" {
|
||||
withMulticastRoute = false
|
||||
args = args[1:]
|
||||
}
|
||||
|
||||
pid, err := strconv.Atoi(args[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ns, err := netns.GetFromPid(pid)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
mtu, err := strconv.Atoi(args[2])
|
||||
if err != nil && args[3] != "" {
|
||||
return err
|
||||
}
|
||||
cidrs, err := parseCIDRs(args[3:])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return weavenet.AttachContainer(ns, args[0], "ethwe", args[1], mtu, withMulticastRoute, cidrs)
|
||||
}
|
||||
|
||||
func parseCIDRs(args []string) (cidrs []*net.IPNet, err error) {
|
||||
for _, ipstr := range args {
|
||||
ip, ipnet, err := net.ParseCIDR(ipstr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ipnet.IP = ip // we want the specific IP plus the mask
|
||||
cidrs = append(cidrs, ipnet)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func detach(args []string) error {
|
||||
if len(args) < 2 {
|
||||
cmdUsage("detach-container", "<pid> <cidr>...")
|
||||
}
|
||||
|
||||
pid, err := strconv.Atoi(args[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ns, err := netns.GetFromPid(pid)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cidrs, err := parseCIDRs(args[1:])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return weavenet.DetachContainer(ns, args[0], "ethwe", cidrs)
|
||||
}
|
||||
@@ -19,6 +19,8 @@ func init() {
|
||||
"create-plugin-network": createPluginNetwork,
|
||||
"remove-plugin-network": removePluginNetwork,
|
||||
"container-addrs": containerAddrs,
|
||||
"attach-container": attach,
|
||||
"detach-container": detach,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user