2017-06-02 09:55:29 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-07-15 21:15:59 +00:00
|
|
|
"strings"
|
2017-06-02 09:55:29 +00:00
|
|
|
"time"
|
2017-07-15 21:15:59 +00:00
|
|
|
|
2018-05-04 19:11:12 +00:00
|
|
|
"github.com/hashicorp/consul/lib"
|
2017-07-15 21:15:59 +00:00
|
|
|
discover "github.com/hashicorp/go-discover"
|
2018-09-05 20:38:13 +00:00
|
|
|
discoverk8s "github.com/hashicorp/go-discover/provider/k8s"
|
2020-01-28 23:50:41 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
2017-06-02 09:55:29 +00:00
|
|
|
)
|
|
|
|
|
2017-08-19 08:44:19 +00:00
|
|
|
func (a *Agent) retryJoinLAN() {
|
|
|
|
r := &retryJoiner{
|
2020-03-09 20:59:02 +00:00
|
|
|
variant: retryJoinSerfVariant,
|
2017-08-19 08:44:19 +00:00
|
|
|
cluster: "LAN",
|
2017-09-25 18:40:42 +00:00
|
|
|
addrs: a.config.RetryJoinLAN,
|
|
|
|
maxAttempts: a.config.RetryJoinMaxAttemptsLAN,
|
|
|
|
interval: a.config.RetryJoinIntervalLAN,
|
2017-08-19 08:44:19 +00:00
|
|
|
join: a.JoinLAN,
|
2020-01-28 23:50:41 +00:00
|
|
|
logger: a.logger.With("cluster", "LAN"),
|
2017-08-19 08:44:19 +00:00
|
|
|
}
|
|
|
|
if err := r.retryJoin(); err != nil {
|
|
|
|
a.retryJoinCh <- err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Agent) retryJoinWAN() {
|
2020-03-09 20:59:02 +00:00
|
|
|
if !a.config.ServerMode {
|
|
|
|
a.logger.Warn("(WAN) couldn't join: Err: Must be a server to join WAN cluster")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
isPrimary := a.config.PrimaryDatacenter == a.config.Datacenter
|
|
|
|
|
|
|
|
var joinAddrs []string
|
|
|
|
if a.config.ConnectMeshGatewayWANFederationEnabled {
|
|
|
|
// When wanfed is activated each datacenter 100% relies upon flood-join
|
|
|
|
// to replicate the LAN members in a dc into the WAN pool. We
|
|
|
|
// completely hijack whatever the user configured to correctly
|
|
|
|
// implement the star-join.
|
|
|
|
//
|
|
|
|
// Elsewhere we enforce that start-join-wan and retry-join-wan cannot
|
|
|
|
// be set if wanfed is enabled so we don't have to emit any warnings
|
|
|
|
// related to that here.
|
|
|
|
|
|
|
|
if isPrimary {
|
|
|
|
// Wanfed requires that secondaries join TO the primary and the
|
|
|
|
// primary doesn't explicitly join down to the secondaries, so as
|
|
|
|
// such in the primary a retry-join operation is a no-op.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// First get a handle on dialing the primary
|
|
|
|
a.refreshPrimaryGatewayFallbackAddresses()
|
|
|
|
|
|
|
|
// Then "retry join" a special address via the gateway which is
|
|
|
|
// load balanced to all servers in the primary datacenter
|
|
|
|
//
|
|
|
|
// Since this address is merely a placeholder we use an address from the
|
|
|
|
// TEST-NET-1 block as described in https://tools.ietf.org/html/rfc5735#section-3
|
|
|
|
const placeholderIPAddress = "192.0.2.2"
|
|
|
|
joinAddrs = []string{
|
|
|
|
fmt.Sprintf("*.%s/%s", a.config.PrimaryDatacenter, placeholderIPAddress),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
joinAddrs = a.config.RetryJoinWAN
|
|
|
|
}
|
|
|
|
|
2017-08-19 08:44:19 +00:00
|
|
|
r := &retryJoiner{
|
2020-03-09 20:59:02 +00:00
|
|
|
variant: retryJoinSerfVariant,
|
2017-08-19 08:44:19 +00:00
|
|
|
cluster: "WAN",
|
2020-03-09 20:59:02 +00:00
|
|
|
addrs: joinAddrs,
|
2017-09-25 18:40:42 +00:00
|
|
|
maxAttempts: a.config.RetryJoinMaxAttemptsWAN,
|
|
|
|
interval: a.config.RetryJoinIntervalWAN,
|
2017-08-19 08:44:19 +00:00
|
|
|
join: a.JoinWAN,
|
2020-01-28 23:50:41 +00:00
|
|
|
logger: a.logger.With("cluster", "WAN"),
|
2017-08-19 08:44:19 +00:00
|
|
|
}
|
|
|
|
if err := r.retryJoin(); err != nil {
|
|
|
|
a.retryJoinCh <- err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-09 20:59:02 +00:00
|
|
|
func (a *Agent) refreshPrimaryGatewayFallbackAddresses() {
|
|
|
|
r := &retryJoiner{
|
|
|
|
variant: retryJoinMeshGatewayVariant,
|
|
|
|
cluster: "primary",
|
|
|
|
addrs: a.config.PrimaryGateways,
|
|
|
|
maxAttempts: 0,
|
|
|
|
interval: a.config.PrimaryGatewaysInterval,
|
|
|
|
join: func(addrs []string) (int, error) {
|
|
|
|
if err := a.RefreshPrimaryGatewayFallbackAddresses(addrs); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return len(addrs), nil
|
|
|
|
},
|
|
|
|
logger: a.logger,
|
|
|
|
stopCh: a.PrimaryMeshGatewayAddressesReadyCh(),
|
|
|
|
}
|
|
|
|
if err := r.retryJoin(); err != nil {
|
|
|
|
a.retryJoinCh <- err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-27 20:22:07 +00:00
|
|
|
func newDiscover() (*discover.Discover, error) {
|
|
|
|
providers := make(map[string]discover.Provider)
|
|
|
|
for k, v := range discover.Providers {
|
|
|
|
providers[k] = v
|
|
|
|
}
|
|
|
|
providers["k8s"] = &discoverk8s.Provider{}
|
|
|
|
|
|
|
|
return discover.New(
|
|
|
|
discover.WithUserAgent(lib.UserAgent()),
|
|
|
|
discover.WithProviders(providers),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-03-09 20:59:02 +00:00
|
|
|
func retryJoinAddrs(disco *discover.Discover, variant, cluster string, retryJoin []string, logger hclog.Logger) []string {
|
2019-06-27 20:22:07 +00:00
|
|
|
addrs := []string{}
|
|
|
|
if disco == nil {
|
|
|
|
return addrs
|
|
|
|
}
|
|
|
|
for _, addr := range retryJoin {
|
|
|
|
switch {
|
|
|
|
case strings.Contains(addr, "provider="):
|
2020-01-28 23:50:41 +00:00
|
|
|
servers, err := disco.Addrs(addr, logger.StandardLogger(&hclog.StandardLoggerOptions{
|
|
|
|
InferLevels: true,
|
|
|
|
}))
|
2019-06-27 20:22:07 +00:00
|
|
|
if err != nil {
|
|
|
|
if logger != nil {
|
2020-01-28 23:50:41 +00:00
|
|
|
logger.Error("Cannot discover address",
|
|
|
|
"address", addr,
|
|
|
|
"error", err,
|
|
|
|
)
|
2019-06-27 20:22:07 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
addrs = append(addrs, servers...)
|
|
|
|
if logger != nil {
|
2020-03-09 20:59:02 +00:00
|
|
|
if variant == retryJoinMeshGatewayVariant {
|
|
|
|
logger.Info("Discovered mesh gateways",
|
|
|
|
"cluster", cluster,
|
|
|
|
"mesh_gateways", strings.Join(servers, " "),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
logger.Info("Discovered servers",
|
|
|
|
"cluster", cluster,
|
|
|
|
"servers", strings.Join(servers, " "),
|
|
|
|
)
|
|
|
|
}
|
2019-06-27 20:22:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
addrs = append(addrs, addr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return addrs
|
|
|
|
}
|
|
|
|
|
2020-03-09 20:59:02 +00:00
|
|
|
const (
|
|
|
|
retryJoinSerfVariant = "serf"
|
|
|
|
retryJoinMeshGatewayVariant = "mesh-gateway"
|
|
|
|
)
|
|
|
|
|
2017-08-19 08:44:19 +00:00
|
|
|
// retryJoiner is used to handle retrying a join until it succeeds or all
|
2017-06-02 09:55:29 +00:00
|
|
|
// retries are exhausted.
|
2017-08-19 08:44:19 +00:00
|
|
|
type retryJoiner struct {
|
2020-03-09 20:59:02 +00:00
|
|
|
// variant is either "serf" or "mesh-gateway" and just adjusts the log messaging
|
|
|
|
// emitted
|
|
|
|
variant string
|
|
|
|
|
2017-08-19 08:44:19 +00:00
|
|
|
// cluster is the name of the serf cluster, e.g. "LAN" or "WAN".
|
|
|
|
cluster string
|
|
|
|
|
|
|
|
// addrs is the list of servers or go-discover configurations
|
|
|
|
// to join with.
|
|
|
|
addrs []string
|
|
|
|
|
|
|
|
// maxAttempts is the number of join attempts before giving up.
|
|
|
|
maxAttempts int
|
|
|
|
|
|
|
|
// interval is the time between two join attempts.
|
|
|
|
interval time.Duration
|
|
|
|
|
|
|
|
// join adds the discovered or configured servers to the given
|
|
|
|
// serf cluster.
|
|
|
|
join func([]string) (int, error)
|
|
|
|
|
2020-03-09 20:59:02 +00:00
|
|
|
// stopCh is an optional stop channel to exit the retry loop early
|
|
|
|
stopCh <-chan struct{}
|
|
|
|
|
|
|
|
// logger is the agent logger.
|
2020-01-28 23:50:41 +00:00
|
|
|
logger hclog.Logger
|
2017-08-19 08:44:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *retryJoiner) retryJoin() error {
|
|
|
|
if len(r.addrs) == 0 {
|
|
|
|
return nil
|
2017-06-23 08:38:55 +00:00
|
|
|
}
|
2017-06-02 09:55:29 +00:00
|
|
|
|
2019-06-27 20:22:07 +00:00
|
|
|
disco, err := newDiscover()
|
2018-05-04 19:11:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-09 20:59:02 +00:00
|
|
|
if r.variant == retryJoinMeshGatewayVariant {
|
|
|
|
r.logger.Info("Refreshing mesh gateways is supported for the following discovery methods",
|
|
|
|
"discovery_methods", strings.Join(disco.Names(), " "),
|
|
|
|
)
|
|
|
|
r.logger.Info("Refreshing mesh gateways...")
|
|
|
|
} else {
|
|
|
|
r.logger.Info("Retry join is supported for the following discovery methods",
|
|
|
|
"discovery_methods", strings.Join(disco.Names(), " "),
|
|
|
|
)
|
|
|
|
r.logger.Info("Joining cluster...")
|
|
|
|
}
|
|
|
|
|
2017-06-23 08:38:55 +00:00
|
|
|
attempt := 0
|
2017-06-02 09:55:29 +00:00
|
|
|
for {
|
2020-03-09 20:59:02 +00:00
|
|
|
addrs := retryJoinAddrs(disco, r.variant, r.cluster, r.addrs, r.logger)
|
2017-07-15 21:15:59 +00:00
|
|
|
if len(addrs) > 0 {
|
2017-08-19 08:44:19 +00:00
|
|
|
n, err := r.join(addrs)
|
2017-06-23 08:38:55 +00:00
|
|
|
if err == nil {
|
2020-03-09 20:59:02 +00:00
|
|
|
if r.variant == retryJoinMeshGatewayVariant {
|
|
|
|
r.logger.Info("Refreshing mesh gateways completed")
|
|
|
|
} else {
|
|
|
|
r.logger.Info("Join cluster completed. Synced with initial agents", "num_agents", n)
|
|
|
|
}
|
2017-08-19 08:44:19 +00:00
|
|
|
return nil
|
2017-06-23 08:38:55 +00:00
|
|
|
}
|
2019-06-27 20:22:07 +00:00
|
|
|
} else if len(addrs) == 0 {
|
2020-03-09 20:59:02 +00:00
|
|
|
if r.variant == retryJoinMeshGatewayVariant {
|
|
|
|
err = fmt.Errorf("No mesh gateways found")
|
|
|
|
} else {
|
|
|
|
err = fmt.Errorf("No servers to join")
|
|
|
|
}
|
2017-07-15 21:15:59 +00:00
|
|
|
}
|
|
|
|
|
2017-06-23 08:38:55 +00:00
|
|
|
attempt++
|
2017-08-19 08:44:19 +00:00
|
|
|
if r.maxAttempts > 0 && attempt > r.maxAttempts {
|
2020-03-09 20:59:02 +00:00
|
|
|
if r.variant == retryJoinMeshGatewayVariant {
|
|
|
|
return fmt.Errorf("agent: max refresh of %s mesh gateways retry exhausted, exiting", r.cluster)
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("agent: max join %s retry exhausted, exiting", r.cluster)
|
|
|
|
}
|
2017-06-02 09:55:29 +00:00
|
|
|
}
|
|
|
|
|
2020-03-09 20:59:02 +00:00
|
|
|
if r.variant == retryJoinMeshGatewayVariant {
|
|
|
|
r.logger.Warn("Refreshing mesh gateways failed, will retry",
|
|
|
|
"retry_interval", r.interval,
|
|
|
|
"error", err,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
r.logger.Warn("Join cluster failed, will retry",
|
|
|
|
"retry_interval", r.interval,
|
|
|
|
"error", err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(r.interval):
|
|
|
|
case <-r.stopCh:
|
|
|
|
return nil
|
|
|
|
}
|
2017-06-02 09:55:29 +00:00
|
|
|
}
|
|
|
|
}
|