diff options
author | OpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com> | 2020-10-07 13:56:56 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-07 13:56:56 -0400 |
commit | 9ae873e60e149677db66782eaf2b4ed1402e97d2 (patch) | |
tree | 1f72e0558316390e8a235edad0f2464725dbd9c9 /libpod/network/devices.go | |
parent | 173e3c2faa74e5ef1b941338c06e5dd7dca68ac2 (diff) | |
parent | fe3faa517e1bbc3b2e82afaae32d8712c844fdae (diff) | |
download | podman-9ae873e60e149677db66782eaf2b4ed1402e97d2.tar.gz podman-9ae873e60e149677db66782eaf2b4ed1402e97d2.tar.bz2 podman-9ae873e60e149677db66782eaf2b4ed1402e97d2.zip |
Merge pull request #7943 from baude/issue7807
prevent unpredictable results with network create|remove
Diffstat (limited to 'libpod/network/devices.go')
-rw-r--r-- | libpod/network/devices.go | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/libpod/network/devices.go b/libpod/network/devices.go new file mode 100644 index 000000000..a5d23fae4 --- /dev/null +++ b/libpod/network/devices.go @@ -0,0 +1,63 @@ +package network + +import ( + "fmt" + "os/exec" + + "github.com/containers/common/pkg/config" + "github.com/containers/podman/v2/pkg/util" + "github.com/containers/podman/v2/utils" + "github.com/sirupsen/logrus" +) + +// GetFreeDeviceName returns a device name that is unused; used when no network +// name is provided by user +func GetFreeDeviceName(config *config.Config) (string, error) { + var ( + deviceNum uint + deviceName string + ) + networkNames, err := GetNetworkNamesFromFileSystem(config) + if err != nil { + return "", err + } + liveNetworksNames, err := GetLiveNetworkNames() + if err != nil { + return "", err + } + bridgeNames, err := GetBridgeNamesFromFileSystem(config) + if err != nil { + return "", err + } + for { + deviceName = fmt.Sprintf("%s%d", CNIDeviceName, deviceNum) + logrus.Debugf("checking if device name %q exists in other cni networks", deviceName) + if util.StringInSlice(deviceName, networkNames) { + deviceNum++ + continue + } + logrus.Debugf("checking if device name %q exists in live networks", deviceName) + if util.StringInSlice(deviceName, liveNetworksNames) { + deviceNum++ + continue + } + logrus.Debugf("checking if device name %q already exists as a bridge name ", deviceName) + if !util.StringInSlice(deviceName, bridgeNames) { + break + } + deviceNum++ + } + return deviceName, nil +} + +// RemoveInterface removes an interface by the given name +func RemoveInterface(interfaceName string) error { + // Make sure we have the ip command on the system + ipPath, err := exec.LookPath("ip") + if err != nil { + return err + } + // Delete the network interface + _, err = utils.ExecCmd(ipPath, []string{"link", "del", interfaceName}...) + return err +} |