Skip to content

Commit e609da1

Browse files
committed
Add DNS options to RequestAddress API
1 parent 48a9f34 commit e609da1

File tree

8 files changed

+68
-31
lines changed

8 files changed

+68
-31
lines changed

Diff for: endpoint.go

+17-5
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,12 @@ func (ep *endpoint) sbJoin(sbox Sandbox, options ...EndpointOption) error {
400400
}
401401
}()
402402

403+
sb.config.dnsList = append(sb.config.dnsList, ep.iface.dnsServers...)
404+
sb.config.dnsSearchList = append(sb.config.dnsSearchList, ep.iface.dnsSearchDomains...)
405+
if err = sb.setupResolutionFiles(); err != nil {
406+
log.Errorf("Error in setting up resolution files: err %+v", err)
407+
}
408+
403409
network.Lock()
404410
nid := network.id
405411
network.Unlock()
@@ -859,13 +865,17 @@ func (ep *endpoint) assignAddress(ipam ipamapi.Ipam, assignIPv4, assignIPv6 bool
859865

860866
func (ep *endpoint) assignAddressVersion(ipVer int, ipam ipamapi.Ipam) error {
861867
var (
862-
poolID *string
863-
address **net.IPNet
864-
prefAdd net.IP
865-
progAdd net.IP
868+
poolID *string
869+
address **net.IPNet
870+
dnsServers *[]string
871+
dnsSearchDomains *[]string
872+
prefAdd net.IP
873+
progAdd net.IP
866874
)
867875

868876
n := ep.getNetwork()
877+
dnsServers = &ep.iface.dnsServers
878+
dnsSearchDomains = &ep.iface.dnsSearchDomains
869879
switch ipVer {
870880
case 4:
871881
poolID = &ep.iface.v4PoolID
@@ -898,10 +908,12 @@ func (ep *endpoint) assignAddressVersion(ipVer int, ipam ipamapi.Ipam) error {
898908
if progAdd != nil && !d.Pool.Contains(progAdd) {
899909
continue
900910
}
901-
addr, _, err := ipam.RequestAddress(d.PoolID, progAdd, ep.ipamOptions)
911+
addr, dnsServerList, dnsSearchList, _, err := ipam.RequestAddress(d.PoolID, progAdd, ep.ipamOptions)
902912
if err == nil {
903913
ep.Lock()
904914
*address = addr
915+
*dnsServers = dnsServerList
916+
*dnsSearchDomains = dnsSearchList
905917
*poolID = d.PoolID
906918
ep.Unlock()
907919
return nil

Diff for: endpoint_info.go

+31-8
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,16 @@ type InterfaceInfo interface {
4646
}
4747

4848
type endpointInterface struct {
49-
mac net.HardwareAddr
50-
addr *net.IPNet
51-
addrv6 *net.IPNet
52-
srcName string
53-
dstPrefix string
54-
routes []*net.IPNet
55-
v4PoolID string
56-
v6PoolID string
49+
mac net.HardwareAddr
50+
addr *net.IPNet
51+
addrv6 *net.IPNet
52+
dnsServers []string
53+
dnsSearchDomains []string
54+
srcName string
55+
dstPrefix string
56+
routes []*net.IPNet
57+
v4PoolID string
58+
v6PoolID string
5759
}
5860

5961
func (epi *endpointInterface) MarshalJSON() ([]byte, error) {
@@ -67,6 +69,8 @@ func (epi *endpointInterface) MarshalJSON() ([]byte, error) {
6769
if epi.addrv6 != nil {
6870
epMap["addrv6"] = epi.addrv6.String()
6971
}
72+
epMap["dnsServers"] = epi.dnsServers
73+
epMap["dnsSearchDomains"] = epi.dnsSearchDomains
7074
epMap["srcName"] = epi.srcName
7175
epMap["dstPrefix"] = epi.dstPrefix
7276
var routes []string
@@ -103,6 +107,16 @@ func (epi *endpointInterface) UnmarshalJSON(b []byte) error {
103107
}
104108
}
105109

110+
ds, _ := json.Marshal(epMap["dnsServers"])
111+
var dnsServers []string
112+
json.Unmarshal(ds, &dnsServers)
113+
epi.dnsServers = dnsServers
114+
115+
dsl, _ := json.Marshal(epMap["dnsSearchDomains"])
116+
var dnsSearchDomains []string
117+
json.Unmarshal(dsl, &dnsSearchDomains)
118+
epi.dnsSearchDomains = dnsSearchDomains
119+
106120
epi.srcName = epMap["srcName"].(string)
107121
epi.dstPrefix = epMap["dstPrefix"].(string)
108122

@@ -127,6 +141,15 @@ func (epi *endpointInterface) CopyTo(dstEpi *endpointInterface) error {
127141
dstEpi.mac = types.GetMacCopy(epi.mac)
128142
dstEpi.addr = types.GetIPNetCopy(epi.addr)
129143
dstEpi.addrv6 = types.GetIPNetCopy(epi.addrv6)
144+
145+
dstEpi.dnsServers = make([]string, len(epi.dnsServers))
146+
copy(dstEpi.dnsServers, epi.dnsServers)
147+
dstEpi.dnsServers = epi.dnsServers
148+
149+
dstEpi.dnsSearchDomains = make([]string, len(epi.dnsSearchDomains))
150+
copy(dstEpi.dnsSearchDomains, epi.dnsSearchDomains)
151+
dstEpi.dnsSearchDomains = epi.dnsSearchDomains
152+
130153
dstEpi.srcName = epi.srcName
131154
dstEpi.dstPrefix = epi.dstPrefix
132155
dstEpi.v4PoolID = epi.v4PoolID

Diff for: ipam/allocator.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -416,32 +416,32 @@ func (a *Allocator) getPredefinedPool(as string, ipV6 bool) (*net.IPNet, error)
416416
}
417417

418418
// RequestAddress returns an address from the specified pool ID
419-
func (a *Allocator) RequestAddress(poolID string, prefAddress net.IP, opts map[string]string) (*net.IPNet, map[string]string, error) {
419+
func (a *Allocator) RequestAddress(poolID string, prefAddress net.IP, opts map[string]string) (*net.IPNet, []string, []string, map[string]string, error) {
420420
log.Debugf("RequestAddress(%s, %v, %v)", poolID, prefAddress, opts)
421421
k := SubnetKey{}
422422
if err := k.FromString(poolID); err != nil {
423-
return nil, nil, types.BadRequestErrorf("invalid pool id: %s", poolID)
423+
return nil, nil, nil, nil, types.BadRequestErrorf("invalid pool id: %s", poolID)
424424
}
425425

426426
if err := a.refresh(k.AddressSpace); err != nil {
427-
return nil, nil, err
427+
return nil, nil, nil, nil, err
428428
}
429429

430430
aSpace, err := a.getAddrSpace(k.AddressSpace)
431431
if err != nil {
432-
return nil, nil, err
432+
return nil, nil, nil, nil, err
433433
}
434434

435435
aSpace.Lock()
436436
p, ok := aSpace.subnets[k]
437437
if !ok {
438438
aSpace.Unlock()
439-
return nil, nil, types.NotFoundErrorf("cannot find address pool for poolID:%s", poolID)
439+
return nil, nil, nil, nil, types.NotFoundErrorf("cannot find address pool for poolID:%s", poolID)
440440
}
441441

442442
if prefAddress != nil && !p.Pool.Contains(prefAddress) {
443443
aSpace.Unlock()
444-
return nil, nil, ipamapi.ErrIPOutOfRange
444+
return nil, nil, nil, nil, ipamapi.ErrIPOutOfRange
445445
}
446446

447447
c := p
@@ -453,15 +453,15 @@ func (a *Allocator) RequestAddress(poolID string, prefAddress net.IP, opts map[s
453453

454454
bm, err := a.retrieveBitmask(k, c.Pool)
455455
if err != nil {
456-
return nil, nil, types.InternalErrorf("could not find bitmask in datastore for %s on address %v request from pool %s: %v",
456+
return nil, nil, nil, nil, types.InternalErrorf("could not find bitmask in datastore for %s on address %v request from pool %s: %v",
457457
k.String(), prefAddress, poolID, err)
458458
}
459459
ip, err := a.getAddress(p.Pool, bm, prefAddress, p.Range)
460460
if err != nil {
461-
return nil, nil, err
461+
return nil, nil, nil, nil, err
462462
}
463463

464-
return &net.IPNet{IP: ip, Mask: p.Pool.Mask}, nil, nil
464+
return &net.IPNet{IP: ip, Mask: p.Pool.Mask}, nil, nil, nil, nil
465465
}
466466

467467
// ReleaseAddress releases the address from the specified pool ID

Diff for: ipamapi/contract.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ type Ipam interface {
7171
// ReleasePool releases the address pool identified by the passed id
7272
ReleasePool(poolID string) error
7373
// Request address from the specified pool ID. Input options or required IP can be passed.
74-
RequestAddress(string, net.IP, map[string]string) (*net.IPNet, map[string]string, error)
74+
RequestAddress(string, net.IP, map[string]string) (*net.IPNet, []string, []string, map[string]string, error)
7575
// Release the address from the specified pool ID
7676
ReleaseAddress(string, net.IP) error
7777
}

Diff for: ipams/remote/api/api.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,10 @@ type RequestAddressRequest struct {
7474
// RequestAddressResponse represents the expected data in the response message to a ``request address`` request
7575
type RequestAddressResponse struct {
7676
Response
77-
Address string // in CIDR format
78-
Data map[string]string
77+
Address string // in CIDR format
78+
DNSServers []string
79+
DNSSearchDomains []string
80+
Data map[string]string
7981
}
8082

8183
// ReleaseAddressRequest represents the expected data in a ``release address`` request message

Diff for: ipams/remote/remote.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func (a *allocator) ReleasePool(poolID string) error {
9595
}
9696

9797
// RequestAddress requests an address from the address pool
98-
func (a *allocator) RequestAddress(poolID string, address net.IP, options map[string]string) (*net.IPNet, map[string]string, error) {
98+
func (a *allocator) RequestAddress(poolID string, address net.IP, options map[string]string) (*net.IPNet, []string, []string, map[string]string, error) {
9999
var (
100100
prefAddress string
101101
retAddress *net.IPNet
@@ -107,12 +107,12 @@ func (a *allocator) RequestAddress(poolID string, address net.IP, options map[st
107107
req := &api.RequestAddressRequest{PoolID: poolID, Address: prefAddress, Options: options}
108108
res := &api.RequestAddressResponse{}
109109
if err := a.call("RequestAddress", req, res); err != nil {
110-
return nil, nil, err
110+
return nil, nil, nil, nil, err
111111
}
112112
if res.Address != "" {
113113
retAddress, err = types.ParseCIDR(res.Address)
114114
}
115-
return retAddress, res.Data, err
115+
return retAddress, res.DNSServers, res.DNSSearchDomains, res.Data, err
116116
}
117117

118118
// ReleaseAddress releases the address from the specified address pool

Diff for: ipams/windowsipam/windowsipam.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (a *allocator) ReleasePool(poolID string) error {
6363

6464
// RequestAddress returns an address from the specified pool ID.
6565
// Always allocate the 0.0.0.0/32 ip if no preferred address was specified
66-
func (a *allocator) RequestAddress(poolID string, prefAddress net.IP, opts map[string]string) (*net.IPNet, map[string]string, error) {
66+
func (a *allocator) RequestAddress(poolID string, prefAddress net.IP, opts map[string]string) (*net.IPNet, []string, []string, map[string]string, error) {
6767
log.Debugf("RequestAddress(%s, %v, %v) %s", poolID, prefAddress, opts, opts["RequestAddressType"])
6868
_, ipNet, err := net.ParseCIDR(poolID)
6969

Diff for: network.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1044,7 +1044,7 @@ func (n *network) ipamAllocateVersion(ipVer int, ipam ipamapi.Ipam) error {
10441044
var gatewayOpts = map[string]string{
10451045
ipamapi.RequestAddressType: netlabel.Gateway,
10461046
}
1047-
if d.Gateway, _, err = ipam.RequestAddress(d.PoolID, net.ParseIP(cfg.Gateway), gatewayOpts); err != nil {
1047+
if d.Gateway, _, _, _, err = ipam.RequestAddress(d.PoolID, net.ParseIP(cfg.Gateway), gatewayOpts); err != nil {
10481048
return types.InternalErrorf("failed to allocate gateway (%v): %v", cfg.Gateway, err)
10491049
}
10501050
}
@@ -1062,7 +1062,7 @@ func (n *network) ipamAllocateVersion(ipVer int, ipam ipamapi.Ipam) error {
10621062
return types.ForbiddenErrorf("auxilairy address: (%s:%s) must belong to the master pool: %s", k, v, d.Pool)
10631063
}
10641064
// Attempt reservation in the container addressable pool, silent the error if address does not belong to that pool
1065-
if d.IPAMData.AuxAddresses[k], _, err = ipam.RequestAddress(d.PoolID, ip, nil); err != nil && err != ipamapi.ErrIPOutOfRange {
1065+
if d.IPAMData.AuxAddresses[k], _, _, _, err = ipam.RequestAddress(d.PoolID, ip, nil); err != nil && err != ipamapi.ErrIPOutOfRange {
10661066
return types.InternalErrorf("failed to allocate secondary ip address (%s:%s): %v", k, v, err)
10671067
}
10681068
}

0 commit comments

Comments
 (0)