Perform AWSVPC checks for attach in script rather than in weaveutil

This commit is contained in:
Bryan Boreham committed 2016-06-08 16:44:54 +01:00
1 parent fb9b4ddec0
commit 37fd54f20b
3 files changed
+27 -55

No files matched your search

+12 -45
View File
@@ -3,42 +3,33 @@ package main
import (
"fmt"
"net"
"os"
"strconv"
"syscall"
docker "github.com/fsouza/go-dockerclient"
"github.com/vishvananda/netns"
"github.com/weaveworks/weave/api"
"github.com/weaveworks/weave/common"
weavenet "github.com/weaveworks/weave/net"
)
func attach(args []string) error {
if len(args) < 4 {
cmdUsage("attach-container", "[--no-multicast-route] <container-id> <bridge-name> <mtu> <cidr>...")
cmdUsage("attach-container", "[--no-multicast-route] [--keep-tx-on] <container-id> <bridge-name> <mtu> <cidr>...")
}
client := api.NewClient(os.Getenv("WEAVE_HTTP_ADDR"), common.Log)
keepTXOn := false
isAWSVPC := false
// In a case of an error, we skip applying necessary steps for AWSVPC, because
// "attach" should work without the weave router running.
if t, err := client.LocalRangeTracker(); err != nil {
fmt.Fprintf(os.Stderr, "unable to determine tracker: %s; skipping AWSVPC initialization\n", err)
} else if t == "awsvpc" {
isAWSVPC = true
keepTXOn = true
}
withMulticastRoute := true
if args[0] == "--no-multicast-route" {
withMulticastRoute = false
args = args[1:]
}
if isAWSVPC {
withMulticastRoute = false
for i := 0; i < len(args); {
switch args[i] {
case "--no-multicast-route":
withMulticastRoute = false
args = append(args[:i], args[i+1:]...)
case "--keep-tx-on":
keepTXOn = true
args = append(args[:i], args[i+1:]...)
default:
i++
}
}
pid, nsContainer, err := containerPidAndNs(args[0])
@@ -59,20 +50,6 @@ func attach(args []string) error {
return err
}
if isAWSVPC {
// Currently, we allow only IP addresses from the default subnet
if defaultSubnet, err := client.DefaultSubnet(); err != nil {
fmt.Fprintf(os.Stderr, "unable to retrieve default subnet: %s; skipping AWSVPC checks\n", err)
} else {
for _, cidr := range cidrs {
if !sameSubnet(cidr, defaultSubnet) {
format := "AWSVPC constraints violation: %s does not belong to the default subnet %s"
return fmt.Errorf(format, cidr, defaultSubnet)
}
}
}
}
err = weavenet.AttachContainer(nsContainer, fmt.Sprint(pid), weavenet.VethName, args[1], mtu, withMulticastRoute, cidrs, keepTXOn)
// If we detected an error but the container has died, tell the user that instead.
if err != nil && !processExists(pid) {
@@ -81,16 +58,6 @@ func attach(args []string) error {
return err
}
// sameSubnet checks whether ip belongs to network's subnet
func sameSubnet(ip *net.IPNet, network *net.IPNet) bool {
if network.Contains(ip.IP) {
i1, i2 := ip.Mask.Size()
n1, n2 := network.Mask.Size()
return i1 == n1 && i2 == n2
}
return false
}
func containerPidAndNs(containerID string) (int, netns.NsHandle, error) {
c, err := docker.NewVersionedClientFromEnv("1.18")
if err != nil {