Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit de7ce70

Browse files
authored
Merge pull request #1241 from ulyssessouza/add-configs-bind
Add configs bind mount support
2 parents f433fb8 + 8d8934e commit de7ce70

File tree

3 files changed

+69
-7
lines changed

3 files changed

+69
-7
lines changed

local/compose/create.go

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -388,36 +388,91 @@ func buildContainerMountOptions(p types.Project, s types.ServiceConfig, img moby
388388
}
389389
if img.ContainerConfig != nil {
390390
for k := range img.ContainerConfig.Volumes {
391-
mount, err := buildMount(p, types.ServiceVolumeConfig{
391+
m, err := buildMount(p, types.ServiceVolumeConfig{
392392
Type: types.VolumeTypeVolume,
393393
Target: k,
394394
})
395395
if err != nil {
396396
return nil, err
397397
}
398-
mounts[k] = mount
398+
mounts[k] = m
399399

400400
}
401401
}
402+
403+
mounts, err := fillBindMounts(p, s, mounts)
404+
if err != nil {
405+
return nil, err
406+
}
407+
408+
values := make([]mount.Mount, 0, len(mounts))
409+
for _, v := range mounts {
410+
values = append(values, v)
411+
}
412+
return values, nil
413+
}
414+
415+
func fillBindMounts(p types.Project, s types.ServiceConfig, m map[string]mount.Mount) (map[string]mount.Mount, error) {
402416
for _, v := range s.Volumes {
403-
mount, err := buildMount(p, v)
417+
bindMount, err := buildMount(p, v)
404418
if err != nil {
405419
return nil, err
406420
}
407-
mounts[mount.Target] = mount
421+
m[bindMount.Target] = bindMount
408422
}
409423

410424
secrets, err := buildContainerSecretMounts(p, s)
411425
if err != nil {
412426
return nil, err
413427
}
414428
for _, s := range secrets {
415-
if _, found := mounts[s.Target]; found {
429+
if _, found := m[s.Target]; found {
430+
continue
431+
}
432+
m[s.Target] = s
433+
}
434+
435+
configs, err := buildContainerConfigMounts(p, s)
436+
if err != nil {
437+
return nil, err
438+
}
439+
for _, c := range configs {
440+
if _, found := m[c.Target]; found {
416441
continue
417442
}
418-
mounts[s.Target] = s
443+
m[c.Target] = c
419444
}
445+
return m, nil
446+
}
447+
448+
func buildContainerConfigMounts(p types.Project, s types.ServiceConfig) ([]mount.Mount, error) {
449+
var mounts = map[string]mount.Mount{}
450+
451+
configsBaseDir := "/"
452+
for _, config := range s.Configs {
453+
target := config.Target
454+
if config.Target == "" {
455+
target = filepath.Join(configsBaseDir, config.Source)
456+
} else if !filepath.IsAbs(config.Target) {
457+
target = filepath.Join(configsBaseDir, config.Target)
458+
}
459+
460+
definedConfig := p.Configs[config.Source]
461+
if definedConfig.External.External {
462+
return nil, fmt.Errorf("unsupported external config %s", definedConfig.Name)
463+
}
420464

465+
bindMount, err := buildMount(p, types.ServiceVolumeConfig{
466+
Type: types.VolumeTypeBind,
467+
Source: definedConfig.File,
468+
Target: target,
469+
ReadOnly: true,
470+
})
471+
if err != nil {
472+
return nil, err
473+
}
474+
mounts[target] = bindMount
475+
}
421476
values := make([]mount.Mount, 0, len(mounts))
422477
for _, v := range mounts {
423478
values = append(values, v)

local/e2e/compose/compose_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ func TestLocalComposeVolume(t *testing.T) {
272272
output := res.Stdout()
273273
// nolint
274274
assert.Assert(t, strings.Contains(output, `"Destination":"/usr/src/app/node_modules","Driver":"local","Mode":"","RW":true,"Propagation":""`), output)
275+
assert.Assert(t, strings.Contains(output, `"Destination":"/myconfig","Mode":"","RW":false,"Propagation":"rprivate"`), output)
275276
})
276277

277278
t.Run("check container bind-mounts specs", func(t *testing.T) {

local/e2e/compose/fixtures/volume-test/compose.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,14 @@ services:
1414
- otherVol:/usr/share/nginx/test
1515
ports:
1616
- 9090:80
17+
configs:
18+
- myconfig
1719

1820
volumes:
1921
staticVol:
2022
otherVol:
21-
name: myVolume
23+
name: myVolume
24+
25+
configs:
26+
myconfig:
27+
file: ./static/index.html

0 commit comments

Comments
 (0)