-
Notifications
You must be signed in to change notification settings - Fork 292
/
scheduler.go
310 lines (269 loc) · 9.37 KB
/
scheduler.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package source
import (
"context"
"fmt"
"sync"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
"github.com/kubeshop/botkube/pkg/api/source"
"github.com/kubeshop/botkube/pkg/config"
"github.com/kubeshop/botkube/pkg/maputil"
)
const (
emptyPluginFilter = ""
)
type pluginDispatcher interface {
Dispatch(dispatch PluginDispatch) error
}
// PluginDispatch holds information about source plugin.
type PluginDispatch struct {
ctx context.Context
pluginName string
pluginConfig *source.Config
sourceName string
sourceDisplayName string
isInteractivitySupported bool
cfg *config.Config
pluginContext config.PluginContext
incomingWebhook IncomingWebhookData
}
// ExternalRequestDispatch is a wrapper for PluginDispatch that holds the payload for external request.
type ExternalRequestDispatch struct {
PluginDispatch
payload []byte
}
// StartedSources holds information about started source plugins grouped by interactivity supported.
type StartedSources map[bool]StartedSource
// StartedSource holds information about started source plugin.
type StartedSource struct {
SourceDisplayName string
PluginName string
PluginConfig *source.Config
IsInteractivitySupported bool
}
// Scheduler analyzes the provided configuration and based on that schedules plugin sources.
type Scheduler struct {
log logrus.FieldLogger
cfg *config.Config
dispatcher pluginDispatcher
dispatchConfig map[string]map[string]PluginDispatch
schedulerChan chan string
openedStreams *openedStreams
startedSourcePlugins map[string]StartedSources
}
type openedStreams struct {
sync.RWMutex
data map[string]map[string]struct{}
}
func (p *openedStreams) reportStartedStreamWithConfiguration(plugin, configuration string) {
p.Lock()
defer p.Unlock()
if p.data == nil {
p.data = map[string]map[string]struct{}{}
}
if p.data[plugin] == nil {
p.data[plugin] = map[string]struct{}{}
}
p.data[plugin][configuration] = struct{}{}
}
func (p *openedStreams) deleteAllStartedStreamsForPlugin(plugin string) {
p.Lock()
defer p.Unlock()
delete(p.data, plugin)
}
func (p *openedStreams) isStartedStreamWithConfiguration(plugin, configuration string) bool {
p.RLock()
defer p.RUnlock()
_, ok := p.data[plugin][configuration]
return ok
}
// NewScheduler create a new Scheduler instance.
func NewScheduler(ctx context.Context, log logrus.FieldLogger, cfg *config.Config, dispatcher pluginDispatcher, schedulerChan chan string) *Scheduler {
s := &Scheduler{
log: log,
cfg: cfg,
dispatcher: dispatcher,
startedSourcePlugins: map[string]StartedSources{},
openedStreams: &openedStreams{data: map[string]map[string]struct{}{}},
dispatchConfig: make(map[string]map[string]PluginDispatch),
schedulerChan: schedulerChan,
}
go s.monitorHealth(ctx)
return s
}
// Start starts all sources and dispatch received events.
func (d *Scheduler) Start(ctx context.Context) error {
if err := d.generateConfigs(ctx); err != nil {
return fmt.Errorf("while generating configs for sources: %w", err)
}
if err := d.schedule(emptyPluginFilter); err != nil {
return fmt.Errorf("while scheduling source dispatch: %w", err)
}
return nil
}
func (d *Scheduler) monitorHealth(ctx context.Context) {
d.log.Info("Starting scheduler plugin health monitor")
for {
select {
case <-ctx.Done():
return
case pluginName := <-d.schedulerChan:
d.log.Debugf("Scheduling restarted plugin %q", pluginName)
// botkube/kubernetes map to source configuration ->
// - k8s-all-events_interactive/true
// - k8s-all-events_interactive/false
d.openedStreams.deleteAllStartedStreamsForPlugin(pluginName)
if err := d.schedule(pluginName); err != nil {
d.log.Errorf("while scheduling %q: %s", pluginName, err)
}
}
}
}
func (d *Scheduler) schedule(pluginFilter string) error {
sortedKeys := maputil.SortKeys(d.dispatchConfig) // ensure config iteration order is alphabetical
for _, configKey := range sortedKeys {
sourceConfig := d.dispatchConfig[configKey]
for pluginName, srcCfg := range sourceConfig {
if pluginFilter != emptyPluginFilter && pluginFilter != pluginName {
d.log.Debugf("Not starting %q as it doesn't pass plugin filter.", pluginName)
continue
}
if ok := d.openedStreams.isStartedStreamWithConfiguration(pluginName, configKey); ok {
d.log.Infof("Not starting %q as it was already started.", pluginName)
continue
}
d.log.Infof("Starting a new stream for plugin %q", pluginName)
if err := d.dispatcher.Dispatch(srcCfg); err != nil {
return fmt.Errorf("while starting plugin source %s: %w", pluginName, err)
}
d.openedStreams.reportStartedStreamWithConfiguration(pluginName, configKey)
}
}
return nil
}
func (d *Scheduler) generateConfigs(ctx context.Context) error {
for _, commGroupCfg := range d.cfg.Communications {
if commGroupCfg.CloudSlack.Enabled {
for _, channel := range commGroupCfg.CloudSlack.Channels {
if err := d.generateSourceConfigs(ctx, config.CloudSlackCommPlatformIntegration.IsInteractive(), channel.Bindings.Sources); err != nil {
return err
}
}
}
if commGroupCfg.SocketSlack.Enabled {
for _, channel := range commGroupCfg.SocketSlack.Channels {
if err := d.generateSourceConfigs(ctx, config.SocketSlackCommPlatformIntegration.IsInteractive(), channel.Bindings.Sources); err != nil {
return err
}
}
}
if commGroupCfg.Mattermost.Enabled {
for _, channel := range commGroupCfg.Mattermost.Channels {
if err := d.generateSourceConfigs(ctx, config.MattermostCommPlatformIntegration.IsInteractive(), channel.Bindings.Sources); err != nil {
return err
}
}
}
if commGroupCfg.CloudTeams.Enabled {
for _, teams := range commGroupCfg.CloudTeams.Teams {
for _, channel := range teams.Channels {
if err := d.generateSourceConfigs(ctx, config.CloudTeamsCommPlatformIntegration.IsInteractive(), channel.Bindings.Sources); err != nil {
return err
}
}
}
}
if commGroupCfg.Discord.Enabled {
for _, channel := range commGroupCfg.Discord.Channels {
if err := d.generateSourceConfigs(ctx, config.DiscordCommPlatformIntegration.IsInteractive(), channel.Bindings.Sources); err != nil {
return err
}
}
}
if commGroupCfg.Webhook.Enabled {
if err := d.generateSourceConfigs(ctx, false, commGroupCfg.Webhook.Bindings.Sources); err != nil {
return err
}
}
if commGroupCfg.Elasticsearch.Enabled {
for _, index := range commGroupCfg.Elasticsearch.Indices {
if err := d.generateSourceConfigs(ctx, false, index.Bindings.Sources); err != nil {
return err
}
}
}
}
// Schedule all sources used by actions
for _, act := range d.cfg.Actions {
if !act.Enabled {
continue
}
if err := d.generateSourceConfigs(ctx, false, act.Bindings.Sources); err != nil {
return err
}
}
return nil
}
func (d *Scheduler) generateSourceConfigs(ctx context.Context, isInteractivitySupported bool, boundSources []string) error {
for _, boundSource := range boundSources {
err := d.generatePluginConfig(ctx, isInteractivitySupported, boundSource)
if err != nil {
return err
}
}
return nil
}
func (d *Scheduler) generatePluginConfig(ctx context.Context, isInteractivitySupported bool, sourceName string) error {
// As not all of our platforms supports interactivity, we need to schedule the same source twice. For example:
// - k8s-all-events_interactive/true
// - k8s-all-events_interactive/false
// As a result, each Stream method will know if it can produce an interactive message or not.
key := fmt.Sprintf("%s_interactive/%v", sourceName, isInteractivitySupported)
srcConfig, exists := d.cfg.Sources[sourceName]
if !exists {
return fmt.Errorf("source %q not found", sourceName)
}
for pluginName, pluginCfg := range srcConfig.Plugins {
if !pluginCfg.Enabled {
continue
}
// Unfortunately we need marshal it to get the raw data:
// https://github.com/go-yaml/yaml/issues/13
rawYAML, err := yaml.Marshal(pluginCfg.Config)
if err != nil {
return fmt.Errorf("while marshaling config for %s from source %s : %w", pluginName, sourceName, err)
}
pluginConfig := &source.Config{
RawYAML: rawYAML,
}
config := PluginDispatch{
ctx: ctx,
pluginName: pluginName,
pluginConfig: pluginConfig,
isInteractivitySupported: isInteractivitySupported,
sourceName: sourceName,
sourceDisplayName: srcConfig.DisplayName,
cfg: d.cfg,
pluginContext: pluginCfg.Context,
incomingWebhook: IncomingWebhookData{
inClusterBaseURL: d.cfg.Plugins.IncomingWebhook.InClusterBaseURL,
},
}
d.dispatchConfig[key] = map[string]PluginDispatch{
pluginName: config,
}
if _, exists := d.startedSourcePlugins[config.sourceName]; !exists {
d.startedSourcePlugins[config.sourceName] = StartedSources{}
}
d.startedSourcePlugins[config.sourceName][config.isInteractivitySupported] = StartedSource{
SourceDisplayName: config.sourceDisplayName,
PluginName: pluginName,
PluginConfig: config.pluginConfig,
IsInteractivitySupported: config.isInteractivitySupported,
}
}
return nil
}
func (d *Scheduler) StartedSourcePlugins() map[string]StartedSources {
return d.startedSourcePlugins
}