Browse Source

Merge pull request #1687 from aboch/rnm

Fix rename for containers on swarm network
Madhu Venugopal 8 years ago
parent
commit
56ae6ff330
1 changed files with 42 additions and 17 deletions
  1. 42 17
      libnetwork/endpoint.go

+ 42 - 17
libnetwork/endpoint.go

@@ -584,39 +584,64 @@ func doUpdateHostsFile(n *network, sb *sandbox) bool {
 }
 
 func (ep *endpoint) rename(name string) error {
-	var err error
+	var (
+		err      error
+		netWatch *netWatch
+		ok       bool
+	)
+
 	n := ep.getNetwork()
 	if n == nil {
 		return fmt.Errorf("network not connected for ep %q", ep.name)
 	}
 
-	n.getController().Lock()
-	netWatch, ok := n.getController().nmap[n.ID()]
-	n.getController().Unlock()
+	c := n.getController()
 
-	if !ok {
-		return fmt.Errorf("watch null for network %q", n.Name())
+	if c.isAgent() {
+		if err = ep.deleteServiceInfoFromCluster(); err != nil {
+			return types.InternalErrorf("Could not delete service state for endpoint %s from cluster on rename: %v", ep.Name(), err)
+		}
+	} else {
+		c.Lock()
+		netWatch, ok = c.nmap[n.ID()]
+		c.Unlock()
+		if !ok {
+			return fmt.Errorf("watch null for network %q", n.Name())
+		}
+		n.updateSvcRecord(ep, c.getLocalEps(netWatch), false)
 	}
 
-	n.updateSvcRecord(ep, n.getController().getLocalEps(netWatch), false)
-
 	oldName := ep.name
 	oldAnonymous := ep.anonymous
 	ep.name = name
 	ep.anonymous = false
 
-	n.updateSvcRecord(ep, n.getController().getLocalEps(netWatch), true)
-	defer func() {
-		if err != nil {
-			n.updateSvcRecord(ep, n.getController().getLocalEps(netWatch), false)
-			ep.name = oldName
-			ep.anonymous = oldAnonymous
-			n.updateSvcRecord(ep, n.getController().getLocalEps(netWatch), true)
+	if c.isAgent() {
+		if err = ep.addServiceInfoToCluster(); err != nil {
+			return types.InternalErrorf("Could not add service state for endpoint %s to cluster on rename: %v", ep.Name(), err)
 		}
-	}()
+		defer func() {
+			if err != nil {
+				ep.deleteServiceInfoFromCluster()
+				ep.name = oldName
+				ep.anonymous = oldAnonymous
+				ep.addServiceInfoToCluster()
+			}
+		}()
+	} else {
+		n.updateSvcRecord(ep, c.getLocalEps(netWatch), true)
+		defer func() {
+			if err != nil {
+				n.updateSvcRecord(ep, c.getLocalEps(netWatch), false)
+				ep.name = oldName
+				ep.anonymous = oldAnonymous
+				n.updateSvcRecord(ep, c.getLocalEps(netWatch), true)
+			}
+		}()
+	}
 
 	// Update the store with the updated name
-	if err = n.getController().updateToStore(ep); err != nil {
+	if err = c.updateToStore(ep); err != nil {
 		return err
 	}
 	// After the name change do a dummy endpoint count update to