Skip to content

Commit 5b8fcc2

Browse files
authored
Collect traffic levels data from node-agent and upload to cloud (#271)
1 parent 14b7591 commit 5b8fcc2

22 files changed

+768
-23
lines changed

build/kafka-watcher.Dockerfile

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM --platform=$BUILDPLATFORM golang:1.22.1-alpine as buildenv
1+
FROM --platform=$BUILDPLATFORM golang:1.22.1-alpine AS buildenv
22
RUN apk add --no-cache ca-certificates git protoc
33
RUN apk add build-base libpcap-dev
44
WORKDIR /src
@@ -9,10 +9,10 @@ RUN go mod download
99

1010
COPY . .
1111

12-
FROM buildenv as test
12+
FROM buildenv AS test
1313
RUN go test ./kafka-watcher/...
1414

15-
FROM test as builder
15+
FROM test AS builder
1616
ARG TARGETOS
1717
ARG TARGETARCH
1818
RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH go build -trimpath -o /main ./kafka-watcher/cmd

build/mapper.Dockerfile

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM --platform=$BUILDPLATFORM golang:1.22.1-alpine as buildenv
1+
FROM --platform=$BUILDPLATFORM golang:1.22.1-alpine AS buildenv
22
RUN apk add --no-cache ca-certificates git protoc
33
RUN apk add build-base libpcap-dev
44
WORKDIR /src
@@ -9,15 +9,15 @@ RUN go mod download
99

1010
COPY . .
1111

12-
FROM buildenv as test
12+
FROM buildenv AS test
1313
# install dependencies for "envtest" package
1414
RUN go install sigs.k8s.io/controller-runtime/tools/[email protected] && \
1515
source <(setup-envtest use -p env) && \
1616
mkdir -p /usr/local/kubebuilder && \
1717
ln -s "$KUBEBUILDER_ASSETS" /usr/local/kubebuilder/bin
1818
RUN go test ./mapper/...
1919

20-
FROM test as builder
20+
FROM test AS builder
2121
ARG TARGETOS
2222
ARG TARGETARCH
2323

build/sniffer.Dockerfile

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM --platform=linux/amd64 golang:1.22.1-alpine as buildenv
1+
FROM --platform=linux/amd64 golang:1.22.1-alpine AS buildenv
22
RUN apk add --no-cache ca-certificates git protoc
33
RUN apk add build-base libpcap-dev
44
WORKDIR /src
@@ -9,12 +9,12 @@ RUN go mod download
99

1010
COPY . .
1111

12-
FROM buildenv as test
12+
FROM buildenv AS test
1313
RUN go test ./sniffer/... && echo dep > /dep
1414

1515
# We start from the base image again, only this time it's using the target arch instead of always amd64. This is done to make the build faster.
1616
# Unlike the mapper, it can't be amd64 throughout and use Go's cross-compilation, since the sniffer depends on libpcap (C library).
17-
FROM golang:1.22.1-alpine as builder
17+
FROM golang:1.22.1-alpine AS builder
1818
COPY --from=test /dep /dep
1919
RUN apk add --no-cache ca-certificates git protoc
2020
RUN apk add build-base libpcap-dev
@@ -29,7 +29,7 @@ RUN go build -trimpath -o /main ./sniffer/cmd
2929
ARG VERSION
3030
RUN echo -n $VERSION > /version
3131

32-
FROM alpine as release
32+
FROM alpine AS release
3333
RUN apk add --no-cache ca-certificates libpcap
3434
WORKDIR /
3535
COPY --from=builder /main /main

src/mapper/cmd/main.go

+13-5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
istiowatcher "github.com/otterize/network-mapper/src/istio-watcher/pkg/watcher"
1515
"github.com/otterize/network-mapper/src/mapper/pkg/awsintentsholder"
1616
"github.com/otterize/network-mapper/src/mapper/pkg/azureintentsholder"
17+
"github.com/otterize/network-mapper/src/mapper/pkg/collectors/traffic"
1718
"github.com/otterize/network-mapper/src/mapper/pkg/dnscache"
1819
"github.com/otterize/network-mapper/src/mapper/pkg/dnsintentspublisher"
1920
"github.com/otterize/network-mapper/src/mapper/pkg/externaltrafficholder"
@@ -166,6 +167,7 @@ func main() {
166167
incomingTrafficIntentsHolder := incomingtrafficholder.NewIncomingTrafficIntentsHolder()
167168
awsIntentsHolder := awsintentsholder.New()
168169
azureIntentsHolder := azureintentsholder.New()
170+
trafficCollector := traffic.NewCollector()
169171

170172
resolver := resolvers.NewResolver(
171173
kubeFinder,
@@ -176,18 +178,14 @@ func main() {
176178
azureIntentsHolder,
177179
dnsCache,
178180
incomingTrafficIntentsHolder,
181+
trafficCollector,
179182
)
180183
resolver.Register(mapperServer)
181184

182185
metricsServer := echo.New()
183186
metricsServer.HideBanner = true
184187
metricsServer.GET("/metrics", echoprometheus.NewHandler())
185188

186-
cloudClient, cloudEnabled, err := cloudclient.NewClient(errGroupCtx)
187-
if err != nil {
188-
logrus.WithError(err).Panic("Failed to initialize cloud client")
189-
}
190-
191189
if viper.GetBool(config.EnableIstioCollectionKey) {
192190
istioWatcher, err := istiowatcher.NewWatcher(resolver.Mutation())
193191
if err != nil {
@@ -201,6 +199,10 @@ func main() {
201199
}
202200

203201
cloudUploaderConfig := clouduploader.ConfigFromViper()
202+
cloudClient, cloudEnabled, err := cloudclient.NewClient(errGroupCtx)
203+
if err != nil {
204+
logrus.WithError(err).Panic("Failed to initialize cloud client")
205+
}
204206
if cloudEnabled {
205207
cloudUploader := clouduploader.NewCloudUploader(intentsHolder, cloudUploaderConfig, cloudClient)
206208

@@ -211,6 +213,7 @@ func main() {
211213
}
212214
awsIntentsHolder.RegisterNotifyIntents(cloudUploader.NotifyAWSIntents)
213215
azureIntentsHolder.RegisterNotifyIntents(cloudUploader.NotifyAzureIntents)
216+
trafficCollector.RegisterNotifyTraffic(cloudUploader.NotifyTrafficLevels)
214217

215218
go cloudUploader.PeriodicStatusReport(errGroupCtx)
216219

@@ -271,6 +274,11 @@ func main() {
271274
azureIntentsHolder.PeriodicIntentsUpload(errGroupCtx, cloudUploaderConfig.UploadInterval)
272275
return nil
273276
})
277+
errgrp.Go(func() error {
278+
defer errorreporter.AutoNotify()
279+
trafficCollector.PeriodicUpload(errGroupCtx, cloudUploaderConfig.UploadInterval)
280+
return nil
281+
})
274282

275283
telemetrysender.SendNetworkMapper(telemetriesgql.EventTypeStarted, 1)
276284
telemetrysender.NetworkMapperRunActiveReporter(errGroupCtx)

src/mapper/pkg/cloudclient/cloud_client.go

+19
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type CloudClient interface {
1515
ReportIncomingTrafficDiscoveredIntents(ctx context.Context, intents []IncomingTrafficDiscoveredIntentInput) error
1616
ReportK8sServices(ctx context.Context, namespace string, services []K8sServiceInput) error
1717
ReportK8sIngresses(ctx context.Context, namespace string, ingresses []K8sIngressInput) error
18+
ReportTrafficLevels(ctx context.Context, trafficLevels []TrafficLevelInput) error
1819
}
1920

2021
type CloudClientImpl struct {
@@ -96,3 +97,21 @@ func (c *CloudClientImpl) ReportK8sIngresses(ctx context.Context, namespace stri
9697

9798
return nil
9899
}
100+
101+
func (c *CloudClientImpl) ReportTrafficLevels(
102+
ctx context.Context,
103+
trafficLevels []TrafficLevelInput,
104+
) error {
105+
logrus.Debug("Uploading traffic info to cloud")
106+
107+
_, err := ReportTrafficLevels(
108+
ctx,
109+
c.client,
110+
trafficLevels,
111+
)
112+
if err != nil {
113+
return errors.Wrap(err)
114+
}
115+
116+
return nil
117+
}

src/mapper/pkg/cloudclient/generated.go

+77
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/mapper/pkg/cloudclient/genqlient.graphql

+6
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,10 @@ mutation ReportK8sServices($namespace: String!, $services: [K8sServiceInput!]!)
2121

2222
mutation ReportK8sIngresses($namespace: String!, $ingresses: [K8sIngressInput!]!) {
2323
reportK8sIngresses(namespace: $namespace, ingresses: $ingresses)
24+
}
25+
26+
mutation ReportTrafficLevels(
27+
$trafficLevels: [TrafficLevelInput!]!
28+
) {
29+
reportTrafficLevels(trafficLevels: $trafficLevels)
2430
}

src/mapper/pkg/cloudclient/mocks/mocks.go

+14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)