diff --git a/common/libnetwork/cni/cni_conversion.go b/common/libnetwork/cni/cni_conversion.go index c7a37ef6ab..6e4514b992 100644 --- a/common/libnetwork/cni/cni_conversion.go +++ b/common/libnetwork/cni/cni_conversion.go @@ -327,6 +327,9 @@ func (n *cniNetwork) createCNIConfigListFromNetwork(network *types.Network, writ } cniPathName := "" if writeToDisk { + if err := os.MkdirAll(n.cniConfigDir, 0o755); err != nil { + return nil, "", err + } cniPathName = filepath.Join(n.cniConfigDir, network.Name+".conflist") err = os.WriteFile(cniPathName, b, 0o644) if err != nil { diff --git a/common/libnetwork/cni/config_test.go b/common/libnetwork/cni/config_test.go index 1b39a8c7ea..4ea1387983 100644 --- a/common/libnetwork/cni/config_test.go +++ b/common/libnetwork/cni/config_test.go @@ -128,6 +128,10 @@ var _ = Describe("Config", func() { }) It("create two networks", func() { + // remove the dir here since we do not expect it to exists in the real context as well + // the backend will create it for us + os.RemoveAll(cniConfDir) + network := types.Network{} network1, err := libpodNet.NetworkCreate(network, nil) Expect(err).To(BeNil()) diff --git a/common/libnetwork/cni/network.go b/common/libnetwork/cni/network.go index ad0f10166a..80b04a3c82 100644 --- a/common/libnetwork/cni/network.go +++ b/common/libnetwork/cni/network.go @@ -141,11 +141,15 @@ func (n *cniNetwork) DefaultNetworkName() string { func (n *cniNetwork) loadNetworks() error { // check the mod time of the config dir + var modTime time.Time f, err := os.Stat(n.cniConfigDir) - if err != nil { + // ignore error if the file does not exists + if err != nil && !errors.Is(err, os.ErrNotExist) { return err } - modTime := f.ModTime() + if err == nil { + modTime = f.ModTime() + } // skip loading networks if they are already loaded and // if the config dir was not modified since the last call