Permalink
Browse files

api: Added Platform Overrides for Task API

Adds functionality to override HostConfig values in order to pass proper default parameter values, based on platform, to Docker's API. This addresses the MemorySwappiness value which is being set to 0 by default in go-dockerclient.
  • Loading branch information...
cabbruzzese committed Aug 10, 2017
1 parent 8083c13 commit 88a2e109395dc4dac8d81d623c16061a4ebc1afe
Showing with 86 additions and 0 deletions.
  1. +2 −0 agent/api/task.go
  2. +2 −0 agent/api/task_test.go
  3. +7 −0 agent/api/task_unix.go
  4. +14 −0 agent/api/task_windows.go
  5. +61 −0 agent/api/task_windows_test.go
@@ -423,6 +423,8 @@ func (task *Task) dockerHostConfig(container *Container, dockerContainerMap map[
}
}
task.platformHostConfigOverride(hostConfig)
return hostConfig, nil
}
@@ -202,6 +202,7 @@ func TestDockerHostConfigRawConfig(t *testing.T) {
Config: map[string]string{"foo": "bar"},
},
Ulimits: []docker.ULimit{{Name: "ulimit name", Soft: 10, Hard: 100}},
MemorySwappiness: memorySwappinessDefault,
}
rawHostConfig, err := json.Marshal(&rawHostConfigInput)
@@ -279,6 +280,7 @@ func TestDockerHostConfigRawConfigMerging(t *testing.T) {
Privileged: true,
SecurityOpt: []string{"foo", "bar"},
VolumesFrom: []string{"dockername-c2"},
MemorySwappiness: memorySwappinessDefault,
}
assertSetStructFieldsEqual(t, expected, *hostConfig)
@@ -15,10 +15,17 @@
package api
import (
"github.com/fsouza/go-dockerclient"
)
const (
portBindingHostIP = "0.0.0.0"
memorySwappinessDefault = 0
)
func (task *Task) adjustForPlatform() {}
func getCanonicalPath(path string) string { return path }
func (task *Task) platformHostConfigOverride(hostConfig *docker.HostConfig) {}
@@ -18,10 +18,13 @@ package api
import (
"path/filepath"
"strings"
"github.com/fsouza/go-dockerclient"
)
const (
portBindingHostIP = ""
memorySwappinessDefault = -1
)
// adjustForPlatform makes Windows-specific changes to the task after unmarshal
@@ -50,3 +53,14 @@ func (task *Task) downcaseAllVolumePaths() {
func getCanonicalPath(path string) string {
return filepath.Clean(strings.ToLower(path))
}
// platformHostConfigOverride Sets up default HostConfig options to be passed to DockerAPI.
// Docker for Windows treats some options different than Unix and some are not supported at all.
// Version 1.12.x of Docker for Windows would ignore unsupported values such as MemorySwappiness.
// Version 17.03.x will cause an error if any value other than -1 is passed in. This bug is not
// noticed when no value is passed in. However, the go-dockerclient client version we are using
// removed the json option omitempty causing this parameter to default to 0 if empty.
// https://github.com/fsouza/go-dockerclient/commit/72342f96fabfa614a94b6ca57d987eccb8a836bf
func (task *Task) platformHostConfigOverride(hostConfig *docker.HostConfig) {
hostConfig.MemorySwappiness = memorySwappinessDefault
}
@@ -16,10 +16,12 @@
package api
import (
"encoding/json"
"testing"
"github.com/aws/amazon-ecs-agent/agent/acs/model/ecsacs"
"github.com/stretchr/testify/assert"
"github.com/fsouza/go-dockerclient"
)
const (
@@ -34,6 +36,8 @@ const (
expectedEmptyVolumeContainerImage = "microsoft/windowsservercore"
expectedEmptyVolumeContainerTag = "latest"
expectedEmptyVoluemContainerCmd = "not-applicable"
expectedMemorySwappinessDefault = memorySwappinessDefault
)
func TestPostUnmarshalWindowsCanonicalPaths(t *testing.T) {
@@ -99,3 +103,60 @@ func TestPostUnmarshalWindowsCanonicalPaths(t *testing.T) {
assert.Equal(t, expectedTask.Containers, task.Containers, "Containers should be equal")
assert.Equal(t, expectedTask.Volumes, task.Volumes, "Volumes should be equal")
}
func TestWindowsPlatformHostConfigOverride(t *testing.T) {
// Testing Windows platform override for HostConfig.
// Expects MemorySwappiness option to be set to -1
task := &Task{
Arn: "myArn",
DesiredStatusUnsafe: TaskRunning,
Family: "myFamily",
Version: "1",
Containers: []*Container{
{
Name: "myName",
},
},
}
hostConfig := &docker.HostConfig{
}
task.platformHostConfigOverride(hostConfig)
assert.EqualValues(t, expectedMemorySwappinessDefault, hostConfig.MemorySwappiness)
}
func TestWindowsMemorySwappinessOption(t *testing.T) {
// Testing sending a task to windows overriding MemorySwappiness value
rawHostConfigInput := docker.HostConfig{
}
rawHostConfig, err := json.Marshal(&rawHostConfigInput)
if err != nil {
t.Fatal(err)
}
testTask := &Task{
Arn: "arn:aws:ecs:us-east-1:012345678910:task/c09f0188-7f87-4b0f-bfc3-16296622b6fe",
Family: "myFamily",
Version: "1",
Containers: []*Container{
{
Name: "c1",
DockerConfig: DockerConfig{
HostConfig: strptr(string(rawHostConfig)),
},
},
},
}
config, configErr := testTask.DockerHostConfig(testTask.Containers[0], dockerMap(testTask))
if configErr != nil {
t.Fatal(configErr)
}
assert.EqualValues(t, expectedMemorySwappinessDefault, config.MemorySwappiness)
}

0 comments on commit 88a2e10

Please sign in to comment.