-
Notifications
You must be signed in to change notification settings - Fork 604
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add method to get host resources reserved for a task #3706
Merged
prateekchaudhry
merged 2 commits into
aws:feature/task-resource-accounting
from
prateekchaudhry:taskResourceMethod
May 24, 2023
Merged
Changes from all commits
Commits
Show all changes
2 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
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 |
---|---|---|
|
@@ -45,6 +45,7 @@ import ( | |
"github.com/aws/amazon-ecs-agent/agent/dockerclient" | ||
"github.com/aws/amazon-ecs-agent/agent/dockerclient/dockerapi" | ||
mock_dockerapi "github.com/aws/amazon-ecs-agent/agent/dockerclient/dockerapi/mocks" | ||
"github.com/aws/amazon-ecs-agent/agent/ecs_client/model/ecs" | ||
mock_s3_factory "github.com/aws/amazon-ecs-agent/agent/s3/factory/mocks" | ||
mock_ssm_factory "github.com/aws/amazon-ecs-agent/agent/ssm/factory/mocks" | ||
"github.com/aws/amazon-ecs-agent/agent/taskresource" | ||
|
@@ -4802,3 +4803,196 @@ func TestInitializeAndGetCredentialSpecResource(t *testing.T) { | |
_, ok := task.GetCredentialSpecResource() | ||
assert.True(t, ok) | ||
} | ||
|
||
func getTestTaskResourceMap(cpu int64, mem int64, ports []*string, portsUdp []*string, numGPUs int64) map[string]*ecs.Resource { | ||
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. this is a well-written test case 🥇 |
||
taskResources := make(map[string]*ecs.Resource) | ||
taskResources["CPU"] = &ecs.Resource{ | ||
Name: utils.Strptr("CPU"), | ||
Type: utils.Strptr("INTEGER"), | ||
IntegerValue: &cpu, | ||
} | ||
|
||
taskResources["MEMORY"] = &ecs.Resource{ | ||
Name: utils.Strptr("MEMORY"), | ||
Type: utils.Strptr("INTEGER"), | ||
IntegerValue: &mem, | ||
} | ||
|
||
taskResources["PORTS_TCP"] = &ecs.Resource{ | ||
Name: utils.Strptr("PORTS_TCP"), | ||
Type: utils.Strptr("STRINGSET"), | ||
StringSetValue: ports, | ||
} | ||
|
||
taskResources["PORTS_UDP"] = &ecs.Resource{ | ||
Name: utils.Strptr("PORTS_UDP"), | ||
Type: utils.Strptr("STRINGSET"), | ||
StringSetValue: portsUdp, | ||
} | ||
|
||
taskResources["GPU"] = &ecs.Resource{ | ||
Name: utils.Strptr("GPU"), | ||
Type: utils.Strptr("INTEGER"), | ||
IntegerValue: &numGPUs, | ||
} | ||
|
||
return taskResources | ||
} | ||
|
||
func TestToHostResources(t *testing.T) { | ||
//Prepare a simple hostConfig with memory reservation field for test cases | ||
hostConfig := dockercontainer.HostConfig{ | ||
// 400 MiB | ||
Resources: dockercontainer.Resources{ | ||
MemoryReservation: int64(419430400), | ||
}, | ||
} | ||
|
||
rawHostConfig, err := json.Marshal(&hostConfig) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// Prefer task level, and check gpu assignment | ||
testTask1 := &Task{ | ||
CPU: 1.0, | ||
Memory: int64(512), | ||
Containers: []*apicontainer.Container{ | ||
{ | ||
CPU: uint(1200), | ||
Memory: uint(1200), | ||
DockerConfig: apicontainer.DockerConfig{ | ||
HostConfig: strptr(string(rawHostConfig)), | ||
}, | ||
GPUIDs: []string{"gpu1", "gpu2"}, | ||
}, | ||
}, | ||
} | ||
|
||
// If task not set, use container level (MemoryReservation pref) | ||
testTask2 := &Task{ | ||
Containers: []*apicontainer.Container{ | ||
{ | ||
CPU: uint(1200), | ||
Memory: uint(1200), | ||
DockerConfig: apicontainer.DockerConfig{ | ||
HostConfig: strptr(string(rawHostConfig)), | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
// If task not set, if MemoryReservation not set, use container level hard limit (c.Memory) | ||
testTask3 := &Task{ | ||
Containers: []*apicontainer.Container{ | ||
{ | ||
CPU: uint(1200), | ||
Memory: uint(1200), | ||
DockerConfig: apicontainer.DockerConfig{}, | ||
}, | ||
}, | ||
} | ||
|
||
// Check ports | ||
testTask4 := &Task{ | ||
CPU: 1.0, | ||
Memory: int64(512), | ||
Containers: []*apicontainer.Container{ | ||
{ | ||
CPU: uint(1200), | ||
Memory: uint(1200), | ||
DockerConfig: apicontainer.DockerConfig{ | ||
HostConfig: strptr(string(rawHostConfig)), | ||
}, | ||
Ports: []apicontainer.PortBinding{ | ||
{ | ||
ContainerPort: 10, | ||
HostPort: 10, | ||
BindIP: "", | ||
Protocol: apicontainer.TransportProtocolTCP, | ||
}, | ||
{ | ||
ContainerPort: 20, | ||
HostPort: 20, | ||
BindIP: "", | ||
Protocol: apicontainer.TransportProtocolUDP, | ||
}, | ||
{ | ||
ContainerPortRange: "99-999", | ||
BindIP: "", | ||
Protocol: apicontainer.TransportProtocolTCP, | ||
}, | ||
{ | ||
ContainerPortRange: "121-221", | ||
BindIP: "", | ||
Protocol: apicontainer.TransportProtocolUDP, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
portsTCP := []uint16{10} | ||
portsUDP := []uint16{20} | ||
|
||
testCases := []struct { | ||
task *Task | ||
expectedResources map[string]*ecs.Resource | ||
}{ | ||
{ | ||
task: testTask1, | ||
expectedResources: getTestTaskResourceMap(int64(1024), int64(512), []*string{}, []*string{}, int64(2)), | ||
}, | ||
{ | ||
task: testTask2, | ||
expectedResources: getTestTaskResourceMap(int64(1200), int64(400), []*string{}, []*string{}, int64(0)), | ||
}, | ||
{ | ||
task: testTask3, | ||
expectedResources: getTestTaskResourceMap(int64(1200), int64(1200), []*string{}, []*string{}, int64(0)), | ||
}, | ||
{ | ||
task: testTask4, | ||
expectedResources: getTestTaskResourceMap(int64(1024), int64(512), utils.Uint16SliceToStringSlice(portsTCP), utils.Uint16SliceToStringSlice(portsUDP), int64(0)), | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
calcResources := tc.task.ToHostResources() | ||
|
||
//CPU | ||
assert.Equal(t, tc.expectedResources["CPU"].IntegerValue, calcResources["CPU"].IntegerValue, "Error converting task CPU tesources") | ||
|
||
//MEMORY | ||
assert.Equal(t, tc.expectedResources["MEMORY"].IntegerValue, calcResources["MEMORY"].IntegerValue, "Error converting task Memory tesources") | ||
|
||
//GPU | ||
assert.Equal(t, tc.expectedResources["GPU"].IntegerValue, calcResources["GPU"].IntegerValue, "Error converting task GPU tesources") | ||
|
||
//PORTS | ||
for _, expectedPort := range tc.expectedResources["PORTS_TCP"].StringSetValue { | ||
found := false | ||
for _, calcPort := range calcResources["PORTS_TCP"].StringSetValue { | ||
if *expectedPort == *calcPort { | ||
found = true | ||
break | ||
} | ||
} | ||
assert.True(t, found, "Could not convert TCP port resources") | ||
} | ||
assert.Equal(t, len(tc.expectedResources["PORTS_TCP"].StringSetValue), len(calcResources["PORTS_TCP"].StringSetValue), "Error converting task TCP port tesources") | ||
|
||
//PORTS_UDP | ||
for _, expectedPort := range tc.expectedResources["PORTS_UDP"].StringSetValue { | ||
found := false | ||
for _, calcPort := range calcResources["PORTS_UDP"].StringSetValue { | ||
if *expectedPort == *calcPort { | ||
found = true | ||
break | ||
} | ||
} | ||
assert.True(t, found, "Could not convert UDP port resources") | ||
} | ||
assert.Equal(t, len(tc.expectedResources["PORTS_UDP"].StringSetValue), len(calcResources["PORTS_UDP"].StringSetValue), "Error converting task UDP port tesources") | ||
} | ||
} |
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.
Just out of curious, is there a case that MemoryReservation actually less than 0? How did we know it's unit with unmarshal?
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 don't see a scenario where it will be less than 0. Units are mentioned in comments here, also verified by manual runs.
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.
int64
implies that this is a signed integer (ie could be negative). You might also add another check to validate that the HostConfig is an int.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.
MemoryReservation is the soft limit we are concerned with here, which is part of the HostConfig. I believe we are already checking if it is zero or negative by the check here
hostConfig.MemoryReservation <= 0
, so should be good imo.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.
And that MemoryReservation field in the HostConfig is zero-initialized by default so we'll always have it if we don't have a null HostConfig. Resolving.
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.
Yes that's right, thanks!