Skip to content

Commit 4f2b229

Browse files
committed
Set most v3 fields on ContainerConfig, add tests
1 parent 054dd24 commit 4f2b229

File tree

2 files changed

+305
-47
lines changed

2 files changed

+305
-47
lines changed

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

Lines changed: 126 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,42 @@ import (
55
"path/filepath"
66

77
"github.com/aws/amazon-ecs-cli/ecs-cli/modules/cli/compose/containerconfig"
8+
"github.com/aws/amazon-ecs-cli/ecs-cli/modules/utils/compose"
9+
"github.com/aws/aws-sdk-go/service/ecs"
810
"github.com/pkg/errors"
9-
"github.com/sirupsen/logrus"
11+
log "github.com/sirupsen/logrus"
1012

1113
"github.com/docker/cli/cli/compose/loader"
1214
"github.com/docker/cli/cli/compose/types"
15+
"github.com/docker/libcompose/yaml"
1316
)
1417

1518
func (p *ecsProject) parseV3() (*[]containerconfig.ContainerConfig, error) {
16-
logrus.Debug("Parsing v3 project...")
19+
log.Debug("Parsing v3 project...")
1720

18-
// load and parse each compose file into a configFile
21+
v3Config, err := getV3Config(p.ecsContext.ComposeFiles)
22+
if err != nil {
23+
return nil, err
24+
}
25+
26+
// convert ServiceConfigs to ContainerConfigs
27+
conConfigs := []containerconfig.ContainerConfig{}
28+
for _, service := range v3Config.Services {
29+
cCon, err := convertToContainerConfig(service)
30+
if err != nil {
31+
return nil, err
32+
}
33+
conConfigs = append(conConfigs, *cCon)
34+
}
35+
36+
// TODO: process v3Config.Volumes as well
37+
return &conConfigs, nil
38+
}
39+
40+
// parses compose files into a docker/cli Config, which contains v3 ServiceConfigs
41+
func getV3Config(composeFiles []string) (*types.Config, error) {
1942
configFiles := []types.ConfigFile{}
20-
for _, file := range p.ecsContext.ComposeFiles {
43+
for _, file := range composeFiles {
2144

2245
loadedFile, err := ioutil.ReadFile(file)
2346
if err != nil {
@@ -34,7 +57,7 @@ func (p *ecsProject) parseV3() (*[]containerconfig.ContainerConfig, error) {
3457
configFiles = append(configFiles, configFile)
3558
}
3659

37-
wrkDir, err := getWorkingDir(p.ecsContext.ComposeFiles[0])
60+
wrkDir, err := getWorkingDir(composeFiles[0])
3861
if err != nil {
3962
return nil, err
4063
}
@@ -51,24 +74,89 @@ func (p *ecsProject) parseV3() (*[]containerconfig.ContainerConfig, error) {
5174
return nil, err
5275
}
5376

54-
// convert ServiceConfigs to ContainerConfigs
55-
conConfigs := []containerconfig.ContainerConfig{}
56-
for _, service := range config.Services {
57-
cCon, err := convertToContainerConfig(service)
77+
return config, nil
78+
}
79+
80+
func convertToContainerConfig(serviceConfig types.ServiceConfig) (*containerconfig.ContainerConfig, error) {
81+
//TODO: Add Healthcheck, Devices to ContainerConfig
82+
c := &containerconfig.ContainerConfig{
83+
CapAdd: serviceConfig.CapAdd,
84+
CapDrop: serviceConfig.CapDrop,
85+
DockerSecurityOptions: serviceConfig.SecurityOpt,
86+
Entrypoint: serviceConfig.Entrypoint,
87+
Name: serviceConfig.Name,
88+
Image: serviceConfig.Image,
89+
Hostname: serviceConfig.Hostname,
90+
Links: serviceConfig.Links,
91+
Privileged: serviceConfig.Privileged,
92+
ReadOnly: serviceConfig.ReadOnly,
93+
Command: serviceConfig.Command,
94+
User: serviceConfig.User,
95+
WorkingDirectory: serviceConfig.WorkingDir,
96+
}
97+
98+
extraHosts, err := utils.ConvertToExtraHosts(serviceConfig.ExtraHosts)
99+
if err != nil {
100+
return nil, err
101+
}
102+
c.ExtraHosts = extraHosts
103+
104+
if serviceConfig.DNS != nil {
105+
c.DNSServers = serviceConfig.DNS
106+
}
107+
if serviceConfig.DNSSearch != nil {
108+
c.DNSSearchDomains = serviceConfig.DNSSearch
109+
}
110+
111+
if serviceConfig.Tmpfs != nil {
112+
tmpfs := yaml.Stringorslice(serviceConfig.Tmpfs)
113+
ecsTmpfs, err := utils.ConvertToTmpfs(tmpfs) // TODO: change ConvertToTmpfs to take in []string
58114
if err != nil {
59115
return nil, err
60116
}
61-
conConfigs = append(conConfigs, *cCon)
117+
c.Tmpfs = ecsTmpfs
62118
}
119+
if serviceConfig.Logging != nil {
120+
logConfig := ecs.LogConfiguration{}
121+
logConfig.SetLogDriver(serviceConfig.Logging.Driver)
63122

64-
return &conConfigs, nil
65-
}
123+
optionsMap := makePointerMapForStringMap(serviceConfig.Logging.Options)
124+
logConfig.SetOptions(optionsMap)
125+
c.LogConfiguration = &logConfig
126+
}
127+
if serviceConfig.Labels != nil {
128+
labelsMap := makePointerMapForStringMap(serviceConfig.Labels)
129+
c.DockerLabels = labelsMap
130+
}
131+
if len(serviceConfig.Ports) > 0 {
132+
var portMappings []*ecs.PortMapping
133+
for _, portConfig := range serviceConfig.Ports {
134+
mapping := convertPortConfigToECSMapping(portConfig)
135+
portMappings = append(portMappings, mapping)
136+
}
137+
c.PortMappings = portMappings
138+
}
139+
// TODO: reconcile with top-level Volumes key
140+
if len(serviceConfig.Volumes) > 0 {
141+
mountPoints := []*ecs.MountPoint{}
66142

67-
func convertToContainerConfig(serviceConfig types.ServiceConfig) (*containerconfig.ContainerConfig, error) {
68-
//TODO: fully convert ServiceConfig to ContainerConfig
69-
c := &containerconfig.ContainerConfig{
70-
Name: serviceConfig.Name,
143+
for _, volConfig := range serviceConfig.Volumes {
144+
if volConfig.Type == "volume" {
145+
mp := &ecs.MountPoint{
146+
ContainerPath: &volConfig.Target,
147+
SourceVolume: &volConfig.Source,
148+
ReadOnly: &volConfig.ReadOnly,
149+
}
150+
mountPoints = append(mountPoints, mp)
151+
} else {
152+
log.Warnf("Unsupported mount type found: %s", volConfig.Type)
153+
}
154+
}
155+
c.MountPoints = mountPoints
71156
}
157+
158+
// TODO: add Environtment, EnvFile to ContainerConfig
159+
// TODO: log out unsupported fields
72160
return c, nil
73161
}
74162

@@ -79,3 +167,25 @@ func getWorkingDir(fileName string) (string, error) {
79167
}
80168
return filepath.Dir(pwd), nil
81169
}
170+
171+
func convertPortConfigToECSMapping(portConfig types.ServicePortConfig) *ecs.PortMapping {
172+
containerPort := int64(portConfig.Target)
173+
hostPort := int64(portConfig.Published)
174+
175+
var ecsMapping = ecs.PortMapping{
176+
ContainerPort: &containerPort,
177+
HostPort: &hostPort,
178+
Protocol: &portConfig.Protocol,
179+
}
180+
return &ecsMapping
181+
}
182+
183+
func makePointerMapForStringMap(m map[string]string) map[string]*string {
184+
pointerMap := make(map[string]*string)
185+
for k, v := range m {
186+
val := new(string)
187+
*val = v
188+
pointerMap[k] = val
189+
}
190+
return pointerMap
191+
}

0 commit comments

Comments
 (0)