Skip to content

Commit

Permalink
fixup! e2e: display debug info on failed test
Browse files Browse the repository at this point in the history
  • Loading branch information
burgerdev committed Apr 17, 2024
1 parent b77ac4c commit 1d57b1c
Showing 1 changed file with 31 additions and 23 deletions.
54 changes: 31 additions & 23 deletions e2e/internal/kubeclient/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 1d57b1c

Please sign in to comment.