-
Notifications
You must be signed in to change notification settings - Fork 52
/
k8spacket.go
78 lines (64 loc) · 2.03 KB
/
k8spacket.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
package main
import (
"context"
"errors"
"fmt"
"log/slog"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/k8spacket/k8spacket/broker"
"github.com/k8spacket/k8spacket/ebpf"
ebpf_inet "github.com/k8spacket/k8spacket/ebpf/inet"
ebpf_tc "github.com/k8spacket/k8spacket/ebpf/tc"
"github.com/k8spacket/k8spacket/modules/nodegraph"
tlsparser "github.com/k8spacket/k8spacket/modules/tls-parser"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func main() {
mux := http.NewServeMux()
nodegraphListener := nodegraph.Init(mux)
tlsParserListener := tlsparser.Init(mux)
broker := broker.Init(nodegraphListener, tlsParserListener)
inetEbpf := &ebpf_inet.InetEbpf{Broker: broker}
tcEbpf := &ebpf_tc.TcEbpf{Broker: broker}
loader := ebpf.Init(inetEbpf, tcEbpf)
buildLogger()
startApp(broker, loader, mux)
}
func startApp(broker broker.IBroker, loader ebpf.ILoader, mux *http.ServeMux) {
go broker.DistributeEvents()
loader.Load()
prometheus.MustRegister(collectors.NewBuildInfoCollector())
startHttpServer(mux)
}
func startHttpServer(mux *http.ServeMux) {
listenerPort := os.Getenv("K8S_PACKET_TCP_LISTENER_PORT")
slog.Info("[api] Serving requests", "Port", listenerPort)
srv := &http.Server{Addr: fmt.Sprintf(":%s", listenerPort), Handler: mux}
go func() {
mux.Handle("/metrics", promhttp.Handler())
if err := srv.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
slog.Error("[api] Cannot start ListenAndServe", "Error", err)
}
}()
// graceful shutdown
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
<-ctx.Done()
if err := srv.Shutdown(ctx); err != nil {
slog.Error("[graceful] Server shutdown failed", "Error", err)
}
slog.Info("[graceful] Application closed gracefully")
}
func buildLogger() {
var l slog.Level
err := l.UnmarshalText([]byte(os.Getenv("LOG_LEVEL")))
if err != nil {
l = slog.LevelInfo
}
slog.SetLogLoggerLevel(l)
}