From 2b7099adcb8e7accb136cc1d9cf6357401d1aa32 Mon Sep 17 00:00:00 2001 From: joey Date: Sun, 1 Sep 2024 15:21:13 +0800 Subject: [PATCH] fix containerd handler missing `restartCount` label Signed-off-by: joey --- container/containerd/handler.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/container/containerd/handler.go b/container/containerd/handler.go index 7746341cd9..70cb6059da 100644 --- a/container/containerd/handler.go +++ b/container/containerd/handler.go @@ -19,6 +19,7 @@ import ( "encoding/json" "errors" "fmt" + "strconv" "strings" "time" @@ -147,6 +148,16 @@ func newContainerdContainerHandler( // Add the name and bare ID as aliases of the container. handler.image = cntr.Image + // ignore err and get zero as default, this happens with sandboxes, not sure why... + // kube isn't sending restart count in labels for sandboxes. + if spec.Annotations != nil { + restartCount, _ := strconv.Atoi(spec.Annotations["io.kubernetes.container.restartCount"]) + // Only adds restartcount label if it's greater than 0 + if restartCount > 0 { + handler.labels["restartcount"] = strconv.Itoa(restartCount) + } + } + for _, exposedEnv := range metadataEnvAllowList { if exposedEnv == "" { // if no containerdEnvWhitelist provided, len(metadataEnvAllowList) == 1, metadataEnvAllowList[0] == ""