Skip to content

Commit

Permalink
Fix StartHost failed when docker container is connected to several ne…
Browse files Browse the repository at this point in the history
…tworks

Fixes #10079
  • Loading branch information
fredericgermain committed Aug 31, 2024
1 parent c2b297a commit d07bee9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
7 changes: 6 additions & 1 deletion pkg/drivers/kic/kic.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,12 @@ func (d *Driver) DriverName() string {

// GetIP returns an IP or hostname that this host is available at
func (d *Driver) GetIP() (string, error) {
ip, _, err := oci.ContainerIPs(d.OCIBinary, d.MachineName)
networkName := d.NodeConfig.Network
if networkName == "" {
networkName = d.NodeConfig.ClusterName
}

ip, _, err := oci.ContainerIPs(d.OCIBinary, d.MachineName, networkName)
return ip, err
}

Expand Down
19 changes: 10 additions & 9 deletions pkg/drivers/kic/oci/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,25 +197,25 @@ func ForwardedPort(ociBin string, ociID string, contPort int) (int, error) {
}

// ContainerIPs returns ipv4,ipv6, error of a container by their name
func ContainerIPs(ociBin string, name string) (string, string, error) {
func ContainerIPs(ociBin string, containerNameOrID string, networkID string) (string, string, error) {
if ociBin == Podman {
return podmanContainerIP(ociBin, name)
return podmanContainerIP(ociBin, containerNameOrID, networkID)
}
return dockerContainerIP(ociBin, name)
return dockerContainerIP(ociBin, containerNameOrID, networkID)
}

// podmanContainerIP returns ipv4, ipv6 of container or error
func podmanContainerIP(ociBin string, name string) (string, string, error) {
func podmanContainerIP(ociBin string, containerNameOrID string, networkID string) (string, string, error) {
rr, err := runCmd(exec.Command(ociBin, "container", "inspect",
"-f", "{{.NetworkSettings.IPAddress}}",
name))
containerNameOrID))
if err != nil {
return "", "", errors.Wrapf(err, "podman inspect ip %s", name)
return "", "", errors.Wrapf(err, "podman inspect ip %s %s", containerNameOrID, networkID)
}
output := strings.TrimSpace(rr.Stdout.String())
if output == "" { // podman returns empty for 127.0.0.1
// check network, if the ip address is missing
ipv4, ipv6, err := dockerContainerIP(ociBin, name)
ipv4, ipv6, err := dockerContainerIP(ociBin, containerNameOrID, networkID)
if err == nil {
return ipv4, ipv6, nil
}
Expand All @@ -225,9 +225,10 @@ func podmanContainerIP(ociBin string, name string) (string, string, error) {
}

// dockerContainerIP returns ipv4, ipv6 of container or error
func dockerContainerIP(ociBin string, name string) (string, string, error) {
func dockerContainerIP(ociBin string, containerNameOrID string, networkID string) (string, string, error) {
// retrieve the IP address of the node using docker inspect
lines, err := inspect(ociBin, name, "{{range .NetworkSettings.Networks}}{{.IPAddress}},{{.GlobalIPv6Address}}{{end}}")
format := fmt.Sprintf("{{with (index .NetworkSettings.Networks \"%s\")}}{{.IPAddress}},{{.GlobalIPv6Address}}{{end}}", networkID)
lines, err := inspect(ociBin, containerNameOrID, format)
if err != nil {
return "", "", errors.Wrap(err, "inspecting NetworkSettings.Networks")
}
Expand Down

0 comments on commit d07bee9

Please sign in to comment.