Skip to content

Commit

Permalink
fix: prevent starting goroutine for the same node certificate
Browse files Browse the repository at this point in the history
  • Loading branch information
ValyaB committed Oct 23, 2024
1 parent 77c155b commit b38ae38
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
11 changes: 9 additions & 2 deletions internal/actions/csr/svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
1 change: 1 addition & 0 deletions internal/actions/csr/svc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}()
Expand Down

0 comments on commit b38ae38

Please sign in to comment.