tsconsensus,cmd/natc: add 'follower only' bootstrap option

Currently consensus has a bootstrap routine where a tsnet node tries to
join each other node with the cluster tag, and if it is not able to join
any other node it starts its own cluster.

That algorithm is racy, and can result in split brain (more than one
leader/cluster) if all the nodes for a cluster are started at the same
time.

Add a FollowOnly argument to the bootstrap function. If provided this
tsnet node will never lead, it will try (and retry with exponential back
off) to follow any node it can contact.

Add a --follow-only flag to cmd/natc that uses this new tsconsensus
functionality.

Also slightly reorganize some arguments into opts structs.

Updates #14667

Signed-off-by: Fran Bull <fran@tailscale.com>
This commit is contained in:
Fran Bull
2025-08-06 07:43:58 -07:00
committed by franbull
parent d4b7200129
commit d986baa18f
4 changed files with 121 additions and 44 deletions

View File

@@ -149,12 +149,21 @@ func (ipp *ConsensusIPPool) domainLookup(from tailcfg.NodeID, addr netip.Addr) (
return ww, true
}
type ClusterOpts struct {
Tag string
StateDir string
FollowOnly bool
}
// StartConsensus is part of the IPPool interface. It starts the raft background routines that handle consensus.
func (ipp *ConsensusIPPool) StartConsensus(ctx context.Context, ts *tsnet.Server, clusterTag string, clusterStateDir string) error {
func (ipp *ConsensusIPPool) StartConsensus(ctx context.Context, ts *tsnet.Server, opts ClusterOpts) error {
cfg := tsconsensus.DefaultConfig()
cfg.ServeDebugMonitor = true
cfg.StateDirPath = clusterStateDir
cns, err := tsconsensus.Start(ctx, ts, ipp, clusterTag, cfg)
cfg.StateDirPath = opts.StateDir
cns, err := tsconsensus.Start(ctx, ts, ipp, tsconsensus.BootstrapOpts{
Tag: opts.Tag,
FollowOnly: opts.FollowOnly,
}, cfg)
if err != nil {
return err
}