From b38ae3856653776429278995ae2dc4b1d55e8c4c Mon Sep 17 00:00:00 2001 From: Valentyna Bukhalova Date: Wed, 23 Oct 2024 11:56:45 +0200 Subject: [PATCH] fix: prevent starting goroutine for the same node certificate --- internal/actions/csr/svc.go | 11 +++++++++-- internal/actions/csr/svc_test.go | 1 + 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/internal/actions/csr/svc.go b/internal/actions/csr/svc.go index 70f75913..d07f79c9 100644 --- a/internal/actions/csr/svc.go +++ b/internal/actions/csr/svc.go @@ -121,6 +121,8 @@ func (h *ApprovalManager) runAutoApproveForCastAINodes(ctx context.Context) { continue } go func(cert *Certificate) { + defer h.removeInProgress(cert.Name) + log := log.WithField("node_name", cert.Name) log.Info("auto approving csr") err := h.handleWithRetry(ctx, log, cert) @@ -167,15 +169,20 @@ func newApproveCSRExponentialBackoff() wait.Backoff { func (h *ApprovalManager) addInProgress(nodeName string) bool { h.m.Lock() defer h.m.Unlock() - h.log.Infof("adding in progress %v", nodeName) if h.inProgress == nil { h.inProgress = make(map[string]struct{}) } _, ok := h.inProgress[nodeName] if ok { - h.log.Infof("skipping adding in progress %v", nodeName) return false } h.inProgress[nodeName] = struct{}{} return true } + +func (h *ApprovalManager) removeInProgress(nodeName string) { + h.m.Lock() + defer h.m.Unlock() + + delete(h.inProgress, nodeName) +} diff --git a/internal/actions/csr/svc_test.go b/internal/actions/csr/svc_test.go index ad0b7dd3..db5c7c4e 100644 --- a/internal/actions/csr/svc_test.go +++ b/internal/actions/csr/svc_test.go @@ -61,6 +61,7 @@ func TestCSRApprove(t *testing.T) { go func() { defer wg.Done() watcher.Add(getCSR(csrName, userName)) + watcher.Add(getCSR(csrName, userName)) //should skip start handler time.Sleep(100 * time.Millisecond) s.Stop() }()