@@ -50,8 +50,17 @@ const (
50
50
Closing
51
51
)
52
52
53
- var fdProcess = make (map [uint64 ]* common.Process )
54
- var pidNetNS map [uint32 ]string
53
+ type processData struct {
54
+ fdProcess map [uint64 ]* common.Process
55
+ pidNetNS map [uint32 ]string
56
+ }
57
+
58
+ func newProcessData () * processData {
59
+ return & processData {
60
+ fdProcess : make (map [uint64 ]* common.Process ),
61
+ pidNetNS : make (map [uint32 ]string ),
62
+ }
63
+ }
55
64
56
65
var skStates = [... ]string {
57
66
"UNKNOWN" ,
@@ -137,7 +146,7 @@ func parseAddr(s string) (*SockEndpoint, error) {
137
146
return & SockEndpoint {IP : ip , Port : uint16 (v )}, nil
138
147
}
139
148
140
- func parseSockTab (reader io.Reader , accept AcceptFn , transport string , podPid uint32 ) ([]SockTabEntry , error ) {
149
+ func ( pd * processData ) parseSockTab (reader io.Reader , accept AcceptFn , transport string , podPid uint32 ) ([]SockTabEntry , error ) {
141
150
scanner := bufio .NewScanner (reader )
142
151
scanner .Scan ()
143
152
@@ -177,13 +186,13 @@ func parseSockTab(reader io.Reader, accept AcceptFn, transport string, podPid ui
177
186
}
178
187
entry .Transport = transport
179
188
if podPid != 0 {
180
- if netNsName , ok := pidNetNS [podPid ]; ok {
189
+ if netNsName , ok := pd . pidNetNS [podPid ]; ok {
181
190
entry .NetNS = netNsName
182
191
} else {
183
192
entry .NetNS = strconv .Itoa (int (podPid ))
184
193
}
185
194
}
186
- entry .Process = fdProcess [entry .Inode ]
195
+ entry .Process = pd . fdProcess [entry .Inode ]
187
196
if accept (& entry ) {
188
197
sockTab = append (sockTab , entry )
189
198
}
@@ -198,15 +207,17 @@ func parseSockTab(reader io.Reader, accept AcceptFn, transport string, podPid ui
198
207
func Netstat (ctx context.Context , feature EnableFeatures , fn AcceptFn ) ([]SockTabEntry , error ) {
199
208
var err error
200
209
201
- pids , err := mergePids (feature )
210
+ pd := newProcessData ()
211
+
212
+ pids , err := pd .mergePids (feature )
202
213
if err != nil {
203
214
return nil , err
204
215
}
205
216
206
217
files := procFiles (feature , pids )
207
218
208
219
if feature .PID {
209
- fdProcess , err = processes .GetProcessFDs (ctx )
220
+ pd . fdProcess , err = processes .GetProcessFDs (ctx )
210
221
if err != nil {
211
222
return nil , err
212
223
}
@@ -226,7 +237,7 @@ func Netstat(ctx context.Context, feature EnableFeatures, fn AcceptFn) ([]SockTa
226
237
chs [i ] <- []SockTabEntry {}
227
238
return
228
239
default :
229
- tabs , err := openFileStream (file , fn )
240
+ tabs , err := pd . openFileStream (file , fn )
230
241
if err != nil {
231
242
// Send an empty slice if there was an error.
232
243
chs [i ] <- []SockTabEntry {}
@@ -252,7 +263,7 @@ func Netstat(ctx context.Context, feature EnableFeatures, fn AcceptFn) ([]SockTa
252
263
return combinedTabs , nil
253
264
}
254
265
255
- func mergePids (feature EnableFeatures ) (pids []string , err error ) {
266
+ func ( pd * processData ) mergePids (feature EnableFeatures ) (pids []string , err error ) {
256
267
if feature .AllNetNs {
257
268
netNsName , err := netns .GetNetNSNames ()
258
269
if err != nil {
@@ -267,17 +278,16 @@ func mergePids(feature EnableFeatures) (pids []string, err error) {
267
278
feature .NetNsPids = []uint32 {}
268
279
}
269
280
270
- pidNetNS = map [uint32 ]string {}
271
281
if len (feature .NetNsName ) > 0 {
272
- pidNetNS = * netns .GetNetNsPids (feature .NetNsName )
282
+ pd . pidNetNS = * netns .GetNetNsPids (feature .NetNsName )
273
283
}
274
284
275
285
hostNetNsIndex := 0
276
286
if ! feature .NoHostNetwork {
277
287
hostNetNsIndex = 1
278
288
}
279
289
280
- lengthPids := len (pidNetNS ) + len (feature .NetNsPids ) + hostNetNsIndex
290
+ lengthPids := len (pd . pidNetNS ) + len (feature .NetNsPids ) + hostNetNsIndex
281
291
pids = make ([]string , lengthPids )
282
292
283
293
if ! feature .NoHostNetwork {
@@ -286,7 +296,7 @@ func mergePids(feature EnableFeatures) (pids []string, err error) {
286
296
287
297
netNsNameIndex := 0
288
298
289
- for pid := range pidNetNS {
299
+ for pid := range pd . pidNetNS {
290
300
pids [netNsNameIndex + hostNetNsIndex ] = strconv .Itoa (int (pid ))
291
301
netNsNameIndex ++
292
302
}
@@ -329,7 +339,7 @@ func procFiles(feature EnableFeatures, pids []string) (files []string) {
329
339
return files
330
340
}
331
341
332
- func openFileStream (file string , fn AcceptFn ) ([]SockTabEntry , error ) {
342
+ func ( pd * processData ) openFileStream (file string , fn AcceptFn ) ([]SockTabEntry , error ) {
333
343
f , err := os .Open (file )
334
344
if err != nil {
335
345
return nil , err
@@ -338,7 +348,7 @@ func openFileStream(file string, fn AcceptFn) ([]SockTabEntry, error) {
338
348
_ , transport := path .Split (file )
339
349
podPid , _ := strconv .ParseUint (strings .Split (file , "/" )[2 ], 10 , 32 )
340
350
341
- tabs , err := parseSockTab (f , fn , transport , uint32 (podPid ))
351
+ tabs , err := pd . parseSockTab (f , fn , transport , uint32 (podPid ))
342
352
if err != nil {
343
353
return nil , err
344
354
}
0 commit comments