From 244fc916ca668a5beec69827ad8c4a34a6844144 Mon Sep 17 00:00:00 2001 From: linxin Date: Fri, 29 Dec 2023 17:43:44 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:TSG-18258?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- coredump-handler/coredump-handler.go | 25 ++++++++++++++++++------- coredump-tool/coredump-tool.go | 6 +++--- types/types.go | 1 + 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/coredump-handler/coredump-handler.go b/coredump-handler/coredump-handler.go index 694e25d..abca769 100644 --- a/coredump-handler/coredump-handler.go +++ b/coredump-handler/coredump-handler.go @@ -123,25 +123,33 @@ func getContainerId(pid string) (string, error) { return containerID, nil } -// get image name by container id -func getImageName(container_id string, sock_path string) (string, error) { +// get container info by container id +func getContainerInfo(container_id string, sock_path string) (string, string, error) { // connect to containerd daemon client, err := containerd.New(sock_path) if err != nil { - return "", err + return "", "", err } defer client.Close() // according to container id to get container info ctx := namespaces.WithNamespace(context.Background(), "k8s.io") container, err := client.LoadContainer(ctx, container_id) if err != nil { - return "", err + return "", "", err } imageRef, err := container.Image(ctx) if err != nil { - return "", err + return "", "", err } - return imageRef.Name(), nil + label, err := container.Labels(ctx) + if err != nil { + return "", "", err + } + pod_name, ok := label["io.kubernetes.pod.name"] + if !ok { + return imageRef.Name(), "", nil + } + return imageRef.Name(), pod_name, nil } // write coredump info to file @@ -336,7 +344,7 @@ func main() { //find image name if err == nil && len(container_id) != 0 { coredump_config.Container_id = container_id - coredump_config.Image_name, err = getImageName(container_id, pipe_config.Containered_sock_path) + coredump_config.Image_name, coredump_config.Pod_name, err = getContainerInfo(container_id, pipe_config.Containered_sock_path) if err != nil { journal.Print(journal.PriInfo, err.Error()) } @@ -347,6 +355,9 @@ func main() { if coredump_config.Image_name == "" { coredump_config.Image_name = "NULL" } + if coredump_config.Pod_name == "" { + coredump_config.Pod_name = "NULL" + } //write coredump file if !pipe_config.Compress { if pipe_config.Storage_type == 1 { diff --git a/coredump-tool/coredump-tool.go b/coredump-tool/coredump-tool.go index 996f7de..b659d1e 100644 --- a/coredump-tool/coredump-tool.go +++ b/coredump-tool/coredump-tool.go @@ -132,7 +132,7 @@ func info(pid string) { {Text: "GID"}, {Text: "SIG"}, {Text: "EXE"}, - {Text: "CONTAINER"}, + {Text: "POD"}, {Text: "IMAGE"}, {Text: "HOSTNAME"}, {Text: "STORAGE"}, @@ -152,7 +152,7 @@ func info(pid string) { {Text: c.GID}, {Text: strconv.Itoa(c.Signal)}, {Text: c.Process_exe_path}, - {Text: c.Container_id}, + {Text: c.Pod_name}, {Text: c.Image_name}, {Text: c.Hostname}, {Text: c.Storage}, @@ -342,7 +342,7 @@ func debugInpod(conf *rest.Config, clientset *kubernetes.Clientset, config types ready = true break } - time.Sleep(2 * time.Second) + time.Sleep(3 * time.Second) } if !ready { return podName, errors.New("create pod timeout") diff --git a/types/types.go b/types/types.go index 77c29b0..c2e11f9 100644 --- a/types/types.go +++ b/types/types.go @@ -20,6 +20,7 @@ type Coredump_config struct { Timestamp int64 Corepipe_config_path string `json:"-"` Container_id string + Pod_name string Image_name string UID string GID string