From d22824dc11916e4155c0daa2ae45faa7d3097c16 Mon Sep 17 00:00:00 2001 From: Arko Dasgupta Date: Wed, 24 Jul 2019 17:06:04 -0700 Subject: [PATCH] Move dummyLinkExists into createDummyLink Signed-off-by: Arko Dasgupta --- libnetwork/drivers/ipvlan/ipvlan_network.go | 28 ++++++------------ libnetwork/drivers/ipvlan/ipvlan_setup.go | 10 +++++++ libnetwork/drivers/macvlan/macvlan_network.go | 29 ++++++------------- libnetwork/drivers/macvlan/macvlan_setup.go | 8 +++++ 4 files changed, 36 insertions(+), 39 deletions(-) diff --git a/libnetwork/drivers/ipvlan/ipvlan_network.go b/libnetwork/drivers/ipvlan/ipvlan_network.go index 6da71c1c6f..89acecc40f 100644 --- a/libnetwork/drivers/ipvlan/ipvlan_network.go +++ b/libnetwork/drivers/ipvlan/ipvlan_network.go @@ -97,16 +97,11 @@ func (d *driver) createNetwork(config *configuration) (bool, error) { if !parentExists(config.Parent) { // if the --internal flag is set, create a dummy link if config.Internal { - if !dummyLinkExists(getDummyName(stringid.TruncateID(config.ID))) { - err := createDummyLink(config.Parent, getDummyName(stringid.TruncateID(config.ID))) - if err != nil { - return false, err - } - config.CreatedSlaveLink = true - - } else { - logrus.Debugf("Dummy Link %s for ipvlan already exists", getDummyName(stringid.TruncateID(config.ID))) + err := createDummyLink(config.Parent, getDummyName(stringid.TruncateID(config.ID))) + if err != nil { + return false, err } + config.CreatedSlaveLink = true // notify the user in logs they have limited communications if config.Parent == getDummyName(stringid.TruncateID(config.ID)) { @@ -116,17 +111,12 @@ func (d *driver) createNetwork(config *configuration) (bool, error) { } else { // if the subinterface parent_iface.vlan_id checks do not pass, return err. // a valid example is 'eth0.10' for a parent iface 'eth0' with a vlan id '10' - if !vlanLinkExists(config.Parent) { - err := createVlanLink(config.Parent) - if err != nil { - return false, err - } - // if driver created the networks slave link, record it for future deletion - config.CreatedSlaveLink = true - } else { - logrus.Debugf("Parent Sub Interface %s already Exists NetID %s", config.Parent, config.ID) + err := createVlanLink(config.Parent) + if err != nil { + return false, err } - + // if driver created the networks slave link, record it for future deletion + config.CreatedSlaveLink = true } } if !foundExisting { diff --git a/libnetwork/drivers/ipvlan/ipvlan_setup.go b/libnetwork/drivers/ipvlan/ipvlan_setup.go index d7edcbedd1..48c3dd65e0 100644 --- a/libnetwork/drivers/ipvlan/ipvlan_setup.go +++ b/libnetwork/drivers/ipvlan/ipvlan_setup.go @@ -81,6 +81,11 @@ func vlanLinkExists(linkStr string) bool { // createVlanLink parses sub-interfaces and vlan id for creation func createVlanLink(parentName string) error { + if vlanLinkExists(parentName) { + logrus.Debugf("Parent Sub Interface %s already exists", parentName) + return nil + } + if strings.Contains(parentName, ".") { parent, vidInt, err := parseVlan(parentName) if err != nil { @@ -176,6 +181,11 @@ func dummyLinkExists(dummyName string) bool { // createDummyLink creates a dummy0 parent link func createDummyLink(dummyName, truncNetID string) error { + // check if dummyLinkExists and return if it does + if dummyLinkExists(truncNetID) { + logrus.Debugf("Dummy Link %s for ipvlan already exists", truncNetID) + return nil + } // create a parent interface since one was not specified parent := &netlink.Dummy{ LinkAttrs: netlink.LinkAttrs{ diff --git a/libnetwork/drivers/macvlan/macvlan_network.go b/libnetwork/drivers/macvlan/macvlan_network.go index c96faccb26..7c27e33f53 100644 --- a/libnetwork/drivers/macvlan/macvlan_network.go +++ b/libnetwork/drivers/macvlan/macvlan_network.go @@ -102,15 +102,11 @@ func (d *driver) createNetwork(config *configuration) (bool, error) { if !parentExists(config.Parent) { // if the --internal flag is set, create a dummy link if config.Internal { - if !dummyLinkExists(getDummyName(stringid.TruncateID(config.ID))) { - err := createDummyLink(config.Parent, getDummyName(stringid.TruncateID(config.ID))) - if err != nil { - return false, err - } - config.CreatedSlaveLink = true - } else { - logrus.Debugf("Dummy Link %s for Mac Vlan already exists", getDummyName(stringid.TruncateID(config.ID))) + err := createDummyLink(config.Parent, getDummyName(stringid.TruncateID(config.ID))) + if err != nil { + return false, err } + config.CreatedSlaveLink = true // notify the user in logs they have limited communications if config.Parent == getDummyName(stringid.TruncateID(config.ID)) { logrus.Debugf("Empty -o parent= and --internal flags limit communications to other containers inside of network: %s", @@ -119,19 +115,12 @@ func (d *driver) createNetwork(config *configuration) (bool, error) { } else { // if the subinterface parent_iface.vlan_id checks do not pass, return err. // a valid example is 'eth0.10' for a parent iface 'eth0' with a vlan id '10' - - if !vlanLinkExists(config.Parent) { - // if the subinterface parent_iface.vlan_id checks do not pass, return err. - // a valid example is 'eth0.10' for a parent iface 'eth0' with a vlan id '10' - err := createVlanLink(config.Parent) - if err != nil { - return false, err - } - // if driver created the networks slave link, record it for future deletion - config.CreatedSlaveLink = true - } else { - logrus.Debugf("Parent Sub Interface %s already Exists NetID %s", config.Parent, config.ID) + err := createVlanLink(config.Parent) + if err != nil { + return false, err } + // if driver created the networks slave link, record it for future deletion + config.CreatedSlaveLink = true } } if !foundExisting { diff --git a/libnetwork/drivers/macvlan/macvlan_setup.go b/libnetwork/drivers/macvlan/macvlan_setup.go index 2d760c2daf..b1e7c14d06 100644 --- a/libnetwork/drivers/macvlan/macvlan_setup.go +++ b/libnetwork/drivers/macvlan/macvlan_setup.go @@ -85,6 +85,10 @@ func vlanLinkExists(linkStr string) bool { // createVlanLink parses sub-interfaces and vlan id for creation func createVlanLink(parentName string) error { + if vlanLinkExists(parentName) { + logrus.Debugf("Parent Sub Interface %s already exists", parentName) + return nil + } if strings.Contains(parentName, ".") { parent, vidInt, err := parseVlan(parentName) if err != nil { @@ -180,6 +184,10 @@ func dummyLinkExists(dummyName string) bool { // createDummyLink creates a dummy0 parent link func createDummyLink(dummyName, truncNetID string) error { + if dummyLinkExists(truncNetID) { + logrus.Debugf("Dummy Link %s for Mac Vlan already exists", truncNetID) + return nil + } // create a parent interface since one was not specified parent := &netlink.Dummy{ LinkAttrs: netlink.LinkAttrs{