-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
autodiscover.go
293 lines (239 loc) · 7.49 KB
/
autodiscover.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// 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 autodiscover
import (
"fmt"
"time"
"github.com/pkg/errors"
"github.com/elastic/beats/libbeat/autodiscover/meta"
"github.com/elastic/beats/libbeat/beat"
"github.com/elastic/beats/libbeat/cfgfile"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/common/bus"
"github.com/elastic/beats/libbeat/common/reload"
"github.com/elastic/beats/libbeat/logp"
)
const (
// If a config reload fails after a new event, a new reload will be run after this period
retryPeriod = 10 * time.Second
)
// TODO autodiscover providers config reload
// Adapter must be implemented by the beat in order to provide Autodiscover
type Adapter interface {
// CreateConfig generates a valid list of configs from the given event, the received event will have all keys defined by `StartFilter`
CreateConfig(bus.Event) ([]*common.Config, error)
// RunnerFactory provides runner creation by feeding valid configs
cfgfile.CheckableRunnerFactory
// EventFilter returns the bus filter to retrieve runner start/stop triggering events
EventFilter() []string
}
// Autodiscover process, it takes a beat adapter and user config and runs autodiscover process, spawning
// new modules when any configured providers does a match
type Autodiscover struct {
bus bus.Bus
defaultPipeline beat.Pipeline
adapter Adapter
providers []Provider
configs map[string]map[uint64]*reload.ConfigWithMeta
runners *cfgfile.RunnerList
meta *meta.Map
listener bus.Listener
logger *logp.Logger
}
// NewAutodiscover instantiates and returns a new Autodiscover manager
func NewAutodiscover(name string, pipeline beat.Pipeline, adapter Adapter, config *Config) (*Autodiscover, error) {
// Init Event bus
bus := bus.New(name)
logger := logp.NewLogger("autodiscover")
// Init providers
var providers []Provider
for _, providerCfg := range config.Providers {
provider, err := Registry.BuildProvider(bus, providerCfg)
if err != nil {
return nil, errors.Wrap(err, "error in autodiscover provider settings")
}
logger.Debugf("Configured autodiscover provider: %s", provider)
providers = append(providers, provider)
}
return &Autodiscover{
bus: bus,
defaultPipeline: pipeline,
adapter: adapter,
configs: map[string]map[uint64]*reload.ConfigWithMeta{},
runners: cfgfile.NewRunnerList("autodiscover", adapter, pipeline),
providers: providers,
meta: meta.NewMap(),
logger: logger,
}, nil
}
// Start autodiscover process
func (a *Autodiscover) Start() {
if a == nil {
return
}
a.logger.Info("Starting autodiscover manager")
a.listener = a.bus.Subscribe(a.adapter.EventFilter()...)
// It is important to start the worker first before starting the producer.
// In hosts that have large number of workloads, it is easy to have an initial
// sync of workloads to have a count that is greater than 100 (which is the size
// of the bounded Go channel. Starting the providers before the consumer would
// result in the channel filling up and never allowing the worker to start up.
go a.worker()
for _, provider := range a.providers {
provider.Start()
}
}
func (a *Autodiscover) worker() {
var updated, retry bool
for {
select {
case event := <-a.listener.Events():
// This will happen on Stop:
if event == nil {
return
}
if _, ok := event["start"]; ok {
updated = a.handleStart(event)
}
if _, ok := event["stop"]; ok {
updated = a.handleStop(event)
}
case <-time.After(retryPeriod):
}
if updated || retry {
if retry {
a.logger.Debug("Reloading existing autodiscover configs after error")
}
configs := []*reload.ConfigWithMeta{}
for _, list := range a.configs {
for _, c := range list {
configs = append(configs, c)
}
}
err := a.runners.Reload(configs)
// On error, make sure the next run also updates because some runners were not properly loaded
retry = err != nil
// reset updated status
updated = false
}
}
}
func (a *Autodiscover) handleStart(event bus.Event) bool {
var updated bool
a.logger.Debugf("Got a start event: %v", event)
eventID := getID(event)
if eventID == "" {
a.logger.Errorf("Event didn't provide instance id: %+v, ignoring it", event)
return false
}
// Ensure configs list exists for this instance
if _, ok := a.configs[eventID]; !ok {
a.configs[eventID] = map[uint64]*reload.ConfigWithMeta{}
}
configs, err := a.adapter.CreateConfig(event)
if err != nil {
a.logger.Debugf("Could not generate config from event %v: %v", event, err)
return false
}
if a.logger.IsDebug() {
for _, c := range configs {
rc := map[string]interface{}{}
c.Unpack(&rc)
a.logger.Debugf("Generated config: %+v", rc)
}
}
meta := a.getMeta(event)
for _, config := range configs {
hash, err := cfgfile.HashConfig(config)
if err != nil {
a.logger.Debugf("Could not hash config %v: %v", config, err)
continue
}
err = a.adapter.CheckConfig(config)
if err != nil {
a.logger.Error(errors.Wrap(err, fmt.Sprintf("Auto discover config check failed for config '%s', won't start runner", common.DebugString(config, true))))
continue
}
// Update meta no matter what
dynFields := a.meta.Store(hash, meta)
if a.configs[eventID][hash] != nil {
a.logger.Debugf("Config %v is already running", config)
continue
}
a.configs[eventID][hash] = &reload.ConfigWithMeta{
Config: config,
Meta: &dynFields,
}
updated = true
}
return updated
}
func (a *Autodiscover) handleStop(event bus.Event) bool {
var updated bool
a.logger.Debugf("Got a stop event: %v", event)
eventID := getID(event)
if eventID == "" {
a.logger.Errorf("Event didn't provide instance id: %+v, ignoring it", event)
return false
}
if len(a.configs[eventID]) > 0 {
a.logger.Debugf("Stopping %d configs", len(a.configs[eventID]))
updated = true
}
delete(a.configs, eventID)
return updated
}
func (a *Autodiscover) getMeta(event bus.Event) common.MapStr {
m := event["meta"]
if m == nil {
return nil
}
a.logger.Debugf("Got a meta field in the event")
meta, ok := m.(common.MapStr)
if !ok {
a.logger.Errorf("Got a wrong meta field for event %v", event)
return nil
}
return meta
}
// getID returns the event "id" field string if present
func getID(e bus.Event) string {
provider, ok := e["provider"]
if !ok {
return ""
}
id, ok := e["id"]
if !ok {
return ""
}
return fmt.Sprintf("%s:%s", provider, id)
}
// Stop autodiscover process
func (a *Autodiscover) Stop() {
if a == nil {
return
}
// Stop listening for events
a.listener.Stop()
// Stop providers
for _, provider := range a.providers {
provider.Stop()
}
// Stop runners
a.runners.Stop()
a.logger.Info("Stopped autodiscover manager")
}