/
mounts.go
68 lines (56 loc) · 1.74 KB
/
mounts.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
package bundlerules
import (
"code.cloudfoundry.org/garden"
spec "code.cloudfoundry.org/guardian/gardener/container-spec"
"code.cloudfoundry.org/guardian/rundmc"
"code.cloudfoundry.org/guardian/rundmc/goci"
"code.cloudfoundry.org/lager"
"github.com/opencontainers/runtime-spec/specs-go"
)
type Mounts struct {
logger lager.Logger
mountOptionsGetter rundmc.MountOptionsGetter
}
func NewMounts(logger lager.Logger, mountOptionsGetter rundmc.MountOptionsGetter) Mounts {
return Mounts{
logger: logger,
mountOptionsGetter: mountOptionsGetter,
}
}
func (b Mounts) Apply(bndl goci.Bndl, spec spec.DesiredContainerSpec) (goci.Bndl, error) {
var mounts []specs.Mount
for _, m := range spec.BindMounts {
mounts = append(mounts, specs.Mount{
Destination: m.DstPath,
Source: m.SrcPath,
Type: "bind",
Options: b.buildMountOptions(m),
})
}
return bndl.WithPrependedMounts(spec.BaseConfig.Mounts...).WithMounts(mounts...), nil
}
func (b Mounts) buildMountOptions(m garden.BindMount) []string {
mountOptions := []string{"bind", getMountMode(m)}
srcMountOptions, err := b.mountOptionsGetter(m.SrcPath)
if err != nil {
b.logger.Info("failed to get mount options, assuming no additional mount options", lager.Data{"error": err})
srcMountOptions = []string{}
}
return append(mountOptions, filterModeOption(srcMountOptions)...)
}
func getMountMode(m garden.BindMount) string {
if m.Mode == garden.BindMountModeRW {
return "rw"
}
return "ro"
}
func filterModeOption(mountOptions []string) []string {
filteredOptions := []string{}
for _, o := range mountOptions {
if o == "rw" || o == "ro" || o == "bind" {
continue
}
filteredOptions = append(filteredOptions, o)
}
return filteredOptions
}