Skip to content

Commit 769a9a6

Browse files
committed
BACKPORT: Start daemon if certificates have been expired
Upstream reference: moby@439de76 Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1503434 Signed-off-by: Antonio Murdaca <runcom@redhat.com>
1 parent f2f719e commit 769a9a6

1 file changed

Lines changed: 50 additions & 30 deletions

File tree

daemon/cluster/cluster.go

Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cluster
22

33
import (
4+
"crypto/x509"
45
"encoding/json"
56
stdliberrors "errors"
67
"fmt"
@@ -27,6 +28,7 @@ import (
2728
types "github.com/docker/engine-api/types/swarm"
2829
swarmagent "github.com/docker/swarmkit/agent"
2930
swarmapi "github.com/docker/swarmkit/api"
31+
pkgerrors "github.com/pkg/errors"
3032
"golang.org/x/net/context"
3133
)
3234

@@ -54,6 +56,9 @@ var ErrPendingSwarmExists = fmt.Errorf("This node is processing an existing join
5456
// ErrSwarmJoinTimeoutReached is returned when cluster join could not complete before timeout was reached.
5557
var ErrSwarmJoinTimeoutReached = fmt.Errorf("Timeout was reached before node was joined. The attempt to join the swarm will continue in the background. Use the \"docker info\" command to see the current swarm status of your node.")
5658

59+
// ErrSwarmCertificatesExipred is returned if docker was not started for the whole validity period and they had no chance to renew automatically.
60+
var ErrSwarmCertificatesExpired = pkgerrors.New("Swarm certificates have expired. To replace them, leave the swarm and join again.")
61+
5762
// defaultSpec contains some sane defaults if cluster options are missing on init
5863
var defaultSpec = types.Spec{
5964
Raft: types.RaftConfig{
@@ -163,6 +168,10 @@ func New(config Config) (*Cluster, error) {
163168
logrus.Errorf("swarm component could not be started before timeout was reached")
164169
case <-n.Ready():
165170
case <-n.done:
171+
if err, ok := pkgerrors.Cause(c.err).(x509.CertificateInvalidError); ok && err.Reason == x509.Expired {
172+
c.err = ErrSwarmCertificatesExpired
173+
return c, nil
174+
}
166175
return nil, fmt.Errorf("swarm component could not be started: %v", c.err)
167176
}
168177
go c.reconnectOnFailure(n)
@@ -517,41 +526,46 @@ func (c *Cluster) Leave(force bool) error {
517526
c.Lock()
518527
node := c.node
519528
if node == nil {
520-
c.Unlock()
521-
return ErrNoSwarm
522-
}
523-
524-
if node.Manager() != nil && !force {
525-
msg := "You are attempting to leave the swarm on a node that is participating as a manager. "
526-
if c.isActiveManager() {
527-
active, reachable, unreachable, err := c.managerStats()
528-
if err == nil {
529-
if active && reachable-2 <= unreachable {
530-
if reachable == 1 && unreachable == 0 {
531-
msg += "Removing the last manager erases all current state of the swarm. Use `--force` to ignore this message. "
532-
c.Unlock()
533-
return fmt.Errorf(msg)
529+
if c.err == ErrSwarmCertificatesExpired {
530+
c.err = nil
531+
c.Unlock()
532+
} else {
533+
c.Unlock()
534+
return ErrNoSwarm
535+
}
536+
} else {
537+
if node.Manager() != nil && !force {
538+
msg := "You are attempting to leave the swarm on a node that is participating as a manager. "
539+
if c.isActiveManager() {
540+
active, reachable, unreachable, err := c.managerStats()
541+
if err == nil {
542+
if active && reachable-2 <= unreachable {
543+
if reachable == 1 && unreachable == 0 {
544+
msg += "Removing the last manager erases all current state of the swarm. Use `--force` to ignore this message. "
545+
c.Unlock()
546+
return fmt.Errorf(msg)
547+
}
548+
msg += fmt.Sprintf("Removing this node leaves %v managers out of %v. Without a Raft quorum your swarm will be inaccessible. ", reachable-1, reachable+unreachable)
534549
}
535-
msg += fmt.Sprintf("Removing this node leaves %v managers out of %v. Without a Raft quorum your swarm will be inaccessible. ", reachable-1, reachable+unreachable)
536550
}
551+
} else {
552+
msg += "Doing so may lose the consensus of your cluster. "
537553
}
538-
} else {
539-
msg += "Doing so may lose the consensus of your cluster. "
540-
}
541554

542-
msg += "The only way to restore a swarm that has lost consensus is to reinitialize it with `--force-new-cluster`. Use `--force` to suppress this message."
543-
c.Unlock()
544-
return fmt.Errorf(msg)
545-
}
546-
if err := c.stopNode(); err != nil {
555+
msg += "The only way to restore a swarm that has lost consensus is to reinitialize it with `--force-new-cluster`. Use `--force` to suppress this message."
556+
c.Unlock()
557+
return fmt.Errorf(msg)
558+
}
559+
if err := c.stopNode(); err != nil {
560+
c.Unlock()
561+
return err
562+
}
547563
c.Unlock()
548-
return err
549-
}
550-
c.Unlock()
551-
if nodeID := node.NodeID(); nodeID != "" {
552-
for _, id := range c.config.Backend.ListContainersForNode(nodeID) {
553-
if err := c.config.Backend.ContainerRm(id, &apitypes.ContainerRmConfig{ForceRemove: true}); err != nil {
554-
logrus.Errorf("error removing %v: %v", id, err)
564+
if nodeID := node.NodeID(); nodeID != "" {
565+
for _, id := range c.config.Backend.ListContainersForNode(nodeID) {
566+
if err := c.config.Backend.ContainerRm(id, &apitypes.ContainerRmConfig{ForceRemove: true}); err != nil {
567+
logrus.Errorf("error removing %v: %v", id, err)
568+
}
555569
}
556570
}
557571
}
@@ -724,6 +738,9 @@ func (c *Cluster) Info() types.Info {
724738
if c.cancelDelay != nil {
725739
info.LocalNodeState = types.LocalNodeStateError
726740
}
741+
if c.err == ErrSwarmCertificatesExpired {
742+
info.LocalNodeState = types.LocalNodeStateError
743+
}
727744
} else {
728745
info.LocalNodeState = types.LocalNodeStatePending
729746
if c.ready == true {
@@ -776,6 +793,9 @@ func (c *Cluster) isActiveManager() bool {
776793
// Call with read lock.
777794
func (c *Cluster) errNoManager() error {
778795
if c.node == nil {
796+
if c.err == ErrSwarmCertificatesExpired {
797+
return ErrSwarmCertificatesExpired
798+
}
779799
return fmt.Errorf("This node is not a swarm manager. Use \"docker swarm init\" or \"docker swarm join\" to connect this node to swarm and try again.")
780800
}
781801
if c.node.Manager() != nil {

0 commit comments

Comments
 (0)