do not use host.docker.internal IP for API on non-docker-machine setups of Docker for Desktop
This commit is contained in:
parent
437f53a72c
commit
aa3a54e3c9
@ -65,14 +65,16 @@ func ClusterCreate(ctx context.Context, runtime k3drt.Runtime, cluster *k3d.Clus
|
|||||||
|
|
||||||
if cluster.ExposeAPI.Host == k3d.DefaultAPIHost && runtime == k3drt.Docker {
|
if cluster.ExposeAPI.Host == k3d.DefaultAPIHost && runtime == k3drt.Docker {
|
||||||
if gort.GOOS == "windows" || gort.GOOS == "darwin" {
|
if gort.GOOS == "windows" || gort.GOOS == "darwin" {
|
||||||
log.Tracef("Running on %s -> Trying to get IP of the docker machine", gort.GOOS)
|
log.Tracef("Running on %s: checking if it's using docker-machine", gort.GOOS)
|
||||||
machineIP, err := runtime.(docker.Docker).GetDockerMachineIP()
|
machineIP, err := runtime.(docker.Docker).GetDockerMachineIP()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warnf("Failed to get Docker Machine IP: %+v", err)
|
log.Warnf("Using docker-machine, but failed to get it's IP: %+v", err)
|
||||||
} else if machineIP != "" {
|
} else if machineIP != "" {
|
||||||
log.Infof("Using the docker machine IP %s to connect to the Kubernetes API", machineIP)
|
log.Infof("Using the docker-machine IP %s to connect to the Kubernetes API", machineIP)
|
||||||
cluster.ExposeAPI.Host = machineIP
|
cluster.ExposeAPI.Host = machineIP
|
||||||
cluster.ExposeAPI.HostIP = machineIP
|
cluster.ExposeAPI.HostIP = machineIP
|
||||||
|
} else {
|
||||||
|
log.Traceln("Not using docker-machine")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,8 +23,6 @@ THE SOFTWARE.
|
|||||||
package docker
|
package docker
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"net"
|
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
@ -34,41 +32,33 @@ import (
|
|||||||
|
|
||||||
func (d Docker) GetDockerMachineIP() (string, error) {
|
func (d Docker) GetDockerMachineIP() (string, error) {
|
||||||
machine := os.ExpandEnv("$DOCKER_MACHINE_NAME")
|
machine := os.ExpandEnv("$DOCKER_MACHINE_NAME")
|
||||||
dockerHostName := "host.docker.internal"
|
|
||||||
|
|
||||||
// Option 1: use the docker-machine executable
|
|
||||||
if machine != "" {
|
if machine != "" {
|
||||||
dockerMachinePath, err := exec.LookPath("docker-machine")
|
log.Debugf("Docker Machine found: %s", machine)
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
out, err := exec.Command(dockerMachinePath, "ip", machine).Output()
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Error executing 'docker-machine ip'")
|
|
||||||
|
|
||||||
if exitError, ok := err.(*exec.ExitError); ok {
|
|
||||||
log.Printf("%s", string(exitError.Stderr))
|
|
||||||
}
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
ipStr := strings.TrimSuffix(string(out), "\n")
|
|
||||||
ipStr = strings.TrimSuffix(ipStr, "\r")
|
|
||||||
|
|
||||||
return ipStr, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Option 2: Try to lookup "host.docker.internal"
|
dockerMachinePath, err := exec.LookPath("docker-machine")
|
||||||
log.Debugf("Docker Machine not found, trying to lookup '%s' instead...", dockerHostName)
|
|
||||||
addrs, err := net.LookupHost(dockerHostName)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Debugf("Lookup of Host %s failed: %+v", dockerHostName, err)
|
if err == exec.ErrNotFound {
|
||||||
return "", fmt.Errorf("Failed to get IP of Docker VM")
|
if machine != "" {
|
||||||
|
log.Debugf("DOCKER_MACHINE_NAME env var present, but executable docker-machine not found: %w", err)
|
||||||
|
} else {
|
||||||
|
log.Tracef("docker-machine executable not found: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "", nil
|
||||||
}
|
}
|
||||||
if len(addrs) == 0 {
|
|
||||||
log.Debugf("Lookup of Host %s didn't return any addresses", dockerHostName)
|
out, err := exec.Command(dockerMachinePath, "ip", machine).Output()
|
||||||
return "", fmt.Errorf("Failed to get IP of Docker VM")
|
if err != nil {
|
||||||
|
log.Printf("Error executing 'docker-machine ip'")
|
||||||
|
|
||||||
|
if exitError, ok := err.(*exec.ExitError); ok {
|
||||||
|
log.Printf("%s", string(exitError.Stderr))
|
||||||
|
}
|
||||||
|
return "", err
|
||||||
}
|
}
|
||||||
log.Debugf("Addresses returned for %s: %+v", dockerHostName, addrs)
|
ipStr := strings.TrimSuffix(string(out), "\n")
|
||||||
return addrs[0], nil
|
ipStr = strings.TrimSuffix(ipStr, "\r")
|
||||||
|
|
||||||
|
return ipStr, nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user