mirror of
https://github.com/kemko/nomad.git
synced 2026-01-06 10:25:42 +03:00
nomad: share port overcommit check
This commit is contained in:
@@ -154,8 +154,8 @@ func AllocationsFit(node *structs.Node, allocs []*structs.Allocation) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// portsOvercommited
|
||||
if portsOvercommited(resourcesUsed) {
|
||||
// Ensure ports are not over commited
|
||||
if structs.PortsOvercommited(resourcesUsed) {
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -205,17 +205,3 @@ func resourceSubset(super, sub *structs.Resources) bool {
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
@@ -21,3 +21,19 @@ func RemoveAllocs(alloc []*Allocation, remove []string) []*Allocation {
|
||||
alloc = alloc[:n]
|
||||
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
|
||||
}
|
||||
|
||||
@@ -18,3 +18,25 @@ func TestRemoveAllocs(t *testing.T) {
|
||||
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")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user