From d62370b0ccf3ee4e157d49a5a05d5330a0c9d3e9 Mon Sep 17 00:00:00 2001 From: andresvia Date: Mon, 4 Nov 2019 14:00:10 -0800 Subject: [PATCH] don't clone original taskdef if no changes are required --- asg.go | 2 +- asg_test.go | 46 +++++++-------- cmd/enforce-aws-ecs-asg-launchconfig/main.go | 2 +- cmd/update-aws-ecs-service/main.go | 2 +- ecs-alter-service.go | 5 +- ecs.go | 59 +++++++++++++++++--- go.mod | 4 +- go.sum | 8 +-- 8 files changed, 85 insertions(+), 43 deletions(-) diff --git a/asg.go b/asg.go index 5020e98..9a3e9b7 100644 --- a/asg.go +++ b/asg.go @@ -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" diff --git a/asg_test.go b/asg_test.go index 9f6f330..c74b772 100644 --- a/asg_test.go +++ b/asg_test.go @@ -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) { diff --git a/cmd/enforce-aws-ecs-asg-launchconfig/main.go b/cmd/enforce-aws-ecs-asg-launchconfig/main.go index 4c873b8..62c78c2 100644 --- a/cmd/enforce-aws-ecs-asg-launchconfig/main.go +++ b/cmd/enforce-aws-ecs-asg-launchconfig/main.go @@ -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" ) diff --git a/cmd/update-aws-ecs-service/main.go b/cmd/update-aws-ecs-service/main.go index ff24797..dcab611 100644 --- a/cmd/update-aws-ecs-service/main.go +++ b/cmd/update-aws-ecs-service/main.go @@ -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" diff --git a/ecs-alter-service.go b/ecs-alter-service.go index 20a41b9..c5f3cf8 100644 --- a/ecs-alter-service.go +++ b/ecs-alter-service.go @@ -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" ) @@ -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 } diff --git a/ecs.go b/ecs.go index 95e4793..40ccdbc 100644 --- a/ecs.go +++ b/ecs.go @@ -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" ) @@ -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 @@ -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(©) - 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) { @@ -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 } diff --git a/go.mod b/go.mod index ce2da22..f15661b 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 5f2845a..737e31d 100644 --- a/go.sum +++ b/go.sum @@ -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=