Skip to content

Commit 0408d3b

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 0408d3b

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

pkg/drivers/kic/oci/network.go

+29-1
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 within 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,25 @@ func dockerGatewayIP(profile string) (net.IP, error) {
7081
if err != nil {
7182
return nil, errors.Wrapf(err, "get network bridge")
7283
}
84+
networksOutput := strings.TrimSpace(rr.Stdout.String())
85+
networksSlice := strings.Fields(networksOutput)
86+
// Look for the minikube container within each docker network
87+
for _, net := range networksSlice {
88+
// get all containers in the network
89+
rs, err := runCmd(exec.Command(Docker, "network", "inspect", net, "-f", "{{range $k, $v := .Containers}}{{$v.Name}} {{end}}"))
90+
if err != nil {
91+
return nil, errors.Wrapf(err, "get containers in network")
92+
}
93+
containersSlice := strings.Fields(rs.Stdout.String())
94+
if profileInContainers(profile, containersSlice) {
95+
bridgeID = net
96+
break
97+
}
98+
}
7399

74-
bridgeID := strings.TrimSpace(rr.Stdout.String())
100+
if bridgeID == "" {
101+
return nil, errors.Errorf("unable to determine bridge network id from %q", networksOutput)
102+
}
75103
rr, err = runCmd(exec.Command(Docker, "network", "inspect",
76104
"--format", "{{(index .IPAM.Config 0).Gateway}}", bridgeID))
77105
if err != nil {

0 commit comments

Comments
 (0)