Skip to content

Commit 79a6d64

Browse files
authored
Fix #28: Tags can not be empty (#29)
1 parent 2372ceb commit 79a6d64

File tree

7 files changed

+96
-23
lines changed

7 files changed

+96
-23
lines changed

asg.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ import (
1515
"sync"
1616
)
1717

18-
func listASGInstaces(ASAPI autoscalingiface.AutoScalingAPI, asgName string) ([]*autoscaling.Instance, *string, error) {
18+
func listASGInstances(ASAPI autoscalingiface.AutoScalingAPI, asgName string) ([]*autoscaling.Instance, *string, error) {
1919
output, err := ASAPI.DescribeAutoScalingGroups(&autoscaling.DescribeAutoScalingGroupsInput{
2020
AutoScalingGroupNames: []*string{aws.String(asgName)},
2121
})
2222
if err != nil {
23-
return []*autoscaling.Instance{}, nil, err
23+
return []*autoscaling.Instance{}, nil, fmt.Errorf("on list ASG instances while describe ASG: %w", err)
2424
}
2525

2626
for _, autoScalingGroup := range output.AutoScalingGroups {
@@ -85,12 +85,12 @@ func instancesToContainerInstances(ECSAPI ecs.ECS, instances []autoscaling.Insta
8585
return true
8686
})
8787
if err != nil {
88-
return []ecsEC2Instance{}, err
88+
return []ecsEC2Instance{}, fmt.Errorf("on instances to container instances map while paginating container instances: %w", err)
8989
}
9090
for _, input := range describeContainerInstancesInputs {
9191
output, err := ECSAPI.DescribeContainerInstances(input)
9292
if err != nil {
93-
return []ecsEC2Instance{}, err
93+
return []ecsEC2Instance{}, fmt.Errorf("on instances to container instances map while describe container instances: %w", err)
9494
}
9595
for _, instance := range instances {
9696
for _, containerInstance := range output.ContainerInstances {
@@ -169,7 +169,7 @@ func drainingContainerInstanceIsDrained(ECSAPI ecs.ECS, clusterName, containerIn
169169
ContainerInstances: []*string{aws.String(containerInstanceID)},
170170
})
171171
if err != nil {
172-
return err
172+
return fmt.Errorf("on draining container instance while describe container instance: %w", err)
173173
}
174174
return findDrainingContainerInstance(output, containerInstanceID)
175175
}
@@ -239,7 +239,7 @@ func drainAll(ASAPI autoscaling.AutoScaling, ECSAPI ecs.ECS, EC2API ec2.EC2, ins
239239
}
240240

241241
func enforceLaunchConfig(ECSAPI ecs.ECS, ASAPI autoscaling.AutoScaling, EC2API ec2.EC2, asgName, clusterName string, bo backoff.BackOff) error {
242-
asgInstances, expectedLaunchConfig, err := listASGInstaces(&ASAPI, asgName)
242+
asgInstances, expectedLaunchConfig, err := listASGInstances(&ASAPI, asgName)
243243
if err != nil {
244244
return err
245245
}

asg_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func (m *mockAutoScalingClient) DescribeAutoScalingGroups(input *autoscaling.Des
2828
}
2929

3030
func TestListASGInstaces(t *testing.T) {
31-
instances, name, err := listASGInstaces(&mockAutoScalingClient{}, "")
31+
instances, name, err := listASGInstances(&mockAutoScalingClient{}, "")
3232
if len(instances) != 0 {
3333
t.Errorf("unexpected")
3434
}

ecs.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"encoding/json"
66
"errors"
7+
"fmt"
78
"github.com/aws/aws-sdk-go/aws"
89
"github.com/aws/aws-sdk-go/aws/arn"
910
"github.com/aws/aws-sdk-go/service/ecs"
@@ -43,6 +44,14 @@ var (
4344
errNoPrimaryDeployment = backoff.Permanent(errors.New("no PRIMARY deployment"))
4445
)
4546

47+
func ifEmptyThenNil(tags []*ecs.Tag) []*ecs.Tag {
48+
l := len(tags)
49+
if l == 0 {
50+
return nil
51+
}
52+
return tags
53+
}
54+
4655
func copyTd(input ecs.TaskDefinition, tags []*ecs.Tag) ecs.RegisterTaskDefinitionInput {
4756
obj := panicMarshal(input)
4857
inputClone := ecs.TaskDefinition{}
@@ -64,7 +73,7 @@ func copyTd(input ecs.TaskDefinition, tags []*ecs.Tag) ecs.RegisterTaskDefinitio
6473
output.TaskRoleArn = inputClone.TaskRoleArn
6574
output.Volumes = inputClone.Volumes
6675
// can't be replaced with reflection
67-
output.Tags = tags
76+
output.Tags = ifEmptyThenNil(tags)
6877
return output
6978
}
7079

@@ -161,7 +170,7 @@ func alterSecret(copy ecs.ContainerDefinition, secretMap map[string]string) ecs.
161170
func copyTaskDef(api ecsiface.ECSAPI, taskdef string, imageMap map[string]string, envMaps map[string]map[string]string, secretMaps map[string]map[string]string, logopts map[string]map[string]map[string]string, logsecrets map[string]map[string]map[string]string, taskRole string) (string, error) {
162171
output, err := api.DescribeTaskDefinition(&ecs.DescribeTaskDefinitionInput{TaskDefinition: aws.String(taskdef)})
163172
if err != nil {
164-
return "", err
173+
return "", fmt.Errorf("on copy task definition while describe existing task definition: %w", err)
165174
}
166175

167176
asRegisterTaskDefinitionInput := copyTd(*output.TaskDefinition, output.Tags)
@@ -176,7 +185,7 @@ func copyTaskDef(api ecsiface.ECSAPI, taskdef string, imageMap map[string]string
176185
}
177186
tdNew, err := api.RegisterTaskDefinition(&tdCopy)
178187
if err != nil {
179-
return "", err
188+
return "", fmt.Errorf("on copy task definition while register new task definition: %w", err)
180189
}
181190
taskDefinitionArn := tdNew.TaskDefinition.TaskDefinitionArn
182191
return *taskDefinitionArn, nil
@@ -185,7 +194,7 @@ func copyTaskDef(api ecsiface.ECSAPI, taskdef string, imageMap map[string]string
185194
func alterService(api ecsiface.ECSAPI, cluster, service string, imageMap map[string]string, envMaps map[string]map[string]string, secretMaps map[string]map[string]string, logopts map[string]map[string]map[string]string, logsecrets map[string]map[string]map[string]string, taskRole string, desiredCount *int64, taskdef string) (ecs.Service, ecs.Service, error) {
186195
output, err := api.DescribeServices(&ecs.DescribeServicesInput{Cluster: aws.String(cluster), Services: []*string{aws.String(service)}})
187196
if err != nil {
188-
return ecs.Service{}, ecs.Service{}, err
197+
return ecs.Service{}, ecs.Service{}, fmt.Errorf("on alter service while describe service: %w", err)
189198
}
190199
copyTaskDefinitionAction := func(sourceTaskDefinition string) (string, error) {
191200
return copyTaskDef(api, sourceTaskDefinition, imageMap, envMaps, secretMaps, logopts, logsecrets, taskRole)
@@ -267,7 +276,7 @@ func validateDraining(ecsapi ecsiface.ECSAPI, elbv2api elbv2iface.ELBV2API, ecsS
267276
describeEcsOutput, err := ecsapi.DescribeServices(&ecs.DescribeServicesInput{Cluster: ecsService.ClusterArn, Services: []*string{ecsService.ServiceName}})
268277

269278
if err != nil {
270-
return backoff.Permanent(err)
279+
return backoff.Permanent(fmt.Errorf("on validate draining while describe service: %w", err))
271280
}
272281
if len(describeEcsOutput.Services) == 0 {
273282
return backoff.Permanent(ErrServiceNotFound)
@@ -334,7 +343,7 @@ func validateDeployment(api ecsiface.ECSAPI, _ elbv2iface.ELBV2API, ecsService e
334343
operation := func() error {
335344
output, err = api.DescribeServices(&ecs.DescribeServicesInput{Cluster: ecsService.ClusterArn, Services: []*string{ecsService.ServiceName}})
336345
if err != nil {
337-
return err
346+
return fmt.Errorf("on validate deployment while describe service: %w", err)
338347
}
339348
for _, svc := range output.Services {
340349
for _, deployment := range svc.Deployments {

ecs_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,47 @@ func TestUpdateService(t *testing.T) {
158158
})
159159
}
160160
}
161+
162+
func Test_ifEmptyThenNil(t *testing.T) {
163+
type args struct {
164+
tags []*ecs.Tag
165+
}
166+
tag := ecs.Tag{
167+
Key: aws.String("key"),
168+
Value: aws.String("value"),
169+
}
170+
tests := []struct {
171+
name string
172+
args args
173+
want []*ecs.Tag
174+
}{
175+
{
176+
name: "if empty then nil",
177+
args: args{
178+
tags: []*ecs.Tag{},
179+
},
180+
want: nil,
181+
},
182+
{
183+
name: "if nil then nil",
184+
args: args{
185+
tags: nil,
186+
},
187+
want: nil,
188+
},
189+
{
190+
name: "if not empty then not nil",
191+
args: args{
192+
tags: []*ecs.Tag{&tag},
193+
},
194+
want: []*ecs.Tag{&tag},
195+
},
196+
}
197+
for _, tt := range tests {
198+
t.Run(tt.name, func(t *testing.T) {
199+
if got := ifEmptyThenNil(tt.args.tags); !reflect.DeepEqual(got, tt.want) {
200+
t.Errorf("ifEmptyThenNil() = %v, want %v", got, tt.want)
201+
}
202+
})
203+
}
204+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/Autodesk/go-awsecs
33
go 1.15
44

55
require (
6-
github.com/aws/aws-sdk-go v1.35.7
6+
github.com/aws/aws-sdk-go v1.44.174
77
github.com/cenkalti/backoff v0.0.0-00010101000000-000000000000
88
github.com/sergi/go-diff v1.1.0
99
)

go.sum

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
github.com/aws/aws-sdk-go v1.35.7 h1:FHMhVhyc/9jljgFAcGkQDYjpC9btM0B8VfkLBfctdNE=
2-
github.com/aws/aws-sdk-go v1.35.7/go.mod h1:tlPOdRjfxPBpNIwqDj61rmsnA85v9jc0Ps9+muhnW+k=
1+
github.com/aws/aws-sdk-go v1.44.174 h1:9lR4a6MKQW/t6YCG0ZKAt1GAkjdEPP8sWch/pfcuR0c=
2+
github.com/aws/aws-sdk-go v1.44.174/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI=
33
github.com/cenkalti/backoff/v4 v4.1.0 h1:c8LkOFQTzuO0WBM/ae5HdGQuZPfPxp7lqBRwQRm4fSc=
44
github.com/cenkalti/backoff/v4 v4.1.0/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw=
5-
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
65
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
76
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
87
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -23,17 +22,38 @@ github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNX
2322
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
2423
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
2524
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
25+
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
2626
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
27-
golang.org/x/net v0.0.0-20200202094626-16171245cfb2 h1:CCH4IOTTfewWjGOlSp+zGcjutRKlBEZQ6wTn8ozI/nI=
28-
golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
27+
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
28+
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
29+
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
30+
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
31+
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
32+
golang.org/x/net v0.1.0 h1:hZ/3BUoy5aId7sCpA/Tc5lt8DkFgdVS2onTpJsZ/fl0=
33+
golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco=
34+
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
35+
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
2936
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
30-
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
37+
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
38+
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
39+
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
40+
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
41+
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
42+
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
43+
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
44+
golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
3145
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
32-
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
46+
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
47+
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
48+
golang.org/x/text v0.4.0 h1:BrVqGRd7+k1DiOgtnFvAkoQEWQvBc25ouMJM6429SFg=
49+
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
50+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
51+
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
52+
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
53+
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
3354
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
3455
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
3556
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
36-
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
3757
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
3858
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
3959
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=

make.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ do
2020
windows_ext=.exe
2121
for goos in linux windows darwin; do
2222
# shellcheck disable=SC2043
23-
for goarch in amd64; do
23+
for goarch in amd64 arm64; do
2424
ext="${goos}_ext"
2525
out_no_ext="${dir}-${goos}-${goarch}"
2626
out="${dir}${!ext:-}"

0 commit comments

Comments
 (0)