Skip to content

Commit

Permalink
Cherry-pick #10905 to 7.0: Add cleanup_timeout option to docker autod…
Browse files Browse the repository at this point in the history
…iscover (#11244)

cleanup_timeout is used in kubernetes autodiscover to wait some time before the
configurations associated to stopped containers are removed. Add an equivalent
option to docker autodiscover.

Fix #10374

(cherry picked from commit f771497)
  • Loading branch information
jsoriano committed Mar 14, 2019
1 parent a0491cf commit 6cad266
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ https://github.com/elastic/beats/compare/v7.0.0-beta1...master[Check the HEAD di

- On Google Cloud Engine (GCE) the add_cloud_metadata will now trim the project
info from the cloud.machine.type and cloud.availability_zone. {issue}10968[10968]
- Add `cleanup_timeout` option to docker autodiscover, to wait some time before removing configurations after a container is stopped. {issue]10374[10374] {pull}10905[10905]

*Auditbeat*

Expand Down
1 change: 1 addition & 0 deletions filebeat/tests/system/test_autodiscover.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def test_docker(self):
inputs=False,
autodiscover={
'docker': {
'cleanup_timeout': '0s',
'templates': '''
- condition:
equals.docker.container.image: busybox
Expand Down
26 changes: 15 additions & 11 deletions libbeat/autodiscover/providers/docker/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,32 @@
package docker

import (
"time"

"github.com/elastic/beats/libbeat/autodiscover/template"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/common/docker"
)

// Config for docker autodiscover provider
type Config struct {
Host string `config:"host"`
TLS *docker.TLSConfig `config:"ssl"`
Prefix string `config:"prefix"`
HintsEnabled bool `config:"hints.enabled"`
Builders []*common.Config `config:"builders"`
Appenders []*common.Config `config:"appenders"`
Templates template.MapperSettings `config:"templates"`
Dedot bool `config:"labels.dedot"`
Host string `config:"host"`
TLS *docker.TLSConfig `config:"ssl"`
Prefix string `config:"prefix"`
HintsEnabled bool `config:"hints.enabled"`
Builders []*common.Config `config:"builders"`
Appenders []*common.Config `config:"appenders"`
Templates template.MapperSettings `config:"templates"`
Dedot bool `config:"labels.dedot"`
CleanupTimeout time.Duration `config:"cleanup_timeout"`
}

func defaultConfig() *Config {
return &Config{
Host: "unix:///var/run/docker.sock",
Prefix: "co.elastic",
Dedot: true,
Host: "unix:///var/run/docker.sock",
Prefix: "co.elastic",
Dedot: true,
CleanupTimeout: 60 * time.Second,
}
}

Expand Down
5 changes: 4 additions & 1 deletion libbeat/autodiscover/providers/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package docker

import (
"errors"
"time"

"github.com/gofrs/uuid"

Expand Down Expand Up @@ -115,7 +116,9 @@ func (d *Provider) Start() {
d.emitContainer(event, "start")

case event := <-d.stopListener.Events():
d.emitContainer(event, "stop")
time.AfterFunc(d.config.CleanupTimeout, func() {
d.emitContainer(event, "stop")
})
}
}
}()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ func TestDockerStart(t *testing.T) {
t.Fatal(err)
}
bus := bus.New("test")
config := common.NewConfig()
provider, err := AutodiscoverBuilder(bus, UUID, config)
config := defaultConfig()
config.CleanupTimeout = 0
provider, err := AutodiscoverBuilder(bus, UUID, common.MustNewConfigFrom(config))
if err != nil {
t.Fatal(err)
}
Expand Down
3 changes: 3 additions & 0 deletions metricbeat/tests/system/test_autodiscover.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def test_docker(self):
self.render_config_template(
autodiscover={
'docker': {
'cleanup_timeout': '0s',
'templates': '''
- condition:
equals.docker.container.image: memcached:latest
Expand Down Expand Up @@ -69,6 +70,7 @@ def test_docker_labels(self):
self.render_config_template(
autodiscover={
'docker': {
'cleanup_timeout': '0s',
'hints.enabled': 'true',
},
},
Expand Down Expand Up @@ -111,6 +113,7 @@ def test_config_appender(self):
self.render_config_template(
autodiscover={
'docker': {
'cleanup_timeout': '0s',
'hints.enabled': 'true',
'appenders': '''
- type: config
Expand Down

0 comments on commit 6cad266

Please sign in to comment.