Skip to content

Commit

Permalink
Merge pull request #3021 from clinta/docker-mount2
Browse files Browse the repository at this point in the history
Expose docker mount options
  • Loading branch information
schmichael authored Aug 17, 2017
2 parents 9d4b98f + 1378f03 commit ad94097
Show file tree
Hide file tree
Showing 16 changed files with 513 additions and 51 deletions.
102 changes: 102 additions & 0 deletions client/driver/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,38 @@ type DockerLoggingOpts struct {
Config map[string]string `mapstructure:"-"`
}

type Mount struct {
Target string `mapstructure:"target"`
Source string `mapstructure:"source"`
Type string `mapstructure:"type"`
ReadOnly bool `mapstructure:"readonly"`
BindOptions *BindOptions `mapstructure:"bind_options"`
VolumeOptions *VolumeOptions `mapstructure:"volume_options"`
TempfsOptions *TempfsOptions `mapstructure:"tempfs_options"`
}

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

type VolumeOptions struct {
NoCopy bool `mapstructure:"no_copy"`
Labels map[string]string `mapstructure:"labels"`
DriverConfig VolumeDriverConfig `mapstructure:"driver_config"`
}

// TempfsOptions contains optional configuration for the tempfs type
type TempfsOptions struct {
SizeBytes int64 `mapstructure:"size_bytes"`
Mode int `mapstructure:"mode"`
}

// VolumeDriverConfig holds a map of volume driver specific options
type VolumeDriverConfig struct {
Name string `mapstructure:"name"`
Options map[string]string `mapstructure:"options"`
}

type DockerDriverConfig struct {
ImageName string `mapstructure:"image"` // Container's Image Name
LoadImage string `mapstructure:"load"` // LoadImage is a path to an image archive file
Expand Down Expand Up @@ -166,6 +198,7 @@ type DockerDriverConfig struct {
WorkDir string `mapstructure:"work_dir"` // Working directory inside the container
Logging []DockerLoggingOpts `mapstructure:"logging"` // Logging options for syslog server
Volumes []string `mapstructure:"volumes"` // Host-Volumes to mount in, syntax: /path/to/host/directory:/destination/path/in/container
Mounts []Mount `mapstructure:"mounts"` // Docker volumes to mount
VolumeDriver string `mapstructure:"volume_driver"` // Docker volume driver used for the container's volumes
ForcePull bool `mapstructure:"force_pull"` // Always force pull before running image, useful if your tags are mutable
MacAddress string `mapstructure:"mac_address"` // Pin mac address to container
Expand Down Expand Up @@ -236,6 +269,40 @@ func NewDockerDriverConfig(task *structs.Task, env *env.TaskEnv) (*DockerDriverC
}
}

for i, m := range dconf.Mounts {
dconf.Mounts[i].Target = env.ReplaceEnv(m.Target)
dconf.Mounts[i].Source = env.ReplaceEnv(m.Source)
dconf.Mounts[i].Type = env.ReplaceEnv(m.Type)
if dconf.Mounts[i].Type == "" {
dconf.Mounts[i].Type = "volume"
}
if dconf.Mounts[i].Type != "volume" {
return nil, fmt.Errorf("mount type %v is not supported")
}
if m.BindOptions != nil {
dconf.Mounts[i].BindOptions.Propagation = env.ReplaceEnv(m.BindOptions.Propagation)
}
if m.VolumeOptions != nil {
if m.VolumeOptions.Labels != nil {
for k, v := range m.VolumeOptions.Labels {
if k != env.ReplaceEnv(k) {
delete(dconf.Mounts[i].VolumeOptions.Labels, k)
}
dconf.Mounts[i].VolumeOptions.Labels[env.ReplaceEnv(k)] = env.ReplaceEnv(v)
}
}
dconf.Mounts[i].VolumeOptions.DriverConfig.Name = env.ReplaceEnv(m.VolumeOptions.DriverConfig.Name)
if m.VolumeOptions.DriverConfig.Options != nil {
for k, v := range m.VolumeOptions.DriverConfig.Options {
if k != env.ReplaceEnv(k) {
delete(dconf.Mounts[i].VolumeOptions.DriverConfig.Options, k)
}
dconf.Mounts[i].VolumeOptions.DriverConfig.Options[env.ReplaceEnv(k)] = env.ReplaceEnv(v)
}
}
}
}

if len(dconf.Logging) > 0 {
dconf.Logging[0].Config = mapMergeStrStr(dconf.Logging[0].ConfigRaw...)
}
Expand Down Expand Up @@ -457,6 +524,9 @@ func (d *DockerDriver) Validate(config map[string]interface{}) error {
"volume_driver": &fields.FieldSchema{
Type: fields.TypeString,
},
"mounts": {
Type: fields.TypeArray,
},
"force_pull": &fields.FieldSchema{
Type: fields.TypeBool,
},
Expand Down Expand Up @@ -940,6 +1010,38 @@ func (d *DockerDriver) createContainerConfig(ctx *ExecContext, task *structs.Tas
}
}

// Setup mounts
for _, m := range driverConfig.Mounts {
hm := docker.HostMount{
Target: m.Target,
Source: m.Source,
Type: m.Type,
ReadOnly: m.ReadOnly,
}
if m.BindOptions != nil {
hm.BindOptions = &docker.BindOptions{
Propagation: m.BindOptions.Propagation,
}
}
if m.VolumeOptions != nil {
hm.VolumeOptions = &docker.VolumeOptions{
NoCopy: m.VolumeOptions.NoCopy,
Labels: m.VolumeOptions.Labels,
DriverConfig: docker.VolumeDriverConfig{
Name: m.VolumeOptions.DriverConfig.Name,
Options: m.VolumeOptions.DriverConfig.Options,
},
}
}
if m.TempfsOptions != nil {
hm.TempfsOptions = &docker.TempfsOptions{
SizeBytes: m.TempfsOptions.SizeBytes,
Mode: m.TempfsOptions.Mode,
}
}
hostConfig.Mounts = append(hostConfig.Mounts, hm)
}

// set DNS search domains and extra hosts
hostConfig.DNSSearch = driverConfig.DNSSearchDomains
hostConfig.DNSOptions = driverConfig.DNSOptions
Expand Down
13 changes: 13 additions & 0 deletions vendor/github.com/fsouza/go-dockerclient/AUTHORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 14 additions & 1 deletion vendor/github.com/fsouza/go-dockerclient/README.markdown

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions vendor/github.com/fsouza/go-dockerclient/appveyor.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 49 additions & 3 deletions vendor/github.com/fsouza/go-dockerclient/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ad94097

Please sign in to comment.