[Fix] only replace default api host with docker host (#879)

- move docker-machine lookup into general GetHost function
- only use GetHost result if API host is default (0.0.0.0) to not override user-provided value
- when looking up IP for host value (e.g. localhost), only use IPv4 to prevent errors
pull/881/head
Thorsten Klein 3 years ago committed by GitHub
parent 858c3142be
commit 25ec400c49
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      cmd/registry/registryCreate.go
  2. 10
      cmd/util/ports.go
  3. 22
      pkg/client/cluster.go
  4. 11
      pkg/client/node.go
  5. 18
      pkg/runtimes/docker/docker.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):

@ -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 := ""

@ -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
}
}
}

@ -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

@ -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 ""

Loading…
Cancel
Save