2016-12-05 16:14:08 -05:00
|
|
|
package convert
|
|
|
|
|
|
|
|
import (
|
2017-04-17 18:07:56 -04:00
|
|
|
composetypes "github.com/docker/cli/cli/compose/types"
|
2016-12-05 16:14:08 -05:00
|
|
|
"github.com/docker/docker/api/types/mount"
|
2017-01-27 16:56:45 -05:00
|
|
|
"github.com/pkg/errors"
|
2016-12-05 16:14:08 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type volumes map[string]composetypes.VolumeConfig
|
|
|
|
|
|
|
|
// Volumes from compose-file types to engine api types
|
2017-01-27 16:56:45 -05:00
|
|
|
func Volumes(serviceVolumes []composetypes.ServiceVolumeConfig, stackVolumes volumes, namespace Namespace) ([]mount.Mount, error) {
|
2016-12-05 16:14:08 -05:00
|
|
|
var mounts []mount.Mount
|
|
|
|
|
2017-01-27 16:56:45 -05:00
|
|
|
for _, volumeConfig := range serviceVolumes {
|
|
|
|
mount, err := convertVolumeToMount(volumeConfig, stackVolumes, namespace)
|
2016-12-05 16:14:08 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
mounts = append(mounts, mount)
|
|
|
|
}
|
|
|
|
return mounts, nil
|
|
|
|
}
|
|
|
|
|
2018-01-16 11:52:26 -05:00
|
|
|
func createMountFromVolume(volume composetypes.ServiceVolumeConfig) mount.Mount {
|
|
|
|
return mount.Mount{
|
2017-04-06 11:37:55 -04:00
|
|
|
Type: mount.Type(volume.Type),
|
|
|
|
Target: volume.Target,
|
|
|
|
ReadOnly: volume.ReadOnly,
|
2018-01-16 11:52:26 -05:00
|
|
|
Source: volume.Source,
|
2017-04-06 11:37:55 -04:00
|
|
|
Consistency: mount.Consistency(volume.Consistency),
|
2016-12-18 19:50:08 -05:00
|
|
|
}
|
2018-01-16 11:52:26 -05:00
|
|
|
}
|
2016-12-18 19:50:08 -05:00
|
|
|
|
2018-01-16 11:52:26 -05:00
|
|
|
func handleVolumeToMount(
|
|
|
|
volume composetypes.ServiceVolumeConfig,
|
|
|
|
stackVolumes volumes,
|
|
|
|
namespace Namespace,
|
|
|
|
) (mount.Mount, error) {
|
|
|
|
result := createMountFromVolume(volume)
|
2016-12-05 16:14:08 -05:00
|
|
|
|
2018-01-16 11:52:26 -05:00
|
|
|
if volume.Tmpfs != nil {
|
|
|
|
return mount.Mount{}, errors.New("tmpfs options are incompatible with type volume")
|
|
|
|
}
|
2017-01-27 16:56:45 -05:00
|
|
|
if volume.Bind != nil {
|
2018-01-16 11:52:26 -05:00
|
|
|
return mount.Mount{}, errors.New("bind options are incompatible with type volume")
|
2017-01-27 16:56:45 -05:00
|
|
|
}
|
2018-01-16 11:52:26 -05:00
|
|
|
// Anonymous volumes
|
|
|
|
if volume.Source == "" {
|
2017-01-27 16:56:45 -05:00
|
|
|
return result, nil
|
2016-12-05 16:14:08 -05:00
|
|
|
}
|
|
|
|
|
2017-01-27 16:56:45 -05:00
|
|
|
stackVolume, exists := stackVolumes[volume.Source]
|
2016-12-05 16:14:08 -05:00
|
|
|
if !exists {
|
2018-01-16 11:52:26 -05:00
|
|
|
return mount.Mount{}, errors.Errorf("undefined volume %q", volume.Source)
|
2016-12-05 16:14:08 -05:00
|
|
|
}
|
|
|
|
|
2017-01-27 16:56:45 -05:00
|
|
|
result.Source = namespace.Scope(volume.Source)
|
|
|
|
result.VolumeOptions = &mount.VolumeOptions{}
|
2016-12-05 16:14:08 -05:00
|
|
|
|
2017-01-27 16:56:45 -05:00
|
|
|
if volume.Volume != nil {
|
|
|
|
result.VolumeOptions.NoCopy = volume.Volume.NoCopy
|
2016-12-05 16:14:08 -05:00
|
|
|
}
|
|
|
|
|
2017-11-06 17:03:43 -05:00
|
|
|
if stackVolume.Name != "" {
|
|
|
|
result.Source = stackVolume.Name
|
|
|
|
}
|
|
|
|
|
2017-01-27 16:56:45 -05:00
|
|
|
// External named volumes
|
|
|
|
if stackVolume.External.External {
|
|
|
|
return result, nil
|
2016-12-05 16:14:08 -05:00
|
|
|
}
|
|
|
|
|
2017-01-27 16:56:45 -05:00
|
|
|
result.VolumeOptions.Labels = AddStackLabel(namespace, stackVolume.Labels)
|
|
|
|
if stackVolume.Driver != "" || stackVolume.DriverOpts != nil {
|
|
|
|
result.VolumeOptions.DriverConfig = &mount.Driver{
|
|
|
|
Name: stackVolume.Driver,
|
|
|
|
Options: stackVolume.DriverOpts,
|
2016-12-05 16:14:08 -05:00
|
|
|
}
|
|
|
|
}
|
2017-01-27 16:56:45 -05:00
|
|
|
|
|
|
|
return result, nil
|
2016-12-05 16:14:08 -05:00
|
|
|
}
|
2018-01-16 11:52:26 -05:00
|
|
|
|
|
|
|
func handleBindToMount(volume composetypes.ServiceVolumeConfig) (mount.Mount, error) {
|
|
|
|
result := createMountFromVolume(volume)
|
|
|
|
|
|
|
|
if volume.Source == "" {
|
|
|
|
return mount.Mount{}, errors.New("invalid bind source, source cannot be empty")
|
|
|
|
}
|
|
|
|
if volume.Volume != nil {
|
|
|
|
return mount.Mount{}, errors.New("volume options are incompatible with type bind")
|
|
|
|
}
|
|
|
|
if volume.Tmpfs != nil {
|
|
|
|
return mount.Mount{}, errors.New("tmpfs options are incompatible with type bind")
|
|
|
|
}
|
|
|
|
if volume.Bind != nil {
|
|
|
|
result.BindOptions = &mount.BindOptions{
|
|
|
|
Propagation: mount.Propagation(volume.Bind.Propagation),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleTmpfsToMount(volume composetypes.ServiceVolumeConfig) (mount.Mount, error) {
|
|
|
|
result := createMountFromVolume(volume)
|
|
|
|
|
|
|
|
if volume.Source != "" {
|
|
|
|
return mount.Mount{}, errors.New("invalid tmpfs source, source must be empty")
|
|
|
|
}
|
|
|
|
if volume.Bind != nil {
|
|
|
|
return mount.Mount{}, errors.New("bind options are incompatible with type tmpfs")
|
|
|
|
}
|
|
|
|
if volume.Volume != nil {
|
|
|
|
return mount.Mount{}, errors.New("volume options are incompatible with type tmpfs")
|
|
|
|
}
|
|
|
|
if volume.Tmpfs != nil {
|
|
|
|
result.TmpfsOptions = &mount.TmpfsOptions{
|
|
|
|
SizeBytes: volume.Tmpfs.Size,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2018-07-08 12:11:26 -04:00
|
|
|
func handleNpipeToMount(volume composetypes.ServiceVolumeConfig) (mount.Mount, error) {
|
|
|
|
result := createMountFromVolume(volume)
|
|
|
|
|
|
|
|
if volume.Source == "" {
|
|
|
|
return mount.Mount{}, errors.New("invalid npipe source, source cannot be empty")
|
|
|
|
}
|
|
|
|
if volume.Volume != nil {
|
|
|
|
return mount.Mount{}, errors.New("volume options are incompatible with type npipe")
|
|
|
|
}
|
|
|
|
if volume.Tmpfs != nil {
|
|
|
|
return mount.Mount{}, errors.New("tmpfs options are incompatible with type npipe")
|
|
|
|
}
|
|
|
|
if volume.Bind != nil {
|
|
|
|
result.BindOptions = &mount.BindOptions{
|
|
|
|
Propagation: mount.Propagation(volume.Bind.Propagation),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2018-01-16 11:52:26 -05:00
|
|
|
func convertVolumeToMount(
|
|
|
|
volume composetypes.ServiceVolumeConfig,
|
|
|
|
stackVolumes volumes,
|
|
|
|
namespace Namespace,
|
|
|
|
) (mount.Mount, error) {
|
|
|
|
|
|
|
|
switch volume.Type {
|
|
|
|
case "volume", "":
|
|
|
|
return handleVolumeToMount(volume, stackVolumes, namespace)
|
|
|
|
case "bind":
|
|
|
|
return handleBindToMount(volume)
|
|
|
|
case "tmpfs":
|
|
|
|
return handleTmpfsToMount(volume)
|
2018-07-08 12:11:26 -04:00
|
|
|
case "npipe":
|
|
|
|
return handleNpipeToMount(volume)
|
2018-01-16 11:52:26 -05:00
|
|
|
}
|
2018-07-08 12:11:26 -04:00
|
|
|
return mount.Mount{}, errors.New("volume type must be volume, bind, tmpfs or npipe")
|
2018-01-16 11:52:26 -05:00
|
|
|
}
|