2017-03-01 22:04:40 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
2017-03-20 03:48:42 +00:00
|
|
|
"context"
|
2017-03-01 22:04:40 +00:00
|
|
|
"fmt"
|
2017-12-13 01:45:03 +00:00
|
|
|
"net"
|
|
|
|
"strconv"
|
2017-03-01 22:04:40 +00:00
|
|
|
|
2017-12-13 01:45:03 +00:00
|
|
|
"github.com/armon/go-metrics"
|
2017-12-12 00:38:52 +00:00
|
|
|
"github.com/hashicorp/consul/agent/consul/autopilot"
|
2017-07-06 10:48:37 +00:00
|
|
|
"github.com/hashicorp/consul/agent/metadata"
|
2017-03-01 22:04:40 +00:00
|
|
|
"github.com/hashicorp/raft"
|
|
|
|
"github.com/hashicorp/serf/serf"
|
|
|
|
)
|
|
|
|
|
2017-12-12 00:38:52 +00:00
|
|
|
// AutopilotDelegate is a Consul delegate for autopilot operations.
|
|
|
|
type AutopilotDelegate struct {
|
2017-03-08 19:31:32 +00:00
|
|
|
server *Server
|
|
|
|
}
|
|
|
|
|
2017-12-13 01:45:03 +00:00
|
|
|
func (d *AutopilotDelegate) AutopilotConfig() *autopilot.Config {
|
2017-12-12 00:38:52 +00:00
|
|
|
return d.server.getOrCreateAutopilotConfig()
|
2017-03-16 01:27:17 +00:00
|
|
|
}
|
2017-03-10 00:43:07 +00:00
|
|
|
|
2017-12-13 01:45:03 +00:00
|
|
|
func (d *AutopilotDelegate) FetchStats(ctx context.Context, servers []serf.Member) map[string]*autopilot.ServerStats {
|
|
|
|
return d.server.statsFetcher.Fetch(ctx, servers)
|
2017-03-10 00:43:07 +00:00
|
|
|
}
|
|
|
|
|
2017-12-13 18:57:37 +00:00
|
|
|
func (d *AutopilotDelegate) IsServer(m serf.Member) (*autopilot.ServerInfo, error) {
|
2017-12-13 01:45:03 +00:00
|
|
|
if m.Tags["role"] != "consul" {
|
2017-12-13 18:57:37 +00:00
|
|
|
return nil, nil
|
2017-12-13 01:45:03 +00:00
|
|
|
}
|
|
|
|
|
2018-01-28 18:40:13 +00:00
|
|
|
portStr := m.Tags["port"]
|
|
|
|
port, err := strconv.Atoi(portStr)
|
2017-12-13 01:45:03 +00:00
|
|
|
if err != nil {
|
2017-12-13 18:57:37 +00:00
|
|
|
return nil, err
|
2017-12-13 01:45:03 +00:00
|
|
|
}
|
|
|
|
|
2018-01-28 18:40:13 +00:00
|
|
|
buildVersion, err := metadata.Build(&m)
|
2017-12-13 01:45:03 +00:00
|
|
|
if err != nil {
|
2017-12-13 18:57:37 +00:00
|
|
|
return nil, err
|
2017-12-13 01:45:03 +00:00
|
|
|
}
|
|
|
|
|
2017-12-13 18:57:37 +00:00
|
|
|
server := &autopilot.ServerInfo{
|
2017-12-13 01:45:03 +00:00
|
|
|
Name: m.Name,
|
|
|
|
ID: m.Tags["id"],
|
|
|
|
Addr: &net.TCPAddr{IP: m.Addr, Port: port},
|
2018-01-28 18:40:13 +00:00
|
|
|
Build: *buildVersion,
|
2017-12-13 01:45:03 +00:00
|
|
|
Status: m.Status,
|
|
|
|
}
|
2017-12-13 18:57:37 +00:00
|
|
|
return server, nil
|
2017-03-01 22:04:40 +00:00
|
|
|
}
|
|
|
|
|
2017-12-13 01:45:03 +00:00
|
|
|
// Heartbeat a metric for monitoring if we're the leader
|
|
|
|
func (d *AutopilotDelegate) NotifyHealth(health autopilot.OperatorHealthReply) {
|
|
|
|
if d.server.raft.State() == raft.Leader {
|
|
|
|
metrics.SetGauge([]string{"autopilot", "failure_tolerance"}, float32(health.FailureTolerance))
|
|
|
|
if health.Healthy {
|
|
|
|
metrics.SetGauge([]string{"autopilot", "healthy"}, 1)
|
|
|
|
} else {
|
|
|
|
metrics.SetGauge([]string{"autopilot", "healthy"}, 0)
|
|
|
|
}
|
|
|
|
}
|
2017-03-15 23:09:55 +00:00
|
|
|
}
|
|
|
|
|
2017-12-12 00:38:52 +00:00
|
|
|
func (d *AutopilotDelegate) PromoteNonVoters(conf *autopilot.Config, health autopilot.OperatorHealthReply) ([]raft.Server, error) {
|
|
|
|
future := d.server.raft.GetConfiguration()
|
|
|
|
if err := future.Error(); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to get raft configuration: %v", err)
|
2017-03-01 22:04:40 +00:00
|
|
|
}
|
2017-03-21 23:36:44 +00:00
|
|
|
|
2017-12-12 00:38:52 +00:00
|
|
|
return autopilot.PromoteStableServers(conf, health, future.Configuration().Servers), nil
|
2017-03-21 23:36:44 +00:00
|
|
|
}
|
2017-12-13 01:45:03 +00:00
|
|
|
|
|
|
|
func (d *AutopilotDelegate) Raft() *raft.Raft {
|
|
|
|
return d.server.raft
|
|
|
|
}
|
|
|
|
|
2019-06-28 17:40:07 +00:00
|
|
|
func (d *AutopilotDelegate) SerfLAN() *serf.Serf {
|
2017-12-13 01:45:03 +00:00
|
|
|
return d.server.serfLAN
|
|
|
|
}
|
2019-06-28 17:40:07 +00:00
|
|
|
|
|
|
|
func (d *AutopilotDelegate) SerfWAN() *serf.Serf {
|
|
|
|
return d.server.serfWAN
|
|
|
|
}
|