This repository has been archived by the owner on Jan 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 330
AWS ECS: validate memory + cpu pairs #1872
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
```release-note:bug | ||
serverinstall/ecs: validate memory and cpu values | ||
``` | ||
|
||
```release-note:bug | ||
plugin/aws/ecs: validate memory and cpu values | ||
``` | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,7 +65,6 @@ func (p *Platform) ConfigSet(config interface{}) error { | |
validation.Empty.When(alb.CertificateId != "" || alb.ZoneId != "" || alb.FQDN != "").Error("listener_arn can not be used with other options"), | ||
), | ||
)) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
@@ -342,7 +341,6 @@ func defaultSubnets(ctx context.Context, sess *session.Session) ([]*string, erro | |
}, | ||
}, | ||
}) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
@@ -367,7 +365,6 @@ func (p *Platform) SetupCluster(ctx context.Context, s LifecycleStatus, sess *se | |
desc, err := ecsSvc.DescribeClusters(&ecs.DescribeClustersInput{ | ||
Clusters: []*string{aws.String(cluster)}, | ||
}) | ||
|
||
if err != nil { | ||
return "", err | ||
} | ||
|
@@ -529,7 +526,6 @@ func (p *Platform) SetupLogs(ctx context.Context, s LifecycleStatus, L hclog.Log | |
Limit: aws.Int64(1), | ||
LogGroupNamePrefix: aws.String(logGroup), | ||
}) | ||
|
||
if err != nil { | ||
return "", err | ||
} | ||
|
@@ -549,7 +545,6 @@ func (p *Platform) SetupLogs(ctx context.Context, s LifecycleStatus, L hclog.Log | |
} | ||
|
||
return logGroup, nil | ||
|
||
} | ||
|
||
func createSG( | ||
|
@@ -570,7 +565,6 @@ func createSG( | |
}, | ||
}, | ||
}) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
@@ -780,7 +774,6 @@ func createALB( | |
}, | ||
}, | ||
}) | ||
|
||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
@@ -1063,58 +1056,26 @@ func (p *Platform) Launch( | |
L.Debug("registering task definition", "id", id) | ||
|
||
var cpuShares int | ||
family := "waypoint-" + app.App | ||
|
||
s.Status("Registering Task definition: %s", family) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: I moved this because it's the first status update in this method, and without moving it (or adding another one) when the validation fails it looks like the previous step failed, and not this one |
||
|
||
runtime := aws.String("FARGATE") | ||
if p.config.EC2Cluster { | ||
runtime = aws.String("EC2") | ||
cpuShares = p.config.CPU | ||
} else { | ||
if p.config.Memory == 0 { | ||
return nil, fmt.Errorf("Memory value required for fargate") | ||
if err := utils.ValidateEcsMemCPUPair(p.config.Memory, p.config.CPU); err != nil { | ||
return nil, err | ||
} | ||
cpuValues, ok := fargateResources[p.config.Memory] | ||
if !ok { | ||
var ( | ||
allValues []int | ||
goodValues []string | ||
) | ||
|
||
for k := range fargateResources { | ||
allValues = append(allValues, k) | ||
} | ||
|
||
sort.Ints(allValues) | ||
|
||
for _, k := range allValues { | ||
goodValues = append(goodValues, strconv.Itoa(k)) | ||
} | ||
cpuValues := fargateResources[p.config.Memory] | ||
|
||
return nil, fmt.Errorf("Invalid memory value: %d (valid values: %s)", | ||
p.config.Memory, strings.Join(goodValues, ", ")) | ||
} | ||
|
||
if p.config.CPU == 0 { | ||
// at this point we know that config.CPU is either 0, or a valid value | ||
// for the memory given | ||
cpuShares = p.config.CPU | ||
if cpuShares == 0 { | ||
cpuShares = cpuValues[0] | ||
} else { | ||
var ( | ||
valid bool | ||
goodValues []string | ||
) | ||
|
||
for _, c := range cpuValues { | ||
goodValues = append(goodValues, strconv.Itoa(c)) | ||
if c == p.config.CPU { | ||
valid = true | ||
break | ||
} | ||
} | ||
|
||
if !valid { | ||
return nil, fmt.Errorf("Invalid cpu value: %d (valid values: %s)", | ||
p.config.Memory, strings.Join(goodValues, ", ")) | ||
} | ||
|
||
cpuShares = p.config.CPU | ||
} | ||
} | ||
|
||
|
@@ -1125,10 +1086,6 @@ func (p *Platform) Launch( | |
} | ||
mems := strconv.Itoa(p.config.Memory) | ||
|
||
family := "waypoint-" + app.App | ||
|
||
s.Status("Registering Task definition: %s", family) | ||
|
||
containerDefinitions := append([]*ecs.ContainerDefinition{&def}, additionalContainers...) | ||
|
||
registerTaskDefinitionInput := ecs.RegisterTaskDefinitionInput{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package utils | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestValidateEcsMemCPUPair(t *testing.T) { | ||
// test values based off of | ||
// https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-cpu-memory-error.html | ||
// circa July 15, 2021 | ||
|
||
cases := map[string]struct { | ||
mem int | ||
cpu int | ||
shouldErr bool | ||
}{ | ||
"zeros": { | ||
shouldErr: true, | ||
}, | ||
"512/0": { | ||
mem: 512, | ||
}, | ||
"512/256": { | ||
mem: 512, | ||
cpu: 256, | ||
}, | ||
"4096": { | ||
mem: 4096, | ||
}, | ||
"4096/512": { | ||
mem: 4096, | ||
cpu: 512, | ||
}, | ||
"4096/256": { | ||
mem: 4096, | ||
cpu: 256, | ||
shouldErr: true, | ||
}, | ||
"512/512": { | ||
mem: 512, | ||
cpu: 512, | ||
shouldErr: true, | ||
}, | ||
"nonsense": { | ||
mem: 7, | ||
shouldErr: true, | ||
}, | ||
"bad_pair": { | ||
mem: 512, | ||
cpu: 512, | ||
shouldErr: true, | ||
}, | ||
"zero_mem": { | ||
cpu: 7, | ||
shouldErr: true, | ||
}, | ||
} | ||
|
||
for name, c := range cases { | ||
t.Run(name, func(t *testing.T) { | ||
if err := ValidateEcsMemCPUPair(c.mem, c.cpu); err != nil { | ||
if !c.shouldErr { | ||
t.Error(err) | ||
} | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these should be in one block, mostly because I think go-changelog only supports one block per file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can have multiple entries in a single go-changelog txt file! https://github.com/hashicorp/waypoint/blob/main/.github/CHANGELOG_GUIDE.md#multiple-entries
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.