Merge pull request #1944 from weaveworks/issues/1940-remove-proxy-weaveps-dep
Use netlink instead of shelling out to `weave ps`
This commit is contained in:
commit
1d3c80ab49
10 files changed
+183
-64
No files matched your search
@@ -3,10 +3,8 @@ package proxy
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
@@ -131,35 +129,6 @@ func inspectContainerInPath(client *docker.Client, path string) (*docker.Contain
|
||||
return container, err
|
||||
}
|
||||
|
||||
func weaveContainerIPs(containerID string) (mac string, ips []net.IP, nets []*net.IPNet, err error) {
|
||||
stdout, stderr, err := callWeave("ps", containerID)
|
||||
if err != nil || len(stderr) > 0 {
|
||||
err = errors.New(string(stderr))
|
||||
return
|
||||
}
|
||||
if len(stdout) <= 0 {
|
||||
return
|
||||
}
|
||||
|
||||
fields := strings.Fields(string(stdout))
|
||||
if len(fields) < 2 {
|
||||
return
|
||||
}
|
||||
mac = fields[1]
|
||||
|
||||
var ip net.IP
|
||||
var ipnet *net.IPNet
|
||||
for _, cidr := range fields[2:] {
|
||||
ip, ipnet, err = net.ParseCIDR(cidr)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
ips = append(ips, ip)
|
||||
nets = append(nets, ipnet)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func addVolume(hostConfig jsonObject, source, target, mode string) error {
|
||||
configBinds, err := hostConfig.StringArray("Binds")
|
||||
if err != nil {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
@@ -45,6 +46,25 @@ func (j jsonObject) String(key string) (string, error) {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (j jsonObject) Int(key string) (int, error) {
|
||||
iface, ok := j[key]
|
||||
if !ok || iface == nil {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
result, ok := iface.(json.Number)
|
||||
if !ok {
|
||||
return 0, &UnmarshalWrongTypeError{key, "json.Number", iface}
|
||||
}
|
||||
|
||||
i64, err := result.Int64()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return int(i64), nil
|
||||
}
|
||||
|
||||
func (j jsonObject) StringArray(key string) ([]string, error) {
|
||||
iface, ok := j[key]
|
||||
if !ok || iface == nil {
|
||||
|
||||
+16
-5
@@ -62,6 +62,7 @@ type Config struct {
|
||||
WithoutDNS bool
|
||||
NoMulticastRoute bool
|
||||
DockerHost string
|
||||
ProcPath string
|
||||
}
|
||||
|
||||
type wait struct {
|
||||
@@ -570,11 +571,21 @@ func (proxy *Proxy) updateContainerNetworkSettings(container jsonObject) error {
|
||||
return err
|
||||
}
|
||||
|
||||
state, err := container.Object("State")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
pid, err := state.Int("Pid")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := proxy.waitForStartByIdent(containerID); err != nil {
|
||||
return err
|
||||
}
|
||||
mac, ips, nets, err := weaveContainerIPs(containerID)
|
||||
if err != nil || len(ips) == 0 {
|
||||
netDevs, err := GetWeaveNetDevs(proxy.ProcPath, pid)
|
||||
if err != nil || len(netDevs) == 0 || len(netDevs[0].CIDRs) == 0 {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -582,9 +593,9 @@ func (proxy *Proxy) updateContainerNetworkSettings(container jsonObject) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
networkSettings["MacAddress"] = mac
|
||||
networkSettings["IPAddress"] = ips[0].String()
|
||||
networkSettings["IPPrefixLen"], _ = nets[0].Mask.Size()
|
||||
networkSettings["MacAddress"] = netDevs[0].MAC.String()
|
||||
networkSettings["IPAddress"] = netDevs[0].CIDRs[0].IP.String()
|
||||
networkSettings["IPPrefixLen"], _ = netDevs[0].CIDRs[0].Mask.Size()
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user