From 2a6ebd4aaa71ace365f292d554106790d38ec6c2 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 14 Sep 2015 18:27:37 -0700 Subject: [PATCH] jobspec: parse network resources --- jobspec/parse.go | 41 ++++++++++++++++++++++++++------- jobspec/parse_test.go | 7 ++++++ jobspec/test-fixtures/basic.hcl | 6 +++++ nomad/structs/structs.go | 8 +++++-- 4 files changed, 52 insertions(+), 10 deletions(-) diff --git a/jobspec/parse.go b/jobspec/parse.go index 1f96739aa..b6b779c74 100644 --- a/jobspec/parse.go +++ b/jobspec/parse.go @@ -305,14 +305,8 @@ func parseTasks(result *[]*structs.Task, obj *hclobj.Object) error { // If we have resources, then parse that if o := o.Get("resources", false); o != nil { var r structs.Resources - for _, o := range o.Elem(false) { - var m map[string]interface{} - if err := hcl.DecodeObject(&m, o); err != nil { - return err - } - if err := mapstructure.WeakDecode(m, &r); err != nil { - return err - } + if err := parseResources(&r, o); err != nil { + return err } t.Resources = &r @@ -323,3 +317,34 @@ func parseTasks(result *[]*structs.Task, obj *hclobj.Object) error { return nil } + +func parseResources(result *structs.Resources, obj *hclobj.Object) error { + for _, o := range obj.Elem(false) { + var m map[string]interface{} + if err := hcl.DecodeObject(&m, o); err != nil { + return err + } + delete(m, "network") + + if err := mapstructure.WeakDecode(m, result); err != nil { + return err + } + + // Parse the network resources + if o := o.Get("network", false); o != nil { + var r structs.NetworkResource + var m map[string]interface{} + if err := hcl.DecodeObject(&m, o); err != nil { + return err + } + if err := mapstructure.WeakDecode(m, &r); err != nil { + return err + } + + result.Networks = []*structs.NetworkResource{&r} + } + + } + + return nil +} diff --git a/jobspec/parse_test.go b/jobspec/parse_test.go index 2661d8914..802fc763c 100644 --- a/jobspec/parse_test.go +++ b/jobspec/parse_test.go @@ -79,6 +79,13 @@ func TestParse(t *testing.T) { Resources: &structs.Resources{ CPU: 500, MemoryMB: 128, + Networks: []*structs.NetworkResource{ + &structs.NetworkResource{ + MBits: 100, + ReservedPorts: []int{1, 2, 3}, + DynamicPorts: 3, + }, + }, }, }, &structs.Task{ diff --git a/jobspec/test-fixtures/basic.hcl b/jobspec/test-fixtures/basic.hcl index b098c52d1..bc107ecc4 100644 --- a/jobspec/test-fixtures/basic.hcl +++ b/jobspec/test-fixtures/basic.hcl @@ -34,6 +34,12 @@ job "binstore-storagelocker" { resources { cpu = 500 memory = 128 + + network { + mbits = "100" + reserved_ports = [1,2,3] + dynamic_ports = 3 + } } } diff --git a/nomad/structs/structs.go b/nomad/structs/structs.go index bd2405c7d..c08973c12 100644 --- a/nomad/structs/structs.go +++ b/nomad/structs/structs.go @@ -612,9 +612,9 @@ type NetworkResource struct { Device string // Name of the device CIDR string // CIDR block of addresses IP string // IP address - ReservedPorts []int // Reserved ports MBits int // Throughput - DynamicPorts int // Dynamically assigned ports + ReservedPorts []int `mapstructure:"reserved_ports"` // Reserved ports + DynamicPorts int `mapstructure:"dynamic_ports"` // Dynamically assigned ports } // Copy returns a deep copy of the network resource @@ -638,6 +638,10 @@ func (n *NetworkResource) Add(delta *NetworkResource) { n.DynamicPorts += delta.DynamicPorts } +func (n *NetworkResource) GoString() string { + return fmt.Sprintf("*%#v", *n) +} + const ( // JobTypeNomad is reserved for internal system tasks and is // always handled by the CoreScheduler.