From 1a974a2d413942ebccf5232a59e21711adc1ac91 Mon Sep 17 00:00:00 2001 From: Florian Apolloner Date: Wed, 10 Mar 2021 16:16:23 +0100 Subject: [PATCH] Added support to specify default docker driver in plugin options. Fixes #6563 For backwards-compat, defaults to the previous defaults. --- drivers/docker/config.go | 18 ++++++++++++++ drivers/docker/driver.go | 9 +++---- drivers/docker/driver_test.go | 31 +++++++++++++++++++++++++ website/content/docs/drivers/docker.mdx | 10 +++++++- 4 files changed, 61 insertions(+), 7 deletions(-) diff --git a/drivers/docker/config.go b/drivers/docker/config.go index 9f956c905328..c92e42784783 100644 --- a/drivers/docker/config.go +++ b/drivers/docker/config.go @@ -206,6 +206,18 @@ var ( // extra docker labels, globs supported "extra_labels": hclspec.NewAttr("extra_labels", "list(string)", false), + // logging options + "logging": hclspec.NewDefault(hclspec.NewBlock("logging", false, hclspec.NewObject(map[string]*hclspec.Spec{ + "type": hclspec.NewAttr("type", "string", false), + "config": hclspec.NewBlockAttrs("config", "string", false), + })), hclspec.NewLiteral(`{ + type = "json-file" + config = { + max-file = "2" + max-size = "2m" + } + }`)), + // garbage collection options // default needed for both if the gc {...} block is not set and // if the default fields are missing @@ -616,6 +628,7 @@ type DriverConfig struct { PullActivityTimeout string `codec:"pull_activity_timeout"` pullActivityTimeoutDuration time.Duration `codec:"-"` ExtraLabels []string `codec:"extra_labels"` + Logging LoggingConfig `codec:"logging"` AllowRuntimesList []string `codec:"allow_runtimes"` allowRuntimes map[string]struct{} `codec:"-"` @@ -646,6 +659,11 @@ type VolumeConfig struct { SelinuxLabel string `codec:"selinuxlabel"` } +type LoggingConfig struct { + Type string `codec:"type"` + Config map[string]string `codec:"config"` +} + func (d *Driver) PluginInfo() (*base.PluginInfoResponse, error) { return pluginInfo, nil } diff --git a/drivers/docker/driver.go b/drivers/docker/driver.go index 2fa3cc24db75..6e574cbab2b7 100644 --- a/drivers/docker/driver.go +++ b/drivers/docker/driver.go @@ -876,12 +876,9 @@ func (d *Driver) createContainerConfig(task *drivers.TaskConfig, driverConfig *T } if hostConfig.LogConfig.Type == "" && hostConfig.LogConfig.Config == nil { - logger.Trace("no docker log driver provided, defaulting to json-file") - hostConfig.LogConfig.Type = "json-file" - hostConfig.LogConfig.Config = map[string]string{ - "max-file": "2", - "max-size": "2m", - } + logger.Trace("no docker log driver provided, defaulting to plugin config") + hostConfig.LogConfig.Type = d.config.Logging.Type + hostConfig.LogConfig.Config = d.config.Logging.Config } logger.Debug("configured resources", diff --git a/drivers/docker/driver_test.go b/drivers/docker/driver_test.go index 8f810580a357..19897a34a912 100644 --- a/drivers/docker/driver_test.go +++ b/drivers/docker/driver_test.go @@ -843,6 +843,37 @@ func TestDockerDriver_ExtraLabels(t *testing.T) { } } +func TestDockerDriver_LoggingConfiguration(t *testing.T) { + if !tu.IsCI() { + t.Parallel() + } + testutil.DockerCompatible(t) + + task, cfg, ports := dockerTask(t) + defer freeport.Return(ports) + + require.NoError(t, task.EncodeConcreteDriverConfig(cfg)) + + dockerClientConfig := make(map[string]interface{}) + loggerConfig := map[string]string{"gelf-address": "udp://1.2.3.4:12201", "tag": "gelf"} + + dockerClientConfig["logging"] = LoggingConfig{ + Type: "gelf", + Config: loggerConfig, + } + client, d, handle, cleanup := dockerSetup(t, task, dockerClientConfig) + defer cleanup() + require.NoError(t, d.WaitUntilStarted(task.ID, 5*time.Second)) + + container, err := client.InspectContainer(handle.containerID) + if err != nil { + t.Fatalf("err: %v", err) + } + + require.Equal(t, "gelf", container.HostConfig.LogConfig.Type) + require.Equal(t, loggerConfig, container.HostConfig.LogConfig.Config) +} + func TestDockerDriver_ForcePull(t *testing.T) { if !tu.IsCI() { t.Parallel() diff --git a/website/content/docs/drivers/docker.mdx b/website/content/docs/drivers/docker.mdx index 024715952ee4..77d14c46d414 100644 --- a/website/content/docs/drivers/docker.mdx +++ b/website/content/docs/drivers/docker.mdx @@ -868,10 +868,18 @@ plugin "docker" { capabilities and exclusively use host based log aggregation, you may consider this option to disable nomad log collection overhead. -- `extra_labels` - Extra labels to add to Docker containers. +- `extra_labels` - Extra labels to add to Docker containers. Available options are `job_name`, `job_id`, `task_group_name`, `task_name`, `namespace`, `node_name`, `node_id`. Globs are supported (e.g. `task*`) +- `logging` stanza: + + - `type` - Defaults to `"json-file"`. Specifies the logging driver docker should + use for all container nomad starts. + + - `config` - Defaults to `{ max-file = "2", max-size = "2m" }`. This option can be + used to pass further configuration to the logging driver. + - `gc` stanza: - `image` - Defaults to `true`. Changing this to `false` will prevent Nomad