From 27335d7d21f5b02ea1e1eda749075098786f9230 Mon Sep 17 00:00:00 2001 From: psihachina Date: Wed, 5 Oct 2022 09:35:13 +0300 Subject: [PATCH] added overwrite docker image name and tag --- internal/manager/ecs/ecs_test.go | 27 ++++++++-- internal/manager/ecs/native.go | 92 +++++++++++++++----------------- 2 files changed, 68 insertions(+), 51 deletions(-) diff --git a/internal/manager/ecs/ecs_test.go b/internal/manager/ecs/ecs_test.go index 667d6038..c75cad29 100644 --- a/internal/manager/ecs/ecs_test.go +++ b/internal/manager/ecs/ecs_test.go @@ -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, @@ -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"), @@ -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) @@ -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{ @@ -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{ { diff --git a/internal/manager/ecs/native.go b/internal/manager/ecs/native.go index a7c9581a..d8b90fe9 100644 --- a/internal/manager/ecs/native.go +++ b/internal/manager/ecs/native.go @@ -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 { @@ -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 }