Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fail with error when autodiscover providers have no defined configs #13078

Merged
merged 8 commits into from
Sep 13, 2019
23 changes: 16 additions & 7 deletions libbeat/autodiscover/providers/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
package docker

import (
"errors"
"fmt"
"time"

"github.com/gofrs/uuid"
"github.com/pkg/errors"

"github.com/elastic/beats/libbeat/autodiscover"
"github.com/elastic/beats/libbeat/autodiscover/builder"
Expand Down Expand Up @@ -55,37 +56,45 @@ type Provider struct {
// AutodiscoverBuilder builds and returns an autodiscover provider
func AutodiscoverBuilder(bus bus.Bus, uuid uuid.UUID, c *common.Config) (autodiscover.Provider, error) {
cfgwarn.Beta("The docker autodiscover is beta")

errWrap := func(err error) error {
return errors.Wrap(err, "error setting up docker autodiscover provider")
}

config := defaultConfig()
err := c.Unpack(&config)
if err != nil {
return nil, err
return nil, errWrap(err)
}

watcher, err := docker.NewWatcher(config.Host, config.TLS, false)
if err != nil {
return nil, err
return nil, errWrap(err)
}

mapper, err := template.NewConfigMapper(config.Templates)
if err != nil {
return nil, err
return nil, errWrap(err)
}
if len(mapper) == 0 && !config.Hints.Enabled() {
return nil, errWrap(fmt.Errorf("no configs or hints defined for autodiscover provider"))
}

builders, err := autodiscover.NewBuilders(config.Builders, config.Hints)
if err != nil {
return nil, err
return nil, errWrap(err)
}

appenders, err := autodiscover.NewAppenders(config.Appenders)
if err != nil {
return nil, err
return nil, errWrap(err)
}

start := watcher.ListenStart()
stop := watcher.ListenStop()

if err := watcher.Start(); err != nil {
return nil, err
return nil, errWrap(err)
}

return &Provider{
Expand Down
18 changes: 14 additions & 4 deletions libbeat/autodiscover/providers/jolokia/jolokia.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
package jolokia

import (
"fmt"

"github.com/gofrs/uuid"
"github.com/pkg/errors"

"github.com/elastic/beats/libbeat/autodiscover"
"github.com/elastic/beats/libbeat/autodiscover/template"
Expand Down Expand Up @@ -50,10 +53,14 @@ type Provider struct {
// AutodiscoverBuilder builds a Jolokia Discovery autodiscover provider, it fails if
// there is some problem with the configuration
func AutodiscoverBuilder(bus bus.Bus, uuid uuid.UUID, c *common.Config) (autodiscover.Provider, error) {
errWrap := func(err error) error {
return errors.Wrap(err, "error setting up jolokia autodiscover provider")
}

config := defaultConfig()
err := c.Unpack(&config)
if err != nil {
return nil, err
return nil, errWrap(err)
}

discovery := &Discovery{
Expand All @@ -63,17 +70,20 @@ func AutodiscoverBuilder(bus bus.Bus, uuid uuid.UUID, c *common.Config) (autodis

mapper, err := template.NewConfigMapper(config.Templates)
if err != nil {
return nil, err
return nil, errWrap(err)
}
if len(mapper) == 0 {
return nil, errWrap(fmt.Errorf("no configs defined for autodiscover provider"))
}

builders, err := autodiscover.NewBuilders(config.Builders, nil)
if err != nil {
return nil, err
return nil, errWrap(err)
}

appenders, err := autodiscover.NewAppenders(config.Appenders)
if err != nil {
return nil, err
return nil, errWrap(err)
}

return &Provider{
Expand Down
23 changes: 16 additions & 7 deletions libbeat/autodiscover/providers/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"time"

"github.com/gofrs/uuid"
"github.com/pkg/errors"

"github.com/elastic/beats/libbeat/autodiscover"
"github.com/elastic/beats/libbeat/autodiscover/builder"
Expand Down Expand Up @@ -53,20 +54,25 @@ type Provider struct {
// AutodiscoverBuilder builds and returns an autodiscover provider
func AutodiscoverBuilder(bus bus.Bus, uuid uuid.UUID, c *common.Config) (autodiscover.Provider, error) {
cfgwarn.Beta("The kubernetes autodiscover is beta")

errWrap := func(err error) error {
return errors.Wrap(err, "error setting up kubernetes autodiscover provider")
}

config := defaultConfig()
err := c.Unpack(&config)
if err != nil {
return nil, err
return nil, errWrap(err)
}

client, err := kubernetes.GetKubernetesClient(config.InCluster, config.KubeConfig)
if err != nil {
return nil, err
return nil, errWrap(err)
}

metagen, err := kubernetes.NewMetaGenerator(c)
if err != nil {
return nil, err
return nil, errWrap(err)
}

config.Host = kubernetes.DiscoverKubernetesNode(config.Host, config.InCluster, client)
Expand All @@ -78,22 +84,25 @@ func AutodiscoverBuilder(bus bus.Bus, uuid uuid.UUID, c *common.Config) (autodis
})
if err != nil {
logp.Err("kubernetes: Couldn't create watcher for %T", &kubernetes.Pod{})
return nil, err
return nil, errWrap(err)
}

mapper, err := template.NewConfigMapper(config.Templates)
if err != nil {
return nil, err
return nil, errWrap(err)
}
if len(mapper) == 0 && !config.Hints.Enabled() {
return nil, errWrap(fmt.Errorf("no configs or hints defined for autodiscover provider"))
}

builders, err := autodiscover.NewBuilders(config.Builders, config.Hints)
if err != nil {
return nil, err
return nil, errWrap(err)
}

appenders, err := autodiscover.NewAppenders(config.Appenders)
if err != nil {
return nil, err
return nil, errWrap(err)
}

p := &Provider{
Expand Down