From b7286213d593316cc47f5eebcac64c815db8bad0 Mon Sep 17 00:00:00 2001 From: Daniil Skomarovskiy <0xbad0c0d3@gmail.com> Date: Thu, 26 Sep 2019 12:13:16 +0300 Subject: [PATCH] --dns-option "resolve-docker-internal" to enable the feature Signed-off-by: Daniil Skomarovskiy <0xbad0c0d3@gmail.com> --- network.go | 17 +++++++++++++++++ sandbox.go | 27 ++++++++++++++++++--------- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/network.go b/network.go index f128cac785..bb273c18ee 100644 --- a/network.go +++ b/network.go @@ -23,6 +23,14 @@ import ( "github.com/sirupsen/logrus" ) +type hostInfoStruct struct { + ipV4 net.IP + ipV6 net.IP +} + +// HostInfo will be used to resolve host.docker.internal hostname to an address +var HostInfo *hostInfoStruct + // A Network represents a logical connectivity zone that containers may // join using the Link method. A Network is managed by a specific driver. type Network interface { @@ -802,6 +810,15 @@ func NetworkOptionScope(scope string) NetworkOption { // NetworkOptionIpam function returns an option setter for the ipam configuration for this network func NetworkOptionIpam(ipamDriver string, addrSpace string, ipV4 []*IpamConf, ipV6 []*IpamConf, opts map[string]string) NetworkOption { + if ipamDriver == "default" && HostInfo == nil { + HostInfo = &hostInfoStruct{} + if len(ipV4) > 0 { + HostInfo.ipV4 = net.ParseIP(ipV4[0].Gateway) + } + if len(ipV6) > 0 { + HostInfo.ipV6 = net.ParseIP(ipV6[0].Gateway) + } + } return func(n *network) { if ipamDriver != "" { n.ipamType = ipamDriver diff --git a/sandbox.go b/sandbox.go index 8c104667b3..87640becf1 100644 --- a/sandbox.go +++ b/sandbox.go @@ -4,7 +4,6 @@ import ( "encoding/json" "fmt" "net" - "runtime" "sort" "strings" "sync" @@ -134,9 +133,10 @@ type containerConfig struct { } const ( - resolverIPSandbox = "127.0.0.11" - hostDockerInternal = "host.docker.internal" - gatewayDockerInternal = "gateway.docker.internal" + resolverIPSandbox = "127.0.0.11" + dnsOptResolveDockerInternal = "resolve-docker-internal" + hostDockerInternal = "host.docker.internal" + gatewayDockerInternal = "gateway.docker.internal" ) func (sb *sandbox) ID() string { @@ -404,6 +404,15 @@ func (sb *sandbox) getEndpoint(id string) *endpoint { return nil } +func (sb *sandbox) shouldResolveInternal() bool { + for _, opt := range sb.config.dnsOptionsList { + if opt == dnsOptResolveDockerInternal { + return true + } + } + return false +} + func (sb *sandbox) updateGateway(ep *endpoint) error { sb.Lock() osSbox := sb.osSbox @@ -430,11 +439,6 @@ func (sb *sandbox) updateGateway(ep *endpoint) error { return fmt.Errorf("failed to set IPv6 gateway while updating gateway: %v", err) } - if ep.needResolver() && runtime.GOOS == "linux" { - ep.network.addSvcRecords(ep.ID(), hostDockerInternal, ep.svcID, ep.Gateway(), ep.GatewayIPv6(), true, "updateGateway") - ep.network.addSvcRecords(ep.ID(), gatewayDockerInternal, ep.svcID, ep.Gateway(), ep.GatewayIPv6(), true, "updateGateway") - } - return nil } @@ -711,6 +715,11 @@ func (sb *sandbox) EnableService() (err error) { return fmt.Errorf("could not update state for endpoint %s into cluster: %v", ep.Name(), err) } ep.enableService() + if ep.needResolver() && sb.shouldResolveInternal() { + ep.network.addSvcRecords(ep.ID(), hostDockerInternal, ep.svcID, HostInfo.ipV4, HostInfo.ipV6, true, "updateGateway") + ep.network.addSvcRecords(ep.ID(), gatewayDockerInternal, ep.svcID, HostInfo.ipV4, HostInfo.ipV6, true, "updateGateway") + } + } } logrus.Debugf("EnableService %s DONE", sb.containerID)