diff --git a/cmd/registry/registryCreate.go b/cmd/registry/registryCreate.go index 0309a666..1d7aeb19 100644 --- a/cmd/registry/registryCreate.go +++ b/cmd/registry/registryCreate.go @@ -41,9 +41,9 @@ type regCreatePreProcessedFlags struct { } type regCreateFlags struct { - Image string + Image string Network string - NoHelp bool + NoHelp bool } var helptext string = `# You can now use the registry like this (example): diff --git a/cmd/util/ports.go b/cmd/util/ports.go index 66eaa49f..769b5f12 100644 --- a/cmd/util/ports.go +++ b/cmd/util/ports.go @@ -26,6 +26,7 @@ import ( "net" "regexp" "strconv" + "strings" "github.com/docker/go-connections/nat" l "github.com/rancher/k3d/v5/pkg/logger" @@ -61,7 +62,14 @@ func ParsePortExposureSpec(exposedPortSpec, internalPort string) (*k3d.ExposureO return nil, fmt.Errorf("Failed to lookup host '%s' specified for Port Exposure: %+v", submatches["hostname"], err) } api.Host = submatches["hostname"] - submatches["hostip"] = addrs[0] // set hostip to the resolved address + for _, addr := range addrs { + if !strings.Contains(addr, ":") { // lazy IPv6 check :D + submatches["hostip"] = addr // set hostip to the resolved address + } + } + if submatches["hostip"] == "" { + return nil, fmt.Errorf("Failed to lookup IPv4 address for host '%s'", submatches["hostname"]) + } } realPortString := "" diff --git a/pkg/client/cluster.go b/pkg/client/cluster.go index 47b32a35..160a197c 100644 --- a/pkg/client/cluster.go +++ b/pkg/client/cluster.go @@ -34,16 +34,14 @@ import ( "strings" "time" - gort "runtime" - "github.com/docker/go-connections/nat" "github.com/imdario/mergo" copystruct "github.com/mitchellh/copystructure" "github.com/rancher/k3d/v5/pkg/actions" config "github.com/rancher/k3d/v5/pkg/config/v1alpha3" l "github.com/rancher/k3d/v5/pkg/logger" + "github.com/rancher/k3d/v5/pkg/runtimes" k3drt "github.com/rancher/k3d/v5/pkg/runtimes" - "github.com/rancher/k3d/v5/pkg/runtimes/docker" runtimeErr "github.com/rancher/k3d/v5/pkg/runtimes/errors" "github.com/rancher/k3d/v5/pkg/types" k3d "github.com/rancher/k3d/v5/pkg/types" @@ -357,17 +355,13 @@ ClusterCreatOpts: * Docker Machine Special Configuration */ if cluster.KubeAPI.Host == k3d.DefaultAPIHost && runtime == k3drt.Docker { - if gort.GOOS == "windows" || gort.GOOS == "darwin" { - l.Log().Tracef("Running on %s: checking if it's using docker-machine", gort.GOOS) - machineIP, err := runtime.(docker.Docker).GetDockerMachineIP() - if err != nil { - l.Log().Warnf("Using docker-machine, but failed to get it's IP: %+v", err) - } else if machineIP != "" { - l.Log().Infof("Using the docker-machine IP %s to connect to the Kubernetes API", machineIP) - cluster.KubeAPI.Host = machineIP - cluster.KubeAPI.Binding.HostIP = machineIP - } else { - l.Log().Traceln("Not using docker-machine") + // If the runtime is docker, attempt to use the docker host + if runtime == runtimes.Docker { + dockerHost := runtime.GetHost() + if dockerHost != "" { + dockerHost = strings.Split(dockerHost, ":")[0] // remove the port + l.Log().Tracef("Using docker host %s", dockerHost) + cluster.KubeAPI.Host = dockerHost } } } diff --git a/pkg/client/node.go b/pkg/client/node.go index f2e12a41..dc3ab806 100644 --- a/pkg/client/node.go +++ b/pkg/client/node.go @@ -673,17 +673,6 @@ func patchServerSpec(node *k3d.Node, runtime runtimes.Runtime) error { node.RuntimeLabels[k3d.LabelServerAPIHost] = node.ServerOpts.KubeAPI.Host node.RuntimeLabels[k3d.LabelServerAPIPort] = node.ServerOpts.KubeAPI.Binding.HostPort - // If the runtime is docker, attempt to use the docker host - if runtime == runtimes.Docker { - dockerHost := runtime.GetHost() - if dockerHost != "" { - dockerHost = strings.Split(dockerHost, ":")[0] // remove the port - l.Log().Tracef("Using docker host %s", dockerHost) - node.RuntimeLabels[k3d.LabelServerAPIHostIP] = dockerHost - node.RuntimeLabels[k3d.LabelServerAPIHost] = dockerHost - } - } - node.Args = append(node.Args, "--tls-san", node.RuntimeLabels[k3d.LabelServerAPIHost]) // add TLS SAN for non default host name return nil diff --git a/pkg/runtimes/docker/docker.go b/pkg/runtimes/docker/docker.go index b81c859b..82138d5c 100644 --- a/pkg/runtimes/docker/docker.go +++ b/pkg/runtimes/docker/docker.go @@ -43,7 +43,19 @@ func (d Docker) ID() string { // GetHost returns the docker daemon host func (d Docker) GetHost() string { - // a) DOCKER_HOST env var + + // a) docker-machine + machineIP, err := d.GetDockerMachineIP() + if err != nil { + l.Log().Warnf("[Docker] Using docker-machine, but failed to get it's IP: %+v", err) + } else if machineIP != "" { + l.Log().Infof("[Docker] Using the docker-machine IP %s to connect to the Kubernetes API", machineIP) + return machineIP + } else { + l.Log().Traceln("[Docker] Not using docker-machine") + } + + // b) DOCKER_HOST env var dockerHost := os.Getenv("DOCKER_HOST") if dockerHost == "" { l.Log().Traceln("[Docker] GetHost: DOCKER_HOST empty/unset") @@ -52,9 +64,9 @@ func (d Docker) GetHost() string { l.Log().Errorf("[Docker] error getting runtime information: %v", err) return "" } - // b) Docker for Desktop (Win/Mac) and it's a local connection + // c) 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 + // c.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("[Docker] wanted to use 'host.docker.internal' as docker host, but it's not reachable in WSL2") return ""