Browse Source

libnetwork: refactor isDistributedControl()

The meaning of the (*Controller).isDistributedControl() method is not
immediately clear from the name, and it does not have any doc comment.
It returns true if and only if the controller is neither a manager node
nor an agent node -- that is, if the daemon is _not_ participating in a
Swarm cluster. The method name likely comes from the old abandoned
datastore-as-IPC control plane architecture for libnetwork. Refactor

    c.isDistributedControl() -> !c.isSwarmNode()

to make it easier to understand code which consumes the method.

Signed-off-by: Cory Snider <csnider@mirantis.com>
Cory Snider 1 year ago
parent
commit
0456c0db87
3 changed files with 10 additions and 10 deletions
  1. 6 6
      libnetwork/controller.go
  2. 2 2
      libnetwork/sandbox.go
  3. 2 2
      libnetwork/store.go

+ 6 - 6
libnetwork/controller.go

@@ -241,7 +241,7 @@ func (c *Controller) clusterAgentInit() {
 			c.mu.Unlock()
 			c.mu.Unlock()
 			fallthrough
 			fallthrough
 		case cluster.EventSocketChange, cluster.EventNodeReady:
 		case cluster.EventSocketChange, cluster.EventNodeReady:
-			if keysAvailable && !c.isDistributedControl() {
+			if keysAvailable && c.isSwarmNode() {
 				c.agentOperationStart()
 				c.agentOperationStart()
 				if err := c.agentSetup(clusterProvider); err != nil {
 				if err := c.agentSetup(clusterProvider); err != nil {
 					c.agentStopComplete()
 					c.agentStopComplete()
@@ -451,8 +451,8 @@ func (c *Controller) isAgent() bool {
 	return c.cfg.ClusterProvider.IsAgent()
 	return c.cfg.ClusterProvider.IsAgent()
 }
 }
 
 
-func (c *Controller) isDistributedControl() bool {
-	return !c.isManager() && !c.isAgent()
+func (c *Controller) isSwarmNode() bool {
+	return c.isManager() || c.isAgent()
 }
 }
 
 
 func (c *Controller) GetPluginGetter() plugingetter.PluginGetter {
 func (c *Controller) GetPluginGetter() plugingetter.PluginGetter {
@@ -553,7 +553,7 @@ func (c *Controller) NewNetwork(networkType, name string, id string, options ...
 
 
 	// At this point the network scope is still unknown if not set by user
 	// At this point the network scope is still unknown if not set by user
 	if (caps.DataScope == scope.Global || nw.scope == scope.Swarm) &&
 	if (caps.DataScope == scope.Global || nw.scope == scope.Swarm) &&
-		!c.isDistributedControl() && !nw.dynamic {
+		c.isSwarmNode() && !nw.dynamic {
 		if c.isManager() {
 		if c.isManager() {
 			// For non-distributed controlled environment, globalscoped non-dynamic networks are redirected to Manager
 			// For non-distributed controlled environment, globalscoped non-dynamic networks are redirected to Manager
 			return nil, ManagerRedirectError(name)
 			return nil, ManagerRedirectError(name)
@@ -561,7 +561,7 @@ func (c *Controller) NewNetwork(networkType, name string, id string, options ...
 		return nil, types.ForbiddenErrorf("Cannot create a multi-host network from a worker node. Please create the network from a manager node.")
 		return nil, types.ForbiddenErrorf("Cannot create a multi-host network from a worker node. Please create the network from a manager node.")
 	}
 	}
 
 
-	if nw.scope == scope.Swarm && c.isDistributedControl() {
+	if nw.scope == scope.Swarm && !c.isSwarmNode() {
 		return nil, types.ForbiddenErrorf("cannot create a swarm scoped network when swarm is not active")
 		return nil, types.ForbiddenErrorf("cannot create a swarm scoped network when swarm is not active")
 	}
 	}
 
 
@@ -704,7 +704,7 @@ addToStore:
 		}
 		}
 	}
 	}
 
 
-	if !c.isDistributedControl() {
+	if c.isSwarmNode() {
 		c.mu.Lock()
 		c.mu.Lock()
 		arrangeIngressFilterRule()
 		arrangeIngressFilterRule()
 		c.mu.Unlock()
 		c.mu.Unlock()

+ 2 - 2
libnetwork/sandbox.go

@@ -162,7 +162,7 @@ func (sb *Sandbox) delete(force bool) error {
 		}
 		}
 		// Retain the sanbdox if we can't obtain the network from store.
 		// Retain the sanbdox if we can't obtain the network from store.
 		if _, err := c.getNetworkFromStore(ep.getNetwork().ID()); err != nil {
 		if _, err := c.getNetworkFromStore(ep.getNetwork().ID()); err != nil {
-			if c.isDistributedControl() {
+			if !c.isSwarmNode() {
 				retain = true
 				retain = true
 			}
 			}
 			log.G(context.TODO()).Warnf("Failed getting network for ep %s during sandbox %s delete: %v", ep.ID(), sb.ID(), err)
 			log.G(context.TODO()).Warnf("Failed getting network for ep %s during sandbox %s delete: %v", ep.ID(), sb.ID(), err)
@@ -459,7 +459,7 @@ func (sb *Sandbox) ResolveName(ctx context.Context, name string, ipType int) ([]
 	// network, ingress network and docker_gwbridge network. Name resolution
 	// network, ingress network and docker_gwbridge network. Name resolution
 	// should prioritize returning the VIP/IPs on user overlay network.
 	// should prioritize returning the VIP/IPs on user overlay network.
 	newList := []*Endpoint{}
 	newList := []*Endpoint{}
-	if !sb.controller.isDistributedControl() {
+	if sb.controller.isSwarmNode() {
 		newList = append(newList, getDynamicNwEndpoints(epList)...)
 		newList = append(newList, getDynamicNwEndpoints(epList)...)
 		ingressEP := getIngressNwEndpoint(epList)
 		ingressEP := getIngressNwEndpoint(epList)
 		if ingressEP != nil {
 		if ingressEP != nil {

+ 2 - 2
libnetwork/store.go

@@ -198,7 +198,7 @@ func (c *Controller) unWatchSvcRecord(ep *Endpoint) {
 
 
 func (c *Controller) processEndpointCreate(ep *Endpoint) {
 func (c *Controller) processEndpointCreate(ep *Endpoint) {
 	n := ep.getNetwork()
 	n := ep.getNetwork()
-	if !c.isDistributedControl() && n.Scope() == scope.Swarm && n.driverIsMultihost() {
+	if c.isSwarmNode() && n.Scope() == scope.Swarm && n.driverIsMultihost() {
 		return
 		return
 	}
 	}
 
 
@@ -220,7 +220,7 @@ func (c *Controller) processEndpointCreate(ep *Endpoint) {
 
 
 func (c *Controller) processEndpointDelete(ep *Endpoint) {
 func (c *Controller) processEndpointDelete(ep *Endpoint) {
 	n := ep.getNetwork()
 	n := ep.getNetwork()
-	if !c.isDistributedControl() && n.Scope() == scope.Swarm && n.driverIsMultihost() {
+	if c.isSwarmNode() && n.Scope() == scope.Swarm && n.driverIsMultihost() {
 		return
 		return
 	}
 	}