Skip to content

Commit 4ddc9c7

Browse files
committed
Moving DNS options to IPAM data
Signed-off-by: Divya Vavili <vavili.divya@gmail.com>
1 parent f9c8a33 commit 4ddc9c7

File tree

10 files changed

+87
-65
lines changed

10 files changed

+87
-65
lines changed

endpoint.go

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -400,10 +400,13 @@ 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)
403+
if len(ep.iface.dnsServers) > 0 || len(ep.iface.dnsSearchDomains) > 0 {
404+
sb.config.dnsList = appendUnique(sb.config.dnsList, ep.iface.dnsServers)
405+
sb.config.dnsSearchList = appendUnique(sb.config.dnsSearchList, ep.iface.dnsSearchDomains)
406+
log.Errorf("DNS information: Servers: %+v, Search domains: %+v", sb.config.dnsList, sb.config.dnsSearchList)
407+
if err = sb.setupResolutionFiles(); err != nil {
408+
log.Errorf("Error in setting up resolution files: err %+v", err)
409+
}
407410
}
408411

409412
network.Lock()
@@ -474,6 +477,18 @@ func (ep *endpoint) sbJoin(sbox Sandbox, options ...EndpointOption) error {
474477
return nil
475478
}
476479

480+
func appendUnique(list1, list2 []string) []string {
481+
encountered := map[string]bool{}
482+
var result []string
483+
for _, el := range append(list1, list2...) {
484+
if !encountered[el] {
485+
encountered[el] = true
486+
result = append(result, el)
487+
}
488+
}
489+
return result
490+
}
491+
477492
func (ep *endpoint) rename(name string) error {
478493
var err error
479494
n := ep.getNetwork()
@@ -908,12 +923,16 @@ func (ep *endpoint) assignAddressVersion(ipVer int, ipam ipamapi.Ipam) error {
908923
if progAdd != nil && !d.Pool.Contains(progAdd) {
909924
continue
910925
}
911-
addr, dnsServerList, dnsSearchList, _, err := ipam.RequestAddress(d.PoolID, progAdd, ep.ipamOptions)
926+
addr, ipamData, err := ipam.RequestAddress(d.PoolID, progAdd, ep.ipamOptions)
912927
if err == nil {
913928
ep.Lock()
914929
*address = addr
915-
*dnsServers = dnsServerList
916-
*dnsSearchDomains = dnsSearchList
930+
if len(ipamData["DNSServers"]) > 0 {
931+
*dnsServers = strings.Split(ipamData["DNSServers"], " ")
932+
}
933+
if len(ipamData["DNSSearchDomains"]) > 0 {
934+
*dnsSearchDomains = strings.Split(ipamData["DNSSearchDomains"], " ")
935+
}
917936
*poolID = d.PoolID
918937
ep.Unlock()
919938
return nil

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, []string, []string, map[string]string, error) {
419+
func (a *Allocator) RequestAddress(poolID string, prefAddress net.IP, opts map[string]string) (*net.IPNet, 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, nil, nil, types.BadRequestErrorf("invalid pool id: %s", poolID)
423+
return nil, nil, types.BadRequestErrorf("invalid pool id: %s", poolID)
424424
}
425425

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

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

435435
aSpace.Lock()
436436
p, ok := aSpace.subnets[k]
437437
if !ok {
438438
aSpace.Unlock()
439-
return nil, nil, nil, nil, types.NotFoundErrorf("cannot find address pool for poolID:%s", poolID)
439+
return 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, nil, nil, ipamapi.ErrIPOutOfRange
444+
return 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, nil, nil, types.InternalErrorf("could not find bitmask in datastore for %s on address %v request from pool %s: %v",
456+
return 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, nil, nil, err
461+
return nil, nil, err
462462
}
463463

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

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

ipam/allocator_test.go

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ func TestSubnetsMarshal(t *testing.T) {
176176
if err != nil {
177177
t.Fatal(err)
178178
}
179-
_, _, _, _, err = a.RequestAddress(pid0, nil, nil)
179+
_, _, err = a.RequestAddress(pid0, nil, nil)
180180
if err != nil {
181181
t.Fatal(err)
182182
}
@@ -192,7 +192,7 @@ func TestSubnetsMarshal(t *testing.T) {
192192
}
193193

194194
expIP := &net.IPNet{IP: net.IP{192, 168, 0, 2}, Mask: net.IPMask{255, 255, 0, 0}}
195-
ip, _, _, _, err := a.RequestAddress(pid0, nil, nil)
195+
ip, _, err := a.RequestAddress(pid0, nil, nil)
196196
if err != nil {
197197
t.Fatal(err)
198198
}
@@ -201,7 +201,7 @@ func TestSubnetsMarshal(t *testing.T) {
201201
}
202202

203203
expIP = &net.IPNet{IP: net.IP{192, 169, 0, 1}, Mask: net.IPMask{255, 255, 0, 0}}
204-
ip, _, _, _, err = a.RequestAddress(pid1, nil, nil)
204+
ip, _, err = a.RequestAddress(pid1, nil, nil)
205205
if err != nil {
206206
t.Fatal(err)
207207
}
@@ -556,12 +556,12 @@ func TestGetSameAddress(t *testing.T) {
556556
}
557557

558558
ip := net.ParseIP("192.168.100.250")
559-
_, _, _, _, err = a.RequestAddress(pid, ip, nil)
559+
_, _, err = a.RequestAddress(pid, ip, nil)
560560
if err != nil {
561561
t.Fatal(err)
562562
}
563563

564-
_, _, _, _, err = a.RequestAddress(pid, ip, nil)
564+
_, _, err = a.RequestAddress(pid, ip, nil)
565565
if err == nil {
566566
t.Fatal(err)
567567
}
@@ -578,7 +578,7 @@ func TestGetAddressSubPoolEqualPool(t *testing.T) {
578578
t.Fatal(err)
579579
}
580580

581-
_, _, _, _, err = a.RequestAddress(pid, nil, nil)
581+
_, _, err = a.RequestAddress(pid, nil, nil)
582582
if err != nil {
583583
t.Fatal(err)
584584
}
@@ -606,7 +606,7 @@ func TestRequestReleaseAddressFromSubPool(t *testing.T) {
606606
expected := &net.IPNet{IP: net.IP{172, 28, 30, 255}, Mask: net.IPMask{255, 255, 0, 0}}
607607
for err == nil {
608608
var c *net.IPNet
609-
if c, _, _, _, err = a.RequestAddress(poolID, nil, nil); err == nil {
609+
if c, _, err = a.RequestAddress(poolID, nil, nil); err == nil {
610610
ip = c
611611
}
612612
}
@@ -620,7 +620,7 @@ func TestRequestReleaseAddressFromSubPool(t *testing.T) {
620620
if err = a.ReleaseAddress(poolID, rp.IP); err != nil {
621621
t.Fatal(err)
622622
}
623-
if ip, _, _, _, err = a.RequestAddress(poolID, nil, nil); err != nil {
623+
if ip, _, err = a.RequestAddress(poolID, nil, nil); err != nil {
624624
t.Fatal(err)
625625
}
626626
if !types.CompareIPNet(rp, ip) {
@@ -638,7 +638,7 @@ func TestRequestReleaseAddressFromSubPool(t *testing.T) {
638638
expected = &net.IPNet{IP: net.IP{10, 0, 0, 255}, Mask: net.IPMask{255, 255, 0, 0}}
639639
for err == nil {
640640
var c *net.IPNet
641-
if c, _, _, _, err = a.RequestAddress(poolID, nil, nil); err == nil {
641+
if c, _, err = a.RequestAddress(poolID, nil, nil); err == nil {
642642
ip = c
643643
}
644644
}
@@ -652,7 +652,7 @@ func TestRequestReleaseAddressFromSubPool(t *testing.T) {
652652
if err = a.ReleaseAddress(poolID, rp.IP); err != nil {
653653
t.Fatal(err)
654654
}
655-
if ip, _, _, _, err = a.RequestAddress(poolID, nil, nil); err != nil {
655+
if ip, _, err = a.RequestAddress(poolID, nil, nil); err != nil {
656656
t.Fatal(err)
657657
}
658658
if !types.CompareIPNet(rp, ip) {
@@ -666,23 +666,23 @@ func TestRequestReleaseAddressFromSubPool(t *testing.T) {
666666
if poolID, _, _, err = a.RequestPool("rosso", "10.2.0.0/16", "10.2.2.0/24", nil, false); err != nil {
667667
t.Fatal(err)
668668
}
669-
tre, _, _, _, err := a.RequestAddress(poolID, treExp.IP, nil)
669+
tre, _, err := a.RequestAddress(poolID, treExp.IP, nil)
670670
if err != nil {
671671
t.Fatal(err)
672672
}
673673
if !types.CompareIPNet(tre, treExp) {
674674
t.Fatalf("Unexpected address: %v", tre)
675675
}
676676

677-
uno, _, _, _, err := a.RequestAddress(poolID, nil, nil)
677+
uno, _, err := a.RequestAddress(poolID, nil, nil)
678678
if err != nil {
679679
t.Fatal(err)
680680
}
681681
if !types.CompareIPNet(uno, unoExp) {
682682
t.Fatalf("Unexpected address: %v", uno)
683683
}
684684

685-
due, _, _, _, err := a.RequestAddress(poolID, nil, nil)
685+
due, _, err := a.RequestAddress(poolID, nil, nil)
686686
if err != nil {
687687
t.Fatal(err)
688688
}
@@ -693,7 +693,7 @@ func TestRequestReleaseAddressFromSubPool(t *testing.T) {
693693
if err = a.ReleaseAddress(poolID, uno.IP); err != nil {
694694
t.Fatal(err)
695695
}
696-
uno, _, _, _, err = a.RequestAddress(poolID, nil, nil)
696+
uno, _, err = a.RequestAddress(poolID, nil, nil)
697697
if err != nil {
698698
t.Fatal(err)
699699
}
@@ -704,7 +704,7 @@ func TestRequestReleaseAddressFromSubPool(t *testing.T) {
704704
if err = a.ReleaseAddress(poolID, tre.IP); err != nil {
705705
t.Fatal(err)
706706
}
707-
tre, _, _, _, err = a.RequestAddress(poolID, nil, nil)
707+
tre, _, err = a.RequestAddress(poolID, nil, nil)
708708
if err != nil {
709709
t.Fatal(err)
710710
}
@@ -765,19 +765,19 @@ func TestRequestSyntaxCheck(t *testing.T) {
765765
t.Fatalf("Unexpected failure: %v", err)
766766
}
767767

768-
_, _, _, _, err = a.RequestAddress("", nil, nil)
768+
_, _, err = a.RequestAddress("", nil, nil)
769769
if err == nil {
770770
t.Fatalf("Failed to detect wrong request: no pool id specified")
771771
}
772772

773773
ip := net.ParseIP("172.17.0.23")
774-
_, _, _, _, err = a.RequestAddress(pid, ip, nil)
774+
_, _, err = a.RequestAddress(pid, ip, nil)
775775
if err == nil {
776776
t.Fatalf("Failed to detect wrong request: requested IP from different subnet")
777777
}
778778

779779
ip = net.ParseIP("192.168.0.50")
780-
_, _, _, _, err = a.RequestAddress(pid, ip, nil)
780+
_, _, err = a.RequestAddress(pid, ip, nil)
781781
if err != nil {
782782
t.Fatalf("Unexpected failure: %v", err)
783783
}
@@ -848,7 +848,7 @@ func TestRelease(t *testing.T) {
848848

849849
// Allocate all addresses
850850
for err != ipamapi.ErrNoAvailableIPs {
851-
_, _, _, _, err = a.RequestAddress(pid, nil, nil)
851+
_, _, err = a.RequestAddress(pid, nil, nil)
852852
}
853853

854854
toRelease := []struct {
@@ -887,7 +887,7 @@ func TestRelease(t *testing.T) {
887887
t.Fatalf("Failed to update free address count after release. Expected %d, Found: %d", i+1, bm.Unselected())
888888
}
889889

890-
nw, _, _, _, err := a.RequestAddress(pid, nil, nil)
890+
nw, _, err := a.RequestAddress(pid, nil, nil)
891891
if err != nil {
892892
t.Fatalf("Failed to obtain the address: %s", err.Error())
893893
}
@@ -954,7 +954,7 @@ func assertNRequests(t *testing.T, subnet string, numReq int, lastExpectedIP str
954954
i := 0
955955
start := time.Now()
956956
for ; i < numReq; i++ {
957-
nw, _, _, _, err = a.RequestAddress(pid, nil, nil)
957+
nw, _, err = a.RequestAddress(pid, nil, nil)
958958
}
959959
if printTime {
960960
fmt.Printf("\nTaken %v, to allocate %d addresses on %s\n", time.Since(start), numReq, subnet)
@@ -968,7 +968,7 @@ func assertNRequests(t *testing.T, subnet string, numReq int, lastExpectedIP str
968968
func benchmarkRequest(b *testing.B, a *Allocator, subnet string) {
969969
pid, _, _, err := a.RequestPool(localAddressSpace, subnet, "", nil, false)
970970
for err != ipamapi.ErrNoAvailableIPs {
971-
_, _, _, _, err = a.RequestAddress(pid, nil, nil)
971+
_, _, err = a.RequestAddress(pid, nil, nil)
972972
}
973973
}
974974

@@ -1019,7 +1019,7 @@ func testAllocateRandomDeallocate(t *testing.T, pool, subPool string, num int) {
10191019
indices := make(map[int]*net.IPNet, num)
10201020
allocated := make(map[string]bool, num)
10211021
for i := 0; i < num; i++ {
1022-
ip, _, _, _, err := a.RequestAddress(pid, nil, nil)
1022+
ip, _, err := a.RequestAddress(pid, nil, nil)
10231023
if err != nil {
10241024
t.Fatal(err)
10251025
}
@@ -1052,7 +1052,7 @@ func testAllocateRandomDeallocate(t *testing.T, pool, subPool string, num int) {
10521052

10531053
// Request a quarter of addresses
10541054
for i := 0; i < num/2; i++ {
1055-
ip, _, _, _, err := a.RequestAddress(pid, nil, nil)
1055+
ip, _, err := a.RequestAddress(pid, nil, nil)
10561056
if err != nil {
10571057
t.Fatal(err)
10581058
}
@@ -1082,7 +1082,7 @@ func TestRetrieveFromStore(t *testing.T) {
10821082
t.Fatal(err)
10831083
}
10841084
for i := 0; i < num; i++ {
1085-
if _, _, _, _, err := a.RequestAddress(pid, nil, nil); err != nil {
1085+
if _, _, err := a.RequestAddress(pid, nil, nil); err != nil {
10861086
t.Fatal(err)
10871087
}
10881088
}
@@ -1104,7 +1104,7 @@ func TestRetrieveFromStore(t *testing.T) {
11041104
t.Fatal(err)
11051105
}
11061106
for i := 0; i < num/2; i++ {
1107-
if _, _, _, _, err := a1.RequestAddress(pid, nil, nil); err != nil {
1107+
if _, _, err := a1.RequestAddress(pid, nil, nil); err != nil {
11081108
t.Fatal(err)
11091109
}
11101110
}
@@ -1121,7 +1121,7 @@ func TestRetrieveFromStore(t *testing.T) {
11211121
t.Fatal(err)
11221122
}
11231123
for i := 0; i < num/2; i++ {
1124-
if _, _, _, _, err := a2.RequestAddress(pid, nil, nil); err != nil {
1124+
if _, _, err := a2.RequestAddress(pid, nil, nil); err != nil {
11251125
t.Fatal(err)
11261126
}
11271127
}
@@ -1138,7 +1138,7 @@ func TestRetrieveFromStore(t *testing.T) {
11381138
t.Fatal(err)
11391139
}
11401140
for i := 0; i < num/2; i++ {
1141-
if _, _, _, _, err := a3.RequestAddress(pid, nil, nil); err != nil {
1141+
if _, _, err := a3.RequestAddress(pid, nil, nil); err != nil {
11421142
t.Fatal(err)
11431143
}
11441144
}

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, []string, []string, map[string]string, error)
76+
RequestAddress(string, net.IP, map[string]string) (*net.IPNet, 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: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,8 @@ 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-
DNSServers []string
79-
DNSSearchDomains []string
80-
Data map[string]string
77+
Address string // in CIDR format
78+
Data map[string]string
8179
}
8280

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

0 commit comments

Comments
 (0)