...
1
16
17 package kubernetes
18
19 import (
20 "context"
21 "io"
22
23 corev1 "k8s.io/api/core/v1"
24 "k8s.io/apimachinery/pkg/labels"
25 clientset "k8s.io/client-go/kubernetes"
26 "sigs.k8s.io/controller-runtime/pkg/client"
27 )
28
29
30
31 func DumpEchoLogs(ns, name string, c client.Client, cs clientset.Interface) ([][]byte, error) {
32 var logs [][]byte
33
34 pods := new(corev1.PodList)
35 podListOptions := &client.ListOptions{
36 LabelSelector: labels.SelectorFromSet(map[string]string{"app": name}),
37 Namespace: ns,
38 }
39 if err := c.List(context.TODO(), pods, podListOptions); err != nil {
40 return nil, err
41 }
42
43 podLogOptions := &corev1.PodLogOptions{
44 Container: name,
45 }
46 for _, pod := range pods.Items {
47 if pod.Status.Phase == corev1.PodFailed {
48 continue
49 }
50 req := cs.CoreV1().Pods(ns).GetLogs(pod.Name, podLogOptions)
51 logStream, err := req.Stream(context.TODO())
52 if err != nil {
53 continue
54 }
55 defer logStream.Close()
56 logBytes, err := io.ReadAll(logStream)
57 if err != nil {
58 continue
59 }
60 logs = append(logs, logBytes)
61 }
62
63 return logs, nil
64 }
65
View as plain text