-
Notifications
You must be signed in to change notification settings - Fork 22
/
helpers.go
71 lines (59 loc) · 2.3 KB
/
helpers.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
package istiowatcher
import (
"github.com/otterize/network-mapper/src/mapper/pkg/graph/model"
"github.com/samber/lo"
"golang.org/x/exp/slices"
"net/http"
"time"
)
var HTTPMethodsToGQLMethods = map[string]model.HTTPMethod{
http.MethodGet: model.HTTPMethodGet,
http.MethodPost: model.HTTPMethodPost,
http.MethodPut: model.HTTPMethodPut,
http.MethodDelete: model.HTTPMethodDelete,
http.MethodOptions: model.HTTPMethodOptions,
http.MethodTrace: model.HTTPMethodTrace,
http.MethodPatch: model.HTTPMethodPatch,
http.MethodConnect: model.HTTPMethodConnect,
}
type ConnectionPairWithPath struct {
SourceWorkload string `json:"sourceWorkload"`
DestinationWorkload string `json:"destinationWorkload"`
RequestPath string `json:"requestPath"`
}
func ToGraphQLIstioConnections(connections map[ConnectionWithPath]time.Time) []model.IstioConnection {
connectionPairToGraphQLConnection := map[ConnectionPairWithPath]model.IstioConnection{}
for connWithPath, timestamp := range connections {
connectionPair := ConnectionPairWithPath{
SourceWorkload: connWithPath.SourceWorkload,
DestinationWorkload: connWithPath.DestinationWorkload,
RequestPath: connWithPath.RequestPath,
}
istioConnection, ok := connectionPairToGraphQLConnection[connectionPair]
if !ok {
istioConnection = model.IstioConnection{
SrcWorkload: connWithPath.SourceWorkload,
SrcWorkloadNamespace: connWithPath.SourceNamespace,
DstWorkload: connWithPath.DestinationWorkload,
DstWorkloadNamespace: connWithPath.DestinationNamespace,
Path: connWithPath.RequestPath,
LastSeen: timestamp,
}
method, ok := HTTPMethodsToGQLMethods[connWithPath.RequestMethod]
if ok {
istioConnection.Methods = []model.HTTPMethod{method}
}
connectionPairToGraphQLConnection[connectionPair] = istioConnection
continue
}
if timestamp.After(istioConnection.LastSeen) {
istioConnection.LastSeen = timestamp
}
method, ok := HTTPMethodsToGQLMethods[connWithPath.RequestMethod]
if ok && !slices.Contains(istioConnection.Methods, method) {
istioConnection.Methods = append(istioConnection.Methods, method)
}
connectionPairToGraphQLConnection[connectionPair] = istioConnection
}
return lo.Values(connectionPairToGraphQLConnection)
}