Skip to content

Commit

Permalink
fix race #2
Browse files Browse the repository at this point in the history
Signed-off-by: Mikhail Scherba <mikhail.scherba@flant.com>
  • Loading branch information
miklezzzz committed Feb 19, 2025
1 parent b67f5a8 commit 9ca1489
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions pkg/helm_resources_manager/helm_resources_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log/slog"
"sync"

"github.com/deckhouse/deckhouse/pkg/log"
"k8s.io/apimachinery/pkg/labels"
Expand Down Expand Up @@ -42,11 +43,12 @@ type helmResourcesManager struct {

kubeClient *klient.Client

monitors map[string]*ResourcesMonitor

eventCh chan ReleaseStatusEvent

logger *log.Logger

l sync.RWMutex
monitors map[string]*ResourcesMonitor
}

var _ HelmResourcesManager = &helmResourcesManager{}
Expand Down Expand Up @@ -122,7 +124,9 @@ func (hm *helmResourcesManager) StartMonitor(moduleName string, manifests []mani

rm := NewResourcesMonitor(hm.ctx, cfg)

hm.l.Lock()
hm.monitors[moduleName] = rm
hm.l.Unlock()
rm.Start()
}

Expand All @@ -143,55 +147,73 @@ func (hm *helmResourcesManager) absentResourcesCallback(moduleName string, unexp
}

func (hm *helmResourcesManager) StopMonitors() {
hm.l.RLock()
for moduleName := range hm.monitors {
hm.StopMonitor(moduleName)
}
hm.l.RUnlock()
}

func (hm *helmResourcesManager) PauseMonitors() {
hm.l.RLock()
for _, monitor := range hm.monitors {
monitor.Pause()
}
hm.l.RUnlock()
}

func (hm *helmResourcesManager) ResumeMonitors() {
hm.l.RLock()
for _, monitor := range hm.monitors {
monitor.Resume()
}
hm.l.RUnlock()
}

func (hm *helmResourcesManager) StopMonitor(moduleName string) {
hm.l.Lock()
if monitor, ok := hm.monitors[moduleName]; ok {
monitor.Stop()
delete(hm.monitors, moduleName)
}
hm.l.Unlock()
}

func (hm *helmResourcesManager) PauseMonitor(moduleName string) {
hm.l.RLock()
if monitor, ok := hm.monitors[moduleName]; ok {
monitor.Pause()
}
hm.l.RUnlock()
}

func (hm *helmResourcesManager) ResumeMonitor(moduleName string) {
hm.l.RLock()
if monitor, ok := hm.monitors[moduleName]; ok {
monitor.Resume()
}
hm.l.RUnlock()
}

func (hm *helmResourcesManager) HasMonitor(moduleName string) bool {
hm.l.RLock()
_, ok := hm.monitors[moduleName]
hm.l.RUnlock()
return ok
}

func (hm *helmResourcesManager) AbsentResources(moduleName string) ([]manifest.Manifest, error) {
hm.l.RLock()
defer hm.l.RUnlock()
if monitor, ok := hm.monitors[moduleName]; ok {
return monitor.AbsentResources()
}
return nil, nil
}

func (hm *helmResourcesManager) GetMonitor(moduleName string) *ResourcesMonitor {
hm.l.RLock()
defer hm.l.RUnlock()
return hm.monitors[moduleName]
}

Expand Down

0 comments on commit 9ca1489

Please sign in to comment.