Merge pull request #4291 from baude/networkcreatecheckbridge

check existing bridge names when creating networks
This commit is contained in:
Daniel J Walsh
2019-10-28 13:02:27 -04:00
committed by GitHub
2 changed files with 38 additions and 5 deletions

View File

@ -24,19 +24,26 @@ func GetFreeDeviceName() (string, error) {
if err != nil { if err != nil {
return "", err return "", err
} }
bridgeNames, err := GetBridgeNamesFromFileSystem()
if err != nil {
return "", err
}
for { for {
deviceName = fmt.Sprintf("%s%d", CNIDeviceName, deviceNum) deviceName = fmt.Sprintf("%s%d", CNIDeviceName, deviceNum)
logrus.Debugf("checking if device name %s exists in other cni networks", deviceName) logrus.Debugf("checking if device name %q exists in other cni networks", deviceName)
if util.StringInSlice(deviceName, networkNames) { if util.StringInSlice(deviceName, networkNames) {
deviceNum++ deviceNum++
continue continue
} }
logrus.Debugf("checking if device name %s exists in live networks", deviceName) logrus.Debugf("checking if device name %q exists in live networks", deviceName)
if !util.StringInSlice(deviceName, liveNetworksNames) { 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 break
} }
// TODO Still need to check the bridge names for a conflict but I dont know
// how to get them yet!
deviceNum++ deviceNum++
} }
return deviceName, nil return deviceName, nil

View File

@ -129,3 +129,29 @@ func GetInterfaceNameFromConfig(path string) (string, error) {
} }
return name, nil return name, nil
} }
// GetBridgeNamesFromFileSystem is a convenience function to get all the bridge
// names from the configured networks
func GetBridgeNamesFromFileSystem() ([]string, error) {
var bridgeNames []string
networks, err := LoadCNIConfsFromDir(CNIConfigDir)
if err != nil {
return nil, err
}
for _, n := range networks {
var name string
// iterate network conflists
for _, cniplugin := range n.Plugins {
// iterate plugins
if cniplugin.Network.Type == "bridge" {
plugin := make(map[string]interface{})
if err := json.Unmarshal(cniplugin.Bytes, &plugin); err != nil {
continue
}
name = plugin["bridge"].(string)
}
}
bridgeNames = append(bridgeNames, name)
}
return bridgeNames, nil
}