Merge pull request #1350 from weaveworks/ipam-json
Add GET /ip handler to return all IPAM mappings.
This commit is contained in:
commit
d0e0df9bd4
1 file changed
+34
@@ -1,6 +1,7 @@
|
|||||||
package ipam
|
package ipam
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
@@ -127,6 +128,39 @@ func (alloc *Allocator) HandleHTTP(router *mux.Router, defaultSubnet address.CID
|
|||||||
writeAddresses(w, addrs)
|
writeAddresses(w, addrs)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
router.Methods("GET").Path("/ip").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
type mapping struct {
|
||||||
|
ContainerID string `json:"containerid"`
|
||||||
|
Addrs []string `json:"addrs"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type mappings struct {
|
||||||
|
Owned []mapping `json:"owned"`
|
||||||
|
}
|
||||||
|
|
||||||
|
resultChan := make(chan mappings)
|
||||||
|
alloc.actionChan <- func() {
|
||||||
|
ms := mappings{}
|
||||||
|
for containerid, d := range alloc.owned {
|
||||||
|
m := mapping{
|
||||||
|
ContainerID: containerid,
|
||||||
|
Addrs: []string{},
|
||||||
|
}
|
||||||
|
for _, addr := range d.Cidrs {
|
||||||
|
m.Addrs = append(m.Addrs, addr.String())
|
||||||
|
}
|
||||||
|
ms.Owned = append(ms.Owned, m)
|
||||||
|
}
|
||||||
|
resultChan <- ms
|
||||||
|
}
|
||||||
|
ms := <-resultChan
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
if err := json.NewEncoder(w).Encode(ms); err != nil {
|
||||||
|
common.Log.Warningln("[allocator]:", err.Error())
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
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) {
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
if subnet, ok := parseCIDR(w, vars["ip"]+"/"+vars["prefixlen"], true); ok {
|
if subnet, ok := parseCIDR(w, vars["ip"]+"/"+vars["prefixlen"], true); ok {
|
||||||
|
|||||||
Reference in new issue
Block a user