Extend 'Lookup' to multi-subnet

This commit is contained in:
Bryan Boreham committed 2015-06-10 17:21:16 +01:00
1 parent d18ccac43b
commit 2a00b0092d
3 files changed
+28 -6

No files matched your search

+6 -5
View File
@@ -200,13 +200,14 @@ func (alloc *Allocator) Allocate(ident string, subnet address.CIDR, cancelChan <
return result.addr, result.err
}
func (alloc *Allocator) Lookup(ident string) (address.Address, error) {
func (alloc *Allocator) Lookup(ident string, subnet address.CIDR) (address.Address, error) {
resultChan := make(chan allocateResult)
alloc.actionChan <- func() {
addr, found := alloc.owned[ident]
if found {
resultChan <- allocateResult{addr: addr[0]}
return
for _, addr := range alloc.owned[ident] {
if subnet.Contains(addr) {
resultChan <- allocateResult{addr: addr}
return
}
}
resultChan <- allocateResult{err: fmt.Errorf("lookup: no address found")}
}
+17 -1
View File
@@ -33,12 +33,28 @@ func (alloc *Allocator) HandleHTTP(router *mux.Router) {
w.WriteHeader(204)
})
router.Methods("GET").Path("/ip/{id}/{ip}/{prefixlen}").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
cidr := vars["ip"] + "/" + vars["prefixlen"]
_, subnet, err := address.ParseCIDR(cidr)
if err != nil {
badRequest(w, err)
return
}
addr, err := alloc.Lookup(vars["id"], subnet)
if err != nil {
http.NotFound(w, r)
return
}
fmt.Fprintf(w, "%s/%d", addr.String(), subnet.PrefixLen)
})
router.Methods("GET").Path("/ip/{id}").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if alloc.defaultSubnet.Blank() {
badRequest(w, fmt.Errorf("No subnet specified"))
return
}
addr, err := alloc.Lookup(mux.Vars(r)["id"])
addr, err := alloc.Lookup(mux.Vars(r)["id"], alloc.defaultSubnet)
if err != nil {
http.NotFound(w, r)
return
+5
View File
@@ -89,6 +89,11 @@ func TestHttp(t *testing.T) {
cidr2a := HTTPPost(t, allocURL(port, testCIDR2, containerID))
wt.AssertEqualString(t, cidr2a, testAddr2, "address")
check := HTTPGet(t, allocURL(port, testCIDR1, containerID))
wt.AssertEqualString(t, check, cidr1a, "address")
check = HTTPGet(t, allocURL(port, testCIDR2, containerID))
wt.AssertEqualString(t, check, cidr2a, "address")
// Ask the http server for a pair of addresses for another container and check they're different
cidr1b := HTTPPost(t, allocURL(port, testCIDR1, container2))
wt.AssertFalse(t, cidr1b == testAddr1, "address")