From f23e0db7806c6b542d34a63491c5ca1cb562b4e1 Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Tue, 4 Aug 2015 17:32:57 -0700 Subject: [PATCH] nomad: share and test superset --- nomad/plan_apply.go | 25 ---------------------- nomad/structs/structs.go | 33 +++++++++++++++++++++++++++++ nomad/structs/structs_test.go | 40 +++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 25 deletions(-) diff --git a/nomad/plan_apply.go b/nomad/plan_apply.go index 87041841f..c834af44b 100644 --- a/nomad/plan_apply.go +++ b/nomad/plan_apply.go @@ -180,28 +180,3 @@ func addResources(base, delta *structs.Resources) { } } } - -// resourceSubset checks if a resource utilization is a subset of another set -func resourceSubset(super, sub *structs.Resources) bool { - if super.CPU < sub.CPU { - return false - } - if super.MemoryMB < sub.MemoryMB { - return false - } - if super.DiskMB < sub.DiskMB { - return false - } - if super.IOPS < sub.IOPS { - return false - } - for _, net := range super.Networks { - idx := sub.NetIndexByCIDR(net.CIDR) - if idx >= 0 { - if net.MBits < sub.Networks[idx].MBits { - return false - } - } - } - return true -} diff --git a/nomad/structs/structs.go b/nomad/structs/structs.go index 35e21d8e7..43d4f6443 100644 --- a/nomad/structs/structs.go +++ b/nomad/structs/structs.go @@ -319,6 +319,39 @@ func (r *Resources) NetIndexByCIDR(cidr string) int { return -1 } +// Superset checks if one set of resources is a superset +// of another. +func (r *Resources) Superset(other *Resources) bool { + if r.CPU < other.CPU { + return false + } + if r.MemoryMB < other.MemoryMB { + return false + } + if r.DiskMB < other.DiskMB { + return false + } + if r.IOPS < other.IOPS { + return false + } + for _, net := range r.Networks { + idx := other.NetIndexByCIDR(net.CIDR) + if idx >= 0 { + if net.MBits < other.Networks[idx].MBits { + return false + } + } + } + // Check that other does not have a network we are missing + for _, net := range other.Networks { + idx := r.NetIndexByCIDR(net.CIDR) + if idx == -1 { + return false + } + } + return true +} + // NetworkResource is used to represesent available network // resources type NetworkResource struct { diff --git a/nomad/structs/structs_test.go b/nomad/structs/structs_test.go index 5d8a727b6..34b8ad00d 100644 --- a/nomad/structs/structs_test.go +++ b/nomad/structs/structs_test.go @@ -23,6 +23,46 @@ func TestResource_NetIndexByCIDR(t *testing.T) { } } +func TestResource_Superset(t *testing.T) { + r1 := &Resources{ + CPU: 2.0, + MemoryMB: 2048, + DiskMB: 10000, + IOPS: 100, + Networks: []*NetworkResource{ + &NetworkResource{ + CIDR: "10.0.0.0/8", + MBits: 100, + }, + }, + } + r2 := &Resources{ + CPU: 1.0, + MemoryMB: 1024, + DiskMB: 5000, + IOPS: 50, + Networks: []*NetworkResource{ + &NetworkResource{ + CIDR: "10.0.0.0/8", + MBits: 50, + }, + }, + } + + if !r1.Superset(r1) { + t.Fatalf("bad") + } + if !r1.Superset(r2) { + t.Fatalf("bad") + } + if r2.Superset(r1) { + t.Fatalf("bad") + } + if !r2.Superset(r2) { + t.Fatalf("bad") + } +} + func TestEncodeDecode(t *testing.T) { type FooRequest struct { Foo string