diff --git a/CHANGELOG.next.asciidoc b/CHANGELOG.next.asciidoc index f4871d41df69..5e3a2efd0c83 100644 --- a/CHANGELOG.next.asciidoc +++ b/CHANGELOG.next.asciidoc @@ -30,6 +30,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d - ILM will be available by default if Elasticsearch > 7.0 is used. {pull}10347[10347] - Allow Central Management to send events back to kibana. {issue}9382[9382] - Initialize the Paths before the keystore and save the keystore into `data/{beatname}.keystore`. {pull}10706[10706] +- 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* diff --git a/filebeat/tests/system/test_autodiscover.py b/filebeat/tests/system/test_autodiscover.py index 973577920d33..c25ec9081024 100644 --- a/filebeat/tests/system/test_autodiscover.py +++ b/filebeat/tests/system/test_autodiscover.py @@ -23,6 +23,7 @@ def test_docker(self): inputs=False, autodiscover={ 'docker': { + 'cleanup_timeout': '0s', 'templates': ''' - condition: equals.docker.container.image: busybox diff --git a/libbeat/autodiscover/providers/docker/config.go b/libbeat/autodiscover/providers/docker/config.go index bba4f32cdb0c..1f3bae7aa5b4 100644 --- a/libbeat/autodiscover/providers/docker/config.go +++ b/libbeat/autodiscover/providers/docker/config.go @@ -18,6 +18,8 @@ package docker import ( + "time" + "github.com/elastic/beats/libbeat/autodiscover/template" "github.com/elastic/beats/libbeat/common" "github.com/elastic/beats/libbeat/common/docker" @@ -34,13 +36,15 @@ type Config struct { 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, } } diff --git a/libbeat/autodiscover/providers/docker/docker.go b/libbeat/autodiscover/providers/docker/docker.go index fb0901e55e51..f0c4d0438fa2 100644 --- a/libbeat/autodiscover/providers/docker/docker.go +++ b/libbeat/autodiscover/providers/docker/docker.go @@ -19,6 +19,7 @@ package docker import ( "errors" + "time" "github.com/gofrs/uuid" @@ -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") + }) } } }() diff --git a/libbeat/autodiscover/providers/docker/docker_integration_test.go b/libbeat/autodiscover/providers/docker/docker_integration_test.go index d790744aa489..ccba884df9ee 100644 --- a/libbeat/autodiscover/providers/docker/docker_integration_test.go +++ b/libbeat/autodiscover/providers/docker/docker_integration_test.go @@ -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) } diff --git a/metricbeat/tests/system/test_autodiscover.py b/metricbeat/tests/system/test_autodiscover.py index 1b41e80a1dfd..ae135a8f7fc5 100644 --- a/metricbeat/tests/system/test_autodiscover.py +++ b/metricbeat/tests/system/test_autodiscover.py @@ -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 @@ -69,6 +70,7 @@ def test_docker_labels(self): self.render_config_template( autodiscover={ 'docker': { + 'cleanup_timeout': '0s', 'hints.enabled': 'true', }, }, @@ -111,6 +113,7 @@ def test_config_appender(self): self.render_config_template( autodiscover={ 'docker': { + 'cleanup_timeout': '0s', 'hints.enabled': 'true', 'appenders': ''' - type: config