-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implemented bind mount type; added tests #4761
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"` | ||
} | ||
|
||
|
@@ -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"` | ||
|
@@ -391,8 +397,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 { | ||
|
@@ -1353,26 +1380,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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should there be a different client option for allowing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did this to preserve configuration compatibility between the |
||
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) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
type
should imo be defaulted tovolume
to keep it compat for nowThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree. Added in 4432486#diff-a6757d41ad7cd7f37f0f0243e3575307R403