Skip to content

Commit

Permalink
engine: save dockerID in createContainer.
Browse files Browse the repository at this point in the history
Previously, the dockerID in the dockerContainer struct of a container is only saved in termination handler. This means that if the agent is forcefully stopped (e.g. via SIGKILL instead of SIGTERM), the dockerID is not saved, and as a result loses track of container if it is stopped by SIGKILL. This commit fixes the issue by saving the dockerID  after creating the container.
  • Loading branch information
fenxiong committed Sep 3, 2020
1 parent 4d8115f commit fceb279
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
6 changes: 4 additions & 2 deletions agent/engine/docker_task_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -1097,9 +1097,11 @@ func (engine *DockerTaskEngine) createContainer(task *apitask.Task, container *a
if metadata.DockerID != "" {
seelog.Infof("Task engine [%s]: created docker container for task: %s -> %s",
task.Arn, container.Name, metadata.DockerID)
engine.state.AddContainer(&apicontainer.DockerContainer{DockerID: metadata.DockerID,
dockerContainer := &apicontainer.DockerContainer{DockerID: metadata.DockerID,
DockerName: dockerContainerName,
Container: container}, task)
Container: container}
engine.state.AddContainer(dockerContainer, task)
engine.saveDockerContainerData(dockerContainer)
}
container.SetLabels(config.Labels)
seelog.Infof("Task engine [%s]: created docker container for task: %s -> %s, took %s",
Expand Down
30 changes: 30 additions & 0 deletions agent/engine/docker_task_engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,36 @@ func TestStopWithPendingStops(t *testing.T) {
// gets the pull image lock
}

func TestCreateContainerSaveDockerIDAndName(t *testing.T) {
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()
ctrl, client, _, privateTaskEngine, _, _, _ := mocks(t, ctx, &defaultConfig)
defer ctrl.Finish()
dataClient, cleanup := newTestDataClient(t)
defer cleanup()

taskEngine, _ := privateTaskEngine.(*DockerTaskEngine)
taskEngine.SetDataClient(dataClient)

sleepTask := testdata.LoadTask("sleep5")
sleepTask.Arn = testTaskARN
sleepContainer, _ := sleepTask.ContainerByName("sleep5")
sleepContainer.TaskARNUnsafe = testTaskARN

client.EXPECT().APIVersion().Return(defaultDockerClientAPIVersion, nil).AnyTimes()
client.EXPECT().CreateContainer(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(dockerapi.DockerContainerMetadata{
DockerID: testDockerID,
})
metadata := taskEngine.createContainer(sleepTask, sleepContainer)
require.NoError(t, metadata.Error)

containers, err := dataClient.GetContainers()
require.NoError(t, err)
require.Len(t, containers, 1)
assert.Equal(t, testDockerID, containers[0].DockerID)
assert.Contains(t, containers[0].DockerName, sleepContainer.Name)
}

func TestCreateContainerMetadata(t *testing.T) {
testcases := []struct {
name string
Expand Down

0 comments on commit fceb279

Please sign in to comment.