Skip to content

Commit 617404b

Browse files
authored
feat: upgrade dump logs to take more information and a proper label selector (#170)
<!-- markdownlint-disable MD041 --> #### What this PR does / why we need it #### Which issue(s) this PR fixes <!-- Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`. --> Signed-off-by: Gergely Brautigam <[email protected]>
1 parent 91e71d2 commit 617404b

File tree

1 file changed

+28
-24
lines changed

1 file changed

+28
-24
lines changed

shared/steps/teardown/dump_cluster_state.go

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,14 @@ import (
1414
"sigs.k8s.io/e2e-framework/pkg/features"
1515
)
1616

17+
// Controller contains information about a controller to dump.
18+
type Controller struct {
19+
LabelSelector map[string]string
20+
Namespace string
21+
}
22+
1723
// DumpClusterState dumps the status of pods and logs of given controllers.
18-
func DumpClusterState(controllers ...string) features.Func {
24+
func DumpClusterState(controllers ...Controller) features.Func {
1925
return func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context {
2026
t.Helper()
2127
// Dump controller logs
@@ -52,7 +58,7 @@ func DumpClusterState(controllers ...string) features.Func {
5258
}
5359
}
5460

55-
func dumpLogs(ctx context.Context, t *testing.T, config *envconf.Config, controller string) error {
61+
func dumpLogs(ctx context.Context, t *testing.T, config *envconf.Config, controller Controller) error {
5662
t.Helper()
5763

5864
client, err := config.NewClient()
@@ -61,39 +67,37 @@ func dumpLogs(ctx context.Context, t *testing.T, config *envconf.Config, control
6167
}
6268

6369
pods := &v1.PodList{}
64-
if err := client.Resources(config.Namespace()).List(ctx, pods, resources.WithLabelSelector(
65-
labels.FormatLabels(map[string]string{"app": controller})),
70+
if err := client.Resources(controller.Namespace).List(ctx, pods, resources.WithLabelSelector(
71+
labels.FormatLabels(controller.LabelSelector)),
6672
); err != nil {
6773
t.Fatal(fmt.Errorf("failed to list pods: %w", err))
6874
}
6975

70-
if len(pods.Items) != 1 {
71-
t.Fatal(fmt.Errorf("invalid number of pods found for registry %d", len(pods.Items)))
72-
}
76+
for _, pod := range pods.Items {
77+
t.Logf("Dumping logs for Pod: %s | Status: %s", pod.Name, pod.Status.String())
7378

74-
pod := pods.Items[0]
79+
// creates the clientset
80+
clientset, err := kubernetes.NewForConfig(config.Client().RESTConfig())
81+
if err != nil {
82+
return fmt.Errorf("failed to create clientset: %w", err)
83+
}
7584

76-
// creates the clientset
77-
clientset, err := kubernetes.NewForConfig(config.Client().RESTConfig())
78-
if err != nil {
79-
return fmt.Errorf("failed to create clientset: %w", err)
80-
}
85+
podReq := clientset.CoreV1().Pods(pod.Namespace).GetLogs(pod.Name, &v1.PodLogOptions{})
8186

82-
podReq := clientset.CoreV1().Pods(pod.Namespace).GetLogs(pod.Name, &v1.PodLogOptions{})
87+
reader, err := podReq.Stream(ctx)
88+
if err != nil {
89+
t.Fatal(fmt.Errorf("failed to fetch pod logs: %w", err))
90+
}
8391

84-
reader, err := podReq.Stream(ctx)
85-
if err != nil {
86-
t.Fatal(fmt.Errorf("failed to fetch pod logs: %w", err))
87-
}
92+
defer reader.Close()
8893

89-
defer reader.Close()
94+
content, err := io.ReadAll(reader)
95+
if err != nil {
96+
t.Fatal(fmt.Errorf("failed to read log: %w", err))
97+
}
9098

91-
content, err := io.ReadAll(reader)
92-
if err != nil {
93-
t.Fatal(fmt.Errorf("failed to read log: %w", err))
99+
t.Logf("Pod: %s | Log: %s", controller, string(content))
94100
}
95101

96-
t.Logf("Pod: %s | Log: %s", controller, string(content))
97-
98102
return nil
99103
}

0 commit comments

Comments
 (0)