Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resmgr: golangci-lint fixes. #441

Merged
merged 1 commit into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 1 addition & 15 deletions pkg/resmgr/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package resmgr

import (
logger "github.com/containers/nri-plugins/pkg/log"
"github.com/containers/nri-plugins/pkg/resmgr/cache"
)

// Our logger instance for events.
Expand All @@ -36,7 +35,7 @@ func (m *resmgr) startEventProcessing() error {
go func() {
for {
select {
case _ = <-stop:
case <-stop:
return
case event := <-m.events:
m.processEvent(event)
Expand All @@ -48,14 +47,6 @@ func (m *resmgr) startEventProcessing() error {
return nil
}

// stopEventProcessing stops event and metrics processing.
func (m *resmgr) stopEventProcessing() {
if m.stop != nil {
close(m.stop)
m.stop = nil
}
}

// SendEvent injects the given event to the resource manager's event processing loop.
func (m *resmgr) SendEvent(event interface{}) error {
if m.events == nil {
Expand All @@ -82,8 +73,3 @@ func (m *resmgr) processEvent(e interface{}) {
evtlog.Warn("event of unexpected type %T...", e)
}
}

// resolveCgroupPath resolves a cgroup path to a container.
func (m *resmgr) resolveCgroupPath(path string) (cache.Container, bool) {
return m.cache.LookupContainerByCgroup(path)
}
4 changes: 3 additions & 1 deletion pkg/resmgr/main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ func (m *Main) Run() error {
log.Infof("starting '%s' policy version %s/build %s...", m.policy.Name(),
version.Version, version.Build)

m.startTracing()
if err := m.startTracing(); err != nil {
log.Warnf("failed to start tracing: %v", err)
}
defer m.stopTracing()

err := m.mgr.Start()
Expand Down
36 changes: 8 additions & 28 deletions pkg/resmgr/nri.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"fmt"
"os"
"slices"
"time"

"github.com/containers/nri-plugins/pkg/instrumentation/metrics"
Expand Down Expand Up @@ -332,12 +333,8 @@ func (p *nriPlugin) StopPodSandbox(ctx context.Context, podSandbox *api.PodSandb
b := metrics.Block()
defer b.Done()

released := []cache.Container{}
pod, _ := m.cache.LookupPod(podSandbox.GetId())

for _, c := range pod.GetContainers() {
released = append(released, c)
}
released := slices.Clone(pod.GetContainers())

if err := p.runPostReleaseHooks(event, released...); err != nil {
nri.Error("%s: failed to run post-release hooks for pod %s: %v",
Expand Down Expand Up @@ -371,12 +368,8 @@ func (p *nriPlugin) RemovePodSandbox(ctx context.Context, podSandbox *api.PodSan

m := p.resmgr

released := []cache.Container{}
pod, _ := m.cache.LookupPod(podSandbox.GetId())

for _, c := range pod.GetContainers() {
released = append(released, c)
}
released := slices.Clone(pod.GetContainers())

if err := p.runPostReleaseHooks(event, released...); err != nil {
nri.Error("%s: failed to run post-release hooks for pod %s: %v",
Expand Down Expand Up @@ -446,7 +439,11 @@ func (p *nriPlugin) CreateContainer(ctx context.Context, pod *api.PodSandbox, co
if err := p.runPostAllocateHooks(event, c); err != nil {
nri.Error("%s: failed to run post-allocate hooks for %s: %v",
event, container.GetName(), err)
p.runPostReleaseHooks(event, c)
relErr := p.runPostReleaseHooks(event, c)
if relErr != nil {
nri.Warnf("%s: failed to run post-release hooks on error for %s: %v",
event, container.GetName(), relErr)
}
return nil, nil, fmt.Errorf("failed to allocate container resources: %w", err)
}

Expand Down Expand Up @@ -1091,20 +1088,3 @@ func (p *nriPlugin) runPostReleaseHooks(method string, released ...cache.Contain
}
return nil
}

// runPostUpdateHooks runs the necessary hooks after reconciliation.
func (p *nriPlugin) runPostUpdateHooks(method string) error {
m := p.resmgr
for _, c := range m.cache.GetPendingContainers() {
switch c.GetState() {
case cache.ContainerStateRunning, cache.ContainerStateCreated:
if err := m.control.RunPostUpdateHooks(c); err != nil {
return err
}
default:
nri.Warn("%s: skipping container %s (in state %v)", method,
c.PrettyName(), c.GetState())
}
}
return nil
}
21 changes: 16 additions & 5 deletions pkg/resmgr/resource-manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,9 @@ func (m *resmgr) start(cfg cfgapi.ResmgrConfig) error {
m.cfg = cfg

mCfg := cfg.CommonConfig()
logger.Configure(&mCfg.Log)
if err := logger.Configure(&mCfg.Log); err != nil {
log.Warnf("failed to configure logger: %v", err)
}

if err := m.policy.Start(m.cfg.PolicyConfig()); err != nil {
return err
Expand Down Expand Up @@ -232,8 +234,13 @@ func (m *resmgr) setupCache() error {
func (m *resmgr) setupPolicy(backend policy.Backend) error {
var err error

m.cache.ResetActivePolicy()
m.cache.SetActivePolicy(backend.Name())
if err := m.cache.ResetActivePolicy(); err != nil {
log.Warnf("failed to reset active policy: %v", err)
}

if err := m.cache.SetActivePolicy(backend.Name()); err != nil {
log.Warnf("failed to set active policy: %v", err)
}

p, err := policy.NewPolicy(backend, m.cache, &policy.Options{SendEvent: m.SendEvent})
if err != nil {
Expand Down Expand Up @@ -285,11 +292,15 @@ func (m *resmgr) reconfigure(cfg cfgapi.ResmgrConfig) error {
apply := func(cfg cfgapi.ResmgrConfig) error {
mCfg := cfg.CommonConfig()

logger.Configure(&mCfg.Log)
if err := logger.Configure(&mCfg.Log); err != nil {
log.Warnf("failed to configure logger: %v", err)
}
if err := instrumentation.Reconfigure(&mCfg.Instrumentation); err != nil {
return err
}
m.control.StartStopControllers(&mCfg.Control)
if err := m.control.StartStopControllers(&mCfg.Control); err != nil {
log.Warnf("failed to restart controllers: %v", err)
}

err := m.policy.Reconfigure(cfg.PolicyConfig())
if err != nil {
Expand Down
Loading