From e108c6f87793bfc914f20d441399fa189afe5e11 Mon Sep 17 00:00:00 2001 From: Matthias Bertschy Date: Wed, 27 Mar 2024 17:06:50 +0100 Subject: [PATCH] stop watching on not found resources Signed-off-by: Matthias Bertschy --- adapters/incluster/v1/client.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/adapters/incluster/v1/client.go b/adapters/incluster/v1/client.go index 2c7d0a6..846efc1 100644 --- a/adapters/incluster/v1/client.go +++ b/adapters/incluster/v1/client.go @@ -23,6 +23,7 @@ import ( "github.com/kubescape/synchronizer/config" "github.com/kubescape/synchronizer/domain" "github.com/kubescape/synchronizer/utils" + k8sErrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime/schema" @@ -181,9 +182,14 @@ func (c *Client) Stop(_ context.Context) error { } func (c *Client) watchRetry(ctx context.Context, watchOpts metav1.ListOptions, eventQueue *utils.CooldownQueue) { + exitFatal := true if err := backoff.RetryNotify(func() error { watcher, err := c.client.Resource(c.res).Namespace("").Watch(context.Background(), watchOpts) if err != nil { + if k8sErrors.ReasonForError(err) == metav1.StatusReasonNotFound { + exitFatal = false + return backoff.Permanent(err) + } return fmt.Errorf("client resource: %w", err) } logger.L().Info("starting watch", helpers.String("resource", c.res.Resource)) @@ -218,8 +224,11 @@ func (c *Client) watchRetry(ctx context.Context, watchOpts metav1.ListOptions, e helpers.String("retry in", d.String())) } }); err != nil { - logger.L().Ctx(ctx).Fatal("giving up watch", helpers.Error(err), - helpers.String("resource", c.res.Resource)) + logger.L().Ctx(ctx).Error("giving up watch", helpers.Error(err), + helpers.String("resource", c.res.String())) + if exitFatal { + os.Exit(1) + } } }