From 0456c0db87f2121ebae5770291341ebdb2114910 Mon Sep 17 00:00:00 2001 From: Cory Snider Date: Tue, 17 Oct 2023 17:59:19 -0400 Subject: [PATCH] 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 --- libnetwork/controller.go | 12 ++++++------ libnetwork/sandbox.go | 4 ++-- libnetwork/store.go | 4 ++-- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/libnetwork/controller.go b/libnetwork/controller.go index 3995e94148..bb3a310920 100644 --- a/libnetwork/controller.go +++ b/libnetwork/controller.go @@ -241,7 +241,7 @@ func (c *Controller) clusterAgentInit() { c.mu.Unlock() fallthrough case cluster.EventSocketChange, cluster.EventNodeReady: - if keysAvailable && !c.isDistributedControl() { + if keysAvailable && c.isSwarmNode() { c.agentOperationStart() if err := c.agentSetup(clusterProvider); err != nil { c.agentStopComplete() @@ -451,8 +451,8 @@ func (c *Controller) isAgent() bool { 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 { @@ -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 if (caps.DataScope == scope.Global || nw.scope == scope.Swarm) && - !c.isDistributedControl() && !nw.dynamic { + c.isSwarmNode() && !nw.dynamic { if c.isManager() { // For non-distributed controlled environment, globalscoped non-dynamic networks are redirected to Manager 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.") } - 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") } @@ -704,7 +704,7 @@ addToStore: } } - if !c.isDistributedControl() { + if c.isSwarmNode() { c.mu.Lock() arrangeIngressFilterRule() c.mu.Unlock() diff --git a/libnetwork/sandbox.go b/libnetwork/sandbox.go index 86c4701770..81a0db1408 100644 --- a/libnetwork/sandbox.go +++ b/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. if _, err := c.getNetworkFromStore(ep.getNetwork().ID()); err != nil { - if c.isDistributedControl() { + if !c.isSwarmNode() { retain = true } 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 // should prioritize returning the VIP/IPs on user overlay network. newList := []*Endpoint{} - if !sb.controller.isDistributedControl() { + if sb.controller.isSwarmNode() { newList = append(newList, getDynamicNwEndpoints(epList)...) ingressEP := getIngressNwEndpoint(epList) if ingressEP != nil { diff --git a/libnetwork/store.go b/libnetwork/store.go index af3acff052..2abdde8364 100644 --- a/libnetwork/store.go +++ b/libnetwork/store.go @@ -198,7 +198,7 @@ func (c *Controller) unWatchSvcRecord(ep *Endpoint) { func (c *Controller) processEndpointCreate(ep *Endpoint) { n := ep.getNetwork() - if !c.isDistributedControl() && n.Scope() == scope.Swarm && n.driverIsMultihost() { + if c.isSwarmNode() && n.Scope() == scope.Swarm && n.driverIsMultihost() { return } @@ -220,7 +220,7 @@ func (c *Controller) processEndpointCreate(ep *Endpoint) { func (c *Controller) processEndpointDelete(ep *Endpoint) { n := ep.getNetwork() - if !c.isDistributedControl() && n.Scope() == scope.Swarm && n.driverIsMultihost() { + if c.isSwarmNode() && n.Scope() == scope.Swarm && n.driverIsMultihost() { return }