Skip to content

Commit

Permalink
fix qemu and update docker with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mahmood Ali committed Sep 4, 2019
1 parent 43a177c commit dff556a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
9 changes: 7 additions & 2 deletions client/taskenv/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -730,9 +730,14 @@ func buildPortEnv(envMap map[string]string, p structs.Port, ip string, driverNet
}
}

func WithPortMapEnvs(envs map[string]string, ports map[string]int) map[string]string {
// SetPortMapEnvs sets the PortMap related environment variables on the map
func SetPortMapEnvs(envs map[string]string, ports map[string]int) map[string]string {
if envs == nil {
envs = map[string]string{}
}

for portLabel, port := range ports {
portEnv := PortPrefix + portLabel
portEnv := helper.CleanEnvVar(PortPrefix+portLabel, '_')
envs[portEnv] = strconv.Itoa(port)
}
return envs
Expand Down
4 changes: 3 additions & 1 deletion drivers/docker/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,9 @@ func (d *Driver) containerBinds(task *drivers.TaskConfig, driverConfig *TaskConf
func (d *Driver) createContainerConfig(task *drivers.TaskConfig, driverConfig *TaskConfig,
imageID string) (docker.CreateContainerOptions, error) {

// ensure that PortMap variables are populated early on
task.Env = taskenv.SetPortMapEnvs(task.Env, driverConfig.PortMap)

logger := d.logger.With("task_name", task.Name)
var c docker.CreateContainerOptions
if task.Resources == nil {
Expand Down Expand Up @@ -967,7 +970,6 @@ func (d *Driver) createContainerConfig(task *drivers.TaskConfig, driverConfig *T
logger.Debug("applied labels on the container", "labels", config.Labels)
}

task.Env = taskenv.WithPortMapEnvs(task.Env, driverConfig.PortMap)
config.Env = task.EnvList()

containerName := fmt.Sprintf("%s-%s", strings.Replace(task.Name, "/", "_", -1), task.AllocID)
Expand Down
32 changes: 32 additions & 0 deletions drivers/docker/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1437,6 +1437,38 @@ func TestDockerDriver_PortsMapping(t *testing.T) {
require.Exactly(t, expectedPortBindings, container.HostConfig.PortBindings)
}

func TestDockerDriver_CreateContainerConfig_PortsMapping(t *testing.T) {
t.Parallel()

task, cfg, port := dockerTask(t)
res := port[0]
dyn := port[1]
cfg.PortMap = map[string]int{
"main": 8080,
"REDIS": 6379,
}
dh := dockerDriverHarness(t, nil)
driver := dh.Impl().(*Driver)

c, err := driver.createContainerConfig(task, cfg, "org/repo:0.1")
require.NoError(t, err)

require.Equal(t, "org/repo:0.1", c.Config.Image)
require.Contains(t, c.Config.Env, "NOMAD_PORT_main=8080")
require.Contains(t, c.Config.Env, "NOMAD_PORT_REDIS=6379")

// Verify that the correct ports are FORWARDED
hostIP := "127.0.0.1"
expectedPortBindings := map[docker.Port][]docker.PortBinding{
docker.Port("8080/tcp"): {{HostIP: hostIP, HostPort: fmt.Sprintf("%d", res)}},
docker.Port("8080/udp"): {{HostIP: hostIP, HostPort: fmt.Sprintf("%d", res)}},
docker.Port("6379/tcp"): {{HostIP: hostIP, HostPort: fmt.Sprintf("%d", dyn)}},
docker.Port("6379/udp"): {{HostIP: hostIP, HostPort: fmt.Sprintf("%d", dyn)}},
}
require.Exactly(t, expectedPortBindings, c.HostConfig.PortBindings)

}

func TestDockerDriver_CleanupContainer(t *testing.T) {
if !tu.IsCI() {
t.Parallel()
Expand Down
4 changes: 4 additions & 0 deletions drivers/qemu/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/coreos/go-semver/semver"
hclog "github.com/hashicorp/go-hclog"
"github.com/hashicorp/nomad/client/taskenv"
"github.com/hashicorp/nomad/drivers/shared/eventer"
"github.com/hashicorp/nomad/drivers/shared/executor"
"github.com/hashicorp/nomad/helper/pluginutils/hclutils"
Expand Down Expand Up @@ -304,6 +305,9 @@ func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *drive
return nil, nil, fmt.Errorf("failed to decode driver config: %v", err)
}

// ensure that PortMap variables are populated early on
cfg.Env = taskenv.SetPortMapEnvs(cfg.Env, driverConfig.PortMap)

handle := drivers.NewTaskHandle(taskHandleVersion)
handle.Config = cfg

Expand Down

0 comments on commit dff556a

Please sign in to comment.