Skip to content

Commit a0df4af

Browse files
e2e: Capture controller and driver pod logs
I found this helpful in recent PR work to catch exit logs before the pods go away. Committing it back to see if other folks are interested. Signed-off-by: Mat Schaffer <[email protected]>
1 parent 920772e commit a0df4af

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ codecov.yaml
55
cover.out
66
.DS_Store
77
*.iml
8+
test/e2e/logs/

test/e2e/sparkapplication_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ var _ = Describe("Example SparkApplication", func() {
6262
key := types.NamespacedName{Namespace: app.Namespace, Name: app.Name}
6363
Expect(k8sClient.Get(ctx, key, app)).To(Succeed())
6464

65+
By("Writing driver logs")
66+
driverPodName := util.GetDriverPodName(app)
67+
writePodLogs(ctx, app.Namespace, driverPodName)
68+
6569
By("Deleting SparkApplication")
6670
Expect(k8sClient.Delete(ctx, app)).To(Succeed())
6771
})
@@ -116,6 +120,10 @@ var _ = Describe("Example SparkApplication", func() {
116120
key := types.NamespacedName{Namespace: app.Namespace, Name: app.Name}
117121
Expect(k8sClient.Get(ctx, key, app)).To(Succeed())
118122

123+
By("Writing driver logs")
124+
driverPodName := util.GetDriverPodName(app)
125+
writePodLogs(ctx, app.Namespace, driverPodName)
126+
119127
volumes := app.Spec.Volumes
120128
By("Deleting SparkApplication")
121129
Expect(k8sClient.Delete(ctx, app)).To(Succeed())
@@ -201,6 +209,10 @@ var _ = Describe("Example SparkApplication", func() {
201209
key := types.NamespacedName{Namespace: app.Namespace, Name: app.Name}
202210
Expect(k8sClient.Get(ctx, key, app)).To(Succeed())
203211

212+
By("Writing driver logs")
213+
driverPodName := util.GetDriverPodName(app)
214+
writePodLogs(ctx, app.Namespace, driverPodName)
215+
204216
By("Deleting SparkApplication")
205217
Expect(k8sClient.Delete(ctx, app)).To(Succeed())
206218
})
@@ -376,6 +388,10 @@ var _ = Describe("Example SparkApplication", func() {
376388
key := types.NamespacedName{Namespace: app.Namespace, Name: app.Name}
377389
Expect(k8sClient.Get(ctx, key, app)).To(Succeed())
378390

391+
By("Writing driver logs")
392+
driverPodName := util.GetDriverPodName(app)
393+
writePodLogs(ctx, app.Namespace, driverPodName)
394+
379395
By("Deleting SparkApplication")
380396
Expect(k8sClient.Delete(ctx, app)).To(Succeed())
381397
})

test/e2e/suit_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ const (
6262
MutatingWebhookName = "spark-operator-webhook"
6363
ValidatingWebhookName = "spark-operator-webhook"
6464

65+
ControllerName = "spark-operator-controller"
66+
6567
PollInterval = 1 * time.Second
6668
WaitTimeout = 5 * time.Minute
6769
)
@@ -152,6 +154,9 @@ var _ = BeforeSuite(func() {
152154
})
153155

154156
var _ = AfterSuite(func() {
157+
By("Capturing the Spark operator logs")
158+
writeControllerLogs(context.Background())
159+
155160
By("Uninstalling the Spark operator helm chart")
156161
envSettings := cli.New()
157162
envSettings.SetNamespace(ReleaseNamespace)
@@ -290,3 +295,28 @@ func collectSparkApplicationsUntilTermination(ctx context.Context, key types.Nam
290295
})
291296
return apps, err
292297
}
298+
299+
func writePodLogs(ctx context.Context, podNamespace string, podName string) {
300+
bytes, err := clientset.CoreV1().Pods(podNamespace).GetLogs(podName, &corev1.PodLogOptions{}).Do(ctx).Raw()
301+
Expect(err).NotTo(HaveOccurred())
302+
dir := filepath.Join("logs", podNamespace)
303+
if _, err := os.Stat(dir); os.IsNotExist(err) {
304+
os.MkdirAll(dir, 0755)
305+
}
306+
err = os.WriteFile(filepath.Join(dir, fmt.Sprintf("%s.log", podName)), bytes, 0644)
307+
Expect(err).NotTo(HaveOccurred())
308+
}
309+
310+
func writeControllerLogs(ctx context.Context) {
311+
deployment, err := clientset.AppsV1().Deployments(ReleaseNamespace).Get(ctx, ControllerName, metav1.GetOptions{})
312+
Expect(err).NotTo(HaveOccurred())
313+
listOptions := metav1.ListOptions{
314+
LabelSelector: metav1.FormatLabelSelector(&metav1.LabelSelector{MatchLabels: deployment.Spec.Selector.MatchLabels}),
315+
}
316+
pods, err := clientset.CoreV1().Pods(ReleaseNamespace).List(ctx, listOptions)
317+
Expect(err).NotTo(HaveOccurred())
318+
Expect(pods).NotTo(BeNil())
319+
for _, pod := range pods.Items {
320+
writePodLogs(ctx, ReleaseNamespace, pod.Name)
321+
}
322+
}

0 commit comments

Comments
 (0)