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

[cni-0.x] Add option to disable the use of inotify #93

Merged
merged 1 commit into from
Jun 21, 2021
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
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 @@ -650,7 +650,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 @@ -725,7 +725,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 @@ -790,7 +790,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