-
Notifications
You must be signed in to change notification settings - Fork 22
/
main.go
153 lines (130 loc) · 4.9 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
package main
import (
"context"
"github.com/otterize/intents-operator/src/shared"
"github.com/otterize/intents-operator/src/shared/errors"
"github.com/otterize/intents-operator/src/shared/telemetries/componentinfo"
"github.com/otterize/network-mapper/src/shared/componentutils"
"fmt"
"github.com/bombsimon/logrusr/v3"
"github.com/labstack/echo-contrib/echoprometheus"
"github.com/labstack/echo/v4"
"github.com/otterize/intents-operator/src/shared/telemetries/errorreporter"
"github.com/otterize/network-mapper/src/kafka-watcher/pkg/config"
logwatcher2 "github.com/otterize/network-mapper/src/kafka-watcher/pkg/logwatcher"
"github.com/otterize/network-mapper/src/kafka-watcher/pkg/mapperclient"
sharedconfig "github.com/otterize/network-mapper/src/shared/config"
"github.com/otterize/network-mapper/src/shared/version"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"golang.org/x/sync/errgroup"
"k8s.io/apimachinery/pkg/types"
"net/http"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
"strings"
"time"
)
func main() {
logrus.SetLevel(logrus.InfoLevel)
if viper.GetBool(sharedconfig.DebugKey) {
logrus.SetLevel(logrus.DebugLevel)
}
logrus.SetFormatter(&logrus.JSONFormatter{
TimestampFormat: time.RFC3339,
})
errorreporter.Init("kafka-watcher", version.Version(), viper.GetString(sharedconfig.TelemetryErrorsAPIKeyKey))
defer errorreporter.AutoNotify()
shared.RegisterPanicHandlers()
ctrl.SetLogger(logrusr.New(logrus.StandardLogger()))
componentutils.SetCloudClientId()
componentinfo.SetGlobalComponentInstanceId()
mapperClient := mapperclient.NewMapperClient(viper.GetString(sharedconfig.MapperApiUrlKey))
mode := viper.GetString(config.KafkaLogReadModeKey)
var err error
var watcher logwatcher2.Watcher
switch mode {
case config.FileReadMode:
logPath := viper.GetString(config.KafkaAuthZLogPathKey)
if logPath == "" {
logrus.Panicf("Kafka log path is not set - please set %s", sharedconfig.GetEnvVarForKey(config.KafkaAuthZLogPathKey))
}
logrus.Infof("Kafka watcher: reading from filesystem - %s", logPath)
serverName := types.NamespacedName{
Namespace: viper.GetString(sharedconfig.EnvNamespaceKey),
Name: viper.GetString(sharedconfig.EnvPodKey),
}
watcher, err = logwatcher2.NewLogFileWatcher(mapperClient, logPath, serverName)
if err != nil {
logrus.WithError(err).Panic("could not initialize log file watcher")
}
case config.KubernetesLogReadMode:
kafkaServers, parseErr := parseKafkaServers(viper.GetStringSlice(config.KafkaServersKey))
logrus.Infof("Reading from k8s logs - %d servers", len(kafkaServers))
if parseErr != nil {
logrus.WithError(err).Panic("could not parse Kafka servers list")
}
watcher, err = logwatcher2.NewKubernetesLogWatcher(mapperClient, kafkaServers)
if err != nil {
logrus.WithError(err).Panic("could not initialize Kubernetes log watcher")
}
case "":
logrus.Panicf("Kafka watcher mode is not set - please set %s", sharedconfig.GetEnvVarForKey(config.KafkaLogReadModeKey))
default:
logrus.Panicf("Kafka watcher mode (%s) is not set to a valid mode", mode)
}
healthServer := echo.New()
healthServer.HideBanner = true
healthServer.GET("/healthz", func(c echo.Context) error {
err := mapperClient.Health(c.Request().Context())
if err != nil {
return errors.Wrap(err)
}
return c.NoContent(http.StatusOK)
})
metricsServer := echo.New()
metricsServer.HideBanner = true
metricsServer.GET("/metrics", echoprometheus.NewHandler())
errgrp, errGroupCtx := errgroup.WithContext(signals.SetupSignalHandler())
errgrp.Go(func() error {
defer errorreporter.AutoNotify()
return metricsServer.Start(fmt.Sprintf(":%d", viper.GetInt(sharedconfig.PrometheusMetricsPortKey)))
})
errgrp.Go(func() error {
defer errorreporter.AutoNotify()
return healthServer.Start(":9090")
})
errgrp.Go(func() error {
defer errorreporter.AutoNotify()
err := watcher.RunForever(errGroupCtx)
return errors.Wrap(err)
})
err = errgrp.Wait()
if err != nil && !errors.Is(err, http.ErrServerClosed) {
logrus.WithError(err).Panic("Error when running server or HTTP server")
}
timeoutCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err = healthServer.Shutdown(timeoutCtx)
if err != nil {
logrus.WithError(err).Panic("Error when shutting down")
}
err = metricsServer.Shutdown(timeoutCtx)
if err != nil {
logrus.WithError(err).Panic("Error when shutting down")
}
}
func parseKafkaServers(serverNames []string) ([]types.NamespacedName, error) {
var servers []types.NamespacedName
for _, serverName := range serverNames {
nameParts := strings.Split(serverName, ".")
if len(nameParts) != 2 {
return nil, errors.Errorf("error parsing server pod name %s - should be formatted as 'name.namespace'", serverName)
}
servers = append(servers, types.NamespacedName{
Name: nameParts[0],
Namespace: nameParts[1],
})
}
return servers, nil
}