Skip to content

Commit

Permalink
Add option to disable the use of inotify
Browse files Browse the repository at this point in the history
Add a new InitCNINoInotify function to allow the use of OCICNI without
the use of inotify.

For some workloads it is not required to watch the cni config directory.
With podman v3.2 we started using OCICNI for rootless users as well.
However the use of inotify is restricted by sysctl values
(fs.inotify.max_user_instances and fs.inotify.max_user_watches).
By default only 128 processes can use inotify.

Since this limit is easy to reach and inotify is not required for our
usecase it would be great to have this option to disable it.

see containers/podman#10686

Signed-off-by: Paul Holzinger <[email protected]>
  • Loading branch information
Luap99 committed Jun 16, 2021
1 parent 95ad096 commit 04d342e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
33 changes: 21 additions & 12 deletions pkg/ocicni/ocicni.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,16 +195,21 @@ func (plugin *cniNetworkPlugin) monitorConfDir(start *sync.WaitGroup) {
// If defaultNetName is empty, CNI config files should be reloaded real-time and
// defaultNetName should be changeable and determined by file sorting.
func InitCNI(defaultNetName string, confDir string, binDirs ...string) (CNIPlugin, error) {
return initCNI(nil, "", defaultNetName, confDir, binDirs...)
return initCNI(nil, "", defaultNetName, confDir, true, binDirs...)
}

// InitCNIWithCache works like InitCNI except that it takes the cni cache directory as third param.
func InitCNIWithCache(defaultNetName, confDir, cacheDir string, binDirs ...string) (CNIPlugin, error) {
return initCNI(nil, cacheDir, defaultNetName, confDir, binDirs...)
return initCNI(nil, cacheDir, defaultNetName, confDir, true, binDirs...)
}

// InitCNINoInotify works like InitCNI except that it does not use inotify to watch for changes in the CNI config dir.
func InitCNINoInotify(defaultNetName, confDir, cacheDir string, binDirs ...string) (CNIPlugin, error) {
return initCNI(nil, cacheDir, defaultNetName, confDir, false, binDirs...)
}

// Internal function to allow faking out exec functions for testing
func initCNI(exec cniinvoke.Exec, cacheDir, defaultNetName string, confDir string, binDirs ...string) (CNIPlugin, error) {
func initCNI(exec cniinvoke.Exec, cacheDir, defaultNetName string, confDir string, useInotify bool, binDirs ...string) (CNIPlugin, error) {
if confDir == "" {
confDir = DefaultConfDir
}
Expand Down Expand Up @@ -245,22 +250,26 @@ func initCNI(exec cniinvoke.Exec, cacheDir, defaultNetName string, confDir strin

plugin.syncNetworkConfig()

plugin.watcher, err = newWatcher(plugin.confDir)
if err != nil {
return nil, err
}
if useInotify {
plugin.watcher, err = newWatcher(plugin.confDir)
if err != nil {
return nil, err
}

startWg := sync.WaitGroup{}
startWg.Add(1)
go plugin.monitorConfDir(&startWg)
startWg.Wait()
startWg := sync.WaitGroup{}
startWg.Add(1)
go plugin.monitorConfDir(&startWg)
startWg.Wait()
}

return plugin, nil
}

func (plugin *cniNetworkPlugin) Shutdown() error {
close(plugin.shutdownChan)
plugin.watcher.Close()
if plugin.watcher != nil {
plugin.watcher.Close()
}
plugin.done.Wait()
return nil
}
Expand Down
18 changes: 9 additions & 9 deletions pkg/ocicni/ocicni_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ var _ = Describe("ocicni operations", func() {
_, _, err = writeConfig(tmpDir, "10-test.conf", "test", "myplugin", "0.3.1")
Expect(err).NotTo(HaveOccurred())

ocicni, err := initCNI(&fakeExec{}, "", "test", tmpDir, "/opt/cni/bin")
ocicni, err := initCNI(&fakeExec{}, "", "test", tmpDir, false, "/opt/cni/bin")
Expect(err).NotTo(HaveOccurred())
Expect(ocicni.Status()).NotTo(HaveOccurred())

Expand All @@ -226,7 +226,7 @@ var _ = Describe("ocicni operations", func() {
})

It("finds an asynchronously written default network configuration", func() {
ocicni, err := initCNI(&fakeExec{}, "", "test", tmpDir, "/opt/cni/bin")
ocicni, err := initCNI(&fakeExec{}, "", "test", tmpDir, true, "/opt/cni/bin")
Expect(err).NotTo(HaveOccurred())

// Writing a config that doesn't match the default network
Expand All @@ -248,7 +248,7 @@ var _ = Describe("ocicni operations", func() {
})

It("finds and refinds an asynchronously written default network configuration", func() {
ocicni, err := initCNI(&fakeExec{}, "", "test", tmpDir, "/opt/cni/bin")
ocicni, err := initCNI(&fakeExec{}, "", "test", tmpDir, true, "/opt/cni/bin")
Expect(err).NotTo(HaveOccurred())

// Write the default network config
Expand Down Expand Up @@ -278,7 +278,7 @@ var _ = Describe("ocicni operations", func() {
})

It("finds an ASCIIbetically first network configuration as default real-time if given no default network name", func() {
ocicni, err := initCNI(&fakeExec{}, "", "", tmpDir, "/opt/cni/bin")
ocicni, err := initCNI(&fakeExec{}, "", "", tmpDir, true, "/opt/cni/bin")
Expect(err).NotTo(HaveOccurred())

_, _, err = writeConfig(tmpDir, "15-test.conf", "test", "myplugin", "0.3.1")
Expand Down Expand Up @@ -492,7 +492,7 @@ var _ = Describe("ocicni operations", func() {
}
fake.addPlugin(nil, conf, expectedResult, nil)

ocicni, err := initCNI(fake, cacheDir, "network2", tmpDir, "/opt/cni/bin")
ocicni, err := initCNI(fake, cacheDir, "network2", tmpDir, true, "/opt/cni/bin")
Expect(err).NotTo(HaveOccurred())

podNet := PodNetwork{
Expand Down Expand Up @@ -573,7 +573,7 @@ var _ = Describe("ocicni operations", func() {
}
fake.addPlugin(nil, conf2, expectedResult2, nil)

ocicni, err := initCNI(fake, cacheDir, "network2", tmpDir, "/opt/cni/bin")
ocicni, err := initCNI(fake, cacheDir, "network2", tmpDir, true, "/opt/cni/bin")
Expect(err).NotTo(HaveOccurred())

podNet := PodNetwork{
Expand Down Expand Up @@ -651,7 +651,7 @@ var _ = Describe("ocicni operations", func() {
}
fake.addPlugin(nil, conf2, expectedResult2, nil)

ocicni, err := initCNI(fake, cacheDir, "network2", tmpDir, "/opt/cni/bin")
ocicni, err := initCNI(fake, cacheDir, "network2", tmpDir, true, "/opt/cni/bin")
Expect(err).NotTo(HaveOccurred())

podNet := PodNetwork{
Expand Down Expand Up @@ -727,7 +727,7 @@ var _ = Describe("ocicni operations", func() {
fake.addPlugin([]string{fmt.Sprintf("CNI_IFNAME=%s", ifname1)}, conf1, nil, nil)
fake.addPlugin([]string{fmt.Sprintf("CNI_IFNAME=%s", ifname2)}, conf2, nil, nil)

ocicni, err = initCNI(fake, cacheDir, defaultNetName, tmpDir, "/opt/cni/bin")
ocicni, err = initCNI(fake, cacheDir, defaultNetName, tmpDir, true, "/opt/cni/bin")
Expect(err).NotTo(HaveOccurred())

podNet = PodNetwork{
Expand Down Expand Up @@ -793,7 +793,7 @@ var _ = Describe("ocicni operations", func() {
fake.addPlugin(nil, conf1, nil, nil)
fake.addPlugin(nil, conf2, nil, nil)

ocicni, err := initCNI(fake, cacheDir, defaultNetName, tmpDir, "/opt/cni/bin")
ocicni, err := initCNI(fake, cacheDir, defaultNetName, tmpDir, true, "/opt/cni/bin")
Expect(err).NotTo(HaveOccurred())
defer ocicni.Shutdown()

Expand Down

0 comments on commit 04d342e

Please sign in to comment.