Rationalize ipam arguments

- rename -defaultsubnet to -ipsubnet
- default subnet defaults to whole -iprange
- parse and check inside ipam package
- removes some error cases
This commit is contained in:
Bryan Boreham committed 2015-06-10 17:21:16 +01:00
1 parent f956a76455
commit 3a9af4413d
6 files changed
+67 -77

No files matched your search

+1 -3
View File
@@ -40,15 +40,13 @@ func ParseCIDR(s string) (Address, CIDR, error) {
func (cidr CIDR) Size() Offset { return 1 << uint(32-cidr.PrefixLen) }
func (cidr CIDR) End() Address { return Add(cidr.Start, cidr.Size()) }
func (cidr CIDR) Blank() bool { return cidr.Start == 0 && cidr.PrefixLen == 0 }
func (cidr CIDR) String() string {
return fmt.Sprintf("%s/%d", cidr.Start.String(), cidr.PrefixLen)
}
func (cidr1 CIDR) Overlaps(cidr2 CIDR) bool {
return cidr1.Start >= cidr2.Start && cidr1.Start < cidr2.End() ||
cidr2.Start >= cidr1.Start && cidr2.Start < cidr1.End()
return !(cidr1.Start >= cidr2.End() || cidr1.End() <= cidr2.Start)
}
func (cidr CIDR) Contains(addr Address) bool {
+38 -23
View File
@@ -21,6 +21,7 @@ const (
msgRingUpdate
paxosInterval = time.Second * 5
minSubnetSize = 4 // first and last addresses are excluded, so 2 would be too small
)
// operation represents something which Allocator wants to do, but
@@ -60,16 +61,21 @@ type Allocator struct {
}
// NewAllocator creates and initialises a new Allocator
func NewAllocator(ourName router.PeerName, ourUID router.PeerUID, ourNickname string, universe address.CIDR, quorum uint) *Allocator {
return &Allocator{
ourName: ourName,
universe: universe,
ring: ring.New(universe.Start, address.Add(universe.Start, universe.Size()), ourName),
owned: make(map[string][]address.Address),
paxos: paxos.NewNode(ourName, ourUID, quorum),
nicknames: map[router.PeerName]string{ourName: ourNickname},
now: time.Now,
func NewAllocator(ourName router.PeerName, ourUID router.PeerUID, ourNickname string, universeStr string, quorum uint) (*Allocator, error) {
universe, err := parseAndCheckCIDR(universeStr)
if err != nil {
return nil, err
}
return &Allocator{
ourName: ourName,
universe: universe,
defaultSubnet: universe,
ring: ring.New(universe.Start, address.Add(universe.Start, universe.Size()), ourName),
owned: make(map[string][]address.Address),
paxos: paxos.NewNode(ourName, ourUID, quorum),
nicknames: map[router.PeerName]string{ourName: ourNickname},
now: time.Now,
}, nil
}
// Start runs the allocator goroutine
@@ -176,11 +182,12 @@ func hasBeenCancelled(cancelChan <-chan bool) func() bool {
// Actor client API
func (alloc *Allocator) SetDefaultSubnet(subnet address.CIDR) error {
resultChan := make(chan error)
if subnet.Size() < 4 {
return fmt.Errorf("Allocation range smaller than minimum size 4: %s", subnet)
func (alloc *Allocator) SetDefaultSubnet(subnetStr string) error {
subnet, err := parseAndCheckCIDR(subnetStr)
if err != nil {
return err
}
resultChan := make(chan error)
alloc.actionChan <- func() {
if !alloc.universe.Overlaps(subnet) {
resultChan <- fmt.Errorf("Default subnet %s out of bounds: %s", subnet, alloc.universe)
@@ -227,6 +234,12 @@ func (alloc *Allocator) Claim(ident string, addr address.Address, cancelChan <-c
return <-resultChan
}
// ContainerDied is provided to satisfy the updater interface; does a 'Delete' underneath. Sync.
func (alloc *Allocator) ContainerDied(ident string) error {
alloc.debugln("Container", ident, "died; releasing addresses")
return alloc.Delete(ident)
}
// Delete (Sync) - release all IP addresses for container with given name
func (alloc *Allocator) Delete(ident string) error {
errChan := make(chan error)
@@ -271,12 +284,6 @@ func (alloc *Allocator) Free(ident string, addrToFree address.Address) error {
return <-errChan
}
// ContainerDied is provided to satisfy the updater interface; does a 'Delete' underneath. Sync.
func (alloc *Allocator) ContainerDied(ident string) error {
alloc.debugln("Container", ident, "died; releasing addresses")
return alloc.Delete(ident)
}
// Sync.
func (alloc *Allocator) String() string {
resultChan := make(chan string)
@@ -487,12 +494,20 @@ func (alloc *Allocator) actorLoop(actionChan <-chan func()) {
// Helper functions
func parseAndCheckCIDR(cidrStr string) (address.CIDR, error) {
_, cidr, err := address.ParseCIDR(cidrStr)
if err != nil {
return address.CIDR{}, err
}
if cidr.Size() < minSubnetSize {
return address.CIDR{}, fmt.Errorf("Allocation range smaller than minimum size %d: %s", minSubnetSize, cidrStr)
}
return cidr, nil
}
func (alloc *Allocator) string() string {
var buf bytes.Buffer
fmt.Fprintf(&buf, "Allocator universe %s", alloc.universe)
if !alloc.defaultSubnet.Blank() {
fmt.Fprintf(&buf, " (default subnet %s)", alloc.defaultSubnet)
}
fmt.Fprintf(&buf, "Allocator range %s (default subnet %s)", alloc.universe, alloc.defaultSubnet)
if alloc.ring.Empty() {
if alloc.paxosTicker != nil {
-8
View File
@@ -50,10 +50,6 @@ func (alloc *Allocator) HandleHTTP(router *mux.Router) {
})
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"], alloc.defaultSubnet)
if err != nil {
http.NotFound(w, r)
@@ -86,10 +82,6 @@ func (alloc *Allocator) HandleHTTP(router *mux.Router) {
})
router.Methods("POST").Path("/ip/{id}").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if alloc.defaultSubnet.Blank() {
badRequest(w, fmt.Errorf("No subnet specified"))
return
}
closedChan := w.(http.CloseNotifier).CloseNotify()
ident := mux.Vars(r)["id"]
newAddr, err := alloc.Allocate(ident, alloc.defaultSubnet, closedChan)
+2 -4
View File
@@ -134,13 +134,11 @@ func makeAllocator(name string, cidrStr string, quorum uint) *Allocator {
panic(err)
}
_, cidr, err := address.ParseCIDR(cidrStr)
alloc, err := NewAllocator(peername, router.PeerUID(rand.Int63()),
"nick-"+name, cidrStr, quorum)
if err != nil {
panic(err)
}
alloc := NewAllocator(peername, router.PeerUID(rand.Int63()),
"nick-"+name, cidr, quorum)
alloc.defaultSubnet = cidr
return alloc
}