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{
|
|
|
|
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() {
|
|
|
|
r := &retryJoiner{
|
|
|
|
cluster: "WAN",
|
2017-09-25 18:40:42 +00:00
|
|
|
addrs: a.config.RetryJoinWAN,
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-01-28 23:50:41 +00:00
|
|
|
func retryJoinAddrs(disco *discover.Discover, 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-01-28 23:50:41 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
// 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)
|
|
|
|
|
|
|
|
// logger is the agent logger. Log messages should contain the
|
|
|
|
// "agent: " prefix.
|
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-01-28 23:50:41 +00:00
|
|
|
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 {
|
2019-06-27 20:22:07 +00:00
|
|
|
addrs := retryJoinAddrs(disco, 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-01-28 23:50:41 +00:00
|
|
|
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 {
|
2017-07-15 21:15:59 +00:00
|
|
|
err = fmt.Errorf("No servers to join")
|
|
|
|
}
|
|
|
|
|
2017-06-23 08:38:55 +00:00
|
|
|
attempt++
|
2017-08-19 08:44:19 +00:00
|
|
|
if r.maxAttempts > 0 && attempt > r.maxAttempts {
|
|
|
|
return fmt.Errorf("agent: max join %s retry exhausted, exiting", r.cluster)
|
2017-06-02 09:55:29 +00:00
|
|
|
}
|
|
|
|
|
2020-01-28 23:50:41 +00:00
|
|
|
r.logger.Warn("Join cluster failed, will retry",
|
|
|
|
"retry_interval", r.interval,
|
|
|
|
"error", err,
|
|
|
|
)
|
2017-08-19 08:44:19 +00:00
|
|
|
time.Sleep(r.interval)
|
2017-06-02 09:55:29 +00:00
|
|
|
}
|
|
|
|
}
|