Skip to content

Commit

Permalink
fix: 🐛 error on cpu and memory combination
Browse files Browse the repository at this point in the history
  • Loading branch information
shufo committed Feb 11, 2023
1 parent 156241d commit 9897b84
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 14 deletions.
14 changes: 12 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ func run(args []string) error {
Aliases: []string{"t"},
Required: true,
},
&cli.Int64Flag{
&cli.Uint64Flag{
Name: "cpu",
Aliases: []string{"C"},
Required: false,
},
&cli.Int64Flag{
&cli.Uint64Flag{
Name: "memory",
Aliases: []string{"m"},
Required: false,
Expand All @@ -101,6 +101,16 @@ func run(args []string) error {
os.Exit(1)
}

if (c.Uint64("cpu") > 0 || c.Uint64("memory") > 0) && (c.Uint64("cpu") == 0 || c.Uint64("memory") == 0) {
log.Errorln("Both --cpu and --memory must be specified. Only one of them was specified.")
os.Exit(1)
}

if !tasks.ValidateCombinationOfCpuAndMemory(c.Uint64("cpu"), c.Uint64("memory")) {
log.Errorln("The combination of cpu and memory value is invalid.\nCheck https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-cpu-memory-error.html for available combination")
os.Exit(1)
}

if c.Bool("verbose") {
log.SetLevel(log.InfoLevel)
}
Expand Down
70 changes: 58 additions & 12 deletions tasks/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,59 @@ func RunTask(c *cli.Context) {
commands := c.Args().Slice()
service := getService(c, ecsSvc)

taskDefinition := describeTaskDefinition(c, ecsSvc, *latestDefinition)

containerOverrides := []*ecs.ContainerOverride{
{
Name: aws.String(container),
Command: aws.StringSlice(commands),
},
}

if cpu != 0 && memory != 0 {
containerOverrides[0].Cpu = aws.Int64(cpu)
containerOverrides[0].Memory = aws.Int64(memory)

for i := 0; i < len(taskDefinition.ContainerDefinitions); i++ {
containerDefinition := taskDefinition.ContainerDefinitions[i]
if *containerDefinition.Name != container {
taskDefinition.ContainerDefinitions[i].Cpu = aws.Int64(0)
taskDefinition.ContainerDefinitions[i].Memory = aws.Int64(0)
}

}

for i := 0; i < len(taskDefinition.ContainerDefinitions); i++ {
containerDefinition := taskDefinition.ContainerDefinitions[i]
if *containerDefinition.Name != container {
containerOverrides = append(containerOverrides, &ecs.ContainerOverride{
Name: containerDefinition.Name,
Cpu: containerDefinition.Cpu,
Memory: containerDefinition.Memory,
})
}
}
}

overrides := &ecs.TaskOverride{
ContainerOverrides: containerOverrides,
}

if cpu != 0 && memory != 0 {
overrides.Cpu = aws.String(strconv.FormatInt(cpu, 10))
overrides.Memory = aws.String(strconv.FormatInt(memory, 10))
}

log.Debugln("Task Overrides: ", overrides)

results, err := ecsSvc.RunTask(&ecs.RunTaskInput{
Count: aws.Int64(1),
Cluster: aws.String(cluster),
LaunchType: aws.String("FARGATE"),
NetworkConfiguration: &ecs.NetworkConfiguration{
AwsvpcConfiguration: service.NetworkConfiguration.AwsvpcConfiguration,
},
Overrides: &ecs.TaskOverride{
ContainerOverrides: []*ecs.ContainerOverride{
{
Name: aws.String(container),
Command: aws.StringSlice(commands),
Cpu: aws.Int64(cpu),
Memory: aws.Int64(memory),
},
},
Cpu: aws.String(strconv.FormatInt(cpu, 10)),
Memory: aws.String(strconv.FormatInt(memory, 10)),
},
Overrides: overrides,
TaskDefinition: latestDefinition,
})

Expand Down Expand Up @@ -132,3 +166,15 @@ func getService(c *cli.Context, svc *ecs.ECS) *ecs.Service {

return res.Services[0]
}

func describeTaskDefinition(c *cli.Context, svc *ecs.ECS, taskDefinition string) *ecs.TaskDefinition {
res, err := svc.DescribeTaskDefinition(&ecs.DescribeTaskDefinitionInput{
TaskDefinition: &taskDefinition,
})

if err != nil {
log.Fatal(err)
}

return res.TaskDefinition
}
65 changes: 65 additions & 0 deletions tasks/validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package tasks

func ValidateCombinationOfCpuAndMemory(cpu uint64, memory uint64) bool {
if cpu == 0 && memory == 0 {
return true
}

if cpu == 256 {
if memory >= 512 && memory <= 2048 {
return true
}

return false
}

if cpu == 512 {
if memory >= 1024 && memory <= 4096 {
return true
}

return false
}

if cpu == 1024 {
if memory >= 2048 && memory <= 8192 {
return true
}

return false
}

if cpu == 2048 {
if memory >= 4096 && memory <= 16384 {
return true
}

return false
}

if cpu == 4096 {
if memory >= 8192 && memory <= 30720 {
return true
}

return false
}

if cpu == 8192 {
if memory >= 16384 && memory <= 61440 {
return true
}

return false
}

if cpu == 16384 {
if memory >= 32768 && memory <= 122880 {
return true
}

return false
}

return false
}
42 changes: 42 additions & 0 deletions tasks/validate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package tasks

import (
"testing"
)

func TestValidateCpuAndMemory(t *testing.T) {
cases := []struct {
cpu uint64
memory uint64
expect bool
}{
{
cpu: 0,
memory: 0,
expect: true,
},
{
cpu: 128,
memory: 128,
expect: false,
},
{
cpu: 256,
memory: 512,
expect: true,
},
{
cpu: 1024,
memory: 2048,
expect: true,
},
}

for _, c := range cases {
result := ValidateCombinationOfCpuAndMemory(c.cpu, c.memory)

if result != c.expect {
t.Fail()
}
}
}

0 comments on commit 9897b84

Please sign in to comment.