-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathcloud_upload.go
More file actions
383 lines (338 loc) · 13.2 KB
/
Copy pathcloud_upload.go
File metadata and controls
383 lines (338 loc) · 13.2 KB
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package clouduploader
import (
"context"
"encoding/json"
"fmt"
"github.com/otterize/intents-operator/src/shared/errors"
"github.com/otterize/intents-operator/src/shared/serviceidresolver/serviceidentity"
"github.com/otterize/network-mapper/src/mapper/pkg/awsintentsholder"
"github.com/otterize/network-mapper/src/mapper/pkg/collectors/traffic"
"github.com/otterize/network-mapper/src/mapper/pkg/externaltrafficholder"
"github.com/otterize/network-mapper/src/mapper/pkg/gcpintentsholder"
"github.com/otterize/network-mapper/src/mapper/pkg/incomingtrafficholder"
"time"
"github.com/cenkalti/backoff/v4"
"github.com/otterize/network-mapper/src/mapper/pkg/cloudclient"
"github.com/otterize/network-mapper/src/mapper/pkg/graph/model"
"github.com/otterize/network-mapper/src/mapper/pkg/intentsstore"
"github.com/samber/lo"
"github.com/sirupsen/logrus"
)
type CloudUploader struct {
intentsHolder *intentsstore.IntentsHolder
config Config
client cloudclient.CloudClient
}
func NewCloudUploader(intentsHolder *intentsstore.IntentsHolder, config Config, client cloudclient.CloudClient) *CloudUploader {
return &CloudUploader{
intentsHolder: intentsHolder,
config: config,
client: client,
}
}
func (c *CloudUploader) NotifyIntents(ctx context.Context, intents []intentsstore.TimestampedIntent) {
if len(intents) == 0 {
return
}
discoveredIntents := lo.Map(intents, func(intent intentsstore.TimestampedIntent, _ int) *cloudclient.DiscoveredIntentInput {
toCloud := &cloudclient.DiscoveredIntentInput{
DiscoveredAt: lo.ToPtr(intent.Timestamp),
Intent: &cloudclient.IntentInput{
ClientName: &intent.Intent.Client.Name,
Namespace: &intent.Intent.Client.Namespace,
ClientNameResolvedUsingAnnotation: intent.Intent.Client.NameResolvedUsingAnnotation,
ServerName: &intent.Intent.Server.Name,
ServerNamespace: &intent.Intent.Server.Namespace,
ServerNameResolvedUsingAnnotation: intent.Intent.Server.NameResolvedUsingAnnotation,
Type: modelIntentTypeToAPI(intent.Intent.Type),
Topics: lo.Map(intent.Intent.KafkaTopics,
func(item model.KafkaConfig, _ int) *cloudclient.KafkaConfigInput {
return lo.ToPtr(modelKafkaConfToAPI(item))
},
),
Resources: httpResourceToHTTPConfInput(intent.Intent.HTTPResources),
},
}
if intent.ConnectionsCount != nil {
toCloud.Intent.ConnectionsCount = intent.ConnectionsCount
}
if intent.Intent.Client.PodOwnerKind != nil && intent.Intent.Client.PodOwnerKind.Kind != "" {
toCloud.Intent.ClientWorkloadKind = lo.ToPtr(intent.Intent.Client.PodOwnerKind.Kind)
}
if intent.Intent.Server.PodOwnerKind != nil && intent.Intent.Server.PodOwnerKind.Kind != "" {
toCloud.Intent.ServerWorkloadKind = lo.ToPtr(intent.Intent.Server.PodOwnerKind.Kind)
}
if intent.Intent.Server.KubernetesService != nil {
toCloud.Intent.ServerAlias = &cloudclient.ServerAliasInput{Name: intent.Intent.Server.KubernetesService, Kind: lo.ToPtr(serviceidentity.KindService)}
}
if intent.Intent.Client.ResolutionData != nil && !lo.IsEmpty(*intent.Intent.Client.ResolutionData) {
jsonStr, err := json.Marshal(intent.Intent.Client.ResolutionData)
if err != nil {
logrus.WithError(err).Error("Failed to marshal client resolution data")
} else {
toCloud.Intent.ClientResolutionData = lo.ToPtr(string(jsonStr))
}
}
if intent.Intent.Server.ResolutionData != nil && !lo.IsEmpty(*intent.Intent.Server.ResolutionData) {
jsonStr, err := json.Marshal(intent.Intent.Server.ResolutionData)
if err != nil {
logrus.WithError(err).Error("Failed to marshal server resolution data")
} else {
toCloud.Intent.ServerResolutionData = lo.ToPtr(string(jsonStr))
}
}
if intent.Intent.ResolutionData != nil {
toCloud.Intent.ResolutionData = lo.ToPtr(*intent.Intent.ResolutionData)
}
// debug log all the fields of intent input one by one with their values
logrus.Debugf("intent ClientName: %s\t Namespace: %s\t ServerName: %s\t ServerNamespace: %s\t ClientWorkloadKind: %s\t ServerWorkloadKind: %s\t ServerAlias: %v", lo.FromPtr(toCloud.Intent.ClientName), lo.FromPtr(toCloud.Intent.Namespace), lo.FromPtr(toCloud.Intent.ServerName), lo.FromPtr(toCloud.Intent.ServerNamespace), lo.FromPtr(toCloud.Intent.ClientWorkloadKind), lo.FromPtr(toCloud.Intent.ServerWorkloadKind), lo.FromPtr(toCloud.Intent.ServerAlias))
return toCloud
})
exponentialBackoff := backoff.NewExponentialBackOff()
discoveredIntentsChunks := lo.Chunk(discoveredIntents, c.config.UploadBatchSize)
currentChunk := 0
err := backoff.Retry(func() error {
for currentChunk < len(discoveredIntentsChunks) {
err := c.client.ReportDiscoveredIntents(ctx, discoveredIntentsChunks[currentChunk])
if err != nil {
logrus.WithError(err).Errorf("Failed to report discovered intents chunk %d to cloud, retrying", currentChunk)
return errors.Wrap(err)
}
currentChunk += 1
}
return nil
}, exponentialBackoff)
if err != nil {
logrus.WithError(err).Error("Failed to report discovered intents to cloud, giving up after 10 retries")
}
}
func (c *CloudUploader) NotifyExternalTrafficIntents(ctx context.Context, intents []externaltrafficholder.TimestampedExternalTrafficIntent) {
if len(intents) == 0 {
return
}
logrus.Debugf("Got external traffic notification, len %d", len(intents))
discoveredIntents := lo.Map(intents, func(intent externaltrafficholder.TimestampedExternalTrafficIntent, _ int) cloudclient.ExternalTrafficDiscoveredIntentInput {
switch typedIntent := intent.Intent.(type) {
case externaltrafficholder.DNSExternalTrafficIntent:
output := cloudclient.ExternalTrafficDiscoveredIntentInput{
DiscoveredAt: intent.Timestamp,
Intent: cloudclient.ExternalTrafficIntentInput{
ClientName: typedIntent.Client.Name,
Namespace: typedIntent.Client.Namespace,
Target: cloudclient.DNSIPPairInput{
DnsName: lo.ToPtr(typedIntent.DNSName),
},
},
}
for ip := range typedIntent.IPs {
output.Intent.Target.Ips = append(output.Intent.Target.Ips, lo.ToPtr(string(ip)))
}
return output
case externaltrafficholder.IPExternalTrafficIntent:
output := cloudclient.ExternalTrafficDiscoveredIntentInput{
DiscoveredAt: intent.Timestamp,
Intent: cloudclient.ExternalTrafficIntentInput{
ClientName: typedIntent.Client.Name,
Namespace: typedIntent.Client.Namespace,
Target: cloudclient.DNSIPPairInput{
Ips: []*string{lo.ToPtr(string(typedIntent.IP))},
},
},
}
return output
}
panic(fmt.Sprintf("Unexpected external traffic intent type: %T", intent))
})
exponentialBackoff := backoff.NewExponentialBackOff()
discoveredIntentsChunks := lo.Chunk(discoveredIntents, c.config.UploadBatchSize)
currentChunk := 0
err := backoff.Retry(func() error {
for currentChunk < len(discoveredIntentsChunks) {
err := c.client.ReportExternalTrafficDiscoveredIntents(ctx, discoveredIntentsChunks[currentChunk])
if err != nil {
logrus.WithError(err).Errorf("Failed to report discovered intents chunk %d to cloud, retrying", currentChunk)
return errors.Wrap(err)
}
currentChunk += 1
}
return nil
}, exponentialBackoff)
if err != nil {
logrus.WithError(err).Error("Failed to report discovered intents to cloud, giving up after 10 retries")
}
}
func (c *CloudUploader) NotifyIncomingTrafficIntents(ctx context.Context, intents []incomingtrafficholder.TimestampedIncomingTrafficIntent) {
if len(intents) == 0 {
return
}
logrus.Debugf("Got incoming traffic notification, len %d", len(intents))
discoveredIntents := lo.Map(intents, func(intent incomingtrafficholder.TimestampedIncomingTrafficIntent, _ int) cloudclient.IncomingTrafficDiscoveredIntentInput {
output := cloudclient.IncomingTrafficDiscoveredIntentInput{
DiscoveredAt: intent.Timestamp,
Intent: cloudclient.IncomingTrafficIntentInput{
ServerName: intent.Intent.Server.Name,
Namespace: intent.Intent.Server.Namespace,
Source: cloudclient.IncomingInternetSourceInput{
Ip: intent.Intent.IP,
},
},
}
return output
})
exponentialBackoff := backoff.NewExponentialBackOff()
discoveredIntentsChunks := lo.Chunk(discoveredIntents, c.config.UploadBatchSize)
currentChunk := 0
err := backoff.Retry(func() error {
for currentChunk < len(discoveredIntentsChunks) {
err := c.client.ReportIncomingTrafficDiscoveredIntents(ctx, discoveredIntentsChunks[currentChunk])
if err != nil {
logrus.WithError(err).Errorf("Failed to report incoming traffic intents chunk %d to cloud, retrying", currentChunk)
return errors.Wrap(err)
}
currentChunk += 1
}
return nil
}, exponentialBackoff)
if err != nil {
logrus.WithError(err).Error("Failed to report incoming traffic intents to cloud, giving up after 10 retries")
}
}
func (c *CloudUploader) NotifyAWSIntents(ctx context.Context, intents []awsintentsholder.AWSIntent) {
if len(intents) == 0 {
return
}
logrus.Debugf("Got AWS intents notification, len %d", len(intents))
intentType := cloudclient.IntentTypeAws
now := time.Now()
err := c.client.ReportDiscoveredIntents(
ctx,
lo.Map(intents, func(intent awsintentsholder.AWSIntent, _ int) *cloudclient.DiscoveredIntentInput {
var awsRole *string
if intent.IamRole != "" {
awsRole = lo.ToPtr(intent.IamRole)
}
toCloud := &cloudclient.DiscoveredIntentInput{
DiscoveredAt: &now,
Intent: &cloudclient.IntentInput{
ClientName: &intent.Client.Name,
Namespace: &intent.Client.Namespace,
ServerName: &intent.ARN,
Type: &intentType,
AwsRole: awsRole,
AwsActions: lo.ToSlicePtr(intent.Actions),
},
}
if intent.Client.PodOwnerKind != nil && intent.Client.PodOwnerKind.Kind != "" {
toCloud.Intent.ClientWorkloadKind = lo.ToPtr(intent.Client.PodOwnerKind.Kind)
}
return toCloud
}),
)
if err != nil {
logrus.WithError(err).Error("Failed to report discovered intents to cloud")
}
}
func (c *CloudUploader) NotifyAzureIntents(ctx context.Context, ops []model.AzureOperation) {
if len(ops) == 0 {
return
}
intentType := cloudclient.IntentTypeAzure
now := time.Now()
intents := lo.Map(ops, func(op model.AzureOperation, _ int) *cloudclient.DiscoveredIntentInput {
toCloud := &cloudclient.DiscoveredIntentInput{
DiscoveredAt: &now,
Intent: &cloudclient.IntentInput{
ClientName: &op.ClientName,
Namespace: &op.ClientNamespace,
ServerName: &op.Scope,
Type: &intentType,
AzureActions: lo.ToSlicePtr(op.Actions),
AzureDataActions: lo.ToSlicePtr(op.DataActions),
},
}
return toCloud
})
err := c.client.ReportDiscoveredIntents(
ctx,
intents,
)
if err != nil {
logrus.WithError(err).Error("Failed to report discovered intents to cloud")
}
}
func (c *CloudUploader) NotifyGCPIntents(ctx context.Context, ops []gcpintentsholder.GCPIntent) {
if len(ops) == 0 {
return
}
intentType := cloudclient.IntentTypeGcp
now := time.Now()
intents := lo.Map(ops, func(op gcpintentsholder.GCPIntent, _ int) *cloudclient.DiscoveredIntentInput {
toCloud := &cloudclient.DiscoveredIntentInput{
DiscoveredAt: &now,
Intent: &cloudclient.IntentInput{
ClientName: &op.Client.Name,
Namespace: &op.Client.Namespace,
ServerName: &op.Resource,
Type: &intentType,
GcpPermissions: lo.ToSlicePtr(op.Permissions),
},
}
return toCloud
})
err := c.client.ReportDiscoveredIntents(
ctx,
intents,
)
if err != nil {
logrus.WithError(err).Error("Failed to report discovered intents to cloud")
}
}
func (c *CloudUploader) NotifyTrafficLevels(ctx context.Context, trafficLevels traffic.TrafficLevelMap) {
if len(trafficLevels) == 0 {
return
}
var inputs []cloudclient.TrafficLevelInput
for trafficPair, trafficData := range trafficLevels {
inputs = append(inputs, cloudclient.TrafficLevelInput{
ClientName: trafficPair.SourceName,
ClientNamespace: trafficPair.SourceNamespace,
ServerName: trafficPair.DestinationName,
ServerNamespace: trafficPair.DestinationNamespace,
DataBytesPerSecond: trafficData.Bytes,
FlowsCountPerSecond: trafficData.Flows,
})
}
err := c.client.ReportTrafficLevels(ctx, inputs)
if err != nil {
logrus.WithError(err).Error("Failed to report traffic levels to cloud")
}
}
func httpResourceToHTTPConfInput(resources []model.HTTPResource) []*cloudclient.HTTPConfigInput {
httpGQLInputs := make([]*cloudclient.HTTPConfigInput, 0)
for _, resource := range resources {
httpGQLInputs = append(httpGQLInputs, &cloudclient.HTTPConfigInput{
Path: lo.ToPtr(resource.Path),
Methods: lo.Map(resource.Methods, func(method model.HTTPMethod, _ int) *cloudclient.HTTPMethod {
return lo.ToPtr(modelHTTPMethodToAPI(method))
})})
}
return httpGQLInputs
}
func (c *CloudUploader) reportStatus(ctx context.Context) {
err := c.client.ReportComponentStatus(ctx, cloudclient.ComponentTypeNetworkMapper)
if err != nil {
logrus.WithError(err).Error("Failed to report component status to cloud")
}
}
func (c *CloudUploader) PeriodicStatusReport(ctx context.Context) {
logrus.Info("Starting status reporting")
c.reportStatus(ctx)
for {
select {
case <-time.After(c.config.UploadInterval):
c.reportStatus(ctx)
case <-ctx.Done():
return
}
}
}