2017-06-02 09:55:29 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-08-19 08:44:19 +00:00
|
|
|
"log"
|
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"
|
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,
|
|
|
|
logger: a.logger,
|
|
|
|
}
|
|
|
|
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,
|
|
|
|
logger: a.logger,
|
|
|
|
}
|
|
|
|
if err := r.retryJoin(); err != nil {
|
|
|
|
a.retryJoinCh <- err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
logger *log.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2018-09-05 20:38:13 +00:00
|
|
|
// Copy the default providers, and then add the non-default
|
|
|
|
providers := make(map[string]discover.Provider)
|
|
|
|
for k, v := range discover.Providers {
|
|
|
|
providers[k] = v
|
|
|
|
}
|
|
|
|
providers["k8s"] = &discoverk8s.Provider{}
|
|
|
|
|
|
|
|
disco, err := discover.New(
|
|
|
|
discover.WithUserAgent(lib.UserAgent()),
|
|
|
|
discover.WithProviders(providers),
|
|
|
|
)
|
2018-05-04 19:11:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-08-19 08:44:19 +00:00
|
|
|
r.logger.Printf("[INFO] agent: Retry join %s is supported for: %s", r.cluster, strings.Join(disco.Names(), " "))
|
|
|
|
r.logger.Printf("[INFO] agent: Joining %s cluster...", r.cluster)
|
2017-06-23 08:38:55 +00:00
|
|
|
attempt := 0
|
2017-06-02 09:55:29 +00:00
|
|
|
for {
|
2017-07-15 21:15:59 +00:00
|
|
|
var addrs []string
|
2017-06-23 08:38:55 +00:00
|
|
|
var err error
|
2017-07-15 21:15:59 +00:00
|
|
|
|
2017-08-19 08:44:19 +00:00
|
|
|
for _, addr := range r.addrs {
|
2017-07-15 21:15:59 +00:00
|
|
|
switch {
|
|
|
|
case strings.Contains(addr, "provider="):
|
2017-08-19 08:44:19 +00:00
|
|
|
servers, err := disco.Addrs(addr, r.logger)
|
2017-07-15 21:15:59 +00:00
|
|
|
if err != nil {
|
2017-08-19 08:44:19 +00:00
|
|
|
r.logger.Printf("[ERR] agent: Join %s: %s", r.cluster, err)
|
2017-07-15 21:15:59 +00:00
|
|
|
} else {
|
|
|
|
addrs = append(addrs, servers...)
|
2017-08-19 08:44:19 +00:00
|
|
|
r.logger.Printf("[INFO] agent: Discovered %s servers: %s", r.cluster, strings.Join(servers, " "))
|
2017-07-15 21:15:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
addrs = append(addrs, addr)
|
2017-06-23 08:38:55 +00:00
|
|
|
}
|
2017-06-21 08:40:38 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2017-08-19 08:44:19 +00:00
|
|
|
r.logger.Printf("[INFO] agent: Join %s completed. Synced with %d initial agents", r.cluster, n)
|
|
|
|
return nil
|
2017-06-23 08:38:55 +00:00
|
|
|
}
|
2017-06-02 09:55:29 +00:00
|
|
|
}
|
|
|
|
|
2017-07-15 21:15:59 +00:00
|
|
|
if len(addrs) == 0 {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-08-19 08:44:19 +00:00
|
|
|
r.logger.Printf("[WARN] agent: Join %s failed: %v, retrying in %v", r.cluster, err, r.interval)
|
|
|
|
time.Sleep(r.interval)
|
2017-06-02 09:55:29 +00:00
|
|
|
}
|
|
|
|
}
|