/
get_pod_logs.go
203 lines (180 loc) · 5.58 KB
/
get_pod_logs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package internal
import (
"bytes"
"context"
"encoding/json"
"io"
"sort"
"strings"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)
func getAllNamespaces(ctx context.Context, clientset *kubernetes.Clientset) ([]string, error) {
namespaces, err := clientset.CoreV1().Namespaces().List(ctx, metav1.ListOptions{})
if err != nil {
return nil, err
}
var nsNames []string
for _, ns := range namespaces.Items {
nsNames = append(nsNames, ns.Name)
}
return nsNames, nil
}
// LogEntry is a struct to hold a log entry and its timestamp.
type LogEntry struct {
Timestamp float64 `json:"ts"`
Entry string `json:"entry"`
}
// GetPodLogsForResource returns logs for a given resource from the CAPI controllers containing the provider name label.
func GetPodLogsForResource(ctx context.Context, c client.Client, restConfig *rest.Config, kind string, namespace string, name string) ([]string, error) {
log := ctrl.LoggerFrom(ctx)
clientset, err := kubernetes.NewForConfig(restConfig)
if err != nil {
return nil, err
}
namespaces, err := getAllNamespaces(ctx, clientset)
if err != nil {
return nil, err
}
logEntries := make([]LogEntry, 0)
selector := metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{
{
Key: clusterv1.ProviderNameLabel,
Operator: metav1.LabelSelectorOpExists,
},
},
}
labelselector, err := metav1.LabelSelectorAsSelector(&selector)
if err != nil {
return nil, err
}
for _, podNamespace := range namespaces {
listOpts := []client.ListOption{
client.MatchingLabelsSelector{Selector: labelselector},
client.InNamespace(podNamespace),
}
podList := &corev1.PodList{}
if err := c.List(ctx, podList, listOpts...); err != nil {
return nil, err
}
for _, pod := range podList.Items {
logs, err := getPodLogs(ctx, clientset, pod)
if err != nil {
return nil, err
}
logs = strings.TrimSuffix(logs, "\n")
res := strings.Split(logs, "\n")
for _, line := range res {
searchString := "{\"ts\":"
if strings.Contains(line, searchString) {
jsonMap := make(map[string]interface{})
if err := json.Unmarshal([]byte(line), &jsonMap); err != nil {
log.Error(err, "Failed to parse log line", "line", line)
continue
} else {
ts, ok := jsonMap["ts"].(float64)
if !ok {
log.Info("Failed to parse timestamp", "line", line)
continue
}
if logMatchesResource(jsonMap, kind, namespace, name) {
logEntries = append(logEntries, LogEntry{Timestamp: ts, Entry: line})
}
}
}
}
}
}
// Note: we could probably optimize this using a merge-sort strategy since each of the log entries for each pod are already sorted.
sort.Slice(logEntries, func(i, j int) bool {
// Return with most recent at top
return logEntries[i].Timestamp > logEntries[j].Timestamp
})
allLogs := make([]string, len(logEntries))
for i, entry := range logEntries {
allLogs[i] = entry.Entry
}
return allLogs, nil
}
// logMatchesResource returns true if the log entry matches the given resource by having a keys in the format of kind.namespace and kind.name.
// For example, if there is a Cluster with "name: test-cluster" and "namespace: test-namespace", then the log entry should have the following keys:
// "Cluster": {"name": "test-cluster", "namespace": "test-namespace"}
func logMatchesResource(jsonMap map[string]interface{}, kind string, namespace string, name string) bool {
if val, ok := GetNestedValue(jsonMap, []string{kind, "name"}); ok && val == name {
if val, ok := GetNestedValue(jsonMap, []string{kind, "namespace"}); ok && val == namespace {
return true
}
}
return false
}
// GetNestedValue returns the value of a nested key in a map.
func GetNestedValue(data map[string]any, path []string) (result string, found bool) {
if len(path) == 0 {
return "", false
}
if len(path) == 1 {
if val, ok := data[path[0]]; ok {
if s, ok := val.(string); ok {
return s, true
}
}
return "", false
}
if val, ok := data[path[0]]; ok {
if m, ok := val.(map[string]any); ok {
return GetNestedValue(m, path[1:])
}
}
return "", false
}
// getPodLogs returns logs for a given pod.
func getPodLogs(ctx context.Context, clientset *kubernetes.Clientset, pod corev1.Pod) (string, error) {
allLogs := ""
for _, c := range pod.Spec.InitContainers {
if logs, err := getPodContainerLogs(ctx, clientset, pod, c.Name); err != nil {
return "", err
} else {
allLogs = allLogs + logs
}
}
for _, c := range pod.Spec.Containers {
if logs, err := getPodContainerLogs(ctx, clientset, pod, c.Name); err != nil {
return "", err
} else {
allLogs = allLogs + logs
}
}
for _, c := range pod.Spec.EphemeralContainers {
if logs, err := getPodContainerLogs(ctx, clientset, pod, c.Name); err != nil {
return "", err
} else {
allLogs = allLogs + logs
}
}
return allLogs, nil
}
// getPodContainerLogs returns logs for a given pod and container.
func getPodContainerLogs(ctx context.Context, clientset *kubernetes.Clientset, pod corev1.Pod, containerName string) (string, error) {
podLogOpts := corev1.PodLogOptions{
Container: containerName,
}
req := clientset.CoreV1().Pods(pod.Namespace).GetLogs(pod.Name, &podLogOpts)
podLogs, err := req.Stream(ctx)
if err != nil {
return "", err
}
defer podLogs.Close()
buf := new(bytes.Buffer)
_, err = io.Copy(buf, podLogs)
if err != nil {
return "", err
}
str := buf.String()
return str, nil
}