nomad: share port overcommit check

This commit is contained in:
Armon Dadgar 2015-08-04 17:28:19 -07:00
parent 0c3bab297c
commit b140ba5f51
3 changed files with 40 additions and 16 deletions

View file

@ -154,8 +154,8 @@ func AllocationsFit(node *structs.Node, allocs []*structs.Allocation) bool {
return false return false
} }
// portsOvercommited // Ensure ports are not over commited
if portsOvercommited(resourcesUsed) { if structs.PortsOvercommited(resourcesUsed) {
return false return false
} }
@ -205,17 +205,3 @@ func resourceSubset(super, sub *structs.Resources) bool {
} }
return true return true
} }
// portsOvercommited checks if any of the port resources are over-committed
func portsOvercommited(r *structs.Resources) bool {
for _, net := range r.Networks {
ports := make(map[int]struct{})
for _, port := range net.ReservedPorts {
if _, ok := ports[port]; ok {
return true
}
ports[port] = struct{}{}
}
}
return false
}

View file

@ -21,3 +21,19 @@ func RemoveAllocs(alloc []*Allocation, remove []string) []*Allocation {
alloc = alloc[:n] alloc = alloc[:n]
return alloc return alloc
} }
// PortsOvercommited checks if any ports are over-committed.
// This does not handle CIDR subsets, and computes for the entire
// CIDR block currently.
func PortsOvercommited(r *Resources) bool {
for _, net := range r.Networks {
ports := make(map[int]struct{})
for _, port := range net.ReservedPorts {
if _, ok := ports[port]; ok {
return true
}
ports[port] = struct{}{}
}
}
return false
}

View file

@ -18,3 +18,25 @@ func TestRemoveAllocs(t *testing.T) {
t.Fatalf("bad: %#v", out) t.Fatalf("bad: %#v", out)
} }
} }
func TestPortsOvercommitted(t *testing.T) {
r := &Resources{
Networks: []*NetworkResource{
&NetworkResource{
ReservedPorts: []int{22, 80},
},
&NetworkResource{
ReservedPorts: []int{22, 80},
},
},
}
if PortsOvercommited(r) {
t.Fatalf("bad")
}
// Overcommit 22
r.Networks[1].ReservedPorts[1] = 22
if !PortsOvercommited(r) {
t.Fatalf("bad")
}
}