Skip to content

Commit bafc66d

Browse files
committed
Add DNS options to RequestAddress API
Signed-off-by: Divya Vavili <vavili.divya@gmail.com>
1 parent f20612e commit bafc66d

7 files changed

Lines changed: 67 additions & 30 deletions

File tree

endpoint.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,12 @@ func (ep *endpoint) sbJoin(sb *sandbox, options ...EndpointOption) error {
391391
}
392392
}()
393393

394+
sb.config.dnsList = append(sb.config.dnsList, ep.iface.dnsServers...)
395+
sb.config.dnsSearchList = append(sb.config.dnsSearchList, ep.iface.dnsSearchDomains...)
396+
if err = sb.setupResolutionFiles(); err != nil {
397+
log.Errorf("Error in setting up resolution files: err %+v", err)
398+
}
399+
394400
nid := n.ID()
395401

396402
ep.processOptions(options...)
@@ -887,13 +893,17 @@ func (ep *endpoint) assignAddress(ipam ipamapi.Ipam, assignIPv4, assignIPv6 bool
887893

888894
func (ep *endpoint) assignAddressVersion(ipVer int, ipam ipamapi.Ipam) error {
889895
var (
890-
poolID *string
891-
address **net.IPNet
892-
prefAdd net.IP
893-
progAdd net.IP
896+
poolID *string
897+
address **net.IPNet
898+
dnsServers *[]string
899+
dnsSearchDomains *[]string
900+
prefAdd net.IP
901+
progAdd net.IP
894902
)
895903

896904
n := ep.getNetwork()
905+
dnsServers = &ep.iface.dnsServers
906+
dnsSearchDomains = &ep.iface.dnsSearchDomains
897907
switch ipVer {
898908
case 4:
899909
poolID = &ep.iface.v4PoolID
@@ -926,10 +936,12 @@ func (ep *endpoint) assignAddressVersion(ipVer int, ipam ipamapi.Ipam) error {
926936
if progAdd != nil && !d.Pool.Contains(progAdd) {
927937
continue
928938
}
929-
addr, _, err := ipam.RequestAddress(d.PoolID, progAdd, ep.ipamOptions)
939+
addr, dnsServerList, dnsSearchList, _, err := ipam.RequestAddress(d.PoolID, progAdd, ep.ipamOptions)
930940
if err == nil {
931941
ep.Lock()
932942
*address = addr
943+
*dnsServers = dnsServerList
944+
*dnsSearchDomains = dnsSearchList
933945
*poolID = d.PoolID
934946
ep.Unlock()
935947
return nil

endpoint_info.go

Lines changed: 31 additions & 8 deletions
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

ipam/allocator.go

Lines changed: 9 additions & 9 deletions
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

ipamapi/contract.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ type Ipam interface {
7373
// ReleasePool releases the address pool identified by the passed id
7474
ReleasePool(poolID string) error
7575
// Request address from the specified pool ID. Input options or required IP can be passed.
76-
RequestAddress(string, net.IP, map[string]string) (*net.IPNet, map[string]string, error)
76+
RequestAddress(string, net.IP, map[string]string) (*net.IPNet, []string, []string, map[string]string, error)
7777
// Release the address from the specified pool ID
7878
ReleaseAddress(string, net.IP) error
7979
}

ipams/remote/api/api.go

Lines changed: 4 additions & 2 deletions
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

ipams/remote/remote.go

Lines changed: 3 additions & 3 deletions
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

network.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,7 +1055,7 @@ func (n *network) ipamAllocateVersion(ipVer int, ipam ipamapi.Ipam) error {
10551055
var gatewayOpts = map[string]string{
10561056
ipamapi.RequestAddressType: netlabel.Gateway,
10571057
}
1058-
if d.Gateway, _, err = ipam.RequestAddress(d.PoolID, net.ParseIP(cfg.Gateway), gatewayOpts); err != nil {
1058+
if d.Gateway, _, _, _, err = ipam.RequestAddress(d.PoolID, net.ParseIP(cfg.Gateway), gatewayOpts); err != nil {
10591059
return types.InternalErrorf("failed to allocate gateway (%v): %v", cfg.Gateway, err)
10601060
}
10611061
}
@@ -1073,7 +1073,7 @@ func (n *network) ipamAllocateVersion(ipVer int, ipam ipamapi.Ipam) error {
10731073
return types.ForbiddenErrorf("auxilairy address: (%s:%s) must belong to the master pool: %s", k, v, d.Pool)
10741074
}
10751075
// Attempt reservation in the container addressable pool, silent the error if address does not belong to that pool
1076-
if d.IPAMData.AuxAddresses[k], _, err = ipam.RequestAddress(d.PoolID, ip, nil); err != nil && err != ipamapi.ErrIPOutOfRange {
1076+
if d.IPAMData.AuxAddresses[k], _, _, _, err = ipam.RequestAddress(d.PoolID, ip, nil); err != nil && err != ipamapi.ErrIPOutOfRange {
10771077
return types.InternalErrorf("failed to allocate secondary ip address (%s:%s): %v", k, v, err)
10781078
}
10791079
}

0 commit comments

Comments
 (0)