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

Make kubernetes indexers/matchers pluggable #4151

Merged
merged 1 commit into from
May 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ https://github.com/elastic/beats/compare/v5.1.1...master[Check the HEAD diff]
- Add support for include_labels and include_annotations in kubernetes processor {pull}4043[4043]
- Support new `index_patterns` field when loading templates for Elasticsearch >= 6.0 {pull}4056[4056]
- Adding goimports support to make check and fmt {pull}4114[4114]
- Make kubernetes indexers/matchers pluggable {pull}4151[4151]

*Filebeat*

Expand Down
10 changes: 5 additions & 5 deletions libbeat/processors/kubernetes/indexing.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,20 @@ type Matchers struct {
// Register contains Indexer and Matchers to use on pod indexing and event matching
type Register struct {
sync.RWMutex
indexers map[string]IndexConstructor
indexers map[string]IndexerConstructor
matchers map[string]MatcherConstructor

defaultIndexerConfigs map[string]common.Config
defaultMatcherConfigs map[string]common.Config
}

type IndexConstructor func(config common.Config, genMeta GenMeta) (Indexer, error)
type IndexerConstructor func(config common.Config, genMeta GenMeta) (Indexer, error)
type MatcherConstructor func(config common.Config) (Matcher, error)

// NewRegister creates and returns a new Register.
func NewRegister() *Register {
return &Register{
indexers: make(map[string]IndexConstructor, 0),
indexers: make(map[string]IndexerConstructor, 0),
matchers: make(map[string]MatcherConstructor, 0),

defaultIndexerConfigs: make(map[string]common.Config, 0),
Expand All @@ -89,7 +89,7 @@ func NewRegister() *Register {
}

// AddIndexer to the register
func (r *Register) AddIndexer(name string, indexer IndexConstructor) {
func (r *Register) AddIndexer(name string, indexer IndexerConstructor) {
r.RWMutex.Lock()
defer r.RWMutex.Unlock()
r.indexers[name] = indexer
Expand All @@ -113,7 +113,7 @@ func (r *Register) AddDefaultMatcherConfig(name string, config common.Config) {
}

// AddIndexer to the register
func (r *Register) GetIndexer(name string) IndexConstructor {
func (r *Register) GetIndexer(name string) IndexerConstructor {
indexer, ok := r.indexers[name]
if ok {
return indexer
Expand Down
63 changes: 63 additions & 0 deletions libbeat/processors/kubernetes/registry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package kubernetes

import (
"errors"
"fmt"

p "github.com/elastic/beats/libbeat/plugin"
)

var (
indexerKey = "libbeat.processor.kubernetes.indexer"
matcherKey = "libbeat.processor.kubernetes.matcher"
)

type indexerPlugin struct {
name string
constructor IndexerConstructor
}

func IndexerPlugin(name string, c IndexerConstructor) map[string][]interface{} {
return p.MakePlugin(indexerKey, indexerPlugin{name, c})
}

type matcherPlugin struct {
name string
constructor MatcherConstructor
}

func MatcherPlugin(name string, m MatcherConstructor) map[string][]interface{} {
return p.MakePlugin(matcherKey, matcherPlugin{name, m})
}

func init() {
p.MustRegisterLoader(indexerKey, func(ifc interface{}) error {
i, ok := ifc.(indexerPlugin)
if !ok {
return errors.New("plugin does not match output plugin type")
}

name := i.name
if Indexing.indexers[name] != nil {
return fmt.Errorf("indexer type %v already registered", name)
}

Indexing.AddIndexer(name, i.constructor)
return nil
})

p.MustRegisterLoader(matcherKey, func(ifc interface{}) error {
m, ok := ifc.(matcherPlugin)
if !ok {
return errors.New("plugin does not match output plugin type")
}

name := m.name
if Indexing.indexers[name] != nil {
return fmt.Errorf("matcher type %v already registered", name)
}

Indexing.AddMatcher(name, m.constructor)
return nil
})
}