Merge pull request #1241 from ulyssessouza/add-configs-bind

Add configs bind mount support
This commit is contained in:
Guillaume Tardif 2021-02-05 11:15:23 +01:00 committed by GitHub
commit de7ce7084e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 69 additions and 7 deletions

View File

@ -388,23 +388,37 @@ func buildContainerMountOptions(p types.Project, s types.ServiceConfig, img moby
} }
if img.ContainerConfig != nil { if img.ContainerConfig != nil {
for k := range img.ContainerConfig.Volumes { for k := range img.ContainerConfig.Volumes {
mount, err := buildMount(p, types.ServiceVolumeConfig{ m, err := buildMount(p, types.ServiceVolumeConfig{
Type: types.VolumeTypeVolume, Type: types.VolumeTypeVolume,
Target: k, Target: k,
}) })
if err != nil { if err != nil {
return nil, err return nil, err
} }
mounts[k] = mount mounts[k] = m
} }
} }
for _, v := range s.Volumes {
mount, err := buildMount(p, v) mounts, err := fillBindMounts(p, s, mounts)
if err != nil { if err != nil {
return nil, err return nil, err
} }
mounts[mount.Target] = mount
values := make([]mount.Mount, 0, len(mounts))
for _, v := range mounts {
values = append(values, v)
}
return values, nil
}
func fillBindMounts(p types.Project, s types.ServiceConfig, m map[string]mount.Mount) (map[string]mount.Mount, error) {
for _, v := range s.Volumes {
bindMount, err := buildMount(p, v)
if err != nil {
return nil, err
}
m[bindMount.Target] = bindMount
} }
secrets, err := buildContainerSecretMounts(p, s) secrets, err := buildContainerSecretMounts(p, s)
@ -412,12 +426,53 @@ func buildContainerMountOptions(p types.Project, s types.ServiceConfig, img moby
return nil, err return nil, err
} }
for _, s := range secrets { for _, s := range secrets {
if _, found := mounts[s.Target]; found { if _, found := m[s.Target]; found {
continue continue
} }
mounts[s.Target] = s m[s.Target] = s
} }
configs, err := buildContainerConfigMounts(p, s)
if err != nil {
return nil, err
}
for _, c := range configs {
if _, found := m[c.Target]; found {
continue
}
m[c.Target] = c
}
return m, nil
}
func buildContainerConfigMounts(p types.Project, s types.ServiceConfig) ([]mount.Mount, error) {
var mounts = map[string]mount.Mount{}
configsBaseDir := "/"
for _, config := range s.Configs {
target := config.Target
if config.Target == "" {
target = filepath.Join(configsBaseDir, config.Source)
} else if !filepath.IsAbs(config.Target) {
target = filepath.Join(configsBaseDir, config.Target)
}
definedConfig := p.Configs[config.Source]
if definedConfig.External.External {
return nil, fmt.Errorf("unsupported external config %s", definedConfig.Name)
}
bindMount, err := buildMount(p, types.ServiceVolumeConfig{
Type: types.VolumeTypeBind,
Source: definedConfig.File,
Target: target,
ReadOnly: true,
})
if err != nil {
return nil, err
}
mounts[target] = bindMount
}
values := make([]mount.Mount, 0, len(mounts)) values := make([]mount.Mount, 0, len(mounts))
for _, v := range mounts { for _, v := range mounts {
values = append(values, v) values = append(values, v)

View File

@ -272,6 +272,7 @@ func TestLocalComposeVolume(t *testing.T) {
output := res.Stdout() output := res.Stdout()
// nolint // nolint
assert.Assert(t, strings.Contains(output, `"Destination":"/usr/src/app/node_modules","Driver":"local","Mode":"","RW":true,"Propagation":""`), output) assert.Assert(t, strings.Contains(output, `"Destination":"/usr/src/app/node_modules","Driver":"local","Mode":"","RW":true,"Propagation":""`), output)
assert.Assert(t, strings.Contains(output, `"Destination":"/myconfig","Mode":"","RW":false,"Propagation":"rprivate"`), output)
}) })
t.Run("check container bind-mounts specs", func(t *testing.T) { t.Run("check container bind-mounts specs", func(t *testing.T) {

View File

@ -14,8 +14,14 @@ services:
- otherVol:/usr/share/nginx/test - otherVol:/usr/share/nginx/test
ports: ports:
- 9090:80 - 9090:80
configs:
- myconfig
volumes: volumes:
staticVol: staticVol:
otherVol: otherVol:
name: myVolume name: myVolume
configs:
myconfig:
file: ./static/index.html