github.com/inspektor-gadget/inspektor-gadget@v0.28.1/pkg/container-utils/runtime-client/interface.go (about) 1 // Copyright 2022 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 runtimeclient 16 17 import ( 18 "errors" 19 "fmt" 20 "strings" 21 22 "github.com/inspektor-gadget/inspektor-gadget/pkg/types" 23 ) 24 25 const ( 26 // Make sure to keep these settings in sync with pkg/resources/manifests/deploy.yaml 27 CrioDefaultSocketPath = "/run/crio/crio.sock" 28 PodmanDefaultSocketPath = "/run/podman/podman.sock" 29 ContainerdDefaultSocketPath = "/run/containerd/containerd.sock" 30 DockerDefaultSocketPath = "/run/docker.sock" 31 CriDockerDefaultSocketPath = "/run/cri-dockerd.sock" 32 ) 33 34 var ErrPauseContainer = errors.New("it is a pause container") 35 36 type K8sContainerData struct { 37 types.BasicK8sMetadata 38 39 // Unique identifier of pod running the container. 40 PodUID string 41 } 42 43 type RuntimeContainerData struct { 44 types.BasicRuntimeMetadata 45 46 // Current state of the container. 47 State string 48 } 49 50 // ContainerData contains container information returned from the container 51 // runtime clients. 52 type ContainerData struct { 53 // Runtime contains all the metadata returned by the container runtime. 54 Runtime RuntimeContainerData 55 56 // K8s contains the Kubernetes metadata of the container. 57 K8s K8sContainerData 58 } 59 60 // ContainerDetailsData contains container extra information returned from the 61 // container runtime clients. This information might not be available when 62 // listing containers. 63 type ContainerDetailsData struct { 64 // ContainerDetailsData contains all ContainerData fields. 65 ContainerData 66 67 // Process identifier. 68 Pid int 69 70 // Path for the container cgroups. 71 CgroupsPath string 72 73 // List of mounts in the container. 74 Mounts []ContainerMountData 75 } 76 77 // ContainerMountData contains mount information in ContainerData. 78 type ContainerMountData struct { 79 // Source of the mount in the host file-system. 80 Source string 81 82 // Destination of the mount in the container. 83 Destination string 84 } 85 86 const ( 87 // Container was created but has not started running. 88 StateCreated = "created" 89 90 // Container is currently running. 91 StateRunning = "running" 92 93 // Container has stopped or exited. 94 StateExited = "exited" 95 96 // Container has an unknown or unrecognized state. 97 StateUnknown = "unknown" 98 ) 99 100 const ( 101 ContainerLabelK8sContainerName = "io.kubernetes.container.name" 102 ContainerLabelK8sPodName = "io.kubernetes.pod.name" 103 ContainerLabelK8sPodNamespace = "io.kubernetes.pod.namespace" 104 ContainerLabelK8sPodUID = "io.kubernetes.pod.uid" 105 ) 106 107 // ContainerRuntimeClient defines the interface to communicate with the 108 // different container runtimes. 109 type ContainerRuntimeClient interface { 110 // GetContainers returns a slice with the information of all the containers. 111 GetContainers() ([]*ContainerData, error) 112 113 // GetContainers returns the information of the container identified by the 114 // provided ID. 115 GetContainer(containerID string) (*ContainerData, error) 116 117 // GetContainerDetails returns the detailed information of the container 118 // identified by the provided ID. 119 // The container details cannot be provided prior to container being in 120 // running state. 121 GetContainerDetails(containerID string) (*ContainerDetailsData, error) 122 123 // Close tears down the connection with the container runtime. 124 Close() error 125 } 126 127 func ParseContainerID(expectedRuntime types.RuntimeName, containerID string) (string, error) { 128 // If ID contains a prefix, it must match the format "<runtime>://<ID>" 129 split := strings.SplitN(containerID, "://", 2) 130 if len(split) == 2 { 131 if types.String2RuntimeName(split[0]) != expectedRuntime { 132 return "", fmt.Errorf("invalid container runtime %q, it should be %q", 133 containerID, expectedRuntime) 134 } 135 return split[1], nil 136 } 137 138 return split[0], nil 139 } 140 141 func EnrichWithK8sMetadata(container *ContainerData, labels map[string]string) { 142 if containerName, ok := labels[ContainerLabelK8sContainerName]; ok { 143 container.K8s.ContainerName = containerName 144 } 145 if podName, ok := labels[ContainerLabelK8sPodName]; ok { 146 container.K8s.PodName = podName 147 } 148 if podNamespace, ok := labels[ContainerLabelK8sPodNamespace]; ok { 149 container.K8s.Namespace = podNamespace 150 } 151 if podUID, ok := labels[ContainerLabelK8sPodUID]; ok { 152 container.K8s.PodUID = podUID 153 } 154 } 155 156 // IsEnrichedWithK8sMetadata returns true if the container already contains 157 // the Kubernetes metadata a container runtime client is able to provide. 158 func IsEnrichedWithK8sMetadata(k8s types.BasicK8sMetadata) bool { 159 return k8s.IsEnriched() 160 } 161 162 // IsEnrichedWithRuntimeMetadata returns true if the container already contains 163 // the runtime metadata a container runtime client is able to provide. 164 func IsEnrichedWithRuntimeMetadata(runtime types.BasicRuntimeMetadata) bool { 165 return runtime.IsEnriched() 166 }