提交 0408d3ba 编写于 作者: P Pablo Caderno

Added code to inspect docker networks to get the bridgeID correctly

Signed-off-by: NPablo Caderno <kaderno@gmail.com>
上级 59af2c1c
...@@ -58,9 +58,20 @@ func digDNS(ociBin, containerName, dns string) (net.IP, error) { ...@@ -58,9 +58,20 @@ func digDNS(ociBin, containerName, dns string) (net.IP, error) {
return ip, nil return ip, nil
} }
// profileInContainers checks whether the profile is within the containers list
func profileInContainers(profile string, containers []string) bool {
for _, container := range containers {
if container == profile {
return true
}
}
return false
}
// dockerGatewayIP gets the default gateway ip for the docker bridge on the user's host machine // dockerGatewayIP gets the default gateway ip for the docker bridge on the user's host machine
// gets the ip from user's host docker // gets the ip from user's host docker
func dockerGatewayIP(profile string) (net.IP, error) { func dockerGatewayIP(profile string) (net.IP, error) {
var bridgeID string
// check if using custom network first // check if using custom network first
if networkExists(profile) { if networkExists(profile) {
ip := net.ParseIP(DefaultGateway) ip := net.ParseIP(DefaultGateway)
...@@ -70,8 +81,25 @@ func dockerGatewayIP(profile string) (net.IP, error) { ...@@ -70,8 +81,25 @@ func dockerGatewayIP(profile string) (net.IP, error) {
if err != nil { if err != nil {
return nil, errors.Wrapf(err, "get network bridge") return nil, errors.Wrapf(err, "get network bridge")
} }
networksOutput := strings.TrimSpace(rr.Stdout.String())
networksSlice := strings.Fields(networksOutput)
// Look for the minikube container within each docker network
for _, net := range networksSlice {
// get all containers in the network
rs, err := runCmd(exec.Command(Docker, "network", "inspect", net, "-f", "{{range $k, $v := .Containers}}{{$v.Name}} {{end}}"))
if err != nil {
return nil, errors.Wrapf(err, "get containers in network")
}
containersSlice := strings.Fields(rs.Stdout.String())
if profileInContainers(profile, containersSlice) {
bridgeID = net
break
}
}
bridgeID := strings.TrimSpace(rr.Stdout.String()) if bridgeID == "" {
return nil, errors.Errorf("unable to determine bridge network id from %q", networksOutput)
}
rr, err = runCmd(exec.Command(Docker, "network", "inspect", rr, err = runCmd(exec.Command(Docker, "network", "inspect",
"--format", "{{(index .IPAM.Config 0).Gateway}}", bridgeID)) "--format", "{{(index .IPAM.Config 0).Gateway}}", bridgeID))
if err != nil { if err != nil {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册