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

IZE-648 Overwrite docker image name and tag when the most recent task definition is used #484

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
27 changes: 24 additions & 3 deletions internal/manager/ecs/ecs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,14 @@ func TestManager_Deploy(t *testing.T) {
}, nil).Times(1)
m.EXPECT().DeregisterTaskDefinition(gomock.Any()).Return(&ecs.DeregisterTaskDefinitionOutput{
TaskDefinition: &ecs.TaskDefinition{},
}, nil).Times(2)
}, nil).Times(1)
m.EXPECT().RegisterTaskDefinition(gomock.Any()).Return(&ecs.RegisterTaskDefinitionOutput{
TaskDefinition: &ecs.TaskDefinition{
Family: aws.String("test"),
Revision: aws.Int64(1),
TaskDefinitionArn: aws.String("test"),
},
}, nil).Times(1)
},
mockELB: func(m *mocks.MockELBV2API) {},
wantErr: false,
Expand Down Expand Up @@ -293,7 +300,7 @@ func TestManager_Deploy(t *testing.T) {
}, nil).Times(1)
m.EXPECT().DeregisterTaskDefinition(gomock.Any()).Return(&ecs.DeregisterTaskDefinitionOutput{
TaskDefinition: &ecs.TaskDefinition{},
}, nil).Times(2)
}, nil).Times(1)
m.EXPECT().RegisterTaskDefinition(gomock.Any()).Return(&ecs.RegisterTaskDefinitionOutput{
TaskDefinition: &ecs.TaskDefinition{
Family: aws.String("test"),
Expand Down Expand Up @@ -334,6 +341,13 @@ func TestManager_Deploy(t *testing.T) {
},
},
}, nil).Times(2)
m.EXPECT().RegisterTaskDefinition(gomock.Any()).Return(&ecs.RegisterTaskDefinitionOutput{
TaskDefinition: &ecs.TaskDefinition{
Family: aws.String("test"),
Revision: aws.Int64(1),
TaskDefinitionArn: aws.String("test"),
},
}, nil).Times(1)
m.EXPECT().ListTaskDefinitions(gomock.Any()).Return(&ecs.ListTaskDefinitionsOutput{
TaskDefinitionArns: []*string{aws.String("test")},
}, nil).Times(1)
Expand Down Expand Up @@ -374,7 +388,7 @@ func TestManager_Deploy(t *testing.T) {
}, nil).Times(1)
m.EXPECT().DeregisterTaskDefinition(gomock.Any()).Return(&ecs.DeregisterTaskDefinitionOutput{
TaskDefinition: &ecs.TaskDefinition{},
}, nil).Times(2)
}, nil).Times(1)
},
mockELB: func(m *mocks.MockELBV2API) {
m.EXPECT().DescribeTargetGroups(gomock.Any()).Return(&elbv2.DescribeTargetGroupsOutput{
Expand Down Expand Up @@ -428,6 +442,13 @@ func TestManager_Deploy(t *testing.T) {
}, nil).Times(1)
},
mockECS: func(m *mocks.MockECSAPI) {
m.EXPECT().RegisterTaskDefinition(gomock.Any()).Return(&ecs.RegisterTaskDefinitionOutput{
TaskDefinition: &ecs.TaskDefinition{
Family: aws.String("test"),
Revision: aws.Int64(1),
TaskDefinitionArn: aws.String("test"),
},
}, nil).Times(1)
m.EXPECT().DescribeServices(gomock.Any()).Return(&ecs.DescribeServicesOutput{
Services: []*ecs.Service{
{
Expand Down
92 changes: 44 additions & 48 deletions internal/manager/ecs/native.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,62 +52,62 @@ func (e *Manager) deployLocal(w io.Writer) error {
var oldTaskDef ecs.TaskDefinition
var newTaskDef ecs.TaskDefinition

oldTaskDef = *dtdo.TaskDefinition

if *dtdo.TaskDefinition.TaskDefinitionArn == *definitions.TaskDefinitionArns[0] {
pterm.Printfln("Deploying based on task definition: %s:%d", *dtdo.TaskDefinition.Family, *dtdo.TaskDefinition.Revision)
oldTaskDef = *dtdo.TaskDefinition
} else {
definition, err := svc.DescribeTaskDefinition(&ecs.DescribeTaskDefinitionInput{
TaskDefinition: definitions.TaskDefinitionArns[0],
})
if err != nil {
return err
}

var image string
oldTaskDef = *definition.TaskDefinition
}

pterm.Printfln("Deploying based on task definition: %s:%d", *oldTaskDef.Family, *oldTaskDef.Revision)

for i := 0; i < len(dtdo.TaskDefinition.ContainerDefinitions); i++ {
container := dtdo.TaskDefinition.ContainerDefinitions[i]
var image string

// We are changing the image/tag only for the app-specific container (not sidecars)
if *container.Name == e.App.Name {
if len(e.Project.Tag) != 0 && len(e.App.Image) == 0 {
name := strings.Split(*container.Image, ":")[0]
image = fmt.Sprintf("%s:%s", name, e.Project.Tag)
} else {
image = e.App.Image
}
for i := 0; i < len(oldTaskDef.ContainerDefinitions); i++ {
container := oldTaskDef.ContainerDefinitions[i]

pterm.Printfln(`Changed image of container "%s" to : "%s" (was: "%s")`, *container.Name, image, *container.Image)
container.Image = &image
// We are changing the image/tag only for the app-specific container (not sidecars)
if *container.Name == e.App.Name {
if len(e.Project.Tag) != 0 && len(e.App.Image) == 0 {
name := strings.Split(*container.Image, ":")[0]
image = fmt.Sprintf("%s:%s", name, e.Project.Tag)
} else {
image = e.App.Image
}
}

pterm.Println("Creating new task definition revision")

rtdo, err := svc.RegisterTaskDefinition(&ecs.RegisterTaskDefinitionInput{
ContainerDefinitions: dtdo.TaskDefinition.ContainerDefinitions,
Family: dtdo.TaskDefinition.Family,
Volumes: dtdo.TaskDefinition.Volumes,
TaskRoleArn: dtdo.TaskDefinition.TaskRoleArn,
ExecutionRoleArn: dtdo.TaskDefinition.ExecutionRoleArn,
RuntimePlatform: dtdo.TaskDefinition.RuntimePlatform,
RequiresCompatibilities: dtdo.TaskDefinition.RequiresCompatibilities,
NetworkMode: dtdo.TaskDefinition.NetworkMode,
Cpu: dtdo.TaskDefinition.Cpu,
Memory: dtdo.TaskDefinition.Memory,
})
if err != nil {
return err
pterm.Printfln(`Changed image of container "%s" to : "%s" (was: "%s")`, *container.Name, image, *container.Image)
container.Image = &image
}
}

newTaskDef = *rtdo.TaskDefinition
pterm.Println("Creating new task definition revision")

pterm.Printfln("Successfully created revision: %s:%d", *rtdo.TaskDefinition.Family, *rtdo.TaskDefinition.Revision)
} else {
definition, err := svc.DescribeTaskDefinition(&ecs.DescribeTaskDefinitionInput{
TaskDefinition: definitions.TaskDefinitionArns[0],
})
if err != nil {
return err
}

newTaskDef = *definition.TaskDefinition
rtdo, err := svc.RegisterTaskDefinition(&ecs.RegisterTaskDefinitionInput{
ContainerDefinitions: oldTaskDef.ContainerDefinitions,
Family: oldTaskDef.Family,
Volumes: oldTaskDef.Volumes,
TaskRoleArn: oldTaskDef.TaskRoleArn,
ExecutionRoleArn: oldTaskDef.ExecutionRoleArn,
RuntimePlatform: oldTaskDef.RuntimePlatform,
RequiresCompatibilities: oldTaskDef.RequiresCompatibilities,
NetworkMode: oldTaskDef.NetworkMode,
Cpu: oldTaskDef.Cpu,
Memory: oldTaskDef.Memory,
})
if err != nil {
return err
}

newTaskDef = *rtdo.TaskDefinition

pterm.Printfln("Successfully created revision: %s:%d", *rtdo.TaskDefinition.Family, *rtdo.TaskDefinition.Revision)

if err = e.updateTaskDefinition(&newTaskDef, &oldTaskDef, name, "Deploying new task definition"); err != nil {
err := e.getLastContainerLogs(fmt.Sprintf("%s-%s", e.Project.Env, e.App.Name))
if err != nil {
Expand All @@ -132,10 +132,6 @@ func (e *Manager) deployLocal(w io.Writer) error {
return fmt.Errorf("deployment failed, but service has been rolled back to previous task definition: %s", *oldTaskDef.Family)
}

if err = deregisterTaskDefinition(svc, &oldTaskDef); err != nil {
return err
}

return nil
}

Expand Down