-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathclient.go
369 lines (329 loc) · 10.1 KB
/
client.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
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
// Copyright (c) 2025 Tigera, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package goldmane
import (
"context"
"fmt"
"os"
"sort"
"strings"
"sync"
"time"
"unique"
"github.com/sirupsen/logrus"
"github.com/projectcalico/calico/felix/collector/flowlog"
"github.com/projectcalico/calico/felix/collector/types/endpoint"
"github.com/projectcalico/calico/felix/collector/types/tuple"
"github.com/projectcalico/calico/felix/collector/utils"
"github.com/projectcalico/calico/goldmane/pkg/client"
"github.com/projectcalico/calico/goldmane/pkg/types"
"github.com/projectcalico/calico/goldmane/proto"
)
type GoldmaneReporter struct {
address string
client *client.FlowClient
once sync.Once
// Fields related to goldmane unix socket
maySendToNodeSocket bool
nodeClient *client.FlowClient
nodeClientLock sync.RWMutex
}
func NewReporter(addr, cert, key, ca string) (*GoldmaneReporter, error) {
cli, err := client.NewFlowClient(addr, cert, key, ca)
if err != nil {
return nil, err
}
return &GoldmaneReporter{
address: addr,
client: cli,
// Do not send flowlogs to node socket, if goldmane address set via FelixConfig is equal to node socket
maySendToNodeSocket: addr != LocalGoldmaneServer,
}, nil
}
func (g *GoldmaneReporter) Start() error {
var err error
g.once.Do(func() {
// We don't wait for the initial connection to start so we don't block the caller.
g.client.Connect(context.Background())
if g.maySendToNodeSocket {
go g.nodeSocketReporter()
}
})
return err
}
func (g *GoldmaneReporter) nodeSocketReporter() {
for {
if NodeSocketExists() {
g.mayStartNodeSocketReporter()
} else {
g.mayStopNodeSocketReporter()
}
time.Sleep(time.Second * 10)
}
}
func NodeSocketExists() bool {
_, err := os.Stat(LocalGoldmaneServer)
// In case of any error, return false
return err == nil
}
func (g *GoldmaneReporter) nodeClientIsNil() bool {
g.nodeClientLock.RLock()
defer g.nodeClientLock.RUnlock()
return g.nodeClient == nil
}
func (g *GoldmaneReporter) mayStartNodeSocketReporter() {
if !g.nodeClientIsNil() {
return
}
var err error
g.nodeClientLock.Lock()
defer g.nodeClientLock.Unlock()
sockAddr := fmt.Sprintf("unix://%v", LocalGoldmaneServer)
g.nodeClient, err = client.NewFlowClient(sockAddr, "", "", "")
if err != nil {
logrus.WithError(err).Warn("Failed to create goldmane unix client")
return
}
logrus.Info("Created goldmane unix client")
g.nodeClient.Connect(context.Background())
}
func (g *GoldmaneReporter) mayStopNodeSocketReporter() {
if g.nodeClientIsNil() {
return
}
g.nodeClientLock.Lock()
defer g.nodeClientLock.Unlock()
g.nodeClient = nil
logrus.Info("Destroyed goldmane unix client")
}
func (g *GoldmaneReporter) Report(logSlice any) error {
switch logs := logSlice.(type) {
case []*flowlog.FlowLog:
if logrus.IsLevelEnabled(logrus.DebugLevel) {
logrus.WithField("num", len(logs)).Debug("Dispatching flow logs to goldmane")
}
for _, l := range logs {
goldmaneLog := convertFlowlogToGoldmane(l)
g.client.Push(goldmaneLog)
if g.maySendToNodeSocket {
// If goldmane local unix server exists, also send it flowlogs.
g.nodeClientLock.RLock()
if g.nodeClient != nil {
g.nodeClient.Push(goldmaneLog)
}
g.nodeClientLock.RUnlock()
}
}
default:
logrus.Panic("Unexpected kind of log dispatcher")
}
return nil
}
func convertType(t endpoint.Type) proto.EndpointType {
var pt proto.EndpointType
switch t {
case endpoint.Wep:
pt = proto.EndpointType_WorkloadEndpoint
case endpoint.Hep:
pt = proto.EndpointType_HostEndpoint
case endpoint.Ns:
pt = proto.EndpointType_NetworkSet
case endpoint.Net:
pt = proto.EndpointType_Network
default:
logrus.WithField("type", t).Warn("Unexpected endpoint type")
}
return pt
}
func convertReporter(r flowlog.ReporterType) proto.Reporter {
switch r {
case flowlog.ReporterSrc:
return proto.Reporter_Src
case flowlog.ReporterDst:
return proto.Reporter_Dst
}
logrus.WithField("reporter", r).Fatal("BUG: Unexpected reporter")
return proto.Reporter_Dst
}
func convertAction(a flowlog.Action) proto.Action {
switch a {
case flowlog.ActionAllow:
return proto.Action_Allow
case flowlog.ActionDeny:
return proto.Action_Deny
default:
logrus.WithField("action", a).Fatal("BUG: Unexpected action")
}
return proto.Action_ActionUnspecified
}
func convertFlowlogToGoldmane(fl *flowlog.FlowLog) *types.Flow {
return &types.Flow{
Key: types.NewFlowKey(
&types.FlowKeySource{
SourceName: fl.SrcMeta.AggregatedName,
SourceNamespace: fl.SrcMeta.Namespace,
SourceType: convertType(fl.SrcMeta.Type),
},
&types.FlowKeyDestination{
DestName: fl.DstMeta.AggregatedName,
DestNamespace: fl.DstMeta.Namespace,
DestType: convertType(fl.DstMeta.Type),
DestPort: int64(fl.Tuple.L4Dst),
DestServiceName: fl.DstService.Name,
DestServiceNamespace: fl.DstService.Namespace,
DestServicePortName: fl.DstService.PortName,
DestServicePort: int64(fl.DstService.PortNum),
},
&types.FlowKeyMeta{
Proto: utils.ProtoToString(fl.Tuple.Proto),
Reporter: convertReporter(fl.Reporter),
Action: convertAction(fl.Action),
},
&proto.PolicyTrace{
EnforcedPolicies: toPolicyHits(fl.FlowEnforcedPolicySet),
PendingPolicies: toPolicyHits(fl.FlowPendingPolicySet),
},
),
StartTime: fl.StartTime.Unix(),
EndTime: fl.StartTime.Unix(),
PacketsIn: int64(fl.PacketsIn),
PacketsOut: int64(fl.PacketsOut),
BytesIn: int64(fl.BytesIn),
BytesOut: int64(fl.BytesOut),
NumConnectionsLive: int64(fl.NumFlows),
NumConnectionsStarted: int64(fl.NumFlowsStarted),
NumConnectionsCompleted: int64(fl.NumFlowsCompleted),
SourceLabels: ensureLabels(fl.SrcLabels),
DestLabels: ensureLabels(fl.DstLabels),
}
}
func ConvertGoldmaneToFlowlog(gl *proto.Flow) flowlog.FlowLog {
fl := flowlog.FlowLog{
StartTime: time.Unix(gl.StartTime, 0),
EndTime: time.Unix(gl.EndTime, 0),
}
fl.PacketsIn = int(gl.PacketsIn)
fl.PacketsOut = int(gl.PacketsOut)
fl.BytesIn = int(gl.BytesIn)
fl.BytesOut = int(gl.BytesOut)
fl.NumFlows = int(gl.NumConnectionsLive)
fl.NumFlowsStarted = int(gl.NumConnectionsStarted)
fl.NumFlowsCompleted = int(gl.NumConnectionsCompleted)
fl.SrcLabels = ensureFlowLogLabels(gl.SourceLabels)
fl.DstLabels = ensureFlowLogLabels(gl.DestLabels)
fl.FlowEnforcedPolicySet = toFlowPolicySet(gl.Key.Policies.EnforcedPolicies)
fl.FlowPendingPolicySet = toFlowPolicySet(gl.Key.Policies.PendingPolicies)
fl.SrcMeta = endpoint.Metadata{
Namespace: gl.Key.SourceNamespace,
Name: flowlog.FieldNotIncluded,
AggregatedName: gl.Key.SourceName,
}
switch gl.Key.SourceType {
case proto.EndpointType_WorkloadEndpoint:
fl.SrcMeta.Type = endpoint.Wep
case proto.EndpointType_HostEndpoint:
fl.SrcMeta.Type = endpoint.Hep
case proto.EndpointType_NetworkSet:
fl.SrcMeta.Type = endpoint.Ns
case proto.EndpointType_Network:
fl.SrcMeta.Type = endpoint.Net
default:
panic(fmt.Sprintf("Unexpected source type: %v", gl.Key.SourceType))
}
fl.DstMeta = endpoint.Metadata{
Namespace: gl.Key.DestNamespace,
Name: flowlog.FieldNotIncluded,
AggregatedName: gl.Key.DestName,
}
switch gl.Key.DestType {
case proto.EndpointType_WorkloadEndpoint:
fl.DstMeta.Type = endpoint.Wep
case proto.EndpointType_HostEndpoint:
fl.DstMeta.Type = endpoint.Hep
case proto.EndpointType_NetworkSet:
fl.DstMeta.Type = endpoint.Ns
case proto.EndpointType_Network:
fl.DstMeta.Type = endpoint.Net
default:
panic(fmt.Sprintf("Unexpected destination type: %v", gl.Key.DestType))
}
fl.DstService = flowlog.FlowService{
Namespace: gl.Key.DestServiceNamespace,
Name: gl.Key.DestServiceName,
PortName: gl.Key.DestServicePortName,
PortNum: int(gl.Key.DestServicePort),
}
fl.Tuple = tuple.Tuple{
Proto: utils.StringToProto(gl.Key.Proto),
L4Dst: int(gl.Key.DestPort),
}
switch gl.Key.Reporter {
case proto.Reporter_Src:
fl.Reporter = flowlog.ReporterSrc
case proto.Reporter_Dst:
fl.Reporter = flowlog.ReporterDst
default:
panic(fmt.Sprintf("Unexpected reporter: %v", gl.Key.Reporter))
}
switch gl.Key.Action {
case proto.Action_Allow:
fl.Action = flowlog.ActionAllow
case proto.Action_Deny:
fl.Action = flowlog.ActionDeny
default:
panic(fmt.Sprintf("Unexpected action: %v", gl.Key.Action))
}
return fl
}
// toPolicyHits converts a FlowPolicySet to a slice of policy hits in Goldmane protobuf format.
func toPolicyHits(labels flowlog.FlowPolicySet) []*proto.PolicyHit {
var hits []*proto.PolicyHit
for p := range labels {
h, err := proto.HitFromString(p)
if err != nil {
logrus.WithError(err).WithField("label", p).Panic("Failed to parse policy hit")
}
hits = append(hits, h)
}
return hits
}
// toFlowPolicySet converts a slice of policy hits in Goldmane protobuf format to a FlowPolicySet.
func toFlowPolicySet(policies []*proto.PolicyHit) flowlog.FlowPolicySet {
if policies == nil {
return nil
}
policySet := make(flowlog.FlowPolicySet)
for _, pol := range policies {
if s, err := pol.ToString(); err != nil {
logrus.WithError(err).WithField("policy", pol).Error("Failed to convert policy hit to string")
} else {
policySet[s] = struct{}{}
}
}
return policySet
}
func ensureLabels(labels map[string]string) unique.Handle[string] {
if labels == nil {
return unique.Make("")
}
flat := utils.FlattenLabels(labels)
sort.Strings(flat)
return unique.Make(strings.Join(flat, ","))
}
func ensureFlowLogLabels(lables []string) map[string]string {
if lables == nil {
return map[string]string{}
}
return utils.UnflattenLabels(lables)
}