Skip to content

Commit f9e3100

Browse files
committed
Support Task Placement via ecs-params
1 parent a6373d6 commit f9e3100

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ type TaskSize struct {
9090
// RunParams specifies non-TaskDefinition specific parameters
9191
type RunParams struct {
9292
NetworkConfiguration NetworkConfiguration `yaml:"network_configuration"`
93+
TaskPlacement TaskPlacement `yaml:"task_placement"`
9394
}
9495

9596
// NetworkConfiguration specifies the network config for the task definition.
@@ -108,11 +109,27 @@ type AwsVpcConfiguration struct {
108109

109110
type AssignPublicIp string
110111

112+
// TODO: Remove; use enum in aws-sdk-go instead (AssignPublicIpEnabled, AssignPublicIpDisabled)
111113
const (
112114
Enabled AssignPublicIp = "ENABLED"
113115
Disabled AssignPublicIp = "DISABLED"
114116
)
115117

118+
type TaskPlacement struct {
119+
Strategies []Strategy `yaml:"strategy"`
120+
Constraints []Constraint `yaml:"constraints"`
121+
}
122+
123+
type Strategy struct {
124+
Field string `yaml:"field"`
125+
Type string `yaml:"type"`
126+
}
127+
128+
type Constraint struct {
129+
Expression string `yaml:"expression"`
130+
Type string `yaml:"type"`
131+
}
132+
116133
/////////////////////////////
117134
///// Parsing Functions /////
118135
/////////////////////////////

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

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,68 @@ run_params:
214214
}
215215
}
216216

217+
func TestReadECSParams_WithTaskPlacement(t *testing.T) {
218+
ecsParamsString := `version: 1
219+
run_params:
220+
task_placement:
221+
strategy:
222+
- field: memory
223+
type: binpack
224+
- field: attribute:ecs.availability-zone
225+
type: spread
226+
constraints:
227+
- expression: attribute:ecs.instance-type =~ t2.*
228+
type: memberOf
229+
- type: distinctInstance`
230+
231+
content := []byte(ecsParamsString)
232+
233+
tmpfile, err := ioutil.TempFile("", "ecs-params")
234+
assert.NoError(t, err, "Could not create ecs fields tempfile")
235+
236+
ecsParamsFileName := tmpfile.Name()
237+
defer os.Remove(ecsParamsFileName)
238+
239+
_, err = tmpfile.Write(content)
240+
assert.NoError(t, err, "Could not write data to ecs fields tempfile")
241+
242+
err = tmpfile.Close()
243+
assert.NoError(t, err, "Could not close tempfile")
244+
245+
expectedStrategies := []Strategy{
246+
{
247+
Field: "memory",
248+
Type: ecs.PlacementStrategyTypeBinpack,
249+
},
250+
{
251+
Field: "attribute:ecs.availability-zone",
252+
Type: ecs.PlacementStrategyTypeSpread,
253+
},
254+
}
255+
256+
expectedConstraints := []Constraint{
257+
{
258+
Expression: "attribute:ecs.instance-type =~ t2.*",
259+
Type: ecs.PlacementConstraintTypeMemberOf,
260+
},
261+
{
262+
Type: ecs.PlacementConstraintTypeDistinctInstance,
263+
},
264+
}
265+
266+
ecsParams, err := ReadECSParams(ecsParamsFileName)
267+
268+
if assert.NoError(t, err) {
269+
taskPlacement := ecsParams.RunParams.TaskPlacement
270+
strategies := taskPlacement.Strategies
271+
constraints := taskPlacement.Constraints
272+
assert.Len(t, strategies, 2)
273+
assert.Len(t, constraints, 2)
274+
assert.ElementsMatch(t, expectedStrategies, strategies)
275+
assert.ElementsMatch(t, expectedConstraints, constraints)
276+
}
277+
}
278+
217279
func TestReadECSParams_MemoryWithUnits(t *testing.T) {
218280
ecsParamsString := `version: 1
219281
task_definition:

0 commit comments

Comments
 (0)