From 1d57b1c93a3937ee812e9c17534a55c772935c64 Mon Sep 17 00:00:00 2001 From: Markus Rudy Date: Wed, 17 Apr 2024 09:06:15 +0200 Subject: [PATCH] fixup! e2e: display debug info on failed test --- e2e/internal/kubeclient/deploy.go | 54 ++++++++++++++++++------------- 1 file changed, 31 insertions(+), 23 deletions(-) diff --git a/e2e/internal/kubeclient/deploy.go b/e2e/internal/kubeclient/deploy.go index ef44047be..d3764fd93 100644 --- a/e2e/internal/kubeclient/deploy.go +++ b/e2e/internal/kubeclient/deploy.go @@ -33,20 +33,13 @@ func (c *Kubeclient) WaitForPod(ctx context.Context, namespace, name string) err if !ok { return fmt.Errorf("watcher received unexpected type %T", evt.Object) } - for _, cond := range pod.Status.Conditions { - if cond.Type == corev1.PodReady && cond.Status == corev1.ConditionTrue { - return nil - } + if isPodReady(pod) { + return nil } default: return fmt.Errorf("unexpected watch event while waiting for pod %s/%s: %#v", namespace, name, evt.Object) } case <-ctx.Done(): - if ctx.Err() == context.DeadlineExceeded { - ctx, cancel := context.WithTimeout(context.Background(), time.Minute) - defer cancel() - c.logDebugInfo(ctx, namespace) //nolint:contextcheck // The parent context expired, we need a fresh one to collect evidence. - } return ctx.Err() } } @@ -79,28 +72,43 @@ func (c *Kubeclient) WaitForDeployment(ctx context.Context, namespace, name stri return fmt.Errorf("unexpected watch event while waiting for deployment %s/%s: %#v", namespace, name, evt.Object) } case <-ctx.Done(): - if ctx.Err() == context.DeadlineExceeded { - ctx, cancel := context.WithTimeout(context.Background(), time.Minute) - defer cancel() - c.logDebugInfo(ctx, namespace) //nolint:contextcheck // The parent context expired, we need a fresh one to collect evidence. + logger := c.log.With("namespace", namespace) + logger.Error("deployment did not become ready", "name", name, "contextErr", ctx.Err()) + if ctx.Err() != context.DeadlineExceeded { + return ctx.Err() + } + // Fetch and print debug information. + ctx, cancel := context.WithTimeout(context.Background(), time.Minute) + defer cancel() + pods, err := c.PodsFromDeployment(ctx, namespace, name) //nolint:contextcheck // The parent context expired. + if err != nil { + logger.Error("could not fetch pods for deployment", "name", name, "error", err) + return ctx.Err() + } + for _, pod := range pods { + if !isPodReady(&pod) { + logger.Debug("pod not ready", "name", pod.Name, "status", c.toJSON(pod.Status)) + } } - return ctx.Err() } } } -func (c *Kubeclient) logDebugInfo(ctx context.Context, namespace string) { - pods, err := c.client.CoreV1().Pods(namespace).List(ctx, metav1.ListOptions{}) +func (c *Kubeclient) toJSON(a any) string { + s, err := json.Marshal(a) if err != nil { - c.log.Error("could not fetch pod list for debugging", "error", err) - return + c.log.Error("could not marshal object to JSON", "object", a) } - data, err := json.MarshalIndent(pods, "", " ") - if err != nil { - c.log.Error("could not render pod list for debugging", "error", err) - return + return string(s) +} + +func isPodReady(pod *corev1.Pod) bool { + for _, cond := range pod.Status.Conditions { + if cond.Type == corev1.PodReady && cond.Status == corev1.ConditionTrue { + return true + } } - c.log.Debug("list of pods", "namespace", namespace, "pods", data) + return false } func (c *Kubeclient) resourceInterfaceFor(obj *unstructured.Unstructured) (dynamic.ResourceInterface, error) {