github.com/inspektor-gadget/inspektor-gadget@v0.28.1/pkg/container-collection/operator.go (about) 1 // Copyright 2022-2024 The Inspektor Gadget authors 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 15 package containercollection 16 17 import ( 18 "github.com/inspektor-gadget/inspektor-gadget/pkg/operators" 19 ) 20 21 func (cc *ContainerCollection) EnrichEventByMntNs(event operators.ContainerInfoFromMountNSID) { 22 event.SetNode(cc.nodeName) 23 24 mountNsId := event.GetMountNSID() 25 container := cc.LookupContainerByMntns(mountNsId) 26 if container == nil && cc.cachedContainers != nil { 27 container = lookupContainerByMntns(cc.cachedContainers, mountNsId) 28 } 29 if container != nil { 30 event.SetContainerMetadata(container) 31 } 32 } 33 34 func (cc *ContainerCollection) EnrichEventByNetNs(event operators.ContainerInfoFromNetNSID) { 35 event.SetNode(cc.nodeName) 36 37 netNsId := event.GetNetNSID() 38 containers := cc.LookupContainersByNetns(netNsId) 39 if len(containers) == 0 { 40 containers = lookupContainersByNetns(cc.cachedContainers, netNsId) 41 } 42 if len(containers) == 0 || containers[0].HostNetwork { 43 return 44 } 45 if len(containers) == 1 { 46 event.SetContainerMetadata(containers[0]) 47 return 48 } 49 if containers[0].K8s.PodName != "" && containers[0].K8s.Namespace != "" { 50 // Kubernetes containers within the same pod. 51 event.SetPodMetadata(containers[0]) 52 } 53 // else { 54 // TODO: Non-Kubernetes containers sharing the same network namespace. 55 // What should we do here? 56 // } 57 58 return 59 }