From 5b390a75f1001897bcd64f831945ed832e8614bb Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Tue, 4 Aug 2015 17:19:05 -0700 Subject: [PATCH] nomad: move and test function --- nomad/plan_apply.go | 24 +----------------------- nomad/structs/funcs.go | 23 +++++++++++++++++++++++ nomad/structs/funcs_test.go | 20 ++++++++++++++++++++ 3 files changed, 44 insertions(+), 23 deletions(-) create mode 100644 nomad/structs/funcs.go create mode 100644 nomad/structs/funcs_test.go diff --git a/nomad/plan_apply.go b/nomad/plan_apply.go index 7f0acdef8..2e1c6bd60 100644 --- a/nomad/plan_apply.go +++ b/nomad/plan_apply.go @@ -79,7 +79,7 @@ func (s *Server) evaluatePlan(plan *structs.Plan) (*structs.PlanResult, error) { proposed := existingAlloc evictions := plan.NodeEvict[nodeID] if len(evictions) > 0 { - proposed = trimAllocations(existingAlloc, evictions) + proposed = structs.RemoveAllocs(existingAlloc, evictions) } proposed = append(proposed, allocList...) @@ -230,25 +230,3 @@ func networkIndexByCidr(list []*structs.NetworkResource, cidr string) int { } return -1 } - -// trimAllocations is used to remove any allocaitons with the given ID -// from the list of allocations -func trimAllocations(alloc []*structs.Allocation, remove []string) []*structs.Allocation { - // Convert remove into a set - removeSet := make(map[string]struct{}) - for _, removeID := range remove { - removeSet[removeID] = struct{}{} - } - - n := len(alloc) - for i := 0; i < n; i++ { - if _, ok := removeSet[alloc[i].ID]; ok { - alloc[i], alloc[n-1] = alloc[n-1], nil - i-- - n-- - } - } - - alloc = alloc[:n] - return alloc -} diff --git a/nomad/structs/funcs.go b/nomad/structs/funcs.go new file mode 100644 index 000000000..cec99b3a5 --- /dev/null +++ b/nomad/structs/funcs.go @@ -0,0 +1,23 @@ +package structs + +// RemoveAllocs is used to remove any allocs with the given IDs +// from the list of allocations +func RemoveAllocs(alloc []*Allocation, remove []string) []*Allocation { + // Convert remove into a set + removeSet := make(map[string]struct{}) + for _, removeID := range remove { + removeSet[removeID] = struct{}{} + } + + n := len(alloc) + for i := 0; i < n; i++ { + if _, ok := removeSet[alloc[i].ID]; ok { + alloc[i], alloc[n-1] = alloc[n-1], nil + i-- + n-- + } + } + + alloc = alloc[:n] + return alloc +} diff --git a/nomad/structs/funcs_test.go b/nomad/structs/funcs_test.go new file mode 100644 index 000000000..c400c811b --- /dev/null +++ b/nomad/structs/funcs_test.go @@ -0,0 +1,20 @@ +package structs + +import "testing" + +func TestRemoveAllocs(t *testing.T) { + l := []*Allocation{ + &Allocation{ID: "foo"}, + &Allocation{ID: "bar"}, + &Allocation{ID: "baz"}, + &Allocation{ID: "zip"}, + } + + out := RemoveAllocs(l, []string{"bar", "zip"}) + if len(out) != 2 { + t.Fatalf("bad: %#v", out) + } + if out[0].ID != "foo" && out[1].ID != "baz" { + t.Fatalf("bad: %#v", out) + } +}