Skip to content

Commit

Permalink
added overwrite docker image name and tag
Browse files Browse the repository at this point in the history
  • Loading branch information
psihachina committed Oct 13, 2022
1 parent 34d7ccd commit 27335d7
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 51 deletions.
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

0 comments on commit 27335d7

Please sign in to comment.