This repository has been archived by the owner on Dec 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.go
118 lines (98 loc) · 2.84 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
package main
import (
"context"
"flag"
"fmt"
"net/url"
"os"
"github.com/kubescape/backend/pkg/servicediscovery"
v2 "github.com/kubescape/backend/pkg/servicediscovery/v2"
"github.com/kubescape/backend/pkg/utils"
logger "github.com/kubescape/go-logger"
"github.com/kubescape/go-logger/helpers"
"github.com/kubescape/kollector/config"
"github.com/kubescape/kollector/consts"
"github.com/kubescape/kollector/watch"
"github.com/armosec/utils-k8s-go/armometadata"
"github.com/armosec/utils-k8s-go/probes"
)
func main() {
ctx := context.Background()
isServerReady := false
go probes.InitReadinessV1(&isServerReady)
displayBuildTag()
clusterConfig, err := armometadata.LoadConfig(os.Getenv(consts.ConfigEnvironmentVariable))
if err != nil {
logger.L().Ctx(ctx).Fatal("failed to load config", helpers.Error(err))
}
services, err := servicediscovery.GetServices(
v2.NewServiceDiscoveryFileV2("/etc/config/services.json"),
)
if err != nil {
logger.L().Ctx(ctx).Fatal("failed to load services", helpers.Error(err))
}
logger.L().Info("loaded event receiver websocket url (service discovery)", helpers.String("url", services.GetReportReceiverWebsocketUrl()))
var credentials *utils.Credentials
if credentials, err = utils.LoadCredentialsFromFile("/etc/credentials"); err != nil {
logger.L().Ctx(ctx).Error("failed to load credentials", helpers.Error(err))
credentials = &utils.Credentials{}
} else {
logger.L().Info("credentials loaded",
helpers.Int("accessKeyLength", len(credentials.AccessKey)),
helpers.Int("accountLength", len(credentials.Account)))
}
kollectorConfig := config.NewKollectorConfig(clusterConfig, *credentials, services.GetReportReceiverWebsocketUrl())
// to enable otel, set OTEL_COLLECTOR_SVC=otel-collector:4317
if otelHost, present := os.LookupEnv(consts.OtelCollectorSvcEnvironmentVariable); present {
ctx = logger.InitOtel("kollector",
os.Getenv(consts.ReleaseBuildTagEnvironmentVariable),
kollectorConfig.AccountID(),
kollectorConfig.ClusterName(),
url.URL{Host: otelHost})
defer logger.ShutdownOtel(ctx)
}
wh, err := watch.CreateWatchHandler(kollectorConfig)
if err != nil {
logger.L().Ctx(ctx).Fatal("failed to initialize the WatchHandler", helpers.Error(err))
}
go func() {
for {
wh.ListenerAndSender(ctx)
}
}()
go func() {
for {
wh.NodeWatch(ctx)
}
}()
go func() {
for {
wh.PodWatch(ctx)
}
}()
go func() {
for {
wh.ServiceWatch(ctx)
}
}()
go func() {
for {
wh.SecretWatch(ctx)
}
}()
go func() {
for {
wh.NamespaceWatch(ctx)
}
}()
go func() {
for {
wh.CronJobWatch(ctx)
}
}()
logger.L().Ctx(ctx).Fatal(wh.WebSocketHandle.SendReportRoutine(ctx, &isServerReady, wh.SetFirstReportFlag).Error())
}
func displayBuildTag() {
flag.Parse()
logger.L().Info(fmt.Sprintf("Image version: %s", os.Getenv(consts.ReleaseBuildTagEnvironmentVariable)))
}