Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the error message when reserved memory is larger than the available memory #897

Merged
merged 2 commits into from
Jul 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions agent/api/ecsclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package ecsclient

import (
"errors"
"fmt"
"runtime"
"strings"
"time"
Expand Down Expand Up @@ -172,7 +173,12 @@ func (client *APIECSClient) registerContainerInstance(clusterRef string, contain
integerStr := "INTEGER"

cpu, mem := getCpuAndMemory()
mem = mem - int64(client.config.ReservedMemory)
remainingMem := mem - int64(client.config.ReservedMemory)
if remainingMem < 0 {
return "", fmt.Errorf(
"api register-container-instance: reserved memory is higher than available memory on the host, total memory: %d, reserved: %d",
mem, client.config.ReservedMemory)
}

cpuResource := ecs.Resource{
Name: utils.Strptr("CPU"),
Expand All @@ -182,7 +188,7 @@ func (client *APIECSClient) registerContainerInstance(clusterRef string, contain
memResource := ecs.Resource{
Name: utils.Strptr("MEMORY"),
Type: &integerStr,
IntegerValue: &mem,
IntegerValue: &remainingMem,
}
portResource := ecs.Resource{
Name: utils.Strptr("PORTS"),
Expand Down
24 changes: 24 additions & 0 deletions agent/api/ecsclient/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,30 @@ func TestRegisterContainerInstance(t *testing.T) {
assert.Equal(t, "registerArn", arn)
}

// TestRegisterContainerInstanceWithNegativeResource tests the registeration should fail with negative resource
func TestRegisterContainerInstanceWithNegativeResource(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

_, mem := getCpuAndMemory()
mockEC2Metadata := mock_ec2.NewMockEC2MetadataClient(mockCtrl)
client := NewECSClient(credentials.AnonymousCredentials,
&config.Config{Cluster: configuredCluster,
AWSRegion: "us-east-1",
ReservedMemory: uint16(mem) + 1,
}, mockEC2Metadata)
mockSDK := mock_api.NewMockECSSDK(mockCtrl)
mockSubmitStateSDK := mock_api.NewMockECSSubmitStateSDK(mockCtrl)
client.(*APIECSClient).SetSDK(mockSDK)
client.(*APIECSClient).SetSubmitStateChangeSDK(mockSubmitStateSDK)

mockEC2Metadata.EXPECT().GetDynamicData(ec2.InstanceIdentityDocumentResource).Return("instanceIdentityDocument", nil)
mockEC2Metadata.EXPECT().GetDynamicData(ec2.InstanceIdentityDocumentSignatureResource).Return("signature", nil)

_, err := client.RegisterContainerInstance("", nil)
assert.Error(t, err, "Register resource with negative value should cause registration fail")
}

func TestValidateRegisteredAttributes(t *testing.T) {
origAttributes := []*ecs.Attribute{
{Name: aws.String("foo"), Value: aws.String("bar")},
Expand Down
1 change: 0 additions & 1 deletion agent/app/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,6 @@ func (agent *ecsAgent) registerContainerInstance(
// Save our shiny new containerInstanceArn
stateManager.Save()
return nil

}

// registerContainerInstance registers a container instance that has already been
Expand Down
21 changes: 11 additions & 10 deletions agent/engine/docker_task_engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -605,16 +605,17 @@ func TestStopWithPendingStops(t *testing.T) {

taskEngine.AddTask(sleepTask2)
<-pullInvoked
stopSleep2 := *sleepTask2
stopSleep2 := testdata.LoadTask("sleep5")
stopSleep2.Arn = "arn2"
stopSleep2.SetDesiredStatus(api.TaskStopped)
stopSleep2.StopSequenceNumber = 4
taskEngine.AddTask(&stopSleep2)
taskEngine.AddTask(stopSleep2)

taskEngine.AddTask(sleepTask1)
stopSleep1 := *sleepTask1
stopSleep1 := testdata.LoadTask("sleep5")
stopSleep1.SetDesiredStatus(api.TaskStopped)
stopSleep1.StopSequenceNumber = 5
taskEngine.AddTask(&stopSleep1)
taskEngine.AddTask(stopSleep1)
pullDone <- true
// this means the PullImage is only called once due to the task is stopped before it
// gets the pull image lock
Expand Down Expand Up @@ -762,9 +763,9 @@ func TestTaskTransitionWhenStopContainerTimesout(t *testing.T) {
assert.Equal(t, event.(api.TaskStateChange).Status, api.TaskRunning, "Expected task to be RUNNING")

// Set the task desired status to be stopped and StopContainer will be called
updateSleepTask := *sleepTask
updateSleepTask := testdata.LoadTask("sleep5")
updateSleepTask.SetDesiredStatus(api.TaskStopped)
go taskEngine.AddTask(&updateSleepTask)
go taskEngine.AddTask(updateSleepTask)

// StopContainer timeout error shouldn't cause cantainer/task status change
// until receive stop event from docker event stream
Expand Down Expand Up @@ -867,9 +868,9 @@ func TestTaskTransitionWhenStopContainerReturnsUnretriableError(t *testing.T) {
eventsReported.Wait()

// Set the task desired status to be stopped and StopContainer will be called
updateSleepTask := *sleepTask
updateSleepTask := testdata.LoadTask("sleep5")
updateSleepTask.SetDesiredStatus(api.TaskStopped)
go taskEngine.AddTask(&updateSleepTask)
go taskEngine.AddTask(updateSleepTask)

// StopContainer was called again and received stop event from docker event stream
// Expect it to go to stopped
Expand Down Expand Up @@ -944,9 +945,9 @@ func TestTaskTransitionWhenStopContainerReturnsTransientErrorBeforeSucceeding(t
}

// Set the task desired status to be stopped and StopContainer will be called
updateSleepTask := *sleepTask
updateSleepTask := testdata.LoadTask("sleep5")
updateSleepTask.SetDesiredStatus(api.TaskStopped)
go taskEngine.AddTask(&updateSleepTask)
go taskEngine.AddTask(updateSleepTask)

// StopContainer invocation should have caused it to stop eventually.
event = <-stateChangeEvents
Expand Down