Skip to content

Commit 68fbd66

Browse files
SoManyHsallisaurus
authored andcommitted
Remove libcompose project embedding
Also added constructor for Volumes for more consistent initialization
1 parent e4d9d13 commit 68fbd66

File tree

7 files changed

+28
-26
lines changed

7 files changed

+28
-26
lines changed

ecs-cli/modules/cli/compose/adapter/convert.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,9 +304,7 @@ func ConvertToULimits(cfgUlimits yaml.Ulimits) ([]*ecs.Ulimit, error) {
304304
// ConvertToVolumes converts the VolumeConfigs map on a libcompose project into
305305
// a Volumes struct and populates the VolumeEmptyHost field with any named volumes
306306
func ConvertToVolumes(volumeConfigs map[string]*config.VolumeConfig) (*Volumes, error) {
307-
volumes := &Volumes{
308-
VolumeWithHost: make(map[string]string), // map with key:=hostSourcePath value:=VolumeName
309-
}
307+
volumes := NewVolumes()
310308

311309
// Add named volume configs:
312310
if volumeConfigs != nil {

ecs-cli/modules/cli/compose/adapter/volumes.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,10 @@ type Volumes struct {
3131
func getVolumeName(suffixNum int) string {
3232
return fmt.Sprintf("%s-%d", ecsVolumeNamePrefix, suffixNum)
3333
}
34+
35+
func NewVolumes() *Volumes {
36+
return &Volumes{
37+
VolumeWithHost: make(map[string]string), // map with key:=hostSourcePath value:=VolumeName
38+
VolumeEmptyHost: []string{},
39+
}
40+
}

ecs-cli/modules/cli/compose/factory/factory.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ package factory
1515

1616
import (
1717
"github.com/aws/amazon-ecs-cli/ecs-cli/modules/cli/compose/context"
18-
ecscompose "github.com/aws/amazon-ecs-cli/ecs-cli/modules/cli/compose/project"
18+
"github.com/aws/amazon-ecs-cli/ecs-cli/modules/cli/compose/project"
1919
"github.com/aws/amazon-ecs-cli/ecs-cli/modules/config"
2020
"github.com/aws/amazon-ecs-cli/ecs-cli/modules/utils/compose"
2121

@@ -25,7 +25,7 @@ import (
2525

2626
// ProjectFactory is an interface that surfaces a function to create ECS Compose Project (intended to make mocking easy in tests)
2727
type ProjectFactory interface {
28-
Create(cliContext *cli.Context, isService bool) (ecscompose.Project, error)
28+
Create(cliContext *cli.Context, isService bool) (project.Project, error)
2929
}
3030

3131
// projectFactory implements ProjectFactory interface
@@ -38,7 +38,7 @@ func NewProjectFactory() ProjectFactory {
3838
}
3939

4040
// Create is a factory function that creates and configures ECS Compose project using the supplied command line arguments
41-
func (projectFactory projectFactory) Create(cliContext *cli.Context, isService bool) (ecscompose.Project, error) {
41+
func (projectFactory projectFactory) Create(cliContext *cli.Context, isService bool) (project.Project, error) {
4242
// creates and populates the ecs context
4343
ecsContext := &context.ECSContext{}
4444
if err := projectFactory.populateContext(ecsContext, cliContext); err != nil {
@@ -47,7 +47,7 @@ func (projectFactory projectFactory) Create(cliContext *cli.Context, isService b
4747
ecsContext.IsService = isService
4848

4949
// creates and initializes project using the context
50-
project := ecscompose.NewProject(ecsContext)
50+
project := project.NewProject(ecsContext)
5151

5252
// load the configs
5353
if err := projectFactory.loadProject(project); err != nil {
@@ -107,7 +107,7 @@ func (projectFactory projectFactory) populateLibcomposeContext(ecsContext *conte
107107
}
108108

109109
// loadProject opens the project by loading configs
110-
func (projectFactory projectFactory) loadProject(project ecscompose.Project) error {
110+
func (projectFactory projectFactory) loadProject(project project.Project) error {
111111
err := project.Parse()
112112
if err != nil {
113113
utils.LogError(err, "Unable to open ECS Compose Project")

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,9 @@ type Project interface {
5454

5555
// ecsProject struct is an implementation of Project.
5656
type ecsProject struct {
57-
project.Project
58-
59-
// TODO: use this instead of project.Project
6057
containerConfigs []adapter.ContainerConfig
6158
volumes *adapter.Volumes
62-
63-
ecsContext *context.ECSContext
59+
ecsContext *context.ECSContext
6460

6561
// TODO: track a map of entities [taskDefinition -> Entity]
6662
// 1 task definition for every disjoint set of containers in the compose file
@@ -69,12 +65,10 @@ type ecsProject struct {
6965

7066
// NewProject creates a new instance of the ECS Compose Project
7167
func NewProject(ecsContext *context.ECSContext) Project {
72-
libcomposeProject := project.NewProject(&ecsContext.Context, nil, nil)
73-
7468
p := &ecsProject{
7569
ecsContext: ecsContext,
76-
Project: *libcomposeProject,
7770
containerConfigs: []adapter.ContainerConfig{},
71+
volumes: adapter.NewVolumes(),
7872
}
7973

8074
if ecsContext.IsService {

ecs-cli/modules/cli/compose/project/project_parseV1V2.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,24 @@ import (
1212
func (p *ecsProject) parseV1V2() (*[]adapter.ContainerConfig, error) {
1313
logrus.Debug("Parsing v1/2 project...")
1414

15+
libcomposeProject := project.NewProject(&p.ecsContext.Context, nil, nil)
1516
// libcompose.Project#Parse populates project information based on its
1617
// context. It sets up the name, the composefile and the composebytes
1718
// (the composefile content). This is where libcompose ServiceConfigs
1819
// and VolumeConfigs gets loaded.
19-
20-
// TODO instantiate the libcompose project here instead of in the factory
21-
if err := p.Project.Parse(); err != nil {
20+
if err := libcomposeProject.Parse(); err != nil {
2221
return nil, err
2322
}
2423

25-
volumeConfigs := p.Project.VolumeConfigs
24+
volumeConfigs := libcomposeProject.VolumeConfigs
2625
volumes, err := adapter.ConvertToVolumes(volumeConfigs)
2726
if err != nil {
2827
return nil, err
2928
}
3029
p.volumes = volumes
3130

3231
context := &p.Context().Context
33-
serviceConfigs := p.Project.ServiceConfigs
32+
serviceConfigs := libcomposeProject.ServiceConfigs
3433

3534
// convert ServiceConfigs to ContainerConfigs
3635
containerConfigs := []adapter.ContainerConfig{}

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"github.com/aws/amazon-ecs-cli/ecs-cli/modules/cli/compose/context"
2323
"github.com/aws/amazon-ecs-cli/ecs-cli/modules/commands/flags"
2424
"github.com/aws/amazon-ecs-cli/ecs-cli/modules/utils/compose"
25-
"github.com/docker/libcompose/project"
2625
"github.com/stretchr/testify/assert"
2726
"github.com/urfave/cli"
2827
)
@@ -61,6 +60,10 @@ services:
6160

6261
// verify project name is set
6362
assert.Equal(t, testProjectName, project.ecsContext.ProjectName, "Expected ProjectName to be overridden.")
63+
64+
// verify top-level volumes are empty
65+
assert.Empty(t, project.VolumeConfigs().VolumeWithHost, "Expected volume configs to be empty")
66+
assert.Empty(t, project.VolumeConfigs().VolumeEmptyHost, "Expected volume configs to be empty")
6467
}
6568

6669
func TestParseCompose_V2_WithVolumeConfigs(t *testing.T) {
@@ -94,9 +97,12 @@ volumes:
9497
project.ecsContext.ComposeFiles = append(project.ecsContext.ComposeFiles, tmpfile.Name())
9598

9699
err = project.parseCompose()
100+
expectedNamedVolumes := []string{"banana", "volume-1"}
101+
expectedHosts := map[string]string{"./cache": "volume-2"}
97102

98103
// verify VolumeConfigs are populated
99-
assert.NotEmpty(t, project.VolumeConfigs(), "Expected volume configs to not be empty")
104+
assert.Equal(t, expectedHosts, project.VolumeConfigs().VolumeWithHost, "Expected volume configs to match")
105+
assert.Equal(t, expectedNamedVolumes, project.VolumeConfigs().VolumeEmptyHost, "Expected volume configs to match")
100106
}
101107

102108
func TestParseECSParams(t *testing.T) {
@@ -276,10 +282,8 @@ func setupTestProjectWithEcsParams(t *testing.T, ecsParamsFileName string) *ecsP
276282
}
277283
ecsContext.EnvironmentLookup = envLookup
278284
ecsContext.ResourceLookup = resourceLookup
279-
libcomposeProject := project.NewProject(&ecsContext.Context, nil, nil)
280285

281286
return &ecsProject{
282287
ecsContext: ecsContext,
283-
Project: *libcomposeProject,
284288
}
285289
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1063,7 +1063,7 @@ func TestMemReservationHigherThanMemLimit(t *testing.T) {
10631063
WorkingDirectory: workingDir,
10641064
}
10651065

1066-
volumeConfigs := &adapter.Volumes{}
1066+
volumeConfigs := adapter.NewVolumes()
10671067
containerConfigs := []adapter.ContainerConfig{containerConfig}
10681068

10691069
context := testContext(t)

0 commit comments

Comments
 (0)