Merge pull request #83 from hashicorp/f-named-ports

Added named ports
This commit is contained in:
Armon Dadgar
2015-09-22 14:41:21 -07:00
11 changed files with 31 additions and 26 deletions

View File

@@ -18,7 +18,7 @@ var (
Networks: []*structs.NetworkResource{
&structs.NetworkResource{
MBits: 50,
DynamicPorts: 1,
DynamicPorts: []string{"http"},
},
},
}

View File

@@ -92,7 +92,7 @@ func TestParse(t *testing.T) {
&structs.NetworkResource{
MBits: 100,
ReservedPorts: []int{1, 2, 3},
DynamicPorts: 3,
DynamicPorts: []string{"http", "https", "admin"},
},
},
},

View File

@@ -43,7 +43,7 @@ job "binstore-storagelocker" {
network {
mbits = "100"
reserved_ports = [1,2,3]
dynamic_ports = 3
dynamic_ports = ["http", "https", "admin"]
}
}
}

View File

@@ -11,13 +11,13 @@ job "binstore-storagelocker" {
network {
mbits = "100"
reserved_ports = [1,2,3]
dynamic_ports = 3
dynamic_ports = ["http", "https", "admin"]
}
network {
mbits = "128"
reserved_ports = [1,2,3]
dynamic_ports = 3
dynamic_ports = ["http", "https", "admin"]
}
}
}

View File

@@ -86,7 +86,7 @@ func Job() *structs.Job {
Networks: []*structs.NetworkResource{
&structs.NetworkResource{
MBits: 50,
DynamicPorts: 1,
DynamicPorts: []string{"http"},
},
},
},
@@ -135,7 +135,7 @@ func Alloc() *structs.Allocation {
IP: "192.168.0.100",
ReservedPorts: []int{12345},
MBits: 100,
DynamicPorts: 1,
DynamicPorts: []string{"http"},
},
},
},
@@ -149,7 +149,7 @@ func Alloc() *structs.Allocation {
IP: "192.168.0.100",
ReservedPorts: []int{5000},
MBits: 50,
DynamicPorts: 1,
DynamicPorts: []string{"http"},
},
},
},

View File

@@ -164,7 +164,7 @@ func (idx *NetworkIndex) AssignNetwork(ask *NetworkResource) (out *NetworkResour
}
// Check if we need to generate any ports
for i := 0; i < ask.DynamicPorts; i++ {
for i := 0; i < len(ask.DynamicPorts); i++ {
attempts := 0
PICK:
attempts++

View File

@@ -287,7 +287,7 @@ func TestNetworkIndex_AssignNetwork(t *testing.T) {
// Ask for dynamic ports
ask = &NetworkResource{
DynamicPorts: 3,
DynamicPorts: []string{"http", "https", "admin"},
}
offer, err = idx.AssignNetwork(ask)
if err != nil {
@@ -306,7 +306,7 @@ func TestNetworkIndex_AssignNetwork(t *testing.T) {
// Ask for reserved + dynamic ports
ask = &NetworkResource{
ReservedPorts: []int{12345},
DynamicPorts: 3,
DynamicPorts: []string{"http", "https", "admin"},
}
offer, err = idx.AssignNetwork(ask)
if err != nil {

View File

@@ -612,12 +612,12 @@ func (r *Resources) GoString() string {
// NetworkResource is used to represesent available network
// resources
type NetworkResource struct {
Device string // Name of the device
CIDR string // CIDR block of addresses
IP string // IP address
MBits int // Throughput
ReservedPorts []int `mapstructure:"reserved_ports"` // Reserved ports
DynamicPorts int `mapstructure:"dynamic_ports"` // Dynamically assigned ports
Device string // Name of the device
CIDR string // CIDR block of addresses
IP string // IP address
MBits int // Throughput
ReservedPorts []int `mapstructure:"reserved_ports"` // Reserved ports
DynamicPorts []string `mapstructure:"dynamic_ports"` // Dynamically assigned ports
}
// Copy returns a deep copy of the network resource
@@ -638,7 +638,7 @@ func (n *NetworkResource) Add(delta *NetworkResource) {
n.ReservedPorts = append(n.ReservedPorts, delta.ReservedPorts...)
}
n.MBits += delta.MBits
n.DynamicPorts += delta.DynamicPorts
n.DynamicPorts = append(n.DynamicPorts, delta.DynamicPorts...)
}
func (n *NetworkResource) GoString() string {

View File

@@ -230,7 +230,7 @@ func TestResource_Add_Network(t *testing.T) {
Networks: []*NetworkResource{
&NetworkResource{
MBits: 50,
DynamicPorts: 2,
DynamicPorts: []string{"http", "https"},
},
},
}
@@ -238,7 +238,7 @@ func TestResource_Add_Network(t *testing.T) {
Networks: []*NetworkResource{
&NetworkResource{
MBits: 25,
DynamicPorts: 1,
DynamicPorts: []string{"admin"},
},
},
}
@@ -256,7 +256,7 @@ func TestResource_Add_Network(t *testing.T) {
Networks: []*NetworkResource{
&NetworkResource{
MBits: 75,
DynamicPorts: 3,
DynamicPorts: []string{"http", "https", "admin"},
},
},
}

View File

@@ -261,7 +261,7 @@ func TestTasksUpdated(t *testing.T) {
}
j6 := mock.Job()
j6.TaskGroups[0].Tasks[0].Resources.Networks[0].DynamicPorts = 3
j6.TaskGroups[0].Tasks[0].Resources.Networks[0].DynamicPorts = []string{"http", "https", "admin"}
if !tasksUpdated(j1.TaskGroups[0], j6.TaskGroups[0]) {
t.Fatalf("bad")
}

View File

@@ -48,7 +48,10 @@ job "my-service" {
memory = 128
network {
mbits = 100
dynamic_ports = 1
dynamic_ports = [
"http",
"https",
]
}
}
}
@@ -182,9 +185,11 @@ The `resources` object supports the following keys:
The `network` object supports teh following keys:
* `dynamic_ports` - The number of dynamic ports. These are
ports that are assigned at task scheduling type, and require
the task to be able to bind dynamically. Defaults to 0.
* `dynamic_ports` - List of strings (`^[a-z0-9_]+$`), called labels. Each label
will be associated with a dynamic port when the task starts. Port allocation
will be passed to the task as an environment variable like `NOMAD_PORT_{{ .Label }}`
(upper-cased). Some Drivers may infer additional semantics from the label. See
[Docker](/docs/drivers/docker.html) for an example.
* `mbits` - The number of MBits in bandwidth required.