Skip to content

Commit

Permalink
Fix #28: Tags can not be empty (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
andresvia authored Jan 5, 2023
1 parent 2372ceb commit 79a6d64
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 23 deletions.
12 changes: 6 additions & 6 deletions asg.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import (
"sync"
)

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

for _, autoScalingGroup := range output.AutoScalingGroups {
Expand Down Expand Up @@ -85,12 +85,12 @@ func instancesToContainerInstances(ECSAPI ecs.ECS, instances []autoscaling.Insta
return true
})
if err != nil {
return []ecsEC2Instance{}, err
return []ecsEC2Instance{}, fmt.Errorf("on instances to container instances map while paginating container instances: %w", err)
}
for _, input := range describeContainerInstancesInputs {
output, err := ECSAPI.DescribeContainerInstances(input)
if err != nil {
return []ecsEC2Instance{}, err
return []ecsEC2Instance{}, fmt.Errorf("on instances to container instances map while describe container instances: %w", err)
}
for _, instance := range instances {
for _, containerInstance := range output.ContainerInstances {
Expand Down Expand Up @@ -169,7 +169,7 @@ func drainingContainerInstanceIsDrained(ECSAPI ecs.ECS, clusterName, containerIn
ContainerInstances: []*string{aws.String(containerInstanceID)},
})
if err != nil {
return err
return fmt.Errorf("on draining container instance while describe container instance: %w", err)
}
return findDrainingContainerInstance(output, containerInstanceID)
}
Expand Down Expand Up @@ -239,7 +239,7 @@ func drainAll(ASAPI autoscaling.AutoScaling, ECSAPI ecs.ECS, EC2API ec2.EC2, ins
}

func enforceLaunchConfig(ECSAPI ecs.ECS, ASAPI autoscaling.AutoScaling, EC2API ec2.EC2, asgName, clusterName string, bo backoff.BackOff) error {
asgInstances, expectedLaunchConfig, err := listASGInstaces(&ASAPI, asgName)
asgInstances, expectedLaunchConfig, err := listASGInstances(&ASAPI, asgName)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion asg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (m *mockAutoScalingClient) DescribeAutoScalingGroups(input *autoscaling.Des
}

func TestListASGInstaces(t *testing.T) {
instances, name, err := listASGInstaces(&mockAutoScalingClient{}, "")
instances, name, err := listASGInstances(&mockAutoScalingClient{}, "")
if len(instances) != 0 {
t.Errorf("unexpected")
}
Expand Down
21 changes: 15 additions & 6 deletions ecs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/ecs"
Expand Down Expand Up @@ -43,6 +44,14 @@ var (
errNoPrimaryDeployment = backoff.Permanent(errors.New("no PRIMARY deployment"))
)

func ifEmptyThenNil(tags []*ecs.Tag) []*ecs.Tag {
l := len(tags)
if l == 0 {
return nil
}
return tags
}

func copyTd(input ecs.TaskDefinition, tags []*ecs.Tag) ecs.RegisterTaskDefinitionInput {
obj := panicMarshal(input)
inputClone := ecs.TaskDefinition{}
Expand All @@ -64,7 +73,7 @@ func copyTd(input ecs.TaskDefinition, tags []*ecs.Tag) ecs.RegisterTaskDefinitio
output.TaskRoleArn = inputClone.TaskRoleArn
output.Volumes = inputClone.Volumes
// can't be replaced with reflection
output.Tags = tags
output.Tags = ifEmptyThenNil(tags)
return output
}

Expand Down Expand Up @@ -161,7 +170,7 @@ func alterSecret(copy ecs.ContainerDefinition, secretMap map[string]string) ecs.
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) {
output, err := api.DescribeTaskDefinition(&ecs.DescribeTaskDefinitionInput{TaskDefinition: aws.String(taskdef)})
if err != nil {
return "", err
return "", fmt.Errorf("on copy task definition while describe existing task definition: %w", err)
}

asRegisterTaskDefinitionInput := copyTd(*output.TaskDefinition, output.Tags)
Expand All @@ -176,7 +185,7 @@ func copyTaskDef(api ecsiface.ECSAPI, taskdef string, imageMap map[string]string
}
tdNew, err := api.RegisterTaskDefinition(&tdCopy)
if err != nil {
return "", err
return "", fmt.Errorf("on copy task definition while register new task definition: %w", err)
}
taskDefinitionArn := tdNew.TaskDefinition.TaskDefinitionArn
return *taskDefinitionArn, nil
Expand All @@ -185,7 +194,7 @@ func copyTaskDef(api ecsiface.ECSAPI, taskdef string, imageMap map[string]string
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) {
output, err := api.DescribeServices(&ecs.DescribeServicesInput{Cluster: aws.String(cluster), Services: []*string{aws.String(service)}})
if err != nil {
return ecs.Service{}, ecs.Service{}, err
return ecs.Service{}, ecs.Service{}, fmt.Errorf("on alter service while describe service: %w", err)
}
copyTaskDefinitionAction := func(sourceTaskDefinition string) (string, error) {
return copyTaskDef(api, sourceTaskDefinition, imageMap, envMaps, secretMaps, logopts, logsecrets, taskRole)
Expand Down Expand Up @@ -267,7 +276,7 @@ func validateDraining(ecsapi ecsiface.ECSAPI, elbv2api elbv2iface.ELBV2API, ecsS
describeEcsOutput, err := ecsapi.DescribeServices(&ecs.DescribeServicesInput{Cluster: ecsService.ClusterArn, Services: []*string{ecsService.ServiceName}})

if err != nil {
return backoff.Permanent(err)
return backoff.Permanent(fmt.Errorf("on validate draining while describe service: %w", err))
}
if len(describeEcsOutput.Services) == 0 {
return backoff.Permanent(ErrServiceNotFound)
Expand Down Expand Up @@ -334,7 +343,7 @@ func validateDeployment(api ecsiface.ECSAPI, _ elbv2iface.ELBV2API, ecsService e
operation := func() error {
output, err = api.DescribeServices(&ecs.DescribeServicesInput{Cluster: ecsService.ClusterArn, Services: []*string{ecsService.ServiceName}})
if err != nil {
return err
return fmt.Errorf("on validate deployment while describe service: %w", err)
}
for _, svc := range output.Services {
for _, deployment := range svc.Deployments {
Expand Down
44 changes: 44 additions & 0 deletions ecs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,47 @@ func TestUpdateService(t *testing.T) {
})
}
}

func Test_ifEmptyThenNil(t *testing.T) {
type args struct {
tags []*ecs.Tag
}
tag := ecs.Tag{
Key: aws.String("key"),
Value: aws.String("value"),
}
tests := []struct {
name string
args args
want []*ecs.Tag
}{
{
name: "if empty then nil",
args: args{
tags: []*ecs.Tag{},
},
want: nil,
},
{
name: "if nil then nil",
args: args{
tags: nil,
},
want: nil,
},
{
name: "if not empty then not nil",
args: args{
tags: []*ecs.Tag{&tag},
},
want: []*ecs.Tag{&tag},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ifEmptyThenNil(tt.args.tags); !reflect.DeepEqual(got, tt.want) {
t.Errorf("ifEmptyThenNil() = %v, want %v", got, tt.want)
}
})
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/Autodesk/go-awsecs
go 1.15

require (
github.com/aws/aws-sdk-go v1.35.7
github.com/aws/aws-sdk-go v1.44.174
github.com/cenkalti/backoff v0.0.0-00010101000000-000000000000
github.com/sergi/go-diff v1.1.0
)
Expand Down
36 changes: 28 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
github.com/aws/aws-sdk-go v1.35.7 h1:FHMhVhyc/9jljgFAcGkQDYjpC9btM0B8VfkLBfctdNE=
github.com/aws/aws-sdk-go v1.35.7/go.mod h1:tlPOdRjfxPBpNIwqDj61rmsnA85v9jc0Ps9+muhnW+k=
github.com/aws/aws-sdk-go v1.44.174 h1:9lR4a6MKQW/t6YCG0ZKAt1GAkjdEPP8sWch/pfcuR0c=
github.com/aws/aws-sdk-go v1.44.174/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI=
github.com/cenkalti/backoff/v4 v4.1.0 h1:c8LkOFQTzuO0WBM/ae5HdGQuZPfPxp7lqBRwQRm4fSc=
github.com/cenkalti/backoff/v4 v4.1.0/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -23,17 +22,38 @@ github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNX
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20200202094626-16171245cfb2 h1:CCH4IOTTfewWjGOlSp+zGcjutRKlBEZQ6wTn8ozI/nI=
golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.1.0 h1:hZ/3BUoy5aId7sCpA/Tc5lt8DkFgdVS2onTpJsZ/fl0=
golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.4.0 h1:BrVqGRd7+k1DiOgtnFvAkoQEWQvBc25ouMJM6429SFg=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
Expand Down
2 changes: 1 addition & 1 deletion make.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ do
windows_ext=.exe
for goos in linux windows darwin; do
# shellcheck disable=SC2043
for goarch in amd64; do
for goarch in amd64 arm64; do
ext="${goos}_ext"
out_no_ext="${dir}-${goos}-${goarch}"
out="${dir}${!ext:-}"
Expand Down

0 comments on commit 79a6d64

Please sign in to comment.