2017-12-18 21:16:23 +00:00
|
|
|
package nomad
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/armon/go-metrics"
|
|
|
|
"github.com/hashicorp/consul/agent/consul/autopilot"
|
|
|
|
"github.com/hashicorp/raft"
|
|
|
|
"github.com/hashicorp/serf/serf"
|
|
|
|
)
|
|
|
|
|
2018-01-30 03:53:34 +00:00
|
|
|
const (
|
2018-01-30 23:14:03 +00:00
|
|
|
// AutopilotRZTag is the Serf tag to use for the redundancy zone value
|
|
|
|
// when passing the server metadata to Autopilot.
|
|
|
|
AutopilotRZTag = "ap_zone"
|
|
|
|
|
|
|
|
// AutopilotRZTag is the Serf tag to use for the custom version value
|
|
|
|
// when passing the server metadata to Autopilot.
|
2018-01-30 03:53:34 +00:00
|
|
|
AutopilotVersionTag = "ap_version"
|
|
|
|
)
|
|
|
|
|
2018-01-16 21:35:32 +00:00
|
|
|
// AutopilotDelegate is a Nomad delegate for autopilot operations.
|
2017-12-18 21:16:23 +00:00
|
|
|
type AutopilotDelegate struct {
|
|
|
|
server *Server
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *AutopilotDelegate) AutopilotConfig() *autopilot.Config {
|
2018-01-30 03:53:34 +00:00
|
|
|
c := d.server.getOrCreateAutopilotConfig()
|
|
|
|
if c == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
conf := &autopilot.Config{
|
|
|
|
CleanupDeadServers: c.CleanupDeadServers,
|
|
|
|
LastContactThreshold: c.LastContactThreshold,
|
|
|
|
MaxTrailingLogs: c.MaxTrailingLogs,
|
|
|
|
ServerStabilizationTime: c.ServerStabilizationTime,
|
|
|
|
DisableUpgradeMigration: c.DisableUpgradeMigration,
|
|
|
|
ModifyIndex: c.ModifyIndex,
|
|
|
|
CreateIndex: c.CreateIndex,
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.EnableRedundancyZones {
|
|
|
|
conf.RedundancyZoneTag = AutopilotRZTag
|
|
|
|
}
|
|
|
|
if c.EnableCustomUpgrades {
|
|
|
|
conf.UpgradeVersionTag = AutopilotVersionTag
|
|
|
|
}
|
|
|
|
|
|
|
|
return conf
|
2017-12-18 21:16:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *AutopilotDelegate) FetchStats(ctx context.Context, servers []serf.Member) map[string]*autopilot.ServerStats {
|
|
|
|
return d.server.statsFetcher.Fetch(ctx, servers)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *AutopilotDelegate) IsServer(m serf.Member) (*autopilot.ServerInfo, error) {
|
|
|
|
ok, parts := isNomadServer(m)
|
|
|
|
if !ok || parts.Region != d.server.Region() {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
server := &autopilot.ServerInfo{
|
|
|
|
Name: m.Name,
|
|
|
|
ID: parts.ID,
|
|
|
|
Addr: parts.Addr,
|
|
|
|
Build: parts.Build,
|
|
|
|
Status: m.Status,
|
|
|
|
}
|
|
|
|
return server, nil
|
|
|
|
}
|
|
|
|
|
2018-01-16 21:35:32 +00:00
|
|
|
// NotifyHealth heartbeats a metric for monitoring if we're the leader.
|
2017-12-18 21:16:23 +00:00
|
|
|
func (d *AutopilotDelegate) NotifyHealth(health autopilot.OperatorHealthReply) {
|
|
|
|
if d.server.raft.State() == raft.Leader {
|
|
|
|
metrics.SetGauge([]string{"nomad", "autopilot", "failure_tolerance"}, float32(health.FailureTolerance))
|
|
|
|
if health.Healthy {
|
|
|
|
metrics.SetGauge([]string{"nomad", "autopilot", "healthy"}, 1)
|
|
|
|
} else {
|
|
|
|
metrics.SetGauge([]string{"nomad", "autopilot", "healthy"}, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
return autopilot.PromoteStableServers(conf, health, future.Configuration().Servers), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *AutopilotDelegate) Raft() *raft.Raft {
|
|
|
|
return d.server.raft
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *AutopilotDelegate) Serf() *serf.Serf {
|
|
|
|
return d.server.serf
|
|
|
|
}
|