Skip to content

Commit 542f15e

Browse files
committed
Added code to inspect docker networks to get the bridgeID correctly
Signed-off-by: Pablo Caderno <[email protected]>
1 parent 59af2c1 commit 542f15e

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

pkg/drivers/kic/oci/network.go

+33-2
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,20 @@ func digDNS(ociBin, containerName, dns string) (net.IP, error) {
5858
return ip, nil
5959
}
6060

61+
// profileInContainers checks whether the profile is withing the containers list
62+
func profileInContainers(profile string, containers []string) bool {
63+
for _, container := range containers {
64+
if container == profile {
65+
return true
66+
}
67+
}
68+
return false
69+
}
70+
6171
// dockerGatewayIP gets the default gateway ip for the docker bridge on the user's host machine
6272
// gets the ip from user's host docker
6373
func dockerGatewayIP(profile string) (net.IP, error) {
74+
var bridgeID string
6475
// check if using custom network first
6576
if networkExists(profile) {
6677
ip := net.ParseIP(DefaultGateway)
@@ -70,8 +81,28 @@ func dockerGatewayIP(profile string) (net.IP, error) {
7081
if err != nil {
7182
return nil, errors.Wrapf(err, "get network bridge")
7283
}
73-
74-
bridgeID := strings.TrimSpace(rr.Stdout.String())
84+
networksOutput := strings.TrimSpace(rr.Stdout.String())
85+
networksSlice := strings.Fields(networksOutput)
86+
if len(networksSlice) == 1 {
87+
bridgeID = networksOutput
88+
} else {
89+
// Look for the minikube container within each docker network
90+
for _, net := range networksSlice {
91+
// get all containers in the network
92+
rs, err := runCmd(exec.Command(Docker, "network", "inspect", net, "-f", "'{{range $k, $v := .Containers}}{{$v.Name}} {{end}}'"))
93+
if err != nil {
94+
return nil, errors.Wrapf(err, "get containers in network")
95+
}
96+
containersSlice := strings.Fields(rs.Stdout.String())
97+
if profileInContainers(profile, containersSlice) {
98+
bridgeID = net
99+
break
100+
}
101+
}
102+
}
103+
if bridgeID == "" {
104+
return nil, errors.Errorf("Error finding docker network")
105+
}
75106
rr, err = runCmd(exec.Command(Docker, "network", "inspect",
76107
"--format", "{{(index .IPAM.Config 0).Gateway}}", bridgeID))
77108
if err != nil {

0 commit comments

Comments
 (0)