@@ -82,15 +82,15 @@ type Store struct {
82
82
83
83
metadataNotifier meta.Notifier
84
84
85
- containerIDs map [string ] * container.Info
85
+ containerIDs maps. Map2 [string , uint32 , * container.Info ]
86
86
87
87
// stores container info by PID. It is only required for
88
88
// deleting entries in namespaces and podsByContainer when DeleteProcess is called
89
89
containerByPID map [uint32 ]* container.Info
90
90
91
91
// a single namespace will point to any container inside the pod
92
92
// but we don't care which one
93
- namespaces map [uint32 ] * container.Info
93
+ namespaces maps. Map2 [uint32 , uint32 , * container.Info ]
94
94
95
95
// container ID to pod matcher
96
96
podsByContainer map [string ]* CachedObjMeta
@@ -126,8 +126,8 @@ func NewStore(kubeMetadata meta.Notifier, resourceLabels ResourceLabels, service
126
126
127
127
db := & Store {
128
128
log : log ,
129
- containerIDs : map [string ] * container.Info {},
130
- namespaces : map [uint32 ] * container.Info {},
129
+ containerIDs : maps. Map2 [string , uint32 , * container.Info ] {},
130
+ namespaces : maps. Map2 [uint32 , uint32 , * container.Info ] {},
131
131
podsByContainer : map [string ]* CachedObjMeta {},
132
132
containerByPID : map [uint32 ]* container.Info {},
133
133
objectMetaByIP : map [string ]* CachedObjMeta {},
@@ -197,13 +197,19 @@ func (s *Store) cacheResourceMetadata(meta *informer.ObjectMeta) *CachedObjMeta
197
197
func (s * Store ) On (event * informer.Event ) error {
198
198
switch event .Type {
199
199
case informer .EventType_CREATED :
200
+ // go func() {
201
+ // time.Sleep(10 * time.Second)
200
202
s .addObjectMeta (event .Resource )
203
+ s .Notify (event )
204
+ // }()
201
205
case informer .EventType_UPDATED :
202
206
s .updateObjectMeta (event .Resource )
207
+ s .Notify (event )
203
208
case informer .EventType_DELETED :
204
209
s .deleteObjectMeta (event .Resource )
210
+ s .Notify (event )
205
211
}
206
- s .Notify (event )
212
+ // s.Notify(event)
207
213
return nil
208
214
}
209
215
@@ -221,8 +227,8 @@ func (s *Store) AddProcess(pid uint32) {
221
227
222
228
s .access .Lock ()
223
229
defer s .access .Unlock ()
224
- s .namespaces [ ifp .PIDNamespace ] = & ifp
225
- s .containerIDs [ ifp .ContainerID ] = & ifp
230
+ s .namespaces . Put ( ifp .PIDNamespace , pid , & ifp )
231
+ s .containerIDs . Put ( ifp .ContainerID , pid , & ifp )
226
232
s .containerByPID [pid ] = & ifp
227
233
}
228
234
@@ -234,8 +240,8 @@ func (s *Store) DeleteProcess(pid uint32) {
234
240
return
235
241
}
236
242
delete (s .containerByPID , pid )
237
- delete ( s .namespaces , info .PIDNamespace )
238
- delete ( s .containerIDs , info .ContainerID )
243
+ s .namespaces . Delete ( info .PIDNamespace , pid )
244
+ s .containerIDs . Delete ( info .ContainerID , pid )
239
245
}
240
246
241
247
func (s * Store ) addObjectMeta (meta * informer.ObjectMeta ) {
@@ -279,9 +285,11 @@ func (s *Store) unlockedAddObjectMeta(meta *informer.ObjectMeta) {
279
285
for _ , c := range meta .Pod .Containers {
280
286
s .podsByContainer [c .Id ] = cmeta
281
287
// TODO: make sure we can handle when the containerIDs is set after this function is triggered
282
- info , ok := s .containerIDs [c .Id ]
288
+ infos , ok := s .containerIDs [c .Id ]
283
289
if ok {
284
- s .namespaces [info .PIDNamespace ] = info
290
+ for pid , info := range infos {
291
+ s .namespaces .Put (info .PIDNamespace , pid , info )
292
+ }
285
293
}
286
294
s .containersByOwner .Put (oID , c .Id , c )
287
295
}
@@ -315,10 +323,14 @@ func (s *Store) unlockedDeleteObjectMeta(meta *informer.ObjectMeta) {
315
323
s .log .Debug ("deleting pod from store" ,
316
324
"ips" , meta .Ips , "pod" , meta .Name , "namespace" , meta .Namespace , "containers" , meta .Pod .Containers )
317
325
for _ , c := range meta .Pod .Containers {
318
- info , ok := s .containerIDs [c .Id ]
326
+ infos , ok := s .containerIDs [c .Id ]
319
327
if ok {
320
- delete (s .containerIDs , c .Id )
321
- delete (s .namespaces , info .PIDNamespace )
328
+ s .containerIDs .DeleteAll (c .Id )
329
+ for _ , info := range infos {
330
+ s .namespaces .DeleteAll (info .PIDNamespace )
331
+ // delete all needs to be done only once, we could alternatively delete one by one pid
332
+ break
333
+ }
322
334
}
323
335
delete (s .podsByContainer , c .Id )
324
336
s .containersByOwner .Delete (oID , c .Id )
@@ -345,14 +357,19 @@ func (s *Store) PodByContainerID(cid string) *CachedObjMeta {
345
357
func (s * Store ) PodContainerByPIDNs (pidns uint32 ) (* CachedObjMeta , string ) {
346
358
s .access .RLock ()
347
359
defer s .access .RUnlock ()
348
- if info , ok := s .namespaces [pidns ]; ok {
349
- if om , ok := s .podsByContainer [info .ContainerID ]; ok {
350
- oID := fetchOwnerID (om .Meta )
351
- containerName := ""
352
- if containerInfo , ok := s .containersByOwner .Get (oID , info .ContainerID ); ok {
353
- containerName = containerInfo .Name
360
+ if infos , ok := s .namespaces [pidns ]; ok {
361
+ for _ , info := range infos {
362
+ if om , ok := s .podsByContainer [info .ContainerID ]; ok {
363
+ oID := fetchOwnerID (om .Meta )
364
+ containerName := ""
365
+ if containerInfo , ok := s .containersByOwner .Get (oID , info .ContainerID ); ok {
366
+ containerName = containerInfo .Name
367
+ }
368
+ return om , containerName
354
369
}
355
- return om , containerName
370
+ // we break here, the namespace is the same for all pids in the container
371
+ // we need to check one only
372
+ break
356
373
}
357
374
}
358
375
return nil , ""
0 commit comments