From 8b3ea4ea9aa57f4d61414bf28d68399887650e4f Mon Sep 17 00:00:00 2001 From: Florian Apolloner Date: Fri, 12 Mar 2021 22:04:33 +0100 Subject: [PATCH] docker: support configuring default log driver in plugin options --- CHANGELOG.md | 1 + drivers/docker/config.go | 18 +++++++++++++++ drivers/docker/driver.go | 9 +++----- drivers/docker/driver_test.go | 29 +++++++++++++++++++++++++ website/content/docs/drivers/docker.mdx | 15 ++++++++++++- 5 files changed, 65 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e82d70345fe..07d2e190f077 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ IMPROVEMENTS: * cli: Update defaults for `nomad operator debug` flags `-interval` and `-server-id` to match common usage. [[GH-10121](https://github.com/hashicorp/nomad/issues/10121)] * consul/connect: Enable setting `local_bind_address` field on connect upstreams [[GH-6248](https://github.com/hashicorp/nomad/issues/6248)] * driver/docker: Added support for optional extra container labels. [[GH-9885](https://github.com/hashicorp/nomad/issues/9885)] + * driver/docker: Added support for configuring default logger behavior in the client configuration. [[GH-10156](https://github.com/hashicorp/nomad/issues/10156)] ## 1.0.4 (February 24, 2021) 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..c748264f6c2c 100644 --- a/drivers/docker/driver_test.go +++ b/drivers/docker/driver_test.go @@ -843,6 +843,35 @@ 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) + require.NoError(t, 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..44c4e4b16086 100644 --- a/website/content/docs/drivers/docker.mdx +++ b/website/content/docs/drivers/docker.mdx @@ -868,10 +868,23 @@ 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 containers Nomad starts. Note that for older versions + of Docker, only `json-file` file or `journald` will allow Nomad to read + the driver's logs via the Docker API, and this will prevent commands such + as `nomad alloc logs` from functioning. + + - `config` - Defaults to `{ max-file = "2", max-size = "2m" }`. This option + can also be used to pass further + [configuration](https://docs.docker.com/config/containers/logging/configure/) + to the logging driver. + - `gc` stanza: - `image` - Defaults to `true`. Changing this to `false` will prevent Nomad