Skip to content

Commit

Permalink
nfd-master: proper shutdown of nfd api informers
Browse files Browse the repository at this point in the history
Ensure that the nfd API informer factory is properly shut down and all
resources released when stop() is called.

This eliminates memory leaks on re-configure events when leader election
is enabled.
  • Loading branch information
marquiz committed Aug 20, 2024
1 parent 5a5b9e3 commit ccd13fc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
24 changes: 24 additions & 0 deletions pkg/nfd-master/nfd-api-controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
)

type nfdController struct {
factory nfdinformers.SharedInformerFactory
featureLister nfdlisters.NodeFeatureLister
ruleLister nfdlisters.NodeFeatureRuleLister
featureGroupLister nfdlisters.NodeFeatureGroupLister
Expand Down Expand Up @@ -181,12 +182,35 @@ func newNfdController(config *restclient.Config, nfdApiControllerOptions nfdApiC
}

klog.InfoS("informer caches synced", "duration", time.Since(now))
c.factory = informerFactory

return c, nil
}

func readChUntilClosed[T any](ch <-chan T) {
for {
if _, ok := <-ch; !ok {
return
}
}
}

func (c *nfdController) stop() {
close(c.stopChan)

// Make sure that there are no blocking event handler callbacks and all
// channels are closed.
// NOTE: This will be unnecessary and could be removed if/when the dynamic
// run-time configuration is dropped.
go readChUntilClosed(c.updateOneNodeChan)
go readChUntilClosed(c.updateAllNodesChan)
go readChUntilClosed(c.updateNodeFeatureGroupChan)
go readChUntilClosed(c.updateAllNodeFeatureGroupsChan)
c.factory.Shutdown()
close(c.updateOneNodeChan)
close(c.updateAllNodesChan)
close(c.updateNodeFeatureGroupChan)
close(c.updateAllNodeFeatureGroupsChan)
}

func getNodeNameForObj(obj metav1.Object) (string, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/nfd-master/nfd-master.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ func (m *nfdMaster) Run() error {

// Update all nodes when the configuration changes
if m.nfdController != nil && nfdfeatures.NFDFeatureGate.Enabled(nfdfeatures.NodeFeatureAPI) {
m.nfdController.updateAllNodesChan <- struct{}{}
m.nfdController.updateAllNodes()
}

case <-m.stop:
Expand Down

0 comments on commit ccd13fc

Please sign in to comment.