Allocate() and Lookup() now return a CIDR, to simplify output
This commit is contained in:
1 parent
2a00b0092d
commit
50eaccc60c
3 files changed
+19
-19
No files matched your search
+4
-4
@@ -191,16 +191,16 @@ func (alloc *Allocator) SetDefaultSubnet(subnet address.CIDR) error {
|
|||||||
|
|
||||||
// Allocate (Sync) - get IP address for container with given name
|
// Allocate (Sync) - get IP address for container with given name
|
||||||
// if there isn't any space we block indefinitely
|
// 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)
|
resultChan := make(chan allocateResult)
|
||||||
op := &allocate{subnet: subnet, resultChan: resultChan, ident: ident,
|
op := &allocate{subnet: subnet, resultChan: resultChan, ident: ident,
|
||||||
hasBeenCancelled: hasBeenCancelled(cancelChan)}
|
hasBeenCancelled: hasBeenCancelled(cancelChan)}
|
||||||
alloc.doOperation(op, &alloc.pendingAllocates)
|
alloc.doOperation(op, &alloc.pendingAllocates)
|
||||||
result := <-resultChan
|
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)
|
resultChan := make(chan allocateResult)
|
||||||
alloc.actionChan <- func() {
|
alloc.actionChan <- func() {
|
||||||
for _, addr := range alloc.owned[ident] {
|
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")}
|
resultChan <- allocateResult{err: fmt.Errorf("lookup: no address found")}
|
||||||
}
|
}
|
||||||
result := <-resultChan
|
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)
|
// Claim an address that we think we should own (Sync)
|
||||||
|
|||||||
+11
-11
@@ -27,8 +27,8 @@ func TestAllocFree(t *testing.T) {
|
|||||||
universe = "10.0.3.0/26"
|
universe = "10.0.3.0/26"
|
||||||
subnet1 = "10.0.3.0/28"
|
subnet1 = "10.0.3.0/28"
|
||||||
subnet2 = "10.0.3.32/28"
|
subnet2 = "10.0.3.32/28"
|
||||||
testAddr1 = "10.0.3.1"
|
testAddr1 = "10.0.3.1/28"
|
||||||
testAddr2 = "10.0.3.33"
|
testAddr2 = "10.0.3.33/28"
|
||||||
spaceSize = 62 // 64 IP addresses in /26, minus .0 and .63
|
spaceSize = 62 // 64 IP addresses in /26, minus .0 and .63
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -134,7 +134,7 @@ func TestAllocatorClaim(t *testing.T) {
|
|||||||
container2 = "baddf00d"
|
container2 = "baddf00d"
|
||||||
container3 = "b01df00d"
|
container3 = "b01df00d"
|
||||||
universe = "10.0.3.0/30"
|
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)
|
alloc := makeAllocatorWithMockGossip(t, "01:00:00:01:00:00", universe, 1)
|
||||||
@@ -224,15 +224,13 @@ func TestGossipShutdown(t *testing.T) {
|
|||||||
container1 = "abcdef"
|
container1 = "abcdef"
|
||||||
container2 = "baddf00d"
|
container2 = "baddf00d"
|
||||||
universe = "10.0.3.0/30"
|
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)
|
alloc := makeAllocatorWithMockGossip(t, "01:00:00:01:00:00", universe, 1)
|
||||||
defer alloc.Stop()
|
defer alloc.Stop()
|
||||||
|
|
||||||
alloc.claimRingForTesting()
|
alloc.claimRingForTesting()
|
||||||
addr1, _ := alloc.Allocate(container1, alloc.defaultSubnet, nil)
|
alloc.Allocate(container1, alloc.defaultSubnet, nil)
|
||||||
wt.AssertEqualString(t, addr1.String(), testAddr1, "address")
|
|
||||||
|
|
||||||
alloc.Shutdown()
|
alloc.Shutdown()
|
||||||
|
|
||||||
@@ -284,8 +282,7 @@ func TestFakeRouterSimple(t *testing.T) {
|
|||||||
alloc1 := allocs[0]
|
alloc1 := allocs[0]
|
||||||
//alloc2 := allocs[1]
|
//alloc2 := allocs[1]
|
||||||
|
|
||||||
addr, _ := alloc1.Allocate("foo", alloc1.defaultSubnet, nil)
|
alloc1.Allocate("foo", alloc1.defaultSubnet, nil)
|
||||||
println("Got addr", addr)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAllocatorFuzz(t *testing.T) {
|
func TestAllocatorFuzz(t *testing.T) {
|
||||||
@@ -387,7 +384,10 @@ func TestAllocatorFuzz(t *testing.T) {
|
|||||||
alloc := allocs[res.alloc]
|
alloc := allocs[res.alloc]
|
||||||
//common.Info.Printf("Freeing %s (%s) on allocator %d", res.name, addr, 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))
|
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)
|
//common.Info.Printf("Asking for %s on allocator %d again", addr, res.alloc)
|
||||||
|
|
||||||
newAddr, _ := alloc.Allocate(res.name, alloc.defaultSubnet, nil)
|
newAddr, _ := alloc.Allocate(res.name, alloc.defaultSubnet, nil)
|
||||||
oldAddr, _ := address.ParseIP(addr)
|
oldAddr, _, _ := address.ParseCIDR(addr)
|
||||||
if newAddr != oldAddr {
|
if newAddr.Start != oldAddr {
|
||||||
panic(fmt.Sprintf("Got different address for repeat request for %s: %s != %s", res.name, newAddr, oldAddr))
|
panic(fmt.Sprintf("Got different address for repeat request for %s: %s != %s", res.name, newAddr, oldAddr))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+4
-4
@@ -46,7 +46,7 @@ func (alloc *Allocator) HandleHTTP(router *mux.Router) {
|
|||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
return
|
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) {
|
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)
|
http.NotFound(w, r)
|
||||||
return
|
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) {
|
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
|
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) {
|
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
|
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) {
|
router.Methods("DELETE").Path("/ip/{id}/{ip}").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|||||||
Reference in new issue
Block a user