Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Dec 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd/registry/registryCreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
10 changes: 9 additions & 1 deletion cmd/util/ports.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"net"
"regexp"
"strconv"
"strings"

"github.com/docker/go-connections/nat"
l "github.com/rancher/k3d/v5/pkg/logger"
Expand Down Expand Up @@ -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 := ""
Expand Down
22 changes: 8 additions & 14 deletions pkg/client/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
}
}
Expand Down
11 changes: 0 additions & 11 deletions pkg/client/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 15 additions & 3 deletions pkg/runtimes/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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 ""
Expand Down