DockerCLI/cli/compose/convert/volume.go

217 lines
6.3 KiB
Go
Raw Normal View History

package convert
import (
"strings"
composetypes "github.com/docker/cli/cli/compose/types"
"github.com/docker/docker/api/types/mount"
"github.com/pkg/errors"
)
type volumes map[string]composetypes.VolumeConfig
// Volumes from compose-file types to engine api types
func Volumes(serviceVolumes []composetypes.ServiceVolumeConfig, stackVolumes volumes, namespace Namespace) ([]mount.Mount, error) {
linting: Consider pre-allocating sliceVar (prealloc) While updating, also addressed some redundant fmt.Sprintf() opts/throttledevice.go:86:2: Consider pre-allocating `out` (prealloc) var out []string ^ opts/ulimit.go:37:2: Consider pre-allocating `out` (prealloc) var out []string ^ opts/ulimit.go:47:2: Consider pre-allocating `ulimits` (prealloc) var ulimits []*units.Ulimit ^ opts/weightdevice.go:68:2: Consider pre-allocating `out` (prealloc) var out []string ^ cli/context/store/metadatastore.go:96:2: Consider pre-allocating `res` (prealloc) var res []Metadata ^ cli/context/store/store.go:127:2: Consider pre-allocating `names` (prealloc) var names []string ^ cli/compose/loader/loader.go:223:2: Consider pre-allocating `keys` (prealloc) var keys []string ^ cli/compose/loader/loader.go:397:2: Consider pre-allocating `services` (prealloc) var services []types.ServiceConfig ^ cli/command/stack/loader/loader.go:63:2: Consider pre-allocating `msgs` (prealloc) var msgs []string ^ cli/command/stack/loader/loader.go:118:2: Consider pre-allocating `configFiles` (prealloc) var configFiles []composetypes.ConfigFile ^ cli/command/formatter/container.go:245:2: Consider pre-allocating `joinLabels` (prealloc) var joinLabels []string ^ cli/command/formatter/container.go:265:2: Consider pre-allocating `mounts` (prealloc) var mounts []string ^ cli/command/formatter/container.go:316:2: Consider pre-allocating `result` (prealloc) var result []string ^ cli/command/formatter/displayutils.go:43:2: Consider pre-allocating `display` (prealloc) var ( ^ cli/command/formatter/volume.go:103:2: Consider pre-allocating `joinLabels` (prealloc) var joinLabels []string ^ cli-plugins/manager/manager_test.go:49:2: Consider pre-allocating `dirs` (prealloc) var dirs []string ^ cli/command/swarm/init.go:69:2: Consider pre-allocating `defaultAddrPool` (prealloc) var defaultAddrPool []string ^ cli/command/manifest/push.go:195:2: Consider pre-allocating `blobReqs` (prealloc) var blobReqs []manifestBlob ^ cli/command/secret/formatter.go:111:2: Consider pre-allocating `joinLabels` (prealloc) var joinLabels []string ^ cli/command/network/formatter.go:104:2: Consider pre-allocating `joinLabels` (prealloc) var joinLabels []string ^ cli/command/context/list.go:52:2: Consider pre-allocating `contexts` (prealloc) var contexts []*formatter.ClientContext ^ cli/command/config/formatter.go:104:2: Consider pre-allocating `joinLabels` (prealloc) var joinLabels []string ^ cli/command/trust/common_test.go:23:2: Consider pre-allocating `targetNames` (prealloc) var targetNames []string ^ cli/command/service/generic_resource_opts.go:55:2: Consider pre-allocating `generic` (prealloc) var generic []swarm.GenericResource ^ cli/command/service/generic_resource_opts.go:98:2: Consider pre-allocating `l` (prealloc) var l []swarm.GenericResource ^ cli/command/service/opts.go:378:2: Consider pre-allocating `netAttach` (prealloc) var netAttach []swarm.NetworkAttachmentConfig ^ cli/command/service/update.go:731:2: Consider pre-allocating `limits` (prealloc) var limits []*units.Ulimit ^ cli/command/service/update.go:1315:2: Consider pre-allocating `newNetworks` (prealloc) var newNetworks []swarm.NetworkAttachmentConfig ^ cli/command/service/update.go:1514:2: Consider pre-allocating `out` (prealloc) var out []string ^ cli/compose/convert/service.go:713:2: Consider pre-allocating `ulimits` (prealloc) var ulimits []*units.Ulimit ^ cli/compose/convert/volume.go:13:2: Consider pre-allocating `mounts` (prealloc) var mounts []mount.Mount ^ cli/command/stack/swarm/list.go:39:2: Consider pre-allocating `stacks` (prealloc) var stacks []*formatter.Stack ^ Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-09-03 14:07:29 -04:00
mounts := make([]mount.Mount, 0, len(serviceVolumes))
for _, volumeConfig := range serviceVolumes {
linting: Consider pre-allocating sliceVar (prealloc) While updating, also addressed some redundant fmt.Sprintf() opts/throttledevice.go:86:2: Consider pre-allocating `out` (prealloc) var out []string ^ opts/ulimit.go:37:2: Consider pre-allocating `out` (prealloc) var out []string ^ opts/ulimit.go:47:2: Consider pre-allocating `ulimits` (prealloc) var ulimits []*units.Ulimit ^ opts/weightdevice.go:68:2: Consider pre-allocating `out` (prealloc) var out []string ^ cli/context/store/metadatastore.go:96:2: Consider pre-allocating `res` (prealloc) var res []Metadata ^ cli/context/store/store.go:127:2: Consider pre-allocating `names` (prealloc) var names []string ^ cli/compose/loader/loader.go:223:2: Consider pre-allocating `keys` (prealloc) var keys []string ^ cli/compose/loader/loader.go:397:2: Consider pre-allocating `services` (prealloc) var services []types.ServiceConfig ^ cli/command/stack/loader/loader.go:63:2: Consider pre-allocating `msgs` (prealloc) var msgs []string ^ cli/command/stack/loader/loader.go:118:2: Consider pre-allocating `configFiles` (prealloc) var configFiles []composetypes.ConfigFile ^ cli/command/formatter/container.go:245:2: Consider pre-allocating `joinLabels` (prealloc) var joinLabels []string ^ cli/command/formatter/container.go:265:2: Consider pre-allocating `mounts` (prealloc) var mounts []string ^ cli/command/formatter/container.go:316:2: Consider pre-allocating `result` (prealloc) var result []string ^ cli/command/formatter/displayutils.go:43:2: Consider pre-allocating `display` (prealloc) var ( ^ cli/command/formatter/volume.go:103:2: Consider pre-allocating `joinLabels` (prealloc) var joinLabels []string ^ cli-plugins/manager/manager_test.go:49:2: Consider pre-allocating `dirs` (prealloc) var dirs []string ^ cli/command/swarm/init.go:69:2: Consider pre-allocating `defaultAddrPool` (prealloc) var defaultAddrPool []string ^ cli/command/manifest/push.go:195:2: Consider pre-allocating `blobReqs` (prealloc) var blobReqs []manifestBlob ^ cli/command/secret/formatter.go:111:2: Consider pre-allocating `joinLabels` (prealloc) var joinLabels []string ^ cli/command/network/formatter.go:104:2: Consider pre-allocating `joinLabels` (prealloc) var joinLabels []string ^ cli/command/context/list.go:52:2: Consider pre-allocating `contexts` (prealloc) var contexts []*formatter.ClientContext ^ cli/command/config/formatter.go:104:2: Consider pre-allocating `joinLabels` (prealloc) var joinLabels []string ^ cli/command/trust/common_test.go:23:2: Consider pre-allocating `targetNames` (prealloc) var targetNames []string ^ cli/command/service/generic_resource_opts.go:55:2: Consider pre-allocating `generic` (prealloc) var generic []swarm.GenericResource ^ cli/command/service/generic_resource_opts.go:98:2: Consider pre-allocating `l` (prealloc) var l []swarm.GenericResource ^ cli/command/service/opts.go:378:2: Consider pre-allocating `netAttach` (prealloc) var netAttach []swarm.NetworkAttachmentConfig ^ cli/command/service/update.go:731:2: Consider pre-allocating `limits` (prealloc) var limits []*units.Ulimit ^ cli/command/service/update.go:1315:2: Consider pre-allocating `newNetworks` (prealloc) var newNetworks []swarm.NetworkAttachmentConfig ^ cli/command/service/update.go:1514:2: Consider pre-allocating `out` (prealloc) var out []string ^ cli/compose/convert/service.go:713:2: Consider pre-allocating `ulimits` (prealloc) var ulimits []*units.Ulimit ^ cli/compose/convert/volume.go:13:2: Consider pre-allocating `mounts` (prealloc) var mounts []mount.Mount ^ cli/command/stack/swarm/list.go:39:2: Consider pre-allocating `stacks` (prealloc) var stacks []*formatter.Stack ^ Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-09-03 14:07:29 -04:00
mnt, err := convertVolumeToMount(volumeConfig, stackVolumes, namespace)
if err != nil {
return nil, err
}
linting: Consider pre-allocating sliceVar (prealloc) While updating, also addressed some redundant fmt.Sprintf() opts/throttledevice.go:86:2: Consider pre-allocating `out` (prealloc) var out []string ^ opts/ulimit.go:37:2: Consider pre-allocating `out` (prealloc) var out []string ^ opts/ulimit.go:47:2: Consider pre-allocating `ulimits` (prealloc) var ulimits []*units.Ulimit ^ opts/weightdevice.go:68:2: Consider pre-allocating `out` (prealloc) var out []string ^ cli/context/store/metadatastore.go:96:2: Consider pre-allocating `res` (prealloc) var res []Metadata ^ cli/context/store/store.go:127:2: Consider pre-allocating `names` (prealloc) var names []string ^ cli/compose/loader/loader.go:223:2: Consider pre-allocating `keys` (prealloc) var keys []string ^ cli/compose/loader/loader.go:397:2: Consider pre-allocating `services` (prealloc) var services []types.ServiceConfig ^ cli/command/stack/loader/loader.go:63:2: Consider pre-allocating `msgs` (prealloc) var msgs []string ^ cli/command/stack/loader/loader.go:118:2: Consider pre-allocating `configFiles` (prealloc) var configFiles []composetypes.ConfigFile ^ cli/command/formatter/container.go:245:2: Consider pre-allocating `joinLabels` (prealloc) var joinLabels []string ^ cli/command/formatter/container.go:265:2: Consider pre-allocating `mounts` (prealloc) var mounts []string ^ cli/command/formatter/container.go:316:2: Consider pre-allocating `result` (prealloc) var result []string ^ cli/command/formatter/displayutils.go:43:2: Consider pre-allocating `display` (prealloc) var ( ^ cli/command/formatter/volume.go:103:2: Consider pre-allocating `joinLabels` (prealloc) var joinLabels []string ^ cli-plugins/manager/manager_test.go:49:2: Consider pre-allocating `dirs` (prealloc) var dirs []string ^ cli/command/swarm/init.go:69:2: Consider pre-allocating `defaultAddrPool` (prealloc) var defaultAddrPool []string ^ cli/command/manifest/push.go:195:2: Consider pre-allocating `blobReqs` (prealloc) var blobReqs []manifestBlob ^ cli/command/secret/formatter.go:111:2: Consider pre-allocating `joinLabels` (prealloc) var joinLabels []string ^ cli/command/network/formatter.go:104:2: Consider pre-allocating `joinLabels` (prealloc) var joinLabels []string ^ cli/command/context/list.go:52:2: Consider pre-allocating `contexts` (prealloc) var contexts []*formatter.ClientContext ^ cli/command/config/formatter.go:104:2: Consider pre-allocating `joinLabels` (prealloc) var joinLabels []string ^ cli/command/trust/common_test.go:23:2: Consider pre-allocating `targetNames` (prealloc) var targetNames []string ^ cli/command/service/generic_resource_opts.go:55:2: Consider pre-allocating `generic` (prealloc) var generic []swarm.GenericResource ^ cli/command/service/generic_resource_opts.go:98:2: Consider pre-allocating `l` (prealloc) var l []swarm.GenericResource ^ cli/command/service/opts.go:378:2: Consider pre-allocating `netAttach` (prealloc) var netAttach []swarm.NetworkAttachmentConfig ^ cli/command/service/update.go:731:2: Consider pre-allocating `limits` (prealloc) var limits []*units.Ulimit ^ cli/command/service/update.go:1315:2: Consider pre-allocating `newNetworks` (prealloc) var newNetworks []swarm.NetworkAttachmentConfig ^ cli/command/service/update.go:1514:2: Consider pre-allocating `out` (prealloc) var out []string ^ cli/compose/convert/service.go:713:2: Consider pre-allocating `ulimits` (prealloc) var ulimits []*units.Ulimit ^ cli/compose/convert/volume.go:13:2: Consider pre-allocating `mounts` (prealloc) var mounts []mount.Mount ^ cli/command/stack/swarm/list.go:39:2: Consider pre-allocating `stacks` (prealloc) var stacks []*formatter.Stack ^ Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-09-03 14:07:29 -04:00
mounts = append(mounts, mnt)
}
return mounts, nil
}
func createMountFromVolume(volume composetypes.ServiceVolumeConfig) mount.Mount {
return mount.Mount{
Type: mount.Type(volume.Type),
Target: volume.Target,
ReadOnly: volume.ReadOnly,
Source: volume.Source,
Consistency: mount.Consistency(volume.Consistency),
}
}
func handleVolumeToMount(
volume composetypes.ServiceVolumeConfig,
stackVolumes volumes,
namespace Namespace,
) (mount.Mount, error) {
result := createMountFromVolume(volume)
if volume.Tmpfs != nil {
return mount.Mount{}, errors.New("tmpfs options are incompatible with type volume")
}
if volume.Bind != nil {
return mount.Mount{}, errors.New("bind options are incompatible with type volume")
}
if volume.Cluster != nil {
return mount.Mount{}, errors.New("cluster options are incompatible with type volume")
}
// Anonymous volumes
if volume.Source == "" {
return result, nil
}
stackVolume, exists := stackVolumes[volume.Source]
if !exists {
return mount.Mount{}, errors.Errorf("undefined volume %q", volume.Source)
}
result.Source = namespace.Scope(volume.Source)
result.VolumeOptions = &mount.VolumeOptions{}
if volume.Volume != nil {
result.VolumeOptions.NoCopy = volume.Volume.NoCopy
}
if stackVolume.Name != "" {
result.Source = stackVolume.Name
}
// External named volumes
if stackVolume.External.External {
return result, nil
}
result.VolumeOptions.Labels = AddStackLabel(namespace, stackVolume.Labels)
if stackVolume.Driver != "" || stackVolume.DriverOpts != nil {
result.VolumeOptions.DriverConfig = &mount.Driver{
Name: stackVolume.Driver,
Options: stackVolume.DriverOpts,
}
}
return result, nil
}
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.Cluster != nil {
return mount.Mount{}, errors.New("cluster 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.Cluster != nil {
return mount.Mount{}, errors.New("cluster options are incompatible with type tmpfs")
}
if volume.Tmpfs != nil {
result.TmpfsOptions = &mount.TmpfsOptions{
SizeBytes: volume.Tmpfs.Size,
}
}
return result, nil
}
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
}
func handleClusterToMount(
volume composetypes.ServiceVolumeConfig,
stackVolumes volumes,
namespace Namespace,
) (mount.Mount, error) {
if volume.Source == "" {
return mount.Mount{}, errors.New("invalid cluster source, source cannot be empty")
}
if volume.Tmpfs != nil {
return mount.Mount{}, errors.New("tmpfs options are incompatible with type cluster")
}
if volume.Bind != nil {
return mount.Mount{}, errors.New("bind options are incompatible with type cluster")
}
if volume.Volume != nil {
return mount.Mount{}, errors.New("volume options are incompatible with type cluster")
}
result := createMountFromVolume(volume)
result.ClusterOptions = &mount.ClusterOptions{}
if !strings.HasPrefix(volume.Source, "group:") {
// if the volume is a cluster volume and the source is a volumegroup, we
// will ignore checking to see if such a volume is defined. the volume
// group isn't namespaced, and there's no simple way to indicate that
// external volumes with a given group exist.
stackVolume, exists := stackVolumes[volume.Source]
if !exists {
return mount.Mount{}, errors.Errorf("undefined volume %q", volume.Source)
}
// if the volume is not specified with a group source, we may namespace
// the name, if one is not otherwise specified.
if stackVolume.Name != "" {
result.Source = stackVolume.Name
} else {
result.Source = namespace.Scope(volume.Source)
}
}
return result, nil
}
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)
case "npipe":
return handleNpipeToMount(volume)
case "cluster":
return handleClusterToMount(volume, stackVolumes, namespace)
}
return mount.Mount{}, errors.New("volume type must be volume, bind, tmpfs, npipe, or cluster")
}