Skip to content

Commit bfea559

Browse files
dicksbuPettitWesley
authored andcommitted
in hashing task definitions also sort containers by Name
1 parent a2b38f3 commit bfea559

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

ecs-cli/modules/cli/compose/adapter/convert.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"fmt"
1919
"reflect"
2020
"regexp"
21+
"sort"
2122
"strconv"
2223
"strings"
2324

@@ -504,6 +505,17 @@ func SortedGoString(v interface{}) (string, error) {
504505
return string(b), nil
505506
}
506507

508+
// Necessary in conjunction with SortedGoString to avoid spurious tdcache misses
509+
func SortedContainerDefinitionsByName(request *ecs.RegisterTaskDefinitionInput) ecs.RegisterTaskDefinitionInput {
510+
cdefs := request.ContainerDefinitions
511+
sort.Slice(cdefs, func(i, j int) bool {
512+
return *cdefs[i].Name < *cdefs[j].Name
513+
})
514+
sorted := *request
515+
sorted.ContainerDefinitions = cdefs
516+
return sorted
517+
}
518+
507519
// ConvertCamelCaseToUnderScore returns an underscore-separated name for a given camelcased string
508520
// e.g., "NetworkMode" -> "network_mode"
509521
func ConvertCamelCaseToUnderScore(s string) string {

ecs-cli/modules/cli/compose/adapter/convert_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ package adapter
1515

1616
import (
1717
"fmt"
18+
"math/rand"
1819
"strconv"
20+
"strings"
1921
"testing"
2022

2123
"github.com/aws/aws-sdk-go/aws"
@@ -563,6 +565,48 @@ func TestConvertToVolumes_ErrorsWithExternalSubfield(t *testing.T) {
563565
assert.Error(t, err, "Expected error converting libcompose volume configs when external is specified")
564566
}
565567

568+
func TestRegisterTaskDefinitionInputEquivalence(t *testing.T) {
569+
family := aws.String("family1")
570+
dockerLabels := map[string]string{
571+
"label1": "",
572+
"com.foo.label2": "value",
573+
}
574+
cdefs := []*ecs.ContainerDefinition{}
575+
N := 10
576+
for i := 0; i < N; i++ {
577+
command := make([]string, i+1)
578+
for j := 0; j < i+1; j++ {
579+
command[j] = strings.Repeat(string(rune(65+j)), i+1)
580+
}
581+
cdefs = append(cdefs, &ecs.ContainerDefinition{
582+
Name: aws.String(strings.Repeat(string(rune(65+i)), i+1)),
583+
Command: aws.StringSlice(command),
584+
DockerLabels: aws.StringMap(dockerLabels),
585+
})
586+
}
587+
inputA := ecs.RegisterTaskDefinitionInput{
588+
Family: family,
589+
ContainerDefinitions: cdefs,
590+
}
591+
592+
shuffle_cdefs := make([]*ecs.ContainerDefinition, len(cdefs))
593+
for i, v := range rand.Perm(len(cdefs)) {
594+
shuffle_cdefs[v] = cdefs[i]
595+
}
596+
597+
inputB := ecs.RegisterTaskDefinitionInput{
598+
ContainerDefinitions: shuffle_cdefs,
599+
Family: family,
600+
}
601+
602+
strA, err := SortedGoString(SortedContainerDefinitionsByName(&inputA))
603+
assert.NoError(t, err, "Unexpected error generating sorted map string")
604+
strB, err := SortedGoString(SortedContainerDefinitionsByName(&inputB))
605+
assert.NoError(t, err, "Unexpected error generating sorted map string")
606+
607+
assert.Equal(t, strA, strB, "Sorted inputs should match")
608+
}
609+
566610
func TestSortedGoString(t *testing.T) {
567611
family := aws.String("family1")
568612
name := aws.String("foo")

ecs-cli/modules/clients/aws/ecs/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ func (c *ecsClient) constructTaskDefinitionCacheHash(taskDefinition *ecs.TaskDef
310310
// Get the region from the ecsClient configuration
311311
region := aws.StringValue(c.config.Session.Config.Region)
312312
awsUserAccountId := utils.GetAwsAccountIdFromArn(aws.StringValue(taskDefinition.TaskDefinitionArn))
313-
sortedRequestString, err := adapter.SortedGoString(request)
313+
sortedRequestString, err := adapter.SortedGoString(adapter.SortedContainerDefinitionsByName(request))
314314
if err != nil {
315315
log.WithFields(log.Fields{
316316
"error": err,

0 commit comments

Comments
 (0)