/
volumes.go
103 lines (83 loc) · 1.92 KB
/
volumes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package component
import (
"github.com/docker/docker/api/types/mount"
"github.com/docker/go-units"
"github.com/mitchellh/mapstructure"
"github.com/rycus86/podlike/pkg/volume"
"strings"
)
func (c *Component) getMounts() ([]mount.Mount, error) {
volumes, err := c.parseVolumes()
if err != nil {
return nil, err
}
if len(volumes) == 0 {
return []mount.Mount{}, nil
}
mounts := make([]mount.Mount, 0, len(volumes))
for _, v := range volumes {
mnt := mount.Mount{
Type: v.GetMountType(),
Source: v.Source,
Target: v.Target,
ReadOnly: v.IsReadOnly(),
}
sharedVolumeSource := c.client.GetSharedVolumeSource(mnt.Source)
if sharedVolumeSource != "" {
mnt.Source = sharedVolumeSource
}
if v.Bind.Propagation != "" {
mnt.BindOptions = &mount.BindOptions{
Propagation: mount.Propagation(v.Bind.Propagation),
}
}
if v.Volume.NoCopy {
mnt.VolumeOptions = &mount.VolumeOptions{
NoCopy: true,
}
}
if v.Tmpfs.Size != "" {
size, err := units.FromHumanSize(v.Tmpfs.Size)
if err != nil {
return nil, err
}
mnt.TmpfsOptions = &mount.TmpfsOptions{
SizeBytes: size,
}
}
mounts = append(mounts, mnt)
}
return mounts, nil
}
func (c *Component) parseVolumes() ([]*volume.Volume, error) {
if len(c.Volumes) == 0 {
return []*volume.Volume{}, nil
}
converted := make([]*volume.Volume, len(c.Volumes), len(c.Volumes))
for idx, item := range c.Volumes {
if asString, ok := item.(string); ok {
v := volume.Volume{}
parts := strings.Split(asString, ":")
switch len(parts) {
case 1:
v.Target = parts[0]
case 2:
v.Source = parts[0]
v.Target = parts[1]
case 3:
v.Source = parts[0]
v.Target = parts[1]
v.Mode = parts[2]
}
converted[idx] = &v
} else {
var v volume.Volume
err := mapstructure.Decode(item, &v)
if err != nil {
return nil, err
}
converted[idx] = &v
}
}
return converted, nil
}