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-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-12 00:38:52 +00:00
|
|
|
func (d *AutopilotDelegate) FetchStats(ctx context.Context, servers []*metadata.Server) map[string]*autopilot.ServerStats {
|
|
|
|
return d.server.statsFetcher.Fetch(ctx, servers)
|
2017-03-01 22:04:40 +00:00
|
|
|
}
|
|
|
|
|
2017-12-12 00:38:52 +00:00
|
|
|
func (d *AutopilotDelegate) GetOrCreateAutopilotConfig() (*autopilot.Config, bool) {
|
|
|
|
return d.server.getOrCreateAutopilotConfig()
|
2017-03-16 01:27:17 +00:00
|
|
|
}
|
2017-03-10 00:43:07 +00:00
|
|
|
|
2017-12-12 00:38:52 +00:00
|
|
|
func (d *AutopilotDelegate) Raft() *raft.Raft {
|
|
|
|
return d.server.raft
|
2017-03-10 00:43:07 +00:00
|
|
|
}
|
|
|
|
|
2017-12-12 00:38:52 +00:00
|
|
|
func (d *AutopilotDelegate) Serf() *serf.Serf {
|
|
|
|
return d.server.serfLAN
|
2017-03-01 22:04:40 +00:00
|
|
|
}
|
|
|
|
|
2017-12-12 00:38:52 +00:00
|
|
|
func (d *AutopilotDelegate) NumPeers() (int, error) {
|
|
|
|
return d.server.numPeers()
|
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
|
|
|
}
|