Skip to content

Commit

Permalink
Merge pull request #4958 from hashicorp/f-mount-docker-087
Browse files Browse the repository at this point in the history
Implemented bind mount type; added tests
  • Loading branch information
dadgar committed Dec 4, 2018
2 parents b5e6483 + a62a151 commit e029a49
Show file tree
Hide file tree
Showing 3 changed files with 403 additions and 25 deletions.
82 changes: 67 additions & 15 deletions client/driver/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,11 @@ type DockerLoggingOpts struct {
}

type DockerMount struct {
Type string `mapstructure:"type"`
Target string `mapstructure:"target"`
Source string `mapstructure:"source"`
ReadOnly bool `mapstructure:"readonly"`
BindOptions []*DockerBindOptions `mapstructure:"bind_options"`
VolumeOptions []*DockerVolumeOptions `mapstructure:"volume_options"`
}

Expand All @@ -180,6 +182,10 @@ type DockerDevice struct {
CgroupPermissions string `mapstructure:"cgroup_permissions"`
}

type DockerBindOptions struct {
Propagation string `mapstructure:"propagation"`
}

type DockerVolumeOptions struct {
NoCopy bool `mapstructure:"no_copy"`
Labels []map[string]string `mapstructure:"labels"`
Expand Down Expand Up @@ -390,8 +396,29 @@ func NewDockerDriverConfig(task *structs.Task, env *env.TaskEnv) (*DockerDriverC
dconf.Mounts[i].Target = env.ReplaceEnv(m.Target)
dconf.Mounts[i].Source = env.ReplaceEnv(m.Source)

if m.Type == "" {
// default to `volume` type for backwards compatibility
m.Type = "volume"
}

if m.Type != "bind" && m.Type != "volume" {
return nil, fmt.Errorf(`Docker mount type must be "bind" or "volume"`)
}

if m.Type == "bind" && len(m.VolumeOptions) > 0 {
return nil, fmt.Errorf(`volume_options invalid on "bind" mount type`)
}

if m.Type == "volume" && len(m.BindOptions) > 0 {
return nil, fmt.Errorf(`bind_options invalid on "volume" mount type`)
}

if len(m.BindOptions) > 1 {
return nil, fmt.Errorf("only one bind_options stanza allowed")
}

if len(m.VolumeOptions) > 1 {
return nil, fmt.Errorf("Only one volume_options stanza allowed")
return nil, fmt.Errorf("only one volume_options stanza allowed")
}

if len(m.VolumeOptions) == 1 {
Expand Down Expand Up @@ -1342,26 +1369,51 @@ func (d *DockerDriver) createContainerConfig(ctx *ExecContext, task *structs.Tas
hm := docker.HostMount{
Target: m.Target,
Source: m.Source,
Type: "volume", // Only type supported
Type: m.Type,
ReadOnly: m.ReadOnly,
}
if len(m.VolumeOptions) == 1 {
vo := m.VolumeOptions[0]
hm.VolumeOptions = &docker.VolumeOptions{
NoCopy: vo.NoCopy,
}

if len(vo.DriverConfig) == 1 {
dc := vo.DriverConfig[0]
hm.VolumeOptions.DriverConfig = docker.VolumeDriverConfig{
Name: dc.Name,
if hm.Type == "bind" {
volumesEnabled := d.config.ReadBoolDefault(dockerVolumesConfigOption, dockerVolumesConfigDefault)
hm.Source = filepath.Clean(hm.Source)

if filepath.IsAbs(hm.Source) {
d.logger.Println("[DEBUG] driver.docker: Job contains an absolute path.")
if !volumesEnabled {
// Disallow mounting arbitrary absolute paths
return c, fmt.Errorf("%s is false; cannot bind mount host paths: %+q", dockerVolumesConfigOption, m.Source)
}
if len(dc.Options) == 1 {
hm.VolumeOptions.DriverConfig.Options = dc.Options[0]
} else {
// Expand path relative to alloc dir
hm.Source = filepath.Join(ctx.TaskDir.Dir, m.Source)
d.logger.Printf("[DEBUG] driver.docker: Job contains a relative path; expanding to %v", hm.Source)
}

if len(m.BindOptions) == 1 {
bo := m.BindOptions[0]
hm.BindOptions = &docker.BindOptions{
Propagation: bo.Propagation,
}
}
if len(vo.Labels) == 1 {
hm.VolumeOptions.Labels = vo.Labels[0]
} else {
if len(m.VolumeOptions) == 1 {
vo := m.VolumeOptions[0]
hm.VolumeOptions = &docker.VolumeOptions{
NoCopy: vo.NoCopy,
}

if len(vo.DriverConfig) == 1 {
dc := vo.DriverConfig[0]
hm.VolumeOptions.DriverConfig = docker.VolumeDriverConfig{
Name: dc.Name,
}
if len(dc.Options) == 1 {
hm.VolumeOptions.DriverConfig.Options = dc.Options[0]
}
}
if len(vo.Labels) == 1 {
hm.VolumeOptions.Labels = vo.Labels[0]
}
}
}
hostConfig.Mounts = append(hostConfig.Mounts, hm)
Expand Down
Loading

0 comments on commit e029a49

Please sign in to comment.