github.com/aporeto-inc/trireme-lib@v10.358.0+incompatible/monitor/internal/docker/helpers.go (about)

     1  package dockermonitor
     2  
     3  import (
     4  	"go.aporeto.io/enforcerd/trireme-lib/common"
     5  	"go.aporeto.io/enforcerd/trireme-lib/monitor/constants"
     6  	"go.aporeto.io/enforcerd/trireme-lib/policy"
     7  	"go.uber.org/zap"
     8  )
     9  
    10  // getPausePUID returns puid of pause container.
    11  func getPausePUID(extensions policy.ExtendedMap) string {
    12  
    13  	if extensions == nil {
    14  		return ""
    15  	}
    16  
    17  	if puid, ok := extensions.Get(constants.DockerHostPUID); ok {
    18  		zap.L().Debug("puid of pause container is", zap.String("puid", puid))
    19  		return puid
    20  	}
    21  
    22  	return ""
    23  }
    24  
    25  // PolicyExtensions retrieves policy extensions
    26  func policyExtensions(runtime policy.RuntimeReader) (extensions policy.ExtendedMap) {
    27  
    28  	if runtime == nil {
    29  		return nil
    30  	}
    31  
    32  	if runtime.Options().PolicyExtensions == nil {
    33  		return nil
    34  	}
    35  
    36  	if extensions, ok := runtime.Options().PolicyExtensions.(policy.ExtendedMap); ok {
    37  		return extensions
    38  	}
    39  	return nil
    40  }
    41  
    42  // IsHostNetworkContainer returns true if container has hostnetwork set
    43  // to true or is linked to container with hostnetwork set to true.
    44  func isHostNetworkContainer(runtime policy.RuntimeReader) bool {
    45  
    46  	return runtime.PUType() == common.LinuxProcessPU || (getPausePUID(policyExtensions(runtime)) != "")
    47  }
    48  
    49  // IsKubernetesContainer checks if the container is in K8s.
    50  func isKubernetesContainer(labels map[string]string) bool {
    51  
    52  	if _, ok := labels[constants.K8sPodNamespace]; ok {
    53  		return true
    54  	}
    55  	return false
    56  }
    57  
    58  // KubePodIdentifier returns identifier for K8s pod.
    59  func kubePodIdentifier(labels map[string]string) string {
    60  
    61  	if !isKubernetesContainer(labels) {
    62  		return ""
    63  	}
    64  	podName := ""
    65  	podNamespace := ""
    66  
    67  	podNamespace, ok := labels[constants.K8sPodNamespace]
    68  	if !ok {
    69  		podNamespace = ""
    70  	}
    71  
    72  	podName, ok = labels[constants.K8sPodName]
    73  	if !ok {
    74  		podName = ""
    75  	}
    76  
    77  	if podName == "" || podNamespace == "" {
    78  		zap.L().Warn("K8s pod does not have podname/podnamespace labels")
    79  		return ""
    80  	}
    81  
    82  	return podNamespace + "/" + podName
    83  }