Skip to content

Commit

Permalink
Moving DNS options to IPAM data
Browse files Browse the repository at this point in the history
Signed-off-by: Divya Vavili <[email protected]>
  • Loading branch information
dvavili committed Mar 9, 2016
1 parent 3d80368 commit a1452ed
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 65 deletions.
32 changes: 25 additions & 7 deletions endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,10 +391,12 @@ func (ep *endpoint) sbJoin(sb *sandbox, options ...EndpointOption) error {
}
}()

sb.config.dnsList = append(sb.config.dnsList, ep.iface.dnsServers...)
sb.config.dnsSearchList = append(sb.config.dnsSearchList, ep.iface.dnsSearchDomains...)
if err = sb.setupResolutionFiles(); err != nil {
log.Errorf("Error in setting up resolution files: err %+v", err)
if len(ep.iface.dnsServers) > 0 || len(ep.iface.dnsSearchDomains) > 0 {
sb.config.dnsList = appendUnique(sb.config.dnsList, ep.iface.dnsServers)
sb.config.dnsSearchList = appendUnique(sb.config.dnsSearchList, ep.iface.dnsSearchDomains)
if err = sb.setupResolutionFiles(); err != nil {
log.Errorf("Error in setting up resolution files: err %+v", err)
}
}

nid := n.ID()
Expand Down Expand Up @@ -478,6 +480,18 @@ func (ep *endpoint) sbJoin(sb *sandbox, options ...EndpointOption) error {
return sb.clearDefaultGW()
}

func appendUnique(list1, list2 []string) []string {
encountered := map[string]bool{}
var result []string
for _, el := range append(list1, list2...) {
if !encountered[el] {
encountered[el] = true
result = append(result, el)
}
}
return result
}

func (ep *endpoint) rename(name string) error {
var err error
n := ep.getNetwork()
Expand Down Expand Up @@ -936,12 +950,16 @@ func (ep *endpoint) assignAddressVersion(ipVer int, ipam ipamapi.Ipam) error {
if progAdd != nil && !d.Pool.Contains(progAdd) {
continue
}
addr, dnsServerList, dnsSearchList, _, err := ipam.RequestAddress(d.PoolID, progAdd, ep.ipamOptions)
addr, ipamData, err := ipam.RequestAddress(d.PoolID, progAdd, ep.ipamOptions)
if err == nil {
ep.Lock()
*address = addr
*dnsServers = dnsServerList
*dnsSearchDomains = dnsSearchList
if len(ipamData["DNSServers"]) > 0 {
*dnsServers = strings.Split(ipamData["DNSServers"], " ")
}
if len(ipamData["DNSSearchDomains"]) > 0 {
*dnsSearchDomains = strings.Split(ipamData["DNSSearchDomains"], " ")
}
*poolID = d.PoolID
ep.Unlock()
return nil
Expand Down
18 changes: 9 additions & 9 deletions ipam/allocator.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,32 +416,32 @@ func (a *Allocator) getPredefinedPool(as string, ipV6 bool) (*net.IPNet, error)
}

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

if err := a.refresh(k.AddressSpace); err != nil {
return nil, nil, nil, nil, err
return nil, nil, err
}

aSpace, err := a.getAddrSpace(k.AddressSpace)
if err != nil {
return nil, nil, nil, nil, err
return nil, nil, err
}

aSpace.Lock()
p, ok := aSpace.subnets[k]
if !ok {
aSpace.Unlock()
return nil, nil, nil, nil, types.NotFoundErrorf("cannot find address pool for poolID:%s", poolID)
return nil, nil, types.NotFoundErrorf("cannot find address pool for poolID:%s", poolID)
}

if prefAddress != nil && !p.Pool.Contains(prefAddress) {
aSpace.Unlock()
return nil, nil, nil, nil, ipamapi.ErrIPOutOfRange
return nil, nil, ipamapi.ErrIPOutOfRange
}

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

bm, err := a.retrieveBitmask(k, c.Pool)
if err != nil {
return nil, nil, nil, nil, types.InternalErrorf("could not find bitmask in datastore for %s on address %v request from pool %s: %v",
return nil, nil, types.InternalErrorf("could not find bitmask in datastore for %s on address %v request from pool %s: %v",
k.String(), prefAddress, poolID, err)
}
ip, err := a.getAddress(p.Pool, bm, prefAddress, p.Range)
if err != nil {
return nil, nil, nil, nil, err
return nil, nil, err
}

return &net.IPNet{IP: ip, Mask: p.Pool.Mask}, nil, nil, nil, nil
return &net.IPNet{IP: ip, Mask: p.Pool.Mask}, nil, nil
}

// ReleaseAddress releases the address from the specified pool ID
Expand Down
56 changes: 28 additions & 28 deletions ipam/allocator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func TestSubnetsMarshal(t *testing.T) {
if err != nil {
t.Fatal(err)
}
_, _, _, _, err = a.RequestAddress(pid0, nil, nil)
_, _, err = a.RequestAddress(pid0, nil, nil)
if err != nil {
t.Fatal(err)
}
Expand All @@ -192,7 +192,7 @@ func TestSubnetsMarshal(t *testing.T) {
}

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

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

ip := net.ParseIP("192.168.100.250")
_, _, _, _, err = a.RequestAddress(pid, ip, nil)
_, _, err = a.RequestAddress(pid, ip, nil)
if err != nil {
t.Fatal(err)
}

_, _, _, _, err = a.RequestAddress(pid, ip, nil)
_, _, err = a.RequestAddress(pid, ip, nil)
if err == nil {
t.Fatal(err)
}
Expand All @@ -578,7 +578,7 @@ func TestGetAddressSubPoolEqualPool(t *testing.T) {
t.Fatal(err)
}

_, _, _, _, err = a.RequestAddress(pid, nil, nil)
_, _, err = a.RequestAddress(pid, nil, nil)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -606,7 +606,7 @@ func TestRequestReleaseAddressFromSubPool(t *testing.T) {
expected := &net.IPNet{IP: net.IP{172, 28, 30, 255}, Mask: net.IPMask{255, 255, 0, 0}}
for err == nil {
var c *net.IPNet
if c, _, _, _, err = a.RequestAddress(poolID, nil, nil); err == nil {
if c, _, err = a.RequestAddress(poolID, nil, nil); err == nil {
ip = c
}
}
Expand All @@ -620,7 +620,7 @@ func TestRequestReleaseAddressFromSubPool(t *testing.T) {
if err = a.ReleaseAddress(poolID, rp.IP); err != nil {
t.Fatal(err)
}
if ip, _, _, _, err = a.RequestAddress(poolID, nil, nil); err != nil {
if ip, _, err = a.RequestAddress(poolID, nil, nil); err != nil {
t.Fatal(err)
}
if !types.CompareIPNet(rp, ip) {
Expand All @@ -638,7 +638,7 @@ func TestRequestReleaseAddressFromSubPool(t *testing.T) {
expected = &net.IPNet{IP: net.IP{10, 0, 0, 255}, Mask: net.IPMask{255, 255, 0, 0}}
for err == nil {
var c *net.IPNet
if c, _, _, _, err = a.RequestAddress(poolID, nil, nil); err == nil {
if c, _, err = a.RequestAddress(poolID, nil, nil); err == nil {
ip = c
}
}
Expand All @@ -652,7 +652,7 @@ func TestRequestReleaseAddressFromSubPool(t *testing.T) {
if err = a.ReleaseAddress(poolID, rp.IP); err != nil {
t.Fatal(err)
}
if ip, _, _, _, err = a.RequestAddress(poolID, nil, nil); err != nil {
if ip, _, err = a.RequestAddress(poolID, nil, nil); err != nil {
t.Fatal(err)
}
if !types.CompareIPNet(rp, ip) {
Expand All @@ -666,23 +666,23 @@ func TestRequestReleaseAddressFromSubPool(t *testing.T) {
if poolID, _, _, err = a.RequestPool("rosso", "10.2.0.0/16", "10.2.2.0/24", nil, false); err != nil {
t.Fatal(err)
}
tre, _, _, _, err := a.RequestAddress(poolID, treExp.IP, nil)
tre, _, err := a.RequestAddress(poolID, treExp.IP, nil)
if err != nil {
t.Fatal(err)
}
if !types.CompareIPNet(tre, treExp) {
t.Fatalf("Unexpected address: %v", tre)
}

uno, _, _, _, err := a.RequestAddress(poolID, nil, nil)
uno, _, err := a.RequestAddress(poolID, nil, nil)
if err != nil {
t.Fatal(err)
}
if !types.CompareIPNet(uno, unoExp) {
t.Fatalf("Unexpected address: %v", uno)
}

due, _, _, _, err := a.RequestAddress(poolID, nil, nil)
due, _, err := a.RequestAddress(poolID, nil, nil)
if err != nil {
t.Fatal(err)
}
Expand All @@ -693,7 +693,7 @@ func TestRequestReleaseAddressFromSubPool(t *testing.T) {
if err = a.ReleaseAddress(poolID, uno.IP); err != nil {
t.Fatal(err)
}
uno, _, _, _, err = a.RequestAddress(poolID, nil, nil)
uno, _, err = a.RequestAddress(poolID, nil, nil)
if err != nil {
t.Fatal(err)
}
Expand All @@ -704,7 +704,7 @@ func TestRequestReleaseAddressFromSubPool(t *testing.T) {
if err = a.ReleaseAddress(poolID, tre.IP); err != nil {
t.Fatal(err)
}
tre, _, _, _, err = a.RequestAddress(poolID, nil, nil)
tre, _, err = a.RequestAddress(poolID, nil, nil)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -765,19 +765,19 @@ func TestRequestSyntaxCheck(t *testing.T) {
t.Fatalf("Unexpected failure: %v", err)
}

_, _, _, _, err = a.RequestAddress("", nil, nil)
_, _, err = a.RequestAddress("", nil, nil)
if err == nil {
t.Fatalf("Failed to detect wrong request: no pool id specified")
}

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

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

// Allocate all addresses
for err != ipamapi.ErrNoAvailableIPs {
_, _, _, _, err = a.RequestAddress(pid, nil, nil)
_, _, err = a.RequestAddress(pid, nil, nil)
}

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

nw, _, _, _, err := a.RequestAddress(pid, nil, nil)
nw, _, err := a.RequestAddress(pid, nil, nil)
if err != nil {
t.Fatalf("Failed to obtain the address: %s", err.Error())
}
Expand Down Expand Up @@ -954,7 +954,7 @@ func assertNRequests(t *testing.T, subnet string, numReq int, lastExpectedIP str
i := 0
start := time.Now()
for ; i < numReq; i++ {
nw, _, _, _, err = a.RequestAddress(pid, nil, nil)
nw, _, err = a.RequestAddress(pid, nil, nil)
}
if printTime {
fmt.Printf("\nTaken %v, to allocate %d addresses on %s\n", time.Since(start), numReq, subnet)
Expand All @@ -968,7 +968,7 @@ func assertNRequests(t *testing.T, subnet string, numReq int, lastExpectedIP str
func benchmarkRequest(b *testing.B, a *Allocator, subnet string) {
pid, _, _, err := a.RequestPool(localAddressSpace, subnet, "", nil, false)
for err != ipamapi.ErrNoAvailableIPs {
_, _, _, _, err = a.RequestAddress(pid, nil, nil)
_, _, err = a.RequestAddress(pid, nil, nil)
}
}

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

// Request a quarter of addresses
for i := 0; i < num/2; i++ {
ip, _, _, _, err := a.RequestAddress(pid, nil, nil)
ip, _, err := a.RequestAddress(pid, nil, nil)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -1082,7 +1082,7 @@ func TestRetrieveFromStore(t *testing.T) {
t.Fatal(err)
}
for i := 0; i < num; i++ {
if _, _, _, _, err := a.RequestAddress(pid, nil, nil); err != nil {
if _, _, err := a.RequestAddress(pid, nil, nil); err != nil {
t.Fatal(err)
}
}
Expand All @@ -1104,7 +1104,7 @@ func TestRetrieveFromStore(t *testing.T) {
t.Fatal(err)
}
for i := 0; i < num/2; i++ {
if _, _, _, _, err := a1.RequestAddress(pid, nil, nil); err != nil {
if _, _, err := a1.RequestAddress(pid, nil, nil); err != nil {
t.Fatal(err)
}
}
Expand All @@ -1121,7 +1121,7 @@ func TestRetrieveFromStore(t *testing.T) {
t.Fatal(err)
}
for i := 0; i < num/2; i++ {
if _, _, _, _, err := a2.RequestAddress(pid, nil, nil); err != nil {
if _, _, err := a2.RequestAddress(pid, nil, nil); err != nil {
t.Fatal(err)
}
}
Expand All @@ -1138,7 +1138,7 @@ func TestRetrieveFromStore(t *testing.T) {
t.Fatal(err)
}
for i := 0; i < num/2; i++ {
if _, _, _, _, err := a3.RequestAddress(pid, nil, nil); err != nil {
if _, _, err := a3.RequestAddress(pid, nil, nil); err != nil {
t.Fatal(err)
}
}
Expand Down
2 changes: 1 addition & 1 deletion ipamapi/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ type Ipam interface {
// ReleasePool releases the address pool identified by the passed id
ReleasePool(poolID string) error
// Request address from the specified pool ID. Input options or required IP can be passed.
RequestAddress(string, net.IP, map[string]string) (*net.IPNet, []string, []string, map[string]string, error)
RequestAddress(string, net.IP, map[string]string) (*net.IPNet, map[string]string, error)
// Release the address from the specified pool ID
ReleaseAddress(string, net.IP) error
}
Expand Down
6 changes: 2 additions & 4 deletions ipams/remote/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,8 @@ type RequestAddressRequest struct {
// RequestAddressResponse represents the expected data in the response message to a ``request address`` request
type RequestAddressResponse struct {
Response
Address string // in CIDR format
DNSServers []string
DNSSearchDomains []string
Data map[string]string
Address string // in CIDR format
Data map[string]string
}

// ReleaseAddressRequest represents the expected data in a ``release address`` request message
Expand Down
Loading

0 comments on commit a1452ed

Please sign in to comment.