Skip to content

Commit

Permalink
GetDockerHost to return host.docker.internal if DfD and local connect…
Browse files Browse the repository at this point in the history
…ion (and it's resolvable)
  • Loading branch information
iwilltry42 committed Dec 3, 2021
1 parent ebea338 commit 59eb8b7
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
8 changes: 2 additions & 6 deletions pkg/client/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ import (
"net"
"regexp"
goruntime "runtime"
"strings"

l "github.com/rancher/k3d/v5/pkg/logger"
"github.com/rancher/k3d/v5/pkg/runtimes"
"github.com/rancher/k3d/v5/pkg/runtimes/docker"
k3d "github.com/rancher/k3d/v5/pkg/types"
"github.com/rancher/k3d/v5/pkg/util"
)
Expand Down Expand Up @@ -64,15 +64,11 @@ func GetHostIP(ctx context.Context, runtime runtimes.Runtime, cluster *k3d.Clust

l.Log().Tracef("GOOS: %s / Runtime OS: %s (%s)", goruntime.GOOS, rtimeInfo.OSType, rtimeInfo.OS)

isDockerDesktop := func(os string) bool {
return strings.ToLower(os) == "docker desktop"
}

// Docker Runtime
if runtime == runtimes.Docker {

// Docker (for Desktop) on MacOS or Windows
if isDockerDesktop(rtimeInfo.OS) {
if docker.IsDockerDesktop(rtimeInfo.OS) {

toolsNode, err := EnsureToolsNode(ctx, runtime, cluster)
if err != nil {
Expand Down
28 changes: 27 additions & 1 deletion pkg/runtimes/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ THE SOFTWARE.
package docker

import (
"net"
"net/url"
"os"

Expand All @@ -42,12 +43,37 @@ func (d Docker) ID() string {

// GetHost returns the docker daemon host
func (d Docker) GetHost() string {
// a) DOCKER_HOST env var
dockerHost := os.Getenv("DOCKER_HOST")
if dockerHost == "" {
l.Log().Traceln("[Docker] GetHost: DOCKER_HOST empty/unset")
info, err := d.Info()
if err != nil {
l.Log().Errorf("error getting runtime information: %v", err)
return ""
}
// b) Docker for Desktop (Win/Mac) and it's a local connection
if IsDockerDesktop(info.OS) && IsLocalConnection(info.Endpoint) {
// b.1) local DfD connection, but inside WSL, where host.docker.internal resolves to an IP, but it's not reachable
if _, ok := os.LookupEnv("WSL_DISTRO_NAME"); ok {
l.Log().Debugln("wanted to use 'host.docker.internal' as docker host, but it's not reachable in WSL2")
return ""
}
l.Log().Debugln("[Docker] Local DfD: using 'host.docker.internal'")
dockerHost = "host.docker.internal"
if _, err := net.LookupHost(dockerHost); err != nil {
l.Log().Debugf("wanted to use 'host.docker.internal' as docker host, but it's not resolvable locally")
return ""
}
}
}
url, err := url.Parse(dockerHost)
if err != nil {
l.Log().Debugf("[Docker] GetHost: error parsing '%s' as URL: %v", dockerHost, url)
return ""
}
l.Log().Debugf("DockerHost: %s", url.Host)
l.Log().Debugf("DockerHost: '%s'", url.Host)

return url.Host
}

Expand Down
18 changes: 18 additions & 0 deletions pkg/runtimes/docker/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import (
"fmt"
"io"
"os"
"regexp"
"strings"

"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/flags"
Expand All @@ -42,6 +44,22 @@ import (
"github.com/spf13/pflag"
)

func IsDockerDesktop(os string) bool {
return strings.ToLower(os) == "docker desktop"
}

/*
* Simple Matching to detect local connection:
* - file (socket): starts with / (absolute path)
* - tcp://(localhost|127.0.0.1)
* - ssh://(localhost|127.0.0.1)
*/
var LocalConnectionRegexp = regexp.MustCompile(`^(/|((tcp|ssh)://(localhost|127\.0\.0\.1))).*`)

func IsLocalConnection(endpoint string) bool {
return LocalConnectionRegexp.Match([]byte(endpoint))
}

// GetDefaultObjectLabelsFilter returns docker type filters created from k3d labels
func GetDefaultObjectLabelsFilter(clusterName string) filters.Args {
filters := filters.NewArgs()
Expand Down

0 comments on commit 59eb8b7

Please sign in to comment.