This repository was archived by the owner on Feb 11, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 247
Expand file tree
/
Copy pathplugins.go
More file actions
80 lines (66 loc) · 1.8 KB
/
plugins.go
File metadata and controls
80 lines (66 loc) · 1.8 KB
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
package plugins
import (
"context"
"fmt"
"github.com/deepfence/PacketStreamer/pkg/config"
"github.com/deepfence/PacketStreamer/pkg/plugins/kafka"
"github.com/deepfence/PacketStreamer/pkg/plugins/s3"
"github.com/deepfence/PacketStreamer/pkg/plugins/types"
"log"
)
//Start uses the provided config to start the execution of any plugin outputs that have been defined.
//Packets that are written to the returned channel will be fanned out to N configured plugins.
func Start(ctx context.Context, config *config.Config) (chan<- string, error) {
if !pluginsAreDefined(config.Output.Plugins) {
return nil, nil
}
var plugins []types.RunningPlugin
if config.Output.Plugins.S3 != nil {
s3plugin, err := s3.NewPlugin(ctx, config.Output.Plugins.S3)
if err != nil {
return nil, fmt.Errorf("error starting S3 plugin, %v", err)
}
startedPlugin := s3plugin.Start(ctx)
plugins = append(plugins, startedPlugin)
go func() {
for e := range startedPlugin.Errors {
log.Println(e)
}
}()
}
if config.Output.Plugins.Kafka != nil {
kafkaPlugin, err := kafka.NewPlugin(config.Output.Plugins.Kafka)
if err != nil {
return nil, fmt.Errorf("error starting Kafka plugin, %v", err)
}
startedPlugin := kafkaPlugin.Start(ctx)
plugins = append(plugins, startedPlugin)
go func() {
for e := range startedPlugin.Errors {
log.Println(e)
}
}()
}
inputChan := make(chan string)
go func() {
defer func() {
for _, p := range plugins {
close(p.Input)
}
}()
for {
select {
case pkt := <-inputChan:
for _, p := range plugins {
p.Input <- pkt
}
case <-ctx.Done():
return
}
}
}()
return inputChan, nil
}
func pluginsAreDefined(pluginsConfig *config.PluginsConfig) bool {
return pluginsConfig != nil && (pluginsConfig.S3 != nil || pluginsConfig.Kafka != nil)
}