Skip to content

Commit 1d5b3cc

Browse files
author
Hsing-Hui Hsu
committed
Use generic method for converting memory into MB
1 parent 03d6bdd commit 1d5b3cc

3 files changed

Lines changed: 24 additions & 26 deletions

File tree

‎ecs-cli/modules/cli/compose/project/project_test.go‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,8 @@ func TestParseComposeForVersion2Files(t *testing.T) {
208208
wordpressImage := "wordpress"
209209
mysqlImage := "mysql"
210210
ports := []string{"80:80"}
211-
memoryReservation := int64(500000000)
211+
memoryReservation := yaml.MemStringorInt(500000000)
212+
memory:= yaml.MemStringorInt(536870912) // 512 * 1024 * 1024
212213
shmSize := yaml.MemStringorInt(1073741824) // 1 gb = 1024 * 1024 * 1024 bytes
213214
tmpfs := yaml.Stringorslice{"/run:size=1gb", "/tmp:size=65536k"}
214215

@@ -218,6 +219,7 @@ services:
218219
image: wordpress
219220
ports: ["80:80"]
220221
mem_reservation: 500000000
222+
mem_limit: 512M
221223
shm_size: 1gb
222224
tmpfs:
223225
- /run:size=1gb
@@ -246,7 +248,8 @@ services:
246248
t.Errorf("Expected ports to be [%v] but got [%v]", ports, wordpress.Ports)
247249
}
248250

249-
assert.Equal(t, memoryReservation, int64(wordpress.MemReservation), "Expected memoryReservation to match")
251+
assert.Equal(t, memoryReservation, wordpress.MemReservation, "Expected memoryReservation to match")
252+
assert.Equal(t, memory, wordpress.MemLimit, "Expected memoryReservation to match")
250253
assert.Equal(t, shmSize, wordpress.ShmSize, "Expected shmSize to match")
251254
assert.Equal(t, tmpfs, wordpress.Tmpfs, "Expected tmpfs to match")
252255

‎ecs-cli/modules/utils/compose/convert_task_definition.go‎

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -263,33 +263,29 @@ func isZero(v reflect.Value) bool {
263263
return v.Interface() == zero.Interface()
264264
}
265265

266-
// convertToContainerDef transforms each service in the compose yml
267-
// to an equivalent ECS container definition
266+
// convertToContainerDef transforms each service in docker-compose.yml and
267+
// ecs-params.yml to an equivalent ECS container definition
268268
func convertToContainerDef(context *project.Context, inputCfg *config.ServiceConfig,
269269
volumes *volumes, outputContDef *ecs.ContainerDefinition, ecsContainerDef *ContainerDef) error {
270+
270271
// setting memory limit
271-
var mem int64
272-
var memoryReservation int64
273-
if inputCfg.MemReservation != 0 {
274-
memoryReservation = int64(inputCfg.MemReservation) / miB // convert bytes to MiB
275-
}
272+
// MemLimit and MemReservation on libcompose ServiceConfig are parsed
273+
// as yaml.MemStringorInt value representing the memory in bytes
274+
mem := ConvertToMemoryInMB(int64(inputCfg.MemLimit))
275+
memoryReservation := ConvertToMemoryInMB(int64(inputCfg.MemReservation))
276276

277-
if inputCfg.MemLimit != 0 {
278-
mem = int64(inputCfg.MemLimit) / miB // convert bytes to MiB
279-
}
280-
// mem_limit should be > mem_reservation, if it is specified
281277
if mem != 0 && memoryReservation != 0 && mem < memoryReservation {
282-
return errors.New("mem_limit should not be less than mem_reservation")
278+
return errors.New("mem_limit must be greater than mem_reservation")
283279
}
284280

285-
// TODO: We should be letting docker set the default here -- see note on default shared
286-
// memory size?
281+
// One or both of memory and memoryReservation is required to register a task definition with ECS
282+
// NOTE: Docker does not set a memory limit for containers
287283
if mem == 0 && memoryReservation == 0 {
288284
mem = defaultMemLimit
289285
}
290286

291287
// convert shared memory size
292-
shmSize := ConvertToSharedMemorySize(inputCfg)
288+
shmSize := ConvertToMemoryInMB(int64(inputCfg.ShmSize))
293289

294290
// convert environment variables
295291
environment := convertToKeyValuePairs(context, inputCfg.Environment, *outputContDef.Name)
@@ -336,7 +332,7 @@ func convertToContainerDef(context *project.Context, inputCfg *config.ServiceCon
336332
return err
337333
}
338334

339-
// populating container definition, offloading the validation to aws-sdk
335+
// Populate ECS container definition, offloading the validation to aws-sdk
340336
outputContDef.Cpu = aws.Int64(int64(inputCfg.CPUShares))
341337
outputContDef.Command = aws.StringSlice(inputCfg.Command)
342338
outputContDef.DnsSearchDomains = aws.StringSlice(inputCfg.DNSSearch)
@@ -784,12 +780,11 @@ func ConvertToLogConfiguration(inputCfg *config.ServiceConfig) (*ecs.LogConfigur
784780
return logConfig, nil
785781
}
786782

787-
// ConvertToSharedMemorySize converts libcompose-parsed bytes
788-
// to MiB, expected by ECS
789-
func ConvertToSharedMemorySize(inputCfg *config.ServiceConfig) int64 {
790-
var shmSize int64
791-
if inputCfg.ShmSize != 0 {
792-
shmSize = int64(inputCfg.ShmSize) / miB
783+
// ConvertToMemoryInMB converts libcompose-parsed bytes to MiB, expected by ECS
784+
func ConvertToMemoryInMB(bytes int64) int64 {
785+
var memory int64
786+
if bytes != 0 {
787+
memory = int64(bytes) / miB
793788
}
794-
return shmSize
789+
return memory
795790
}

‎ecs-cli/modules/utils/compose/convert_task_definition_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1246,7 +1246,7 @@ func TestMemReservationHigherThanMemLimit(t *testing.T) {
12461246
ResourceLookup: resourceLookup,
12471247
}
12481248
_, err = ConvertToTaskDefinition(context, volumeConfigs, serviceConfigs, "", "", nil)
1249-
assert.EqualError(t, err, "mem_limit should not be less than mem_reservation")
1249+
assert.EqualError(t, err, "mem_limit must be greater than mem_reservation")
12501250
}
12511251

12521252
func TestSortedGoString(t *testing.T) {

0 commit comments

Comments
 (0)