Skip to content

Commit

Permalink
Make conditions in autodiscovery configs optional (elastic#9029)
Browse files Browse the repository at this point in the history
* Make conditions in autodiscovery configs optional

This change lets users leave the `condition` option undeclared when writing autodiscover configurations. When not declared all configurations will match all events.

* Cleanup

* Fix fmt

* Fix changelog

* Fix failing conditions test

(cherry picked from commit a1bec73)
  • Loading branch information
andrewvc committed Dec 4, 2018
1 parent f8f6801 commit 207ca58
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ https://github.com/elastic/beats/compare/v6.4.0...v6.5.0[View commits]
- Add Beats Central Management {pull}8559[8559]
- Report configured queue type. {pull}8091[8091]
- Enable `host` and `cloud` metadata processors by default. {pull}8596[8596]
- Autodiscovery no longer requires that the `condition` field be set. If left unset all configs will be matched. {pull}9029[9029]
*Filebeat*
Expand Down
2 changes: 1 addition & 1 deletion libbeat/autodiscover/providers/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type Provider struct {
builders autodiscover.Builders
appenders autodiscover.Appenders
watcher docker.Watcher
templates *template.Mapper
templates template.Mapper
stop chan interface{}
startListener bus.Listener
stopListener bus.Listener
Expand Down
2 changes: 1 addition & 1 deletion libbeat/autodiscover/providers/jolokia/jolokia.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type Provider struct {
bus bus.Bus
builders autodiscover.Builders
appenders autodiscover.Appenders
templates *template.Mapper
templates template.Mapper
discovery DiscoveryProber
}

Expand Down
2 changes: 1 addition & 1 deletion libbeat/autodiscover/providers/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type Provider struct {
bus bus.Bus
watcher kubernetes.Watcher
metagen kubernetes.MetaGenerator
templates *template.Mapper
templates template.Mapper
builders autodiscover.Builders
appenders autodiscover.Appenders
}
Expand Down
28 changes: 15 additions & 13 deletions libbeat/autodiscover/template/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,20 @@ type MapperSettings []*struct {
}

// NewConfigMapper builds a template Mapper from given settings
func NewConfigMapper(configs MapperSettings) (*Mapper, error) {
var mapper Mapper
func NewConfigMapper(configs MapperSettings) (mapper Mapper, err error) {
for _, c := range configs {
condition, err := conditions.NewCondition(c.ConditionConfig)
if err != nil {
return nil, err
condMap := &ConditionMap{Configs: c.Configs}
if c.ConditionConfig != nil {
condMap.Condition, err = conditions.NewCondition(c.ConditionConfig)
if err != nil {
return nil, err
}
}
mapper = append(mapper, &ConditionMap{
Condition: condition,
Configs: c.Configs,
})

mapper = append(mapper, condMap)
}

return &mapper, nil
return mapper, nil
}

// Event adapts MapStr to processors.ValuesMap interface
Expand All @@ -71,11 +71,13 @@ func (e Event) GetValue(key string) (interface{}, error) {
}

// GetConfig returns a matching Config if any, nil otherwise
func (c *Mapper) GetConfig(event bus.Event) []*common.Config {
func (c Mapper) GetConfig(event bus.Event) []*common.Config {
var result []*common.Config

for _, mapping := range *c {
if mapping.Configs != nil && !mapping.Condition.Check(Event(event)) {
for _, mapping := range c {
// An empty condition matches everything
conditionOk := mapping.Condition == nil || mapping.Condition.Check(Event(event))
if mapping.Configs != nil && !conditionOk {
continue
}

Expand Down
13 changes: 12 additions & 1 deletion libbeat/autodiscover/template/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ func TestConfigsMapping(t *testing.T) {
},
expected: []*common.Config{config},
},
// No condition
{
mapping: `
- config:
- correct: config`,
event: bus.Event{
"foo": 3,
},
expected: []*common.Config{config},
},
}

for _, test := range tests {
Expand Down Expand Up @@ -98,5 +108,6 @@ func TestNilConditionConfig(t *testing.T) {
}

_, err = NewConfigMapper(mappings)
assert.Error(t, err)
assert.NoError(t, err)
assert.Nil(t, mappings[0].ConditionConfig)
}
4 changes: 2 additions & 2 deletions libbeat/docs/shared-autodiscover.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ config file. To enable autodiscover, you specify a list of providers.
=== Providers

Autodiscover providers work by watching for events on the system and translating those events into internal autodiscover
events with a common format. When you configure the provider, you can use fields from the autodiscover event to set
conditions that, when met, launch specific configurations.
events with a common format. When you configure the provider, you can optionally use fields from the autodiscover event
to set conditions that, when met, launch specific configurations.

On start, {beatname_uc} will scan existing containers and launch the proper configs for them. Then it will watch for new
start/stop events. This ensures you don't need to worry about state, but only define your desired configs.
Expand Down

0 comments on commit 207ca58

Please sign in to comment.