Skip to content

Commit

Permalink
Added code to inspect docker networks to get the bridgeID correctly
Browse files Browse the repository at this point in the history
Signed-off-by: Pablo Caderno <[email protected]>
  • Loading branch information
kadern0 committed Aug 22, 2020
1 parent 59af2c1 commit ba3c9ad
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions pkg/drivers/kic/oci/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,20 @@ func digDNS(ociBin, containerName, dns string) (net.IP, error) {
return ip, nil
}

// profileInContainers checks whether the profile is withing 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
// gets the ip from user's host docker
func dockerGatewayIP(profile string) (net.IP, error) {
var bridgeID string
// check if using custom network first
if networkExists(profile) {
ip := net.ParseIP(DefaultGateway)
Expand All @@ -70,8 +81,28 @@ func dockerGatewayIP(profile string) (net.IP, error) {
if err != nil {
return nil, errors.Wrapf(err, "get network bridge")
}

bridgeID := strings.TrimSpace(rr.Stdout.String())
networksOutput := strings.TrimSpace(rr.Stdout.String())
networksSlice := strings.Fields(networksOutput)
if len(networksSlice) == 1 {
bridgeID = networksOutput
} else {
// 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
}
}
}
if bridgeID == "" {
return nil, errors.Errorf("Error finding docker network")
}
rr, err = runCmd(exec.Command(Docker, "network", "inspect",
"--format", "{{(index .IPAM.Config 0).Gateway}}", bridgeID))
if err != nil {
Expand Down

0 comments on commit ba3c9ad

Please sign in to comment.