-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
247 lines (206 loc) · 5.57 KB
/
main.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
package main
import (
"context"
"flag"
"fmt"
_ "net/http/pprof" // Blank import to pprof
"os"
"os/signal"
"syscall"
"time"
"github.com/kubeshark/tracer/misc"
"github.com/kubeshark/tracer/pkg/kubernetes"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/rest"
stdlog "log"
runtimeDebug "runtime/debug"
zlogsentry "github.com/archdx/zerolog-sentry"
"github.com/getsentry/sentry-go"
"github.com/kubeshark/tracer/pkg/version"
"github.com/kubeshark/tracer/server"
sentrypkg "github.com/kubeshark/utils/sentry"
"github.com/moby/sys/mount"
"github.com/moby/sys/mountinfo"
)
var port = flag.Int("port", 80, "Port number of the HTTP server")
// capture
var procfs = flag.String("procfs", "/proc", "The procfs directory, used when mapping host volumes into a container")
// development
var debug = flag.Bool("debug", false, "Enable debug mode")
var disableEbpfCapture = flag.Bool("disable-ebpf", false, "Disable capture packet via eBPF")
var disableTlsLog = flag.Bool("disable-tls-log", false, "Disable tls logging")
var tracer *Tracer
func main() {
var sentryDSN string
if sentrypkg.IsSentryEnabled() {
sentryDSN, err := sentrypkg.GetDSN(context.Background(), "tracer", version.Ver)
if err != nil {
log.Error().Err(err).Msg("Failed to get Sentry DSN")
}
// To initialize Sentry's handler, you need to initialize Sentry itself beforehand
if err := sentry.Init(sentry.ClientOptions{
Dsn: sentryDSN,
EnableTracing: true,
// Set TracesSampleRate to 1.0 to capture 100%
// of transactions for tracing.
// We recommend adjusting this value in production,
TracesSampleRate: 1.0,
Release: version.Ver,
Environment: sentrypkg.Environment(),
}); err != nil {
log.Error().Err(err).Msg("Sentry initialization failed:")
} else {
defer sentry.Flush(2 * time.Second)
}
}
zerolog.SetGlobalLevel(zerolog.InfoLevel)
w, err := zlogsentry.New(
sentryDSN,
)
if err != nil {
stdlog.Fatal(err)
}
defer w.Close()
multi := zerolog.MultiLevelWriter(w, zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.RFC3339})
log.Logger = zerolog.New(multi).With().Timestamp().Caller().Logger()
flag.Parse()
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.RFC3339}).With().Caller().Logger()
if *debug {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}
runtimeDebug.SetPanicOnFault(true)
defer func() {
if err := recover(); err != nil {
log.Fatal().Err(fmt.Errorf("panic: %v", err)).Send()
}
}()
misc.InitDataDir()
run()
}
func run() {
log.Info().Msg("Starting tracer...")
if err := checkMountedTracerInfo(); err != nil {
log.Error().Msg("bpffs or debugfs are not available. Tracer is disabled")
select {}
}
tracer = &Tracer{
procfs: *procfs,
targetedCgroupIDs: map[uint64]struct{}{},
runningPods: make(map[types.UID]podInfo),
}
_, err := rest.InClusterConfig()
clusterMode := err == nil
errOut := make(chan error, 100)
go func() {
for err := range errOut {
if err != nil {
log.Warn().Err(err).Msg("watch failed:")
}
}
}()
watcher := kubernetes.NewFromInCluster(errOut, tracer.updateTargets)
ctx := context.Background()
nodeName, err := kubernetes.GetThisNodeName(watcher)
if err != nil {
log.Fatal().Err(err).Send()
}
enrichSentryContext(watcher)
if clusterMode {
misc.SetDataDir(fmt.Sprintf("/app/data/%s", nodeName))
}
err = createTracer()
if err != nil {
log.Fatal().Err(err).Msg("Couldn't initialize the tracer:")
}
watcher.Start(ctx, clusterMode)
if server.GetProfilingEnabled() {
log.Info().Msg("Profiling enabled")
ginApp := server.Build()
server.Start(ginApp, *port)
} else {
stopChan := make(chan os.Signal, 1)
signal.Notify(stopChan,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT)
go signalHandler(stopChan)
select {}
}
}
func stop() {
if tracer != nil {
if err := tracer.Deinit(); err != nil {
log.Error().Err(err).Msg("Tracer stop failed")
}
}
}
func createTracer() (err error) {
chunksBufferSize := os.Getpagesize() * 10000
logBufferSize := os.Getpagesize()
if err = tracer.Init(
chunksBufferSize,
logBufferSize,
*procfs,
); err != nil {
return
}
return
}
func enrichSentryContext(watcher *kubernetes.Watcher) {
clusterId, err := kubernetes.GetClusterID(watcher)
if err != nil {
log.Error().Err(err).Msg("Failed to get cluster ID for Sentry tag")
}
tags := map[string]string{
"clusterID": clusterId,
}
sentrypkg.AddTags(tags)
}
const bpfMountPath = "/sys/fs/bpf"
const debugMountPath = "/sys/kernel/debug"
func checkMountedTracerInfo() error {
var mounted bool
var err error
if mounted, err = mountinfo.Mounted(bpfMountPath); err != nil {
log.Error().Err(err).Msg("Unable to get mountinfo")
return err
}
if !mounted {
if err = mount.Mount("bpf", bpfMountPath, "bpf", ""); err != nil {
log.Error().Err(err).Msg("Unable to mount bpf filesystem")
return err
}
log.Print("bpf filesystem has been mounted")
}
if mounted, err = mountinfo.Mounted(debugMountPath); err != nil {
log.Error().Err(err).Msg("Unable to get mountinfo for debugfs")
return err
}
if !mounted {
if err = mount.Mount("debugfs", debugMountPath, "debugfs", ""); err != nil {
log.Error().Err(err).Msg("Unable to mount debugfs filesystem")
return err
}
}
return nil
}
func signalHandler(stopChan chan os.Signal) {
for {
s := <-stopChan
switch s {
case syscall.SIGHUP:
fallthrough
case syscall.SIGINT:
fallthrough
case syscall.SIGTERM:
fallthrough
case syscall.SIGQUIT:
stop()
os.Exit(0)
default:
}
}
}