/
core.go
60 lines (46 loc) · 1.75 KB
/
core.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
package support
import (
"io"
"github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func Pods(t Test, namespace string, options ...Option[*metav1.ListOptions]) func(g gomega.Gomega) []corev1.Pod {
return func(g gomega.Gomega) []corev1.Pod {
listOptions := &metav1.ListOptions{}
for _, option := range options {
t.Expect(option.applyTo(listOptions)).To(gomega.Succeed())
}
pods, err := t.Client().Core().CoreV1().Pods(namespace).List(t.Ctx(), *listOptions)
g.Expect(err).NotTo(gomega.HaveOccurred())
return pods.Items
}
}
func storeAllPodLogs(t Test, namespace *corev1.Namespace) {
t.T().Helper()
pods, err := t.Client().Core().CoreV1().Pods(namespace.Name).List(t.Ctx(), metav1.ListOptions{})
t.Expect(err).NotTo(gomega.HaveOccurred())
for _, pod := range pods.Items {
for _, container := range pod.Spec.Containers {
t.T().Logf("Retrieving Pod Container %s/%s/%s logs", pod.Namespace, pod.Name, container.Name)
storeContainerLog(t, namespace, pod.Name, container.Name)
}
}
}
func storeContainerLog(t Test, namespace *corev1.Namespace, podName, containerName string) {
t.T().Helper()
options := corev1.PodLogOptions{Container: containerName}
stream, err := t.Client().Core().CoreV1().Pods(namespace.Name).GetLogs(podName, &options).Stream(t.Ctx())
if err != nil {
t.T().Logf("Error getting logs from container %s/%s/%s", namespace.Name, podName, containerName)
return
}
t.Expect(err).NotTo(gomega.HaveOccurred())
defer func() {
t.Expect(stream.Close()).To(gomega.Succeed())
}()
bytes, err := io.ReadAll(stream)
t.Expect(err).NotTo(gomega.HaveOccurred())
containerLogFileName := "pod-" + podName + "-" + containerName
WriteToOutputDir(t, containerLogFileName, Log, bytes)
}