Skip to content

Commit

Permalink
The stacks don't need to be stored in the manager struct
Browse files Browse the repository at this point in the history
The map is only ever used in the loop to create and remove stacks, so it
doesn't need to be stored in the struct. This ensures that there can't
be any racy concurrent accesses to it.

Signed-off-by: Tom Wieczorek <twieczorek@mirantis.com>
  • Loading branch information
twz123 committed Oct 1, 2024
1 parent 62ef4c6 commit ba547ed
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions pkg/applier/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,10 @@ type Manager struct {
K0sVars *config.CfgVars
KubeClientFactory kubeutil.ClientFactoryInterface

// client kubernetes.Interface
applier Applier
bundlePath string
cancelWatcher context.CancelFunc
log *logrus.Entry
stacks map[string]stack

LeaderElector leaderelector.Interface
}
Expand All @@ -62,7 +60,6 @@ func (m *Manager) Init(ctx context.Context) error {
return fmt.Errorf("failed to create manifest bundle dir %s: %w", m.K0sVars.ManifestsDir, err)
}
m.log = logrus.WithField("component", constant.ApplierManagerComponentName)
m.stacks = make(map[string]stack)
m.bundlePath = m.K0sVars.ManifestsDir

m.applier = NewApplier(m.K0sVars.ManifestsDir, m.KubeClientFactory)
Expand Down Expand Up @@ -120,8 +117,10 @@ func (m *Manager) runWatchers(ctx context.Context) error {
return err
}

stacks := make(map[string]stack, len(dirs))

for _, dir := range dirs {
m.createStack(ctx, path.Join(m.bundlePath, dir))
m.createStack(ctx, stacks, path.Join(m.bundlePath, dir))
}

for {
Expand All @@ -139,27 +138,28 @@ func (m *Manager) runWatchers(ctx context.Context) error {
switch event.Op {
case fsnotify.Create:
if dir.IsDirectory(event.Name) {
m.createStack(ctx, event.Name)
m.createStack(ctx, stacks, event.Name)
}
case fsnotify.Remove:
m.removeStack(ctx, event.Name)
m.removeStack(ctx, stacks, event.Name)
}

case <-ctx.Done():
log.Info("manifest watcher done")
return nil
}
}
}

func (m *Manager) createStack(ctx context.Context, name string) {
func (m *Manager) createStack(ctx context.Context, stacks map[string]stack, name string) {
// safeguard in case the fswatcher would trigger an event for an already existing stack
if _, ok := m.stacks[name]; ok {
if _, ok := stacks[name]; ok {
return
}

stackCtx, cancelStack := context.WithCancel(ctx)
stack := stack{cancelStack, NewStackApplier(name, m.KubeClientFactory)}
m.stacks[name] = stack
stacks[name] = stack

go func() {
log := m.log.WithField("stack", name)
Expand All @@ -180,16 +180,16 @@ func (m *Manager) createStack(ctx context.Context, name string) {
}()
}

func (m *Manager) removeStack(ctx context.Context, name string) {
stack, ok := m.stacks[name]
func (m *Manager) removeStack(ctx context.Context, stacks map[string]stack, name string) {
stack, ok := stacks[name]
if !ok {
m.log.
WithField("path", name).
Debug("attempted to remove non-existent stack, probably not a directory")
return
}

delete(m.stacks, name)
delete(stacks, name)
stack.CancelFunc()

log := m.log.WithField("stack", name)
Expand Down

0 comments on commit ba547ed

Please sign in to comment.