CNI: network remove do not error for ENOENT

Make podman network rm more robust by checking for ENOENT if we cannot
remove the config file. If it does not exists there is no reason to
error. This is especially useful for podman network prune.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
This commit is contained in:
Paul Holzinger 2021-09-27 15:34:28 +02:00
parent 869cb9a654
commit 1df0646b01
No known key found for this signature in database
GPG Key ID: EB145DD938A3CAF2
2 changed files with 26 additions and 1 deletions

View File

@ -170,7 +170,11 @@ func (n *cniNetwork) NetworkRemove(nameOrID string) error {
file := network.filename
delete(n.networks, network.libpodNet.Name)
return os.Remove(file)
// make sure to not error for ErrNotExist
if err := os.Remove(file); err != nil && !errors.Is(err, os.ErrNotExist) {
return err
}
return nil
}
// NetworkList will return all known Networks. Optionally you can

View File

@ -1021,6 +1021,27 @@ var _ = Describe("Config", func() {
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() {