From 6923ff1a1bfc595b6fa4b8afb04481bea34d7816 Mon Sep 17 00:00:00 2001 From: Scott Leggett Date: Thu, 20 Jul 2023 11:16:10 +0800 Subject: [PATCH] fix: avoid duplicate log lines edge case --- internal/k8s/logs.go | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/internal/k8s/logs.go b/internal/k8s/logs.go index d95811ce..7b1ec723 100644 --- a/internal/k8s/logs.go +++ b/internal/k8s/logs.go @@ -101,6 +101,19 @@ func (c *Client) readLogs(ctx context.Context, egSend *errgroup.Group, linewiseCopy(ctx, fmt.Sprintf("[pod/%s/%s]", p.Name, cName), logs, logStream) fmt.Println("done linewiseCopy") + // The k8s API sometimes sends an event showing a healthy pod even though + // the pod is terminating. This seems to occur sometimes on scale down of + // a deployment. When this happens there is a race that occurs where the + // existing log stream closes (linewiseCopy() returns), then the + // "healthy" event comes in and the container log streaming is started + // again, only to return immediately. This can result in duplicated log + // lines being returned. + // To hack around this behaviour, pause for a couple of seconds here + // before exiting. This means that the container ID is retained in + // c.logCIDs for a brief period after logs stop streaming which causes + // "healthy pod" events from the k8s API to be ignored for that period + // and thereby avoiding duplicate log lines being returned. + time.Sleep(2) return nil }) } @@ -116,10 +129,11 @@ func (c *Client) podEventHandler(ctx context.Context, // panic if obj is not a pod, since this is specifically a pod informer pod := obj.(*corev1.Pod) spew.Dump(pod.Status) - if !slices.ContainsFunc(pod.Status.Conditions, func(cond corev1.PodCondition) bool { - return (pod.Status.Phase == corev1.PodRunning) && - (cond.Type == corev1.ContainersReady && cond.Status == corev1.ConditionTrue) - }) { + if !slices.ContainsFunc(pod.Status.Conditions, + func(cond corev1.PodCondition) bool { + return cond.Type == corev1.ContainersReady && + cond.Status == corev1.ConditionTrue + }) { fmt.Println("pod not ready") return }