Skip to content

Commit

Permalink
Support usage of custom builders without hints and mappers (elastic#1…
Browse files Browse the repository at this point in the history
…3839)

* Support usage of custom builders without hints and mappers
  • Loading branch information
vjsamuel authored and jorgemarey committed Dec 13, 2019
1 parent 90a2a78 commit 4df4392
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Kubernetes watcher at `add_kubernetes_metadata` fails with StatefulSets {pull}13905[13905]
- Fix panics that could result from invalid TLS certificates. This can affect Beats that connect over
TLS or Beats that accept connections over TLS and validate client certificates. {pull}14146[14146]
- Support usage of custom builders without hints and mappers {pull}13839[13839]

*Auditbeat*

Expand Down
9 changes: 8 additions & 1 deletion libbeat/autodiscover/providers/kubernetes/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package kubernetes

import (
"fmt"
"time"

"github.com/elastic/beats/libbeat/autodiscover/template"
Expand Down Expand Up @@ -50,9 +51,15 @@ func defaultConfig() *Config {
}

// Validate ensures correctness of config
func (c *Config) Validate() {
func (c *Config) Validate() error {
// Make sure that prefix doesn't ends with a '.'
if c.Prefix[len(c.Prefix)-1] == '.' && c.Prefix != "." {
c.Prefix = c.Prefix[:len(c.Prefix)-2]
}

if len(c.Templates) == 0 && !c.Hints.Enabled() && len(c.Builders) == 0 {
return fmt.Errorf("no configs or hints defined for autodiscover provider")
}

return nil
}
65 changes: 65 additions & 0 deletions libbeat/autodiscover/providers/kubernetes/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package kubernetes

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/elastic/beats/libbeat/autodiscover"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/common/bus"
)

func TestConfigWithCustomBuilders(t *testing.T) {
autodiscover.Registry.AddBuilder("mock", newMockBuilder)

cfg := common.MapStr{
"hints.enabled": false,
"builders": []common.MapStr{
{
"mock": common.MapStr{},
},
},
}

config := common.MustNewConfigFrom(&cfg)
c := defaultConfig()
err := config.Unpack(&c)
assert.Nil(t, err)

cfg1 := common.MapStr{
"hints.enabled": false,
}
config, err = common.NewConfigFrom(&cfg1)
c = defaultConfig()
err = config.Unpack(&c)
assert.NotNil(t, err)
}

type mockBuilder struct {
}

func newMockBuilder(_ *common.Config) (autodiscover.Builder, error) {
return &mockBuilder{}, nil
}

func (m *mockBuilder) CreateConfig(event bus.Event) []*common.Config {
return nil
}
3 changes: 0 additions & 3 deletions libbeat/autodiscover/providers/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,6 @@ func AutodiscoverBuilder(bus bus.Bus, uuid uuid.UUID, c *common.Config) (autodis
if err != nil {
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 {
Expand Down

0 comments on commit 4df4392

Please sign in to comment.