Skip to content

Commit

Permalink
add constraints to docker driver for json-file logs
Browse files Browse the repository at this point in the history
  • Loading branch information
jazzyfresh committed Jul 1, 2019
1 parent fcd42f6 commit b3a2e2c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
8 changes: 8 additions & 0 deletions drivers/docker/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,14 @@ func (d *Driver) createContainerConfig(task *drivers.TaskConfig, driverConfig *T
Config: driverConfig.Logging.Config,
}

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 = make(map[string]string)
hostConfig.LogConfig.Config["max-file"] = "2"
hostConfig.LogConfig.Config["max-size"] = "2m"
}

logger.Debug("configured resources", "memory", hostConfig.Memory,
"cpu_shares", hostConfig.CPUShares, "cpu_quota", hostConfig.CPUQuota,
"cpu_period", hostConfig.CPUPeriod)
Expand Down
42 changes: 37 additions & 5 deletions drivers/docker/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1014,25 +1014,55 @@ func TestDockerDriver_CreateContainerConfig_Logging(t *testing.T) {
cases := []struct {
name string
loggingConfig DockerLogging
expectedDriver string
expectedConfig DockerLogging
}{
{
"simple type",
DockerLogging{Type: "fluentd"},
"fluentd",
DockerLogging{
Type: "fluentd",
Config: map[string]string{},
},
},
{
"simple driver",
DockerLogging{Driver: "fluentd"},
"fluentd",
DockerLogging{
Type: "fluentd",
Config: map[string]string{},
},
},
{
"type takes precedence",
DockerLogging{
Type: "json-file",
Driver: "fluentd",
},
"json-file",
DockerLogging{
Type: "json-file",
Config: map[string]string{},
},
},
{
"user config takes precedence, even if no type provided",
DockerLogging{
Type: "",
Config: map[string]string{"max-file": "3", "max-size": "10m"},
},
DockerLogging{
Type: "",
Config: map[string]string{"max-file": "3", "max-size": "10m"},
},
},
{
"defaults to json-file w/ log rotation",
DockerLogging{
Type: "",
},
DockerLogging{
Type: "json-file",
Config: map[string]string{"max-file": "2", "max-size": "2m"},
},
},
}

Expand All @@ -1049,7 +1079,9 @@ func TestDockerDriver_CreateContainerConfig_Logging(t *testing.T) {
cc, err := driver.createContainerConfig(task, cfg, "org/repo:0.1")
require.NoError(t, err)

require.Equal(t, c.expectedDriver, cc.HostConfig.LogConfig.Type)
require.Equal(t, c.expectedConfig.Type, cc.HostConfig.LogConfig.Type)
require.Equal(t, c.expectedConfig.Config["max-file"], cc.HostConfig.LogConfig.Config["max-file"])
require.Equal(t, c.expectedConfig.Config["max-size"], cc.HostConfig.LogConfig.Config["max-size"])
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion website/source/docs/drivers/docker.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ The `docker` driver supports the following configuration in the job spec. Only
```

* `logging` - (Optional) A key-value map of Docker logging options. The default
value is `syslog`.
value is `json-file`.

```hcl
config {
Expand Down

0 comments on commit b3a2e2c

Please sign in to comment.