mirror of https://github.com/containers/podman.git
				
				
				
			Merge pull request #8493 from Luap99/net-rm-macvlan
Fix problems with network remove
This commit is contained in:
		
						commit
						8b2c0a4fa3
					
				|  | @ -14,6 +14,9 @@ import ( | ||||||
| 	"github.com/pkg/errors" | 	"github.com/pkg/errors" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
|  | // ErrNoSuchNetworkInterface indicates that no network interface exists
 | ||||||
|  | var ErrNoSuchNetworkInterface = errors.New("unable to find interface name for network") | ||||||
|  | 
 | ||||||
| // GetCNIConfDir get CNI configuration directory
 | // GetCNIConfDir get CNI configuration directory
 | ||||||
| func GetCNIConfDir(configArg *config.Config) string { | func GetCNIConfDir(configArg *config.Config) string { | ||||||
| 	if len(configArg.Network.NetworkConfigDir) < 1 { | 	if len(configArg.Network.NetworkConfigDir) < 1 { | ||||||
|  | @ -142,7 +145,7 @@ func GetInterfaceNameFromConfig(path string) (string, error) { | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 	if len(name) == 0 { | 	if len(name) == 0 { | ||||||
| 		return "", errors.New("unable to find interface name for network") | 		return "", ErrNoSuchNetworkInterface | ||||||
| 	} | 	} | ||||||
| 	return name, nil | 	return name, nil | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -10,6 +10,7 @@ import ( | ||||||
| 	"github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator" | 	"github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator" | ||||||
| 	"github.com/containers/common/pkg/config" | 	"github.com/containers/common/pkg/config" | ||||||
| 	"github.com/containers/podman/v2/libpod/define" | 	"github.com/containers/podman/v2/libpod/define" | ||||||
|  | 	"github.com/containers/podman/v2/pkg/rootless" | ||||||
| 	"github.com/containers/podman/v2/pkg/util" | 	"github.com/containers/podman/v2/pkg/util" | ||||||
| 	"github.com/pkg/errors" | 	"github.com/pkg/errors" | ||||||
| 	"github.com/sirupsen/logrus" | 	"github.com/sirupsen/logrus" | ||||||
|  | @ -181,21 +182,26 @@ func RemoveNetwork(config *config.Config, name string) error { | ||||||
| 	// Before we delete the configuration file, we need to make sure we can read and parse
 | 	// Before we delete the configuration file, we need to make sure we can read and parse
 | ||||||
| 	// it to get the network interface name so we can remove that too
 | 	// it to get the network interface name so we can remove that too
 | ||||||
| 	interfaceName, err := GetInterfaceNameFromConfig(cniPath) | 	interfaceName, err := GetInterfaceNameFromConfig(cniPath) | ||||||
| 	if err != nil { | 	if err == nil { | ||||||
| 		return errors.Wrapf(err, "failed to find network interface name in %q", cniPath) | 		// Don't try to remove the network interface if we are not root
 | ||||||
| 	} | 		if !rootless.IsRootless() { | ||||||
| 	liveNetworkNames, err := GetLiveNetworkNames() | 			liveNetworkNames, err := GetLiveNetworkNames() | ||||||
| 	if err != nil { | 			if err != nil { | ||||||
| 		return errors.Wrapf(err, "failed to get live network names") | 				return errors.Wrapf(err, "failed to get live network names") | ||||||
| 	} | 			} | ||||||
| 	if util.StringInSlice(interfaceName, liveNetworkNames) { | 			if util.StringInSlice(interfaceName, liveNetworkNames) { | ||||||
| 		if err := RemoveInterface(interfaceName); err != nil { | 				if err := RemoveInterface(interfaceName); err != nil { | ||||||
| 			return errors.Wrapf(err, "failed to delete the network interface %q", interfaceName) | 					return errors.Wrapf(err, "failed to delete the network interface %q", interfaceName) | ||||||
|  | 				} | ||||||
|  | 			} | ||||||
| 		} | 		} | ||||||
|  | 	} else if err != ErrNoSuchNetworkInterface { | ||||||
|  | 		// Don't error if we couldn't find the network interface name
 | ||||||
|  | 		return err | ||||||
| 	} | 	} | ||||||
| 	// Remove the configuration file
 | 	// Remove the configuration file
 | ||||||
| 	if err := os.Remove(cniPath); err != nil { | 	if err := os.Remove(cniPath); err != nil { | ||||||
| 		return errors.Wrapf(err, "failed to remove network configuration file %q", cniPath) | 		return errors.Wrap(err, "failed to remove network configuration") | ||||||
| 	} | 	} | ||||||
| 	return nil | 	return nil | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -499,4 +499,15 @@ var _ = Describe("Podman network", func() { | ||||||
| 		exec.WaitWithDefaultTimeout() | 		exec.WaitWithDefaultTimeout() | ||||||
| 		Expect(exec.ExitCode()).To(BeZero()) | 		Expect(exec.ExitCode()).To(BeZero()) | ||||||
| 	}) | 	}) | ||||||
|  | 
 | ||||||
|  | 	It("podman network create/remove macvlan", func() { | ||||||
|  | 		net := "macvlan" + stringid.GenerateNonCryptoID() | ||||||
|  | 		nc := podmanTest.Podman([]string{"network", "create", "--macvlan", "lo", net}) | ||||||
|  | 		nc.WaitWithDefaultTimeout() | ||||||
|  | 		Expect(nc.ExitCode()).To(Equal(0)) | ||||||
|  | 
 | ||||||
|  | 		nc = podmanTest.Podman([]string{"network", "rm", net}) | ||||||
|  | 		nc.WaitWithDefaultTimeout() | ||||||
|  | 		Expect(nc.ExitCode()).To(Equal(0)) | ||||||
|  | 	}) | ||||||
| }) | }) | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue