Skip to content

Commit 13768d7

Browse files
committed
Support Task Placement in CreateService
1 parent 309fbee commit 13768d7

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

ecs-cli/modules/cli/compose/entity/service/service.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,8 +364,17 @@ func (s *Service) EntityType() types.Type {
364364
func (s *Service) buildCreateServiceInput(serviceName, taskDefName string) (*ecs.CreateServiceInput, error) {
365365
launchType := s.Context().CommandConfig.LaunchType
366366
cluster := s.Context().CommandConfig.Cluster
367+
ecsParams := s.ecsContext.ECSParams
367368

368-
networkConfig, err := composeutils.ConvertToECSNetworkConfiguration(s.ecsContext.ECSParams)
369+
networkConfig, err := composeutils.ConvertToECSNetworkConfiguration(ecsParams)
370+
if err != nil {
371+
return nil, err
372+
}
373+
placementConstraints, err := composeutils.ConvertToECSPlacementConstraints(ecsParams)
374+
if err != nil {
375+
return nil, err
376+
}
377+
placementStrategy, err := composeutils.ConvertToECSPlacementStrategy(ecsParams)
369378
if err != nil {
370379
return nil, err
371380
}
@@ -396,6 +405,14 @@ func (s *Service) buildCreateServiceInput(serviceName, taskDefName string) (*ecs
396405
createServiceInput.NetworkConfiguration = networkConfig
397406
}
398407

408+
if placementConstraints != nil {
409+
createServiceInput.PlacementConstraints = placementConstraints
410+
}
411+
412+
if placementStrategy != nil {
413+
createServiceInput.PlacementStrategy = placementStrategy
414+
}
415+
399416
if launchType != "" {
400417
createServiceInput.LaunchType = aws.String(launchType)
401418
}

ecs-cli/modules/cli/compose/entity/service/service_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,69 @@ func TestCreateEC2Explicitly(t *testing.T) {
205205
)
206206
}
207207

208+
func TestCreateWithTaskPlacement(t *testing.T) {
209+
flagSet := flag.NewFlagSet("ecs-cli-up", 0)
210+
211+
createServiceTest(
212+
t,
213+
flagSet,
214+
&config.CommandConfig{},
215+
ecsParamsWithTaskPlacement(),
216+
func(input *ecs.CreateServiceInput) {
217+
placementConstraints := input.PlacementConstraints
218+
placementStrategy := input.PlacementStrategy
219+
expectedConstraints := []*ecs.PlacementConstraint{
220+
{
221+
Type: aws.String("distinctInstance"),
222+
}, {
223+
Expression: aws.String("attribute:ecs.instance-type =~ t2.*"),
224+
Type: aws.String("memberOf"),
225+
},
226+
}
227+
expectedStrategy := []*ecs.PlacementStrategy{
228+
{
229+
Type: aws.String("random"),
230+
}, {
231+
Field: aws.String("instanceId"),
232+
Type: aws.String("binpack"),
233+
},
234+
}
235+
236+
assert.Len(t, placementConstraints, 2)
237+
assert.Equal(t, expectedConstraints, placementConstraints, "Expected Placement Constraints to match")
238+
assert.Len(t, placementStrategy, 2)
239+
assert.Equal(t, expectedStrategy, placementStrategy, "Expected Placement Strategy to match")
240+
},
241+
)
242+
}
243+
244+
func ecsParamsWithTaskPlacement() *utils.ECSParams {
245+
return &utils.ECSParams{
246+
RunParams: utils.RunParams{
247+
TaskPlacement: utils.TaskPlacement{
248+
Constraints: []utils.Constraint{
249+
utils.Constraint{
250+
Type: ecs.PlacementConstraintTypeDistinctInstance,
251+
},
252+
utils.Constraint{
253+
Expression: "attribute:ecs.instance-type =~ t2.*",
254+
Type: ecs.PlacementConstraintTypeMemberOf,
255+
},
256+
},
257+
Strategies: []utils.Strategy{
258+
utils.Strategy{
259+
Type: ecs.PlacementStrategyTypeRandom,
260+
},
261+
utils.Strategy{
262+
Field: "instanceId",
263+
Type: ecs.PlacementStrategyTypeBinpack,
264+
},
265+
},
266+
},
267+
},
268+
}
269+
}
270+
208271
// Specifies TargeGroupArn to test ALB
209272
func TestCreateWithALB(t *testing.T) {
210273
targetGroupArn := "targetGroupArn"

0 commit comments

Comments
 (0)