浏览代码

Move dummyLinkExists into createDummyLink

Signed-off-by: Arko Dasgupta <arko.dasgupta@docker.com>
Arko Dasgupta 6 年之前
父节点
当前提交
d22824dc11

+ 9 - 19
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 {

+ 10 - 0
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{

+ 9 - 20
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 {

+ 8 - 0
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{