Skip to content

Commit

Permalink
Merge pull request #93 from Luap99/no-inotify-backport
Browse files Browse the repository at this point in the history
[cni-0.x] Add option to disable the use of inotify
  • Loading branch information
openshift-merge-robot authored Jun 21, 2021
2 parents 10dec7e + 817f95e commit d0acc78
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 @@ -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

0 comments on commit d0acc78

Please sign in to comment.