@@ -102,6 +102,7 @@ func NewContainerdHandler() *ContainerdHandler {
102
102
// Subscribe to containerd events
103
103
104
104
// docker namespace
105
+ ch .docker = context .Background ()
105
106
ch .docker = namespaces .WithNamespace (context .Background (), "moby" )
106
107
107
108
dockerEventsCh , _ := client .EventService ().Subscribe (ch .docker , "" )
@@ -128,7 +129,7 @@ func (ch *ContainerdHandler) Close() {
128
129
// ==================== //
129
130
130
131
// GetContainerInfo Function
131
- func (ch * ContainerdHandler ) GetContainerInfo (ctx context.Context , containerID string , OwnerInfo map [string ]tp.PodOwner ) (tp.Container , error ) {
132
+ func (ch * ContainerdHandler ) GetContainerInfo (ctx context.Context , containerID string , eventpid uint32 , OwnerInfo map [string ]tp.PodOwner ) (tp.Container , error ) {
132
133
res , err := ch .client .ContainerService ().Get (ctx , containerID )
133
134
if err != nil {
134
135
return tp.Container {}, err
@@ -184,6 +185,36 @@ func (ch *ContainerdHandler) GetContainerInfo(ctx context.Context, containerID s
184
185
}
185
186
186
187
// == //
188
+ if eventpid == 0 {
189
+ taskReq := task.ListPidsRequest {ContainerID : container .ContainerID }
190
+ if taskRes , err := ch .client .TaskService ().ListPids (ctx , & taskReq ); err == nil {
191
+ if len (taskRes .Processes ) == 0 {
192
+ return container , err
193
+ }
194
+
195
+ container .Pid = taskRes .Processes [0 ].Pid
196
+
197
+ } else {
198
+ return container , err
199
+ }
200
+
201
+ } else {
202
+ container .Pid = eventpid
203
+ }
204
+
205
+ pid := strconv .Itoa (int (container .Pid ))
206
+
207
+ if data , err := os .Readlink (filepath .Join (cfg .GlobalCfg .ProcFsMount , pid , "/ns/pid" )); err == nil {
208
+ if _ , err := fmt .Sscanf (data , "pid:[%d]\n " , & container .PidNS ); err != nil {
209
+ kg .Warnf ("Unable to get PidNS (%s, %s, %s)" , containerID , pid , err .Error ())
210
+ }
211
+ }
212
+
213
+ if data , err := os .Readlink (filepath .Join (cfg .GlobalCfg .ProcFsMount , pid , "/ns/mnt" )); err == nil {
214
+ if _ , err := fmt .Sscanf (data , "mnt:[%d]\n " , & container .MntNS ); err != nil {
215
+ kg .Warnf ("Unable to get MntNS (%s, %s, %s)" , containerID , pid , err .Error ())
216
+ }
217
+ }
187
218
188
219
taskReq := task.ListPidsRequest {ContainerID : container .ContainerID }
189
220
if taskRes , err := ch .client .TaskService ().ListPids (ctx , & taskReq ); err == nil {
@@ -267,15 +298,15 @@ func (ch *ContainerdHandler) GetContainerdContainers() map[string]context.Contex
267
298
}
268
299
269
300
// UpdateContainerdContainer Function
270
- func (dm * KubeArmorDaemon ) UpdateContainerdContainer (ctx context.Context , containerID , action string ) bool {
301
+ func (dm * KubeArmorDaemon ) UpdateContainerdContainer (ctx context.Context , containerID string , containerPid uint32 , action string ) bool {
271
302
// check if Containerd exists
272
303
if Containerd == nil {
273
304
return false
274
305
}
275
306
276
307
if action == "start" {
277
308
// get container information from containerd client
278
- container , err := Containerd .GetContainerInfo (ctx , containerID , dm .OwnerInfo )
309
+ container , err := Containerd .GetContainerInfo (ctx , containerID , containerPid , dm .OwnerInfo )
279
310
if err != nil {
280
311
kg .Err (err .Error ())
281
312
return false
@@ -554,7 +585,7 @@ func (dm *KubeArmorDaemon) MonitorContainerdEvents() {
554
585
555
586
if len (containers ) > 0 {
556
587
for containerID , context := range containers {
557
- if ! dm .UpdateContainerdContainer (context , containerID , "start" ) {
588
+ if ! dm .UpdateContainerdContainer (context , containerID , 0 , "start" ) {
558
589
continue
559
590
}
560
591
}
@@ -588,7 +619,7 @@ func (dm *KubeArmorDaemon) handleContainerdEvent(envelope *events.Envelope, cont
588
619
if err != nil {
589
620
kg .Errf ("failed to unmarshal container's delete event: %v" , err )
590
621
}
591
- dm .UpdateContainerdContainer (context , deleteContainer .GetID (), "destroy" )
622
+ dm .UpdateContainerdContainer (context , deleteContainer .GetID (), 0 , "destroy" )
592
623
593
624
case "/tasks/start" :
594
625
startTask := & apievents.TaskStart {}
@@ -597,7 +628,7 @@ func (dm *KubeArmorDaemon) handleContainerdEvent(envelope *events.Envelope, cont
597
628
if err != nil {
598
629
kg .Errf ("failed to unmarshal container's start task: %v" , err )
599
630
}
600
- dm .UpdateContainerdContainer (context , startTask .GetContainerID (), "start" )
631
+ dm .UpdateContainerdContainer (context , startTask .GetContainerID (), startTask . GetPid (), "start" )
601
632
602
633
case "/tasks/exit" :
603
634
exitTask := & apievents.TaskStart {}
@@ -612,7 +643,7 @@ func (dm *KubeArmorDaemon) handleContainerdEvent(envelope *events.Envelope, cont
612
643
dm .ContainersLock .RUnlock ()
613
644
614
645
if pid == exitTask .GetPid () {
615
- dm .UpdateContainerdContainer (context , exitTask .GetContainerID (), "destroy" )
646
+ dm .UpdateContainerdContainer (context , exitTask .GetContainerID (), pid , "destroy" )
616
647
}
617
648
618
649
}
0 commit comments