From 10359595390ff38818f4772a27d5acab5a101796 Mon Sep 17 00:00:00 2001 From: Vijay Samuel Date: Fri, 23 Feb 2018 03:55:40 -0800 Subject: [PATCH] Add plugin support for autodiscover builders, providers --- CHANGELOG.asciidoc | 1 + libbeat/autodiscover/builder/plugin.go | 31 ++++++++++++++++++++++++ libbeat/autodiscover/providers/plugin.go | 31 ++++++++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 libbeat/autodiscover/builder/plugin.go create mode 100644 libbeat/autodiscover/providers/plugin.go diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc index 97d30fef375d..685132335ccf 100644 --- a/CHANGELOG.asciidoc +++ b/CHANGELOG.asciidoc @@ -179,6 +179,7 @@ https://github.com/elastic/beats/compare/v6.0.0-beta2...master[Check the HEAD di - The node name can be discovered automatically by machine-id matching when beat deployed outside kubernetes cluster. {pull}6146[6146] - Panics will be written to the logger before exiting. {pull}6199[6199] - Add builder support for autodiscover and annotations builder {pull}6408[6408] +- Add plugin support for autodiscover builders, providers {pull}6457[6457] *Auditbeat* diff --git a/libbeat/autodiscover/builder/plugin.go b/libbeat/autodiscover/builder/plugin.go new file mode 100644 index 000000000000..1db99b1b020c --- /dev/null +++ b/libbeat/autodiscover/builder/plugin.go @@ -0,0 +1,31 @@ +package builder + +import ( + "errors" + + "github.com/elastic/beats/libbeat/autodiscover" + p "github.com/elastic/beats/libbeat/plugin" +) + +type builderPlugin struct { + name string + builder autodiscover.BuilderConstructor +} + +var pluginKey = "libbeat.autodiscover.builder" + +// Plugin accepts a BuilderConstructor to be registered as a plugin +func Plugin(name string, b autodiscover.BuilderConstructor) map[string][]interface{} { + return p.MakePlugin(pluginKey, builderPlugin{name, b}) +} + +func init() { + p.MustRegisterLoader(pluginKey, func(ifc interface{}) error { + b, ok := ifc.(builderPlugin) + if !ok { + return errors.New("plugin does not match processor plugin type") + } + + return autodiscover.Registry.AddBuilder(b.name, b.builder) + }) +} diff --git a/libbeat/autodiscover/providers/plugin.go b/libbeat/autodiscover/providers/plugin.go new file mode 100644 index 000000000000..cfdc88a73e75 --- /dev/null +++ b/libbeat/autodiscover/providers/plugin.go @@ -0,0 +1,31 @@ +package providers + +import ( + "errors" + + "github.com/elastic/beats/libbeat/autodiscover" + p "github.com/elastic/beats/libbeat/plugin" +) + +type providerPlugin struct { + name string + provider autodiscover.ProviderBuilder +} + +var pluginKey = "libbeat.autodiscover.provider" + +// Plugin accepts a ProviderBuilder to be registered as a plugin +func Plugin(name string, provider autodiscover.ProviderBuilder) map[string][]interface{} { + return p.MakePlugin(pluginKey, providerPlugin{name, provider}) +} + +func init() { + p.MustRegisterLoader(pluginKey, func(ifc interface{}) error { + prov, ok := ifc.(providerPlugin) + if !ok { + return errors.New("plugin does not match processor plugin type") + } + + return autodiscover.Registry.AddProvider(prov.name, prov.provider) + }) +}