1
+ // Copyright (c) 2025 Tigera, Inc. All rights reserved.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
1
15
package flowlogs
2
16
3
17
import (
@@ -15,7 +29,7 @@ import (
15
29
"github.com/sirupsen/logrus"
16
30
)
17
31
18
- func StartServerAndWatch () {
32
+ func StartServerAndWatch (num int ) {
19
33
ctx , stop := signal .NotifyContext (context .Background (), syscall .SIGINT , syscall .SIGTERM )
20
34
defer stop ()
21
35
@@ -31,29 +45,44 @@ func StartServerAndWatch() {
31
45
return
32
46
}
33
47
48
+ infinitLoop := num < 0
49
+ var count int
34
50
for {
35
- if ctx .Err () != nil {
36
- logrus .Info ("Closing goldmane unix server" )
51
+ if ctx .Err () != nil ||
52
+ (! infinitLoop && count >= num ) {
53
+ logrus .Debug ("Closing goldmane unix server" )
54
+ nodeServer .Stop ()
37
55
cleanupGoldmaneSocket ()
38
56
return
39
57
}
58
+
40
59
flows := nodeServer .ListAndFlush ()
41
60
for _ , flow := range flows {
42
- fmt .Printf ("%s\n " , flowToString (flow ))
61
+ fmt .Printf ("%s" , flowToString (flow ))
43
62
}
63
+ count = count + len (flows )
44
64
time .Sleep (time .Second )
45
65
}
46
66
}
47
67
48
68
func flowToString (f * types.Flow ) string {
49
- output := fmt .Sprintf ("Src={%s(%s/%s) %vP %vB} Dst={%s(%s/%s) %vP %vB} Proto=%s(%v) Action=%v" ,
50
- endpointTypeToString (f .Key .SourceType ()), f .Key .SourceNamespace (), f .Key .SourceName (), f .PacketsIn , f .BytesIn ,
51
- endpointTypeToString (f .Key .DestType ()), f .Key .DestNamespace (), f .Key .DestName (), f .PacketsOut , f .BytesOut ,
52
- f .Key .Proto (), f .Key .DestPort (),
53
- f .Key .Action (),
69
+ startTime := time .Unix (f .StartTime , 0 )
70
+ policyTrace := types .FlowLogPolicyToProto (f .Key .Policies ())
71
+ return fmt .Sprintf (
72
+ "- Time=%v Reporter=%v Action=%v\n " +
73
+ " Src=%s(%s/%s) Dst=%s(%s/%s) Svc=%s/%s Proto=%s(%v svc:%s/%v)\n " +
74
+ " Counts={Ingress: %vPkts/%vBytes Egress:%vPkts/%vBytes} Connections={Started:%v Completed:%v Live:%v}\n " +
75
+ " Enforced:\n %v\n " +
76
+ " Pending:\n %v\n " ,
77
+ startTime , f .Key .Reporter (), f .Key .Action (),
78
+ endpointTypeToString (f .Key .SourceType ()), f .Key .SourceNamespace (), f .Key .SourceName (),
79
+ endpointTypeToString (f .Key .DestType ()), f .Key .DestNamespace (), f .Key .DestName (),
80
+ f .Key .DestServiceName (), f .Key .DestServiceNamespace (),
81
+ f .Key .Proto (), f .Key .DestPort (), f .Key .DestServicePortName (), f .Key .DestServicePort (),
82
+ f .PacketsIn , f .BytesIn , f .PacketsOut , f .BytesOut ,
83
+ f .NumConnectionsStarted , f .NumConnectionsCompleted , f .NumConnectionsLive ,
84
+ policyHitsToString (policyTrace .EnforcedPolicies ), policyHitsToString (policyTrace .PendingPolicies ),
54
85
)
55
-
56
- return output
57
86
}
58
87
59
88
func endpointTypeToString (ep proto.EndpointType ) string {
@@ -71,17 +100,25 @@ func endpointTypeToString(ep proto.EndpointType) string {
71
100
}
72
101
}
73
102
103
+ func policyHitsToString (policies []* proto.PolicyHit ) string {
104
+ var out string
105
+ for _ , p := range policies {
106
+ out = out + fmt .Sprintf (" - %v" , p )
107
+ }
108
+ return out
109
+ }
110
+
74
111
func ensureGoldmaneSocketDirectory (addr string ) error {
75
112
path := path .Dir (addr )
76
113
// Check if goldmane unix server exists at the expected location.
77
- logrus .Info ("Checking if goldmane unix server exists." )
114
+ logrus .Debug ("Checking if goldmane unix server exists." )
78
115
if _ , err := os .Stat (path ); os .IsNotExist (err ) {
79
- logrus .WithField ("path" , path ).Info ("Goldmane unix socket directory does not exist." )
116
+ logrus .WithField ("path" , path ).Debug ("Goldmane unix socket directory does not exist." )
80
117
err := os .MkdirAll (path , 0o600 )
81
118
if err != nil {
82
119
return err
83
120
}
84
- logrus .WithField ("path" , path ).Info ("Created goldmane unix server directory." )
121
+ logrus .WithField ("path" , path ).Debug ("Created goldmane unix server directory." )
85
122
}
86
123
return nil
87
124
}
0 commit comments