From 50eaccc60c94d7a59c598da7ff4c9e3d7a211583 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Wed, 3 Jun 2015 13:58:17 +0100 Subject: [PATCH] Allocate() and Lookup() now return a CIDR, to simplify output --- ipam/allocator.go | 8 ++++---- ipam/allocator_test.go | 22 +++++++++++----------- ipam/http.go | 8 ++++---- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/ipam/allocator.go b/ipam/allocator.go index 0444ff69..71fad920 100644 --- a/ipam/allocator.go +++ b/ipam/allocator.go @@ -191,16 +191,16 @@ func (alloc *Allocator) SetDefaultSubnet(subnet address.CIDR) error { // Allocate (Sync) - get IP address for container with given name // if there isn't any space we block indefinitely -func (alloc *Allocator) Allocate(ident string, subnet address.CIDR, cancelChan <-chan bool) (address.Address, error) { +func (alloc *Allocator) Allocate(ident string, subnet address.CIDR, cancelChan <-chan bool) (address.CIDR, error) { resultChan := make(chan allocateResult) op := &allocate{subnet: subnet, resultChan: resultChan, ident: ident, hasBeenCancelled: hasBeenCancelled(cancelChan)} alloc.doOperation(op, &alloc.pendingAllocates) result := <-resultChan - return result.addr, result.err + return address.CIDR{Start: result.addr, PrefixLen: subnet.PrefixLen}, result.err } -func (alloc *Allocator) Lookup(ident string, subnet address.CIDR) (address.Address, error) { +func (alloc *Allocator) Lookup(ident string, subnet address.CIDR) (address.CIDR, error) { resultChan := make(chan allocateResult) alloc.actionChan <- func() { for _, addr := range alloc.owned[ident] { @@ -212,7 +212,7 @@ func (alloc *Allocator) Lookup(ident string, subnet address.CIDR) (address.Addre resultChan <- allocateResult{err: fmt.Errorf("lookup: no address found")} } result := <-resultChan - return result.addr, result.err + return address.CIDR{Start: result.addr, PrefixLen: subnet.PrefixLen}, result.err } // Claim an address that we think we should own (Sync) diff --git a/ipam/allocator_test.go b/ipam/allocator_test.go index ad30d675..bf378ad0 100644 --- a/ipam/allocator_test.go +++ b/ipam/allocator_test.go @@ -27,8 +27,8 @@ func TestAllocFree(t *testing.T) { universe = "10.0.3.0/26" subnet1 = "10.0.3.0/28" subnet2 = "10.0.3.32/28" - testAddr1 = "10.0.3.1" - testAddr2 = "10.0.3.33" + testAddr1 = "10.0.3.1/28" + testAddr2 = "10.0.3.33/28" spaceSize = 62 // 64 IP addresses in /26, minus .0 and .63 ) @@ -134,7 +134,7 @@ func TestAllocatorClaim(t *testing.T) { container2 = "baddf00d" container3 = "b01df00d" universe = "10.0.3.0/30" - testAddr1 = "10.0.3.1" // first address allocated should be .1 because .0 is network addr + testAddr1 = "10.0.3.1/30" // first address allocated should be .1 because .0 is network addr ) alloc := makeAllocatorWithMockGossip(t, "01:00:00:01:00:00", universe, 1) @@ -224,15 +224,13 @@ func TestGossipShutdown(t *testing.T) { container1 = "abcdef" container2 = "baddf00d" universe = "10.0.3.0/30" - testAddr1 = "10.0.3.1" // first address allocated should be .1 because .0 is network addr ) alloc := makeAllocatorWithMockGossip(t, "01:00:00:01:00:00", universe, 1) defer alloc.Stop() alloc.claimRingForTesting() - addr1, _ := alloc.Allocate(container1, alloc.defaultSubnet, nil) - wt.AssertEqualString(t, addr1.String(), testAddr1, "address") + alloc.Allocate(container1, alloc.defaultSubnet, nil) alloc.Shutdown() @@ -284,8 +282,7 @@ func TestFakeRouterSimple(t *testing.T) { alloc1 := allocs[0] //alloc2 := allocs[1] - addr, _ := alloc1.Allocate("foo", alloc1.defaultSubnet, nil) - println("Got addr", addr) + alloc1.Allocate("foo", alloc1.defaultSubnet, nil) } func TestAllocatorFuzz(t *testing.T) { @@ -387,7 +384,10 @@ func TestAllocatorFuzz(t *testing.T) { alloc := allocs[res.alloc] //common.Info.Printf("Freeing %s (%s) on allocator %d", res.name, addr, res.alloc) - oldAddr, _ := address.ParseIP(addr) + oldAddr, _, err := address.ParseCIDR(addr) + if err != nil { + panic(err) + } wt.AssertSuccess(t, alloc.Free(res.name, oldAddr)) } @@ -406,8 +406,8 @@ func TestAllocatorFuzz(t *testing.T) { //common.Info.Printf("Asking for %s on allocator %d again", addr, res.alloc) newAddr, _ := alloc.Allocate(res.name, alloc.defaultSubnet, nil) - oldAddr, _ := address.ParseIP(addr) - if newAddr != oldAddr { + oldAddr, _, _ := address.ParseCIDR(addr) + if newAddr.Start != oldAddr { panic(fmt.Sprintf("Got different address for repeat request for %s: %s != %s", res.name, newAddr, oldAddr)) } diff --git a/ipam/http.go b/ipam/http.go index 35cdb437..13889cf1 100644 --- a/ipam/http.go +++ b/ipam/http.go @@ -46,7 +46,7 @@ func (alloc *Allocator) HandleHTTP(router *mux.Router) { http.NotFound(w, r) return } - fmt.Fprintf(w, "%s/%d", addr.String(), subnet.PrefixLen) + fmt.Fprint(w, addr) }) router.Methods("GET").Path("/ip/{id}").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -59,7 +59,7 @@ func (alloc *Allocator) HandleHTTP(router *mux.Router) { http.NotFound(w, r) return } - fmt.Fprintf(w, "%s/%d", addr.String(), alloc.defaultSubnet.PrefixLen) + fmt.Fprint(w, addr) }) router.Methods("POST").Path("/ip/{id}/{ip}/{prefixlen}").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -78,7 +78,7 @@ func (alloc *Allocator) HandleHTTP(router *mux.Router) { return } - fmt.Fprintf(w, "%s/%d", addr.String(), cidr.PrefixLen) + fmt.Fprint(w, addr) }) router.Methods("POST").Path("/ip/{id}").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -94,7 +94,7 @@ func (alloc *Allocator) HandleHTTP(router *mux.Router) { return } - fmt.Fprintf(w, "%s/%d", newAddr.String(), alloc.defaultSubnet.PrefixLen) + fmt.Fprint(w, newAddr) }) router.Methods("DELETE").Path("/ip/{id}/{ip}").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {