diff --git a/pkg/resmgr/events.go b/pkg/resmgr/events.go index 2ad5265de..1a9e3b7e7 100644 --- a/pkg/resmgr/events.go +++ b/pkg/resmgr/events.go @@ -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. @@ -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) @@ -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 { @@ -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) -} diff --git a/pkg/resmgr/main/main.go b/pkg/resmgr/main/main.go index d2e3c7579..bf3f97f4d 100644 --- a/pkg/resmgr/main/main.go +++ b/pkg/resmgr/main/main.go @@ -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() diff --git a/pkg/resmgr/nri.go b/pkg/resmgr/nri.go index ba7c58eba..2858d5853 100644 --- a/pkg/resmgr/nri.go +++ b/pkg/resmgr/nri.go @@ -18,6 +18,7 @@ import ( "context" "fmt" "os" + "slices" "time" "github.com/containers/nri-plugins/pkg/instrumentation/metrics" @@ -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", @@ -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", @@ -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) } @@ -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 -} diff --git a/pkg/resmgr/resource-manager.go b/pkg/resmgr/resource-manager.go index b5ba74f23..02676aa87 100644 --- a/pkg/resmgr/resource-manager.go +++ b/pkg/resmgr/resource-manager.go @@ -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 @@ -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 { @@ -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 {