Skip to content

Commit

Permalink
--dns-option "resolve-docker-internal" to enable the feature
Browse files Browse the repository at this point in the history
Signed-off-by: Daniil Skomarovskiy <0xbad0c0d3@gmail.com>
  • Loading branch information
0xbad0c0d3 committed Sep 26, 2019
1 parent f2637f9 commit b728621
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
17 changes: 17 additions & 0 deletions network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
27 changes: 18 additions & 9 deletions sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"
"net"
"runtime"
"sort"
"strings"
"sync"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit b728621

Please sign in to comment.