From 02e832a7710da27e79a4b359d2c42a7ccd721cfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charles-Edouard=20Br=C3=A9t=C3=A9ch=C3=A9?= Date: Mon, 17 Oct 2022 11:28:33 +0200 Subject: [PATCH] fix: update policy status (#5006) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Charles-Edouard Brétéché Signed-off-by: Charles-Edouard Brétéché --- pkg/controllers/webhook/controller.go | 19 ++++++++++++------- pkg/utils/controller/run.go | 2 +- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/pkg/controllers/webhook/controller.go b/pkg/controllers/webhook/controller.go index 1feef8b3a9..9f2eb8a42c 100644 --- a/pkg/controllers/webhook/controller.go +++ b/pkg/controllers/webhook/controller.go @@ -96,7 +96,7 @@ type controller struct { runtime runtimeutils.Runtime // state - lock sync.RWMutex + lock sync.Mutex policyState map[string]sets.String } @@ -201,7 +201,7 @@ func (c *controller) Run(ctx context.Context, workers int) { // add our known webhooks to the queue c.enqueueAll() go c.watchdog(ctx) - controllerutils.Run(ctx, ControllerName, logger.V(3), c.queue, workers, maxRetries, c.reconcile) + controllerutils.Run(ctx, ControllerName, logger, c.queue, workers, maxRetries, c.reconcile) } func (c *controller) watchdog(ctx context.Context) { @@ -297,8 +297,9 @@ func (c *controller) recordPolicyState(webhookConfigurationName string, policies policyKey, err := cache.MetaNamespaceKeyFunc(policy) if err != nil { logger.Error(err, "failed to compute policy key", "policy", policy) + } else { + c.policyState[webhookConfigurationName].Insert(policyKey) } - c.policyState[webhookConfigurationName].Insert(policyKey) } } @@ -406,8 +407,8 @@ func (c *controller) reconcileMutatingWebhookConfiguration(ctx context.Context, } func (c *controller) updatePolicyStatuses(ctx context.Context) error { - c.lock.RLock() - defer c.lock.RUnlock() + c.lock.Lock() + defer c.lock.Unlock() policies, err := c.getAllPolicies() if err != nil { return err @@ -446,7 +447,9 @@ func (c *controller) updatePolicyStatuses(ctx context.Context) error { return updateStatusFunc(policy) }, ) - return err + if err != nil { + return err + } } else { _, err := controllerutils.UpdateStatus( ctx, @@ -456,7 +459,9 @@ func (c *controller) updatePolicyStatuses(ctx context.Context) error { return updateStatusFunc(policy) }, ) - return err + if err != nil { + return err + } } } return nil diff --git a/pkg/utils/controller/run.go b/pkg/utils/controller/run.go index 93074caaf6..1a78f9a88b 100644 --- a/pkg/utils/controller/run.go +++ b/pkg/utils/controller/run.go @@ -88,6 +88,6 @@ func reconcile(ctx context.Context, logger logr.Logger, obj interface{}, r recon } logger = logger.WithValues("key", k, "namespace", ns, "name", n) logger.Info("reconciling ...") - defer logger.Info("done", "duration", time.Since(start)) + defer logger.Info("done", "duration", time.Since(start).String()) return r(ctx, logger, k, ns, n) }