Skip to content

Commit

Permalink
don't clone original taskdef if no changes are required
Browse files Browse the repository at this point in the history
  • Loading branch information
andresvia committed Nov 4, 2019
1 parent 87bef2b commit d62370b
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 43 deletions.
2 changes: 1 addition & 1 deletion asg.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/aws/aws-sdk-go/service/autoscaling/autoscalingiface"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ecs"
"github.com/cenkalti/backoff"
"github.com/cenkalti/backoff/v3"
"log"
"strings"
"sync"
Expand Down
46 changes: 23 additions & 23 deletions asg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,38 @@ type mockAutoScalingClient struct {
}

func (m *mockAutoScalingClient) DescribeAutoScalingGroups(input *autoscaling.DescribeAutoScalingGroupsInput) (*autoscaling.DescribeAutoScalingGroupsOutput, error) {
name := ""
name := ""
return &autoscaling.DescribeAutoScalingGroupsOutput{
AutoScalingGroups: []*autoscaling.Group{
&autoscaling.Group{
Instances: []*autoscaling.Instance{
// TODO
},
LaunchConfigurationName: &name,
},
},
}, nil
AutoScalingGroups: []*autoscaling.Group{
&autoscaling.Group{
Instances: []*autoscaling.Instance{
// TODO
},
LaunchConfigurationName: &name,
},
},
}, nil
}

func TestListASGInstaces(t *testing.T) {
instances, name, err := listASGInstaces(&mockAutoScalingClient{}, "")
if len(instances) != 0 {
t.Errorf("unexpected")
}
if *name != "" {
t.Errorf("unexpected")
}
if err != nil {
t.Errorf("unexpected")
}
if len(instances) != 0 {
t.Errorf("unexpected")
}
if *name != "" {
t.Errorf("unexpected")
}
if err != nil {
t.Errorf("unexpected")
}
}

func TestNeedReplacement(t *testing.T) {
name := ""
name := ""
replace := needReplacement("", autoscaling.Instance{LaunchConfigurationName: &name})
if replace {
t.Errorf("unexpected")
}
if replace {
t.Errorf("unexpected")
}
}

func TestFilterInstancesToReplace(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/enforce-aws-ecs-asg-launchconfig/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ecs"
"github.com/cenkalti/backoff"
"github.com/cenkalti/backoff/v3"
"log"
)

Expand Down
2 changes: 1 addition & 1 deletion cmd/update-aws-ecs-service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ecs"
"github.com/cenkalti/backoff"
"github.com/cenkalti/backoff/v3"
"log"
"os"
"strings"
Expand Down
5 changes: 3 additions & 2 deletions ecs-alter-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package awsecs

import (
"errors"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ecs"
"github.com/cenkalti/backoff"
"github.com/cenkalti/backoff/v3"
"log"
)

Expand All @@ -26,7 +27,7 @@ func alterServiceOrValidatedRollBack(api ecs.ECS, cluster, service string, image
return ErrPermanentNothingToRollback
}
log.Printf("attempt rollback %v", alterSvcErr)
rollback, err := api.UpdateService(&ecs.UpdateServiceInput{Cluster: oldsvc.ClusterArn, Service: oldsvc.ServiceName, TaskDefinition: oldsvc.TaskDefinition, DesiredCount: oldsvc.DesiredCount})
rollback, err := api.UpdateService(&ecs.UpdateServiceInput{Cluster: oldsvc.ClusterArn, Service: oldsvc.ServiceName, TaskDefinition: oldsvc.TaskDefinition, DesiredCount: oldsvc.DesiredCount, ForceNewDeployment: aws.Bool(true)})
if err != nil {
return err
}
Expand Down
59 changes: 50 additions & 9 deletions ecs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"errors"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ecs"
"github.com/cenkalti/backoff"
"github.com/cenkalti/backoff/v3"
"log"
)

Expand All @@ -27,18 +27,21 @@ var (
errNoPrimaryDeployment = backoff.Permanent(errors.New("no PRIMARY deployment"))
)

func copy(input ecs.TaskDefinition) ecs.RegisterTaskDefinitionInput {
func copyTd(input ecs.TaskDefinition, tags []*ecs.Tag) ecs.RegisterTaskDefinitionInput {
output := ecs.RegisterTaskDefinitionInput{}
output.ContainerDefinitions = input.ContainerDefinitions
output.Cpu = input.Cpu
output.ExecutionRoleArn = input.ExecutionRoleArn
output.Family = input.Family
// output.InferenceAccelerators // not supported by the current version of the SDK
output.IpcMode = input.IpcMode
output.Memory = input.Memory
output.NetworkMode = input.NetworkMode
output.PidMode = input.PidMode
output.PlacementConstraints = input.PlacementConstraints
output.ProxyConfiguration = input.ProxyConfiguration
output.RequiresCompatibilities = input.RequiresCompatibilities
output.Tags = tags
output.TaskRoleArn = input.TaskRoleArn
output.Volumes = input.Volumes
return output
Expand Down Expand Up @@ -130,13 +133,51 @@ func copyTaskDef(api ecs.ECS, taskdef string, imageMap map[string]string, envMap
if err != nil {
return "", err
}
copy := alterSecrets(alterEnvironments(alterImages(copy(*output.TaskDefinition), imageMap), envMaps), secretMaps)
new, err := api.RegisterTaskDefinition(&copy)
if err != nil {
return "", err

tdCopy := alterSecrets(alterEnvironments(alterImages(copyTd(*output.TaskDefinition, output.Tags), imageMap), envMaps), secretMaps)

modified := false
for _, containerDefinition := range tdCopy.ContainerDefinitions {
if image, found := imageMap[*containerDefinition.Name]; found {
if *containerDefinition.Image != image {
modified = true
break
}
}

if envMap, found := envMaps[*containerDefinition.Name]; found {
for _, keyValuePair := range containerDefinition.Environment {
if env, found := envMap[*keyValuePair.Name]; found {
if *keyValuePair.Value != env {
modified = true
break
}
}
}
}

if secretMap, found := secretMaps[*containerDefinition.Name]; found {
for _, secret := range containerDefinition.Secrets {
if from, found := secretMap[*secret.Name]; found {
if *secret.ValueFrom != from {
modified = true
break
}
}
}
}
}

if modified {
tdNew, err := api.RegisterTaskDefinition(&tdCopy)
if err != nil {
return "", err
}
arn := tdNew.TaskDefinition.TaskDefinitionArn
return *arn, nil
} else {
return *output.TaskDefinition.TaskDefinitionArn, nil
}
arn := new.TaskDefinition.TaskDefinitionArn
return *arn, nil
}

func alterService(api ecs.ECS, cluster, service string, imageMap map[string]string, envMaps map[string]map[string]string, secretMaps map[string]map[string]string, desiredCount *int64, taskdef string) (ecs.Service, ecs.Service, error) {
Expand All @@ -156,7 +197,7 @@ func alterService(api ecs.ECS, cluster, service string, imageMap map[string]stri
if desiredCount == nil {
desiredCount = svc.DesiredCount
}
updated, err := api.UpdateService(&ecs.UpdateServiceInput{Cluster: aws.String(cluster), Service: aws.String(service), TaskDefinition: aws.String(newTd), DesiredCount: desiredCount})
updated, err := api.UpdateService(&ecs.UpdateServiceInput{Cluster: aws.String(cluster), Service: aws.String(service), TaskDefinition: aws.String(newTd), DesiredCount: desiredCount, ForceNewDeployment: aws.Bool(true)})
if err != nil {
return *svc, ecs.Service{}, err
}
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module github.com/Autodesk/go-awsecs
go 1.12

require (
github.com/aws/aws-sdk-go v1.19.24
github.com/cenkalti/backoff v0.0.0-20190506075156-2146c9339422
github.com/aws/aws-sdk-go v1.19.49
github.com/cenkalti/backoff/v3 v3.0.0
)
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
github.com/aws/aws-sdk-go v1.19.24 h1:qOIYaFxcFg07Vdn799ERpGiuUUIEi5MQ2vYib3CNMp4=
github.com/aws/aws-sdk-go v1.19.24/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
github.com/cenkalti/backoff v0.0.0-20190506075156-2146c9339422 h1:+FKjzBIdfBHYDvxCv+djmDJdes/AoDtg8gpcxowBlF8=
github.com/cenkalti/backoff v0.0.0-20190506075156-2146c9339422/go.mod h1:b6Nc7NRH5C4aCISLry0tLnTjcuTEvoiqcWDdsU0sOGM=
github.com/aws/aws-sdk-go v1.19.49 h1:GUlenK625g5iKrIiRcqRS/CvPMLc8kZRtMxXuXBhFx4=
github.com/aws/aws-sdk-go v1.19.49/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
github.com/cenkalti/backoff/v3 v3.0.0 h1:ske+9nBpD9qZsTBoF41nW5L+AIuFBKMeze18XQ3eG1c=
github.com/cenkalti/backoff/v3 v3.0.0/go.mod h1:cIeZDE3IrqwwJl6VUwCN6trj1oXrTS4rc0ij+ULvLYs=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=

0 comments on commit d62370b

Please sign in to comment.