From f8c5e73db291f16852e46b163fc72fbe6386412e Mon Sep 17 00:00:00 2001 From: Kornilios Kourtis Date: Fri, 22 Mar 2024 17:25:23 +0100 Subject: [PATCH] tetragon-oci-hook: support container name Signed-off-by: Kornilios Kourtis --- .../tetragon-oci-hook/cmd/hook/main.go | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/contrib/rthooks/tetragon-oci-hook/cmd/hook/main.go b/contrib/rthooks/tetragon-oci-hook/cmd/hook/main.go index 871cbf1d847..5234586937a 100644 --- a/contrib/rthooks/tetragon-oci-hook/cmd/hook/main.go +++ b/contrib/rthooks/tetragon-oci-hook/cmd/hook/main.go @@ -130,6 +130,20 @@ func getCgroupPath(spec *specs.Spec) (string, error) { return "", fmt.Errorf("Unknown cgroup path: %s", cgroupPath) } +func containerNameFromAnnotations(annotations map[string]string) string { + // containerd + if val, ok := annotations["io.kubernetes.cri.container-name"]; ok { + return val + } + + // crio + if val, ok := annotations["io.kubernetes.container.name"]; ok { + return val + } + + return "" +} + // NB: the second argument is only used in case of an error, so disable revive's complains // revive:disable:error-return func createContainerHook(log *slog.Logger) (error, map[string]string) { @@ -176,12 +190,15 @@ func createContainerHook(log *slog.Logger) (error, map[string]string) { return fmt.Errorf("unable to determine either RootDir or cgroupPath, bailing out"), nil } + containerName := containerNameFromAnnotations(spec.Annotations) + req := &tetragon.RuntimeHookRequest{ Event: &tetragon.RuntimeHookRequest_CreateContainer{ CreateContainer: &tetragon.CreateContainer{ - CgroupsPath: cgroupPath, - RootDir: rootDir, - Annotations: spec.Annotations, + CgroupsPath: cgroupPath, + RootDir: rootDir, + Annotations: spec.Annotations, + ContainerName: containerName, }, }, } @@ -189,6 +206,7 @@ func createContainerHook(log *slog.Logger) (error, map[string]string) { log = log.With( "req-cgroups", cgroupPath, "req-rootdir", rootDir, + "req-containerName", containerName, ) if log.Enabled(context.TODO(), slog.LevelDebug) { // NB: only add annotations in debug level since they are too noisy