aboutsummaryrefslogtreecommitdiff
path: root/libpod
diff options
context:
space:
mode:
authorOpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com>2021-10-04 15:01:38 -0400
committerGitHub <noreply@github.com>2021-10-04 15:01:38 -0400
commit2f72f17a114f4e20b56deb2c21908d1d4610919c (patch)
tree27d03a819da8a21414a7a312b5d9d3b8c319ba42 /libpod
parent800e91dafd47c284489be586bd18c3cdf7dc5737 (diff)
parenta726043d0b5c4ff9e59b01f2a48c575d53d67ed0 (diff)
downloadpodman-2f72f17a114f4e20b56deb2c21908d1d4610919c.tar.gz
podman-2f72f17a114f4e20b56deb2c21908d1d4610919c.tar.bz2
podman-2f72f17a114f4e20b56deb2c21908d1d4610919c.zip
Merge pull request #11846 from Luap99/network-reload
CNI networks: reload networks if needed
Diffstat (limited to 'libpod')
-rw-r--r--libpod/network/cni/config_test.go22
-rw-r--r--libpod/network/cni/network.go22
-rw-r--r--libpod/runtime.go3
3 files changed, 20 insertions, 27 deletions
diff --git a/libpod/network/cni/config_test.go b/libpod/network/cni/config_test.go
index 288cf4626..5b0feb859 100644
--- a/libpod/network/cni/config_test.go
+++ b/libpod/network/cni/config_test.go
@@ -1020,28 +1020,6 @@ var _ = Describe("Config", func() {
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("subnet 10.10.0.0/24 is already used on the host or by another config"))
})
-
- It("remove network should not error when config file does not exists on disk", func() {
- name := "mynet"
- network := types.Network{Name: name}
- _, err := libpodNet.NetworkCreate(network)
- Expect(err).To(BeNil())
-
- path := filepath.Join(cniConfDir, name+".conflist")
- Expect(path).To(BeARegularFile())
-
- err = os.Remove(path)
- Expect(err).To(BeNil())
- Expect(path).ToNot(BeARegularFile())
-
- err = libpodNet.NetworkRemove(name)
- Expect(err).To(BeNil())
-
- nets, err := libpodNet.NetworkList()
- Expect(err).To(BeNil())
- Expect(nets).To(HaveLen(1))
- Expect(nets).ToNot(ContainElement(HaveNetworkName(name)))
- })
})
Context("network load valid existing ones", func() {
diff --git a/libpod/network/cni/network.go b/libpod/network/cni/network.go
index 02801641e..a37a84373 100644
--- a/libpod/network/cni/network.go
+++ b/libpod/network/cni/network.go
@@ -10,6 +10,7 @@ import (
"net"
"os"
"strings"
+ "time"
"github.com/containernetworking/cni/libcni"
"github.com/containers/podman/v3/libpod/define"
@@ -40,6 +41,9 @@ type cniNetwork struct {
// lock is a internal lock for critical operations
lock lockfile.Locker
+ // modTime is the timestamp when the config dir was modified
+ modTime time.Time
+
// networks is a map with loaded networks, the key is the network name
networks map[string]*network
}
@@ -113,10 +117,22 @@ func (n *cniNetwork) Drivers() []string {
}
func (n *cniNetwork) loadNetworks() error {
- // skip loading networks if they are already loaded
- if n.networks != nil {
+ // check the mod time of the config dir
+ f, err := os.Stat(n.cniConfigDir)
+ if err != nil {
+ return err
+ }
+ modTime := f.ModTime()
+
+ // skip loading networks if they are already loaded and
+ // if the config dir was not modified since the last call
+ if n.networks != nil && modTime.Equal(n.modTime) {
return nil
}
+ // make sure the remove all networks before we reload them
+ n.networks = nil
+ n.modTime = modTime
+
// FIXME: do we have to support other file types as well, e.g. .conf?
files, err := libcni.ConfFiles(n.cniConfigDir, []string{".conflist"})
if err != nil {
@@ -153,7 +169,7 @@ func (n *cniNetwork) loadNetworks() error {
logrus.Errorf("CNI config list %s could not be converted to a libpod config, skipping: %v", file, err)
continue
}
- logrus.Tracef("Successfully loaded network %s: %v", net.Name, net)
+ logrus.Debugf("Successfully loaded network %s: %v", net.Name, net)
networkInfo := network{
filename: file,
cniNet: conf,
diff --git a/libpod/runtime.go b/libpod/runtime.go
index 27885bf5c..855f3a9f9 100644
--- a/libpod/runtime.go
+++ b/libpod/runtime.go
@@ -489,8 +489,7 @@ func makeRuntime(ctx context.Context, runtime *Runtime) (retErr error) {
DefaultNetwork: runtime.config.Network.DefaultNetwork,
DefaultSubnet: runtime.config.Network.DefaultSubnet,
IsMachine: runtime.config.Engine.MachineEnabled,
- // TODO use cni.lock
- LockFile: filepath.Join(runtime.config.Network.NetworkConfigDir, "cni1.lock"),
+ LockFile: filepath.Join(runtime.config.Network.NetworkConfigDir, "cni.lock"),
})
if err != nil {
return errors.Wrapf(err, "could not create network interface")