From 7f10c6a2c2a19023bf902fbad52b39c77c1403a4 Mon Sep 17 00:00:00 2001 From: Rob Watson Date: Wed, 1 Jun 2022 22:04:02 +0200 Subject: [PATCH] Improve error handling --- logs/pod_watcher.go | 10 +++++----- logs/watcher.go | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/logs/pod_watcher.go b/logs/pod_watcher.go index b5ff63d..d1ff769 100644 --- a/logs/pod_watcher.go +++ b/logs/pod_watcher.go @@ -12,6 +12,7 @@ import ( "time" corev1 "k8s.io/api/core/v1" + apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/watch" @@ -165,11 +166,10 @@ func copyPodLogs(ctx context.Context, wg *sync.WaitGroup, clientset KubernetesCl req := clientset.CoreV1().Pods(p.Namespace).GetLogs(p.Name, &podLogOpts) logs, err := req.Stream(ctx) - // If one pod or container is in a non-running state, we don't want to quit. - // Checking the response string avoids the risk of a race condition but - // obviously feels a bit brittle too. - // TODO: introspect error - if err != nil && strings.Contains(err.Error(), "is waiting to start") { + // If one container is still being created, do not treat this as a fatal error. + // We try to verify the error type as strictly as possible. + var statusErr *apierrors.StatusError + if errors.As(err, &statusErr) && statusErr.Status().Reason == metav1.StatusReasonBadRequest && strings.Contains(statusErr.Error(), "ContainerCreating") { return &streamError{err: err, podName: p.Name, recoverable: true} } else if err != nil { return &streamError{err: err, podName: p.Name} diff --git a/logs/watcher.go b/logs/watcher.go index ae94e10..10b2af4 100644 --- a/logs/watcher.go +++ b/logs/watcher.go @@ -78,9 +78,9 @@ func (w *Watcher) Watch(ctx context.Context) error { // Check if the deployment exists before we commence watching, to allow us to // return an error if needed. _, err := deploymentsClient.Get(ctx, w.deployName, metav1.GetOptions{}) - var statusError *apierrors.StatusError - if errors.As(err, &statusError) && statusError.Status().Reason == metav1.StatusReasonNotFound { - if !w.allowNonExistentDeployment { + var statusErr *apierrors.StatusError + if errors.As(err, &statusErr) && statusErr.Status().Reason == metav1.StatusReasonNotFound { + if w.strictExist { return err } log.Printf(`deployment "%s" does not exist, waiting`, w.deployName)