Skip to content

Commit 871e095

Browse files
committed
Revert "Add method to get host resources reserved for a task (aws#3706)"
This reverts commit 67dcd21.
1 parent 854b8a8 commit 871e095

File tree

2 files changed

+0
-331
lines changed

2 files changed

+0
-331
lines changed

agent/api/task/task.go

-137
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,13 @@ import (
2525
"time"
2626

2727
apiappmesh "github.com/aws/amazon-ecs-agent/agent/api/appmesh"
28-
"github.com/aws/amazon-ecs-agent/agent/api/container"
2928
apicontainer "github.com/aws/amazon-ecs-agent/agent/api/container"
3029
apicontainerstatus "github.com/aws/amazon-ecs-agent/agent/api/container/status"
3130
"github.com/aws/amazon-ecs-agent/agent/api/serviceconnect"
3231
apitaskstatus "github.com/aws/amazon-ecs-agent/agent/api/task/status"
3332
"github.com/aws/amazon-ecs-agent/agent/config"
3433
"github.com/aws/amazon-ecs-agent/agent/dockerclient"
3534
"github.com/aws/amazon-ecs-agent/agent/dockerclient/dockerapi"
36-
"github.com/aws/amazon-ecs-agent/agent/ecs_client/model/ecs"
3735
"github.com/aws/amazon-ecs-agent/agent/taskresource"
3836
"github.com/aws/amazon-ecs-agent/agent/taskresource/asmauth"
3937
"github.com/aws/amazon-ecs-agent/agent/taskresource/asmsecret"
@@ -3527,138 +3525,3 @@ func (task *Task) IsServiceConnectConnectionDraining() bool {
35273525
defer task.lock.RUnlock()
35283526
return task.ServiceConnectConnectionDrainingUnsafe
35293527
}
3530-
3531-
// ToHostResources will convert a task to a map of resources which ECS takes into account when scheduling tasks on instances
3532-
// * CPU
3533-
// - If task level CPU is set, use that
3534-
// - Else add up container CPUs
3535-
//
3536-
// * Memory
3537-
// - If task level memory is set, use that
3538-
// - Else add up container level
3539-
// - If memoryReservation field is set, use that
3540-
// - Else use memory field
3541-
//
3542-
// * Ports (TCP/UDP)
3543-
// - Only account for hostPort
3544-
// - Don't need to account for awsvpc mode, each task gets its own namespace
3545-
//
3546-
// * GPU
3547-
// - Return num of gpus requested (len of GPUIDs field)
3548-
//
3549-
// TODO remove this once ToHostResources is used
3550-
//
3551-
//lint:file-ignore U1000 Ignore all unused code
3552-
func (task *Task) ToHostResources() map[string]*ecs.Resource {
3553-
resources := make(map[string]*ecs.Resource)
3554-
// CPU
3555-
if task.CPU > 0 {
3556-
// cpu unit is vcpu at task level
3557-
// convert to cpushares
3558-
taskCPUint64 := int64(task.CPU * 1024)
3559-
resources["CPU"] = &ecs.Resource{
3560-
Name: utils.Strptr("CPU"),
3561-
Type: utils.Strptr("INTEGER"),
3562-
IntegerValue: &taskCPUint64,
3563-
}
3564-
} else {
3565-
// cpu unit is cpushares at container level
3566-
containerCPUint64 := int64(0)
3567-
for _, container := range task.Containers {
3568-
containerCPUint64 += int64(container.CPU)
3569-
}
3570-
resources["CPU"] = &ecs.Resource{
3571-
Name: utils.Strptr("CPU"),
3572-
Type: utils.Strptr("INTEGER"),
3573-
IntegerValue: &containerCPUint64,
3574-
}
3575-
}
3576-
3577-
// Memory
3578-
if task.Memory > 0 {
3579-
// memory unit is MiB at task level
3580-
taskMEMint64 := task.Memory
3581-
resources["MEMORY"] = &ecs.Resource{
3582-
Name: utils.Strptr("MEMORY"),
3583-
Type: utils.Strptr("INTEGER"),
3584-
IntegerValue: &taskMEMint64,
3585-
}
3586-
} else {
3587-
containerMEMint64 := int64(0)
3588-
// To parse memory reservation / soft limit
3589-
hostConfig := &dockercontainer.HostConfig{}
3590-
3591-
for _, c := range task.Containers {
3592-
if c.DockerConfig.HostConfig != nil {
3593-
err := json.Unmarshal([]byte(*c.DockerConfig.HostConfig), hostConfig)
3594-
if err != nil || hostConfig.MemoryReservation <= 0 {
3595-
// container memory unit is MiB, keeping as is
3596-
containerMEMint64 += int64(c.Memory)
3597-
} else {
3598-
// Soft limit is specified in MiB units but translated to bytes while being transferred to Agent
3599-
// Converting back to MiB
3600-
containerMEMint64 += hostConfig.MemoryReservation / (1024 * 1024)
3601-
}
3602-
} else {
3603-
// container memory unit is MiB, keeping as is
3604-
containerMEMint64 += int64(c.Memory)
3605-
}
3606-
}
3607-
resources["MEMORY"] = &ecs.Resource{
3608-
Name: utils.Strptr("MEMORY"),
3609-
Type: utils.Strptr("INTEGER"),
3610-
IntegerValue: &containerMEMint64,
3611-
}
3612-
}
3613-
3614-
// PORTS_TCP and PORTS_UDP
3615-
var tcpPortSet []uint16
3616-
var udpPortSet []uint16
3617-
3618-
// AWSVPC tasks have 'host' ports mapped to task ENI, not to host
3619-
// So don't need to keep an 'account' of awsvpc tasks with host ports fields assigned
3620-
if !task.IsNetworkModeAWSVPC() {
3621-
for _, c := range task.Containers {
3622-
for _, port := range c.Ports {
3623-
hostPort := port.HostPort
3624-
protocol := port.Protocol
3625-
if hostPort > 0 && protocol == container.TransportProtocolTCP {
3626-
tcpPortSet = append(tcpPortSet, hostPort)
3627-
} else if hostPort > 0 && protocol == container.TransportProtocolUDP {
3628-
udpPortSet = append(udpPortSet, hostPort)
3629-
}
3630-
}
3631-
}
3632-
}
3633-
resources["PORTS_TCP"] = &ecs.Resource{
3634-
Name: utils.Strptr("PORTS_TCP"),
3635-
Type: utils.Strptr("STRINGSET"),
3636-
StringSetValue: utils.Uint16SliceToStringSlice(tcpPortSet),
3637-
}
3638-
resources["PORTS_UDP"] = &ecs.Resource{
3639-
Name: utils.Strptr("PORTS_UDP"),
3640-
Type: utils.Strptr("STRINGSET"),
3641-
StringSetValue: utils.Uint16SliceToStringSlice(udpPortSet),
3642-
}
3643-
3644-
// GPU
3645-
var num_gpus int64
3646-
num_gpus = 0
3647-
for _, c := range task.Containers {
3648-
num_gpus += int64(len(c.GPUIDs))
3649-
}
3650-
resources["GPU"] = &ecs.Resource{
3651-
Name: utils.Strptr("GPU"),
3652-
Type: utils.Strptr("INTEGER"),
3653-
IntegerValue: &num_gpus,
3654-
}
3655-
logger.Debug("Task host resources to account for", logger.Fields{
3656-
"taskArn": task.Arn,
3657-
"CPU": *resources["CPU"].IntegerValue,
3658-
"MEMORY": *resources["MEMORY"].IntegerValue,
3659-
"PORTS_TCP": resources["PORTS_TCP"].StringSetValue,
3660-
"PORTS_UDP": resources["PORTS_UDP"].StringSetValue,
3661-
"GPU": *resources["GPU"].IntegerValue,
3662-
})
3663-
return resources
3664-
}

agent/api/task/task_test.go

-194
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ import (
4242
"github.com/aws/amazon-ecs-agent/agent/dockerclient"
4343
"github.com/aws/amazon-ecs-agent/agent/dockerclient/dockerapi"
4444
mock_dockerapi "github.com/aws/amazon-ecs-agent/agent/dockerclient/dockerapi/mocks"
45-
"github.com/aws/amazon-ecs-agent/agent/ecs_client/model/ecs"
4645
mock_s3_factory "github.com/aws/amazon-ecs-agent/agent/s3/factory/mocks"
4746
mock_ssm_factory "github.com/aws/amazon-ecs-agent/agent/ssm/factory/mocks"
4847
"github.com/aws/amazon-ecs-agent/agent/taskresource"
@@ -4855,196 +4854,3 @@ func TestInitializeAndGetCredentialSpecResource(t *testing.T) {
48554854
_, ok := task.GetCredentialSpecResource()
48564855
assert.True(t, ok)
48574856
}
4858-
4859-
func getTestTaskResourceMap(cpu int64, mem int64, ports []*string, portsUdp []*string, numGPUs int64) map[string]*ecs.Resource {
4860-
taskResources := make(map[string]*ecs.Resource)
4861-
taskResources["CPU"] = &ecs.Resource{
4862-
Name: utils.Strptr("CPU"),
4863-
Type: utils.Strptr("INTEGER"),
4864-
IntegerValue: &cpu,
4865-
}
4866-
4867-
taskResources["MEMORY"] = &ecs.Resource{
4868-
Name: utils.Strptr("MEMORY"),
4869-
Type: utils.Strptr("INTEGER"),
4870-
IntegerValue: &mem,
4871-
}
4872-
4873-
taskResources["PORTS_TCP"] = &ecs.Resource{
4874-
Name: utils.Strptr("PORTS_TCP"),
4875-
Type: utils.Strptr("STRINGSET"),
4876-
StringSetValue: ports,
4877-
}
4878-
4879-
taskResources["PORTS_UDP"] = &ecs.Resource{
4880-
Name: utils.Strptr("PORTS_UDP"),
4881-
Type: utils.Strptr("STRINGSET"),
4882-
StringSetValue: portsUdp,
4883-
}
4884-
4885-
taskResources["GPU"] = &ecs.Resource{
4886-
Name: utils.Strptr("GPU"),
4887-
Type: utils.Strptr("INTEGER"),
4888-
IntegerValue: &numGPUs,
4889-
}
4890-
4891-
return taskResources
4892-
}
4893-
4894-
func TestToHostResources(t *testing.T) {
4895-
//Prepare a simple hostConfig with memory reservation field for test cases
4896-
hostConfig := dockercontainer.HostConfig{
4897-
// 400 MiB
4898-
Resources: dockercontainer.Resources{
4899-
MemoryReservation: int64(419430400),
4900-
},
4901-
}
4902-
4903-
rawHostConfig, err := json.Marshal(&hostConfig)
4904-
if err != nil {
4905-
t.Fatal(err)
4906-
}
4907-
4908-
// Prefer task level, and check gpu assignment
4909-
testTask1 := &Task{
4910-
CPU: 1.0,
4911-
Memory: int64(512),
4912-
Containers: []*apicontainer.Container{
4913-
{
4914-
CPU: uint(1200),
4915-
Memory: uint(1200),
4916-
DockerConfig: apicontainer.DockerConfig{
4917-
HostConfig: strptr(string(rawHostConfig)),
4918-
},
4919-
GPUIDs: []string{"gpu1", "gpu2"},
4920-
},
4921-
},
4922-
}
4923-
4924-
// If task not set, use container level (MemoryReservation pref)
4925-
testTask2 := &Task{
4926-
Containers: []*apicontainer.Container{
4927-
{
4928-
CPU: uint(1200),
4929-
Memory: uint(1200),
4930-
DockerConfig: apicontainer.DockerConfig{
4931-
HostConfig: strptr(string(rawHostConfig)),
4932-
},
4933-
},
4934-
},
4935-
}
4936-
4937-
// If task not set, if MemoryReservation not set, use container level hard limit (c.Memory)
4938-
testTask3 := &Task{
4939-
Containers: []*apicontainer.Container{
4940-
{
4941-
CPU: uint(1200),
4942-
Memory: uint(1200),
4943-
DockerConfig: apicontainer.DockerConfig{},
4944-
},
4945-
},
4946-
}
4947-
4948-
// Check ports
4949-
testTask4 := &Task{
4950-
CPU: 1.0,
4951-
Memory: int64(512),
4952-
Containers: []*apicontainer.Container{
4953-
{
4954-
CPU: uint(1200),
4955-
Memory: uint(1200),
4956-
DockerConfig: apicontainer.DockerConfig{
4957-
HostConfig: strptr(string(rawHostConfig)),
4958-
},
4959-
Ports: []apicontainer.PortBinding{
4960-
{
4961-
ContainerPort: 10,
4962-
HostPort: 10,
4963-
BindIP: "",
4964-
Protocol: apicontainer.TransportProtocolTCP,
4965-
},
4966-
{
4967-
ContainerPort: 20,
4968-
HostPort: 20,
4969-
BindIP: "",
4970-
Protocol: apicontainer.TransportProtocolUDP,
4971-
},
4972-
{
4973-
ContainerPortRange: "99-999",
4974-
BindIP: "",
4975-
Protocol: apicontainer.TransportProtocolTCP,
4976-
},
4977-
{
4978-
ContainerPortRange: "121-221",
4979-
BindIP: "",
4980-
Protocol: apicontainer.TransportProtocolUDP,
4981-
},
4982-
},
4983-
},
4984-
},
4985-
}
4986-
4987-
portsTCP := []uint16{10}
4988-
portsUDP := []uint16{20}
4989-
4990-
testCases := []struct {
4991-
task *Task
4992-
expectedResources map[string]*ecs.Resource
4993-
}{
4994-
{
4995-
task: testTask1,
4996-
expectedResources: getTestTaskResourceMap(int64(1024), int64(512), []*string{}, []*string{}, int64(2)),
4997-
},
4998-
{
4999-
task: testTask2,
5000-
expectedResources: getTestTaskResourceMap(int64(1200), int64(400), []*string{}, []*string{}, int64(0)),
5001-
},
5002-
{
5003-
task: testTask3,
5004-
expectedResources: getTestTaskResourceMap(int64(1200), int64(1200), []*string{}, []*string{}, int64(0)),
5005-
},
5006-
{
5007-
task: testTask4,
5008-
expectedResources: getTestTaskResourceMap(int64(1024), int64(512), utils.Uint16SliceToStringSlice(portsTCP), utils.Uint16SliceToStringSlice(portsUDP), int64(0)),
5009-
},
5010-
}
5011-
5012-
for _, tc := range testCases {
5013-
calcResources := tc.task.ToHostResources()
5014-
5015-
//CPU
5016-
assert.Equal(t, tc.expectedResources["CPU"].IntegerValue, calcResources["CPU"].IntegerValue, "Error converting task CPU tesources")
5017-
5018-
//MEMORY
5019-
assert.Equal(t, tc.expectedResources["MEMORY"].IntegerValue, calcResources["MEMORY"].IntegerValue, "Error converting task Memory tesources")
5020-
5021-
//GPU
5022-
assert.Equal(t, tc.expectedResources["GPU"].IntegerValue, calcResources["GPU"].IntegerValue, "Error converting task GPU tesources")
5023-
5024-
//PORTS
5025-
for _, expectedPort := range tc.expectedResources["PORTS_TCP"].StringSetValue {
5026-
found := false
5027-
for _, calcPort := range calcResources["PORTS_TCP"].StringSetValue {
5028-
if *expectedPort == *calcPort {
5029-
found = true
5030-
break
5031-
}
5032-
}
5033-
assert.True(t, found, "Could not convert TCP port resources")
5034-
}
5035-
assert.Equal(t, len(tc.expectedResources["PORTS_TCP"].StringSetValue), len(calcResources["PORTS_TCP"].StringSetValue), "Error converting task TCP port tesources")
5036-
5037-
//PORTS_UDP
5038-
for _, expectedPort := range tc.expectedResources["PORTS_UDP"].StringSetValue {
5039-
found := false
5040-
for _, calcPort := range calcResources["PORTS_UDP"].StringSetValue {
5041-
if *expectedPort == *calcPort {
5042-
found = true
5043-
break
5044-
}
5045-
}
5046-
assert.True(t, found, "Could not convert UDP port resources")
5047-
}
5048-
assert.Equal(t, len(tc.expectedResources["PORTS_UDP"].StringSetValue), len(calcResources["PORTS_UDP"].StringSetValue), "Error converting task UDP port tesources")
5049-
}
5050-
}

0 commit comments

Comments
 (0)