mirror of
https://github.com/kemko/nomad.git
synced 2026-01-04 17:35:43 +03:00
Skip DRIVER env vars for labels without a port mapping
This commit is contained in:
4
client/driver/env/env.go
vendored
4
client/driver/env/env.go
vendored
@@ -514,9 +514,9 @@ func buildPortEnv(envMap map[string]string, p structs.Port, ip string, driverNet
|
||||
envMap[HostAddrPrefix+p.Label] = net.JoinHostPort(ip, portStr)
|
||||
|
||||
// Driver IP, PORT, and ADDR if available
|
||||
if driverNet != nil {
|
||||
driverPortStr := strconv.Itoa(driverNet.PortMap[p.Label])
|
||||
if driverNet != nil && driverNet.PortMap[p.Label] != 0 {
|
||||
envMap[DriverIpPrefix+p.Label] = driverNet.IP
|
||||
driverPortStr := strconv.Itoa(driverNet.PortMap[p.Label])
|
||||
envMap[DriverPortPrefix+p.Label] = driverPortStr
|
||||
envMap[DriverAddrPrefix+p.Label] = net.JoinHostPort(driverNet.IP, driverPortStr)
|
||||
}
|
||||
|
||||
17
client/driver/env/env_test.go
vendored
17
client/driver/env/env_test.go
vendored
@@ -8,6 +8,7 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
cstructs "github.com/hashicorp/nomad/client/structs"
|
||||
"github.com/hashicorp/nomad/nomad/mock"
|
||||
"github.com/hashicorp/nomad/nomad/structs"
|
||||
)
|
||||
@@ -164,7 +165,9 @@ func TestEnvironment_AsList(t *testing.T) {
|
||||
DynamicPorts: []structs.Port{{Label: "https", Value: 8080}},
|
||||
},
|
||||
}
|
||||
env := NewBuilder(n, a, task, "global").SetPortMap(map[string]int{"https": 443})
|
||||
env := NewBuilder(n, a, task, "global").SetDriverNetwork(
|
||||
&cstructs.DriverNetwork{PortMap: map[string]int{"https": 443}},
|
||||
)
|
||||
|
||||
act := env.Build().List()
|
||||
exp := []string{
|
||||
@@ -184,6 +187,13 @@ func TestEnvironment_AsList(t *testing.T) {
|
||||
"NOMAD_IP_ssh_ssh=192.168.0.100",
|
||||
"NOMAD_PORT_ssh_other=1234",
|
||||
"NOMAD_PORT_ssh_ssh=22",
|
||||
"NOMAD_DRIVER_ADDR_https=:443",
|
||||
"NOMAD_DRIVER_IP_https=",
|
||||
"NOMAD_DRIVER_PORT_https=443",
|
||||
"NOMAD_HOST_ADDR_http=127.0.0.1:80",
|
||||
"NOMAD_HOST_ADDR_https=127.0.0.1:8080",
|
||||
"NOMAD_HOST_IP_http=127.0.0.1",
|
||||
"NOMAD_HOST_IP_https=127.0.0.1",
|
||||
"NOMAD_CPU_LIMIT=500",
|
||||
"NOMAD_DC=dc1",
|
||||
"NOMAD_REGION=global",
|
||||
@@ -204,7 +214,7 @@ func TestEnvironment_AsList(t *testing.T) {
|
||||
sort.Strings(act)
|
||||
sort.Strings(exp)
|
||||
if len(act) != len(exp) {
|
||||
t.Fatalf("expected %d vars != %d actual, actual: %s\n\nexpected: %s\n",
|
||||
t.Fatalf("expected %d vars != %d actual, actual:\n%s\n\nexpected:\n%s\n",
|
||||
len(act), len(exp), strings.Join(act, "\n"), strings.Join(exp, "\n"))
|
||||
}
|
||||
for i := range act {
|
||||
@@ -249,7 +259,8 @@ func TestEnvironment_Envvars(t *testing.T) {
|
||||
a := mock.Alloc()
|
||||
task := a.Job.TaskGroups[0].Tasks[0]
|
||||
task.Env = envMap
|
||||
act := NewBuilder(n, a, task, "global").SetPortMap(portMap).Build().All()
|
||||
net := &cstructs.DriverNetwork{PortMap: portMap}
|
||||
act := NewBuilder(n, a, task, "global").SetDriverNetwork(net).Build().All()
|
||||
for k, v := range envMap {
|
||||
actV, ok := act[k]
|
||||
if !ok {
|
||||
|
||||
@@ -3119,8 +3119,9 @@ func TestTaskDiff(t *testing.T) {
|
||||
New: &Task{
|
||||
Services: []*Service{
|
||||
{
|
||||
Name: "foo",
|
||||
PortLabel: "bar",
|
||||
Name: "foo",
|
||||
PortLabel: "bar",
|
||||
AddressMode: "driver",
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -3131,6 +3132,11 @@ func TestTaskDiff(t *testing.T) {
|
||||
Type: DiffTypeEdited,
|
||||
Name: "Service",
|
||||
Fields: []*FieldDiff{
|
||||
{
|
||||
Type: DiffTypeAdded,
|
||||
Name: "AddressMode",
|
||||
New: "driver",
|
||||
},
|
||||
{
|
||||
Type: DiffTypeNone,
|
||||
Name: "Name",
|
||||
@@ -3410,6 +3416,12 @@ func TestTaskDiff(t *testing.T) {
|
||||
Type: DiffTypeEdited,
|
||||
Name: "Service",
|
||||
Fields: []*FieldDiff{
|
||||
{
|
||||
Type: DiffTypeNone,
|
||||
Name: "AddressMode",
|
||||
Old: "",
|
||||
New: "",
|
||||
},
|
||||
{
|
||||
Type: DiffTypeNone,
|
||||
Name: "Name",
|
||||
|
||||
@@ -2540,7 +2540,7 @@ func (s *Service) Validate() error {
|
||||
}
|
||||
|
||||
switch s.AddressMode {
|
||||
case AddressModeAuto, AddressModeHost, AddressModeDriver:
|
||||
case "", AddressModeAuto, AddressModeHost, AddressModeDriver:
|
||||
// OK
|
||||
default:
|
||||
mErr.Errors = append(mErr.Errors, fmt.Errorf("service address_mode must be %q, %q, or %q; not %q", AddressModeAuto, AddressModeHost, AddressModeDriver, s.AddressMode))
|
||||
|
||||
Reference in New Issue
Block a user