From 401172966f6082329f1860ab1761d3e6140e5585 Mon Sep 17 00:00:00 2001 From: yangchenliang Date: Mon, 18 Sep 2017 14:47:19 +0800 Subject: [PATCH] "docker swarm init --force-new-cluster" use limit Signed-off-by: yangchenliang When worker executor `docker swarm init --force-new-cluster`,docker would hang.So only manager can process it. Signed-off-by: yangchenliang Upstream-commit: 12e947efdba5481020f6543514ade83d87c69a28 Component: engine --- components/engine/daemon/cluster/errors.go | 3 +++ components/engine/daemon/cluster/swarm.go | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/components/engine/daemon/cluster/errors.go b/components/engine/daemon/cluster/errors.go index 1698229427..0ffe78b98b 100644 --- a/components/engine/daemon/cluster/errors.go +++ b/components/engine/daemon/cluster/errors.go @@ -15,6 +15,9 @@ const ( // errSwarmCertificatesExpired is returned if docker was not started for the whole validity period and they had no chance to renew automatically. errSwarmCertificatesExpired notAvailableError = "Swarm certificates have expired. To replace them, leave the swarm and join again." + + // errSwarmNotManager is returned if the node is not a swarm manager. + errSwarmNotManager notAvailableError = "This node is not a swarm manager. Worker nodes can't be used to view or modify cluster state. Please run this command on a manager node or promote the current node to a manager." ) type notFoundError struct { diff --git a/components/engine/daemon/cluster/swarm.go b/components/engine/daemon/cluster/swarm.go index 1fa62920e3..e3fffe983d 100644 --- a/components/engine/daemon/cluster/swarm.go +++ b/components/engine/daemon/cluster/swarm.go @@ -26,9 +26,13 @@ func (c *Cluster) Init(req types.InitRequest) (string, error) { defer c.controlMutex.Unlock() if c.nr != nil { if req.ForceNewCluster { + // Take c.mu temporarily to wait for presently running // API handlers to finish before shutting down the node. c.mu.Lock() + if !c.nr.nodeState.IsManager() { + return "", errSwarmNotManager + } c.mu.Unlock() if err := c.nr.Stop(); err != nil {