Skip to content

Commit 309fbee

Browse files
committed
Add converters for TaskPlacement fields
1 parent f9e3100 commit 309fbee

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed

ecs-cli/modules/utils/compose/ecs_params_reader.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,3 +293,48 @@ func parseHealthCheckTime(field string) (*int64, error) {
293293

294294
return nil, nil
295295
}
296+
297+
// ConvertToECSPlacementConstraint converts a list of Constraints specified in the
298+
// ecs-params into a format that is compatible with ECSClient calls.
299+
func ConvertToECSPlacementConstraints(ecsParams *ECSParams) ([]*ecs.PlacementConstraint, error) {
300+
if ecsParams == nil {
301+
return nil, nil
302+
}
303+
304+
constraints := ecsParams.RunParams.TaskPlacement.Constraints
305+
306+
output := []*ecs.PlacementConstraint{}
307+
for _, constraint := range constraints {
308+
ecsConstraint := &ecs.PlacementConstraint{
309+
Type: aws.String(constraint.Type),
310+
}
311+
if constraint.Expression != "" {
312+
ecsConstraint.Expression = aws.String(constraint.Expression)
313+
}
314+
output = append(output, ecsConstraint)
315+
}
316+
317+
return output, nil
318+
}
319+
320+
// ConvertToECSPlacementStrategy converts a list of Strategies specified in the
321+
// ecs-params into a format that is compatible with ECSClient calls.
322+
func ConvertToECSPlacementStrategy(ecsParams *ECSParams) ([]*ecs.PlacementStrategy, error) {
323+
if ecsParams == nil {
324+
return nil, nil
325+
}
326+
strategies := ecsParams.RunParams.TaskPlacement.Strategies
327+
328+
output := []*ecs.PlacementStrategy{}
329+
for _, strategy := range strategies {
330+
ecsStrategy := &ecs.PlacementStrategy{
331+
Type: aws.String(strategy.Type),
332+
}
333+
if strategy.Field != "" {
334+
ecsStrategy.Field = aws.String(strategy.Field)
335+
}
336+
output = append(output, ecsStrategy)
337+
}
338+
339+
return output, nil
340+
}

ecs-cli/modules/utils/compose/ecs_params_reader_test.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,86 @@ func TestConvertToECSNetworkConfiguration_NoNetworkConfig(t *testing.T) {
471471
}
472472
}
473473

474+
func TestConvertToECSPlacementConstraints(t *testing.T) {
475+
constraint1 := Constraint{
476+
Expression: "attribute:ecs.instance-type =~ t2.*",
477+
Type: ecs.PlacementConstraintTypeMemberOf,
478+
}
479+
constraint2 := Constraint{
480+
Type: ecs.PlacementConstraintTypeDistinctInstance,
481+
}
482+
constraints := []Constraint{constraint1, constraint2}
483+
taskPlacement := TaskPlacement{
484+
Constraints: constraints,
485+
}
486+
487+
ecsParams := &ECSParams{
488+
RunParams: RunParams{
489+
TaskPlacement: taskPlacement,
490+
},
491+
}
492+
493+
expectedConstraints := []*ecs.PlacementConstraint{
494+
&ecs.PlacementConstraint{
495+
Expression: aws.String("attribute:ecs.instance-type =~ t2.*"),
496+
Type: aws.String(ecs.PlacementConstraintTypeMemberOf),
497+
},
498+
&ecs.PlacementConstraint{
499+
Type: aws.String(ecs.PlacementConstraintTypeDistinctInstance),
500+
},
501+
}
502+
503+
ecsPlacementConstraints, err := ConvertToECSPlacementConstraints(ecsParams)
504+
505+
if assert.NoError(t, err) {
506+
assert.ElementsMatch(t, expectedConstraints, ecsPlacementConstraints, "Expected placement constraints to match")
507+
}
508+
}
509+
510+
func TestConvertToECSPlacementStrategy(t *testing.T) {
511+
strategy1 := Strategy{
512+
Field: "instanceId",
513+
Type: ecs.PlacementStrategyTypeBinpack,
514+
}
515+
strategy2 := Strategy{
516+
Field: "attribute:ecs.availability-zone",
517+
Type: ecs.PlacementStrategyTypeSpread,
518+
}
519+
strategy3 := Strategy{
520+
Type: ecs.PlacementStrategyTypeRandom,
521+
}
522+
strategy := []Strategy{strategy1, strategy2, strategy3}
523+
taskPlacement := TaskPlacement{
524+
Strategies: strategy,
525+
}
526+
527+
ecsParams := &ECSParams{
528+
RunParams: RunParams{
529+
TaskPlacement: taskPlacement,
530+
},
531+
}
532+
533+
expectedStrategy := []*ecs.PlacementStrategy{
534+
&ecs.PlacementStrategy{
535+
Field: aws.String("instanceId"),
536+
Type: aws.String(ecs.PlacementStrategyTypeBinpack),
537+
},
538+
&ecs.PlacementStrategy{
539+
Field: aws.String("attribute:ecs.availability-zone"),
540+
Type: aws.String(ecs.PlacementStrategyTypeSpread),
541+
},
542+
&ecs.PlacementStrategy{
543+
Type: aws.String(ecs.PlacementStrategyTypeRandom),
544+
},
545+
}
546+
547+
ecsPlacementStrategy, err := ConvertToECSPlacementStrategy(ecsParams)
548+
549+
if assert.NoError(t, err) {
550+
assert.ElementsMatch(t, expectedStrategy, ecsPlacementStrategy, "Expected placement strategy to match")
551+
}
552+
}
553+
474554
func TestReadECSParams_WithHealthCheck(t *testing.T) {
475555
ecsParamsString := `version: 1
476556
task_definition:

0 commit comments

Comments
 (0)