-
Notifications
You must be signed in to change notification settings - Fork 32
/
netobserv-ebpf-agent.go
87 lines (72 loc) · 2.13 KB
/
netobserv-ebpf-agent.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
//go:build linux
package main
import (
"context"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/caarlos0/env/v6"
"github.com/sirupsen/logrus"
"github.com/netobserv/netobserv-ebpf-agent/pkg/agent"
_ "net/http/pprof"
)
func terminateAgent() (ctx context.Context) {
logrus.Infof("push CTRL+C or send SIGTERM to interrupt execution")
ctx, canceler := context.WithCancel(context.Background())
// Subscribe to signals for terminating the program.
go func() {
stopper := make(chan os.Signal, 1)
signal.Notify(stopper, os.Interrupt, syscall.SIGTERM)
<-stopper
canceler()
}()
return ctx
}
func main() {
logrus.Infof("starting NetObserv eBPF Agent")
config := agent.Config{}
if err := env.Parse(&config); err != nil {
logrus.WithError(err).Fatal("can't load configuration from environment")
}
setLoggerVerbosity(&config)
if config.ProfilePort != 0 {
go func() {
logrus.WithField("port", config.ProfilePort).Info("starting PProf HTTP listener")
logrus.WithError(http.ListenAndServe(fmt.Sprintf(":%d", config.ProfilePort), nil)).
Error("PProf HTTP listener stopped working")
}()
}
if config.DeduperFCExpiry == 0 {
config.DeduperFCExpiry = 2 * config.CacheActiveTimeout
}
logrus.WithField("configuration", fmt.Sprintf("%#v", config)).Debugf("configuration loaded")
if config.EnablePCA {
packetsAgent, err := agent.PacketsAgent(&config)
if err != nil {
logrus.WithError(err).Fatal("[PCA] can't instantiate NetObserv eBPF Agent")
}
ctx := terminateAgent()
if err := packetsAgent.Run(ctx); err != nil {
logrus.WithError(err).Fatal("[PCA] can't start netobserv-ebpf-agent")
}
} else {
flowsAgent, err := agent.FlowsAgent(&config)
if err != nil {
logrus.WithError(err).Fatal("can't instantiate NetObserv eBPF Agent")
}
ctx := terminateAgent()
if err := flowsAgent.Run(ctx); err != nil {
logrus.WithError(err).Fatal("can't start netobserv-ebpf-agent")
}
}
}
func setLoggerVerbosity(cfg *agent.Config) {
lvl, err := logrus.ParseLevel(cfg.LogLevel)
if err != nil {
logrus.WithError(err).Warn("assuming 'info' logger level as default")
lvl = logrus.InfoLevel
}
logrus.SetLevel(lvl)
}