2017-03-30 19:35:50 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2020-09-25 17:46:38 +00:00
|
|
|
autopilot "github.com/hashicorp/raft-autopilot"
|
|
|
|
"github.com/hashicorp/serf/serf"
|
2021-04-08 22:58:15 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/acl"
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2017-03-30 19:35:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// AutopilotGetConfiguration is used to retrieve the current Autopilot configuration.
|
2020-09-25 17:46:38 +00:00
|
|
|
func (op *Operator) AutopilotGetConfiguration(args *structs.DCSpecificRequest, reply *structs.AutopilotConfig) error {
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := op.srv.ForwardRPC("Operator.AutopilotGetConfiguration", args, reply); done {
|
2017-03-30 19:35:50 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// This action requires operator read access.
|
2021-08-04 21:51:19 +00:00
|
|
|
identity, authz, err := op.srv.acls.ResolveTokenToIdentityAndAuthorizer(args.Token)
|
2017-03-30 19:35:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-04 20:58:56 +00:00
|
|
|
if err := op.srv.validateEnterpriseToken(identity); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-08-04 21:51:19 +00:00
|
|
|
if authz.OperatorRead(nil) != acl.Allow {
|
2020-09-25 17:46:38 +00:00
|
|
|
return acl.PermissionDenied("Missing operator:read permissions")
|
2017-03-30 19:35:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
state := op.srv.fsm.State()
|
|
|
|
_, config, err := state.AutopilotConfig()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-04-13 17:43:07 +00:00
|
|
|
if config == nil {
|
|
|
|
return fmt.Errorf("autopilot config not initialized yet")
|
|
|
|
}
|
2017-03-30 19:35:50 +00:00
|
|
|
|
|
|
|
*reply = *config
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AutopilotSetConfiguration is used to set the current Autopilot configuration.
|
|
|
|
func (op *Operator) AutopilotSetConfiguration(args *structs.AutopilotSetConfigRequest, reply *bool) error {
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := op.srv.ForwardRPC("Operator.AutopilotSetConfiguration", args, reply); done {
|
2017-03-30 19:35:50 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// This action requires operator write access.
|
2021-08-04 21:51:19 +00:00
|
|
|
identity, authz, err := op.srv.acls.ResolveTokenToIdentityAndAuthorizer(args.Token)
|
2017-03-30 19:35:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-04 20:58:56 +00:00
|
|
|
if err := op.srv.validateEnterpriseToken(identity); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-08-04 21:51:19 +00:00
|
|
|
if authz.OperatorWrite(nil) != acl.Allow {
|
2020-09-25 17:46:38 +00:00
|
|
|
return acl.PermissionDenied("Missing operator:write permissions")
|
2017-03-30 19:35:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Apply the update
|
|
|
|
resp, err := op.srv.raftApply(structs.AutopilotRequestType, args)
|
|
|
|
if err != nil {
|
2021-04-08 22:58:15 +00:00
|
|
|
return fmt.Errorf("raft apply failed: %w", err)
|
2017-03-30 19:35:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the return type is a bool.
|
|
|
|
if respBool, ok := resp.(bool); ok {
|
|
|
|
*reply = respBool
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServerHealth is used to get the current health of the servers.
|
2020-09-25 17:46:38 +00:00
|
|
|
func (op *Operator) ServerHealth(args *structs.DCSpecificRequest, reply *structs.AutopilotHealthReply) error {
|
2017-03-30 19:35:50 +00:00
|
|
|
// This must be sent to the leader, so we fix the args since we are
|
|
|
|
// re-using a structure where we don't support all the options.
|
|
|
|
args.RequireConsistent = true
|
|
|
|
args.AllowStale = false
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := op.srv.ForwardRPC("Operator.ServerHealth", args, reply); done {
|
2017-03-30 19:35:50 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// This action requires operator read access.
|
2021-08-04 21:51:19 +00:00
|
|
|
identity, authz, err := op.srv.acls.ResolveTokenToIdentityAndAuthorizer(args.Token)
|
2017-03-30 19:35:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-04 20:58:56 +00:00
|
|
|
if err := op.srv.validateEnterpriseToken(identity); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-08-04 21:51:19 +00:00
|
|
|
if authz.OperatorRead(nil) != acl.Allow {
|
2020-09-25 17:46:38 +00:00
|
|
|
return acl.PermissionDenied("Missing operator:read permissions")
|
|
|
|
}
|
|
|
|
|
|
|
|
state := op.srv.autopilot.GetState()
|
|
|
|
|
2020-11-16 22:08:17 +00:00
|
|
|
if state == nil {
|
|
|
|
// this behavior seems odd but its functionally equivalent to 1.8.5 where if
|
|
|
|
// autopilot didn't have a health reply yet it would just return no error
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-25 17:46:38 +00:00
|
|
|
health := structs.AutopilotHealthReply{
|
|
|
|
Healthy: state.Healthy,
|
|
|
|
FailureTolerance: state.FailureTolerance,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, srv := range state.Servers {
|
|
|
|
srvHealth := structs.AutopilotServerHealth{
|
|
|
|
ID: string(srv.Server.ID),
|
|
|
|
Name: srv.Server.Name,
|
|
|
|
Address: string(srv.Server.Address),
|
|
|
|
Version: srv.Server.Version,
|
|
|
|
Leader: srv.State == autopilot.RaftLeader,
|
|
|
|
Voter: srv.State == autopilot.RaftLeader || srv.State == autopilot.RaftVoter,
|
|
|
|
LastContact: srv.Stats.LastContact,
|
|
|
|
LastTerm: srv.Stats.LastTerm,
|
|
|
|
LastIndex: srv.Stats.LastIndex,
|
|
|
|
Healthy: srv.Health.Healthy,
|
|
|
|
StableSince: srv.Health.StableSince,
|
|
|
|
}
|
|
|
|
|
|
|
|
switch srv.Server.NodeStatus {
|
|
|
|
case autopilot.NodeAlive:
|
|
|
|
srvHealth.SerfStatus = serf.StatusAlive
|
|
|
|
case autopilot.NodeLeft:
|
|
|
|
srvHealth.SerfStatus = serf.StatusLeft
|
|
|
|
case autopilot.NodeFailed:
|
|
|
|
srvHealth.SerfStatus = serf.StatusFailed
|
|
|
|
default:
|
|
|
|
srvHealth.SerfStatus = serf.StatusNone
|
|
|
|
}
|
|
|
|
|
|
|
|
health.Servers = append(health.Servers, srvHealth)
|
|
|
|
}
|
|
|
|
|
|
|
|
*reply = health
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (op *Operator) AutopilotState(args *structs.DCSpecificRequest, reply *autopilot.State) error {
|
|
|
|
// This must be sent to the leader, so we fix the args since we are
|
|
|
|
// re-using a structure where we don't support all the options.
|
|
|
|
args.RequireConsistent = true
|
|
|
|
args.AllowStale = false
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := op.srv.ForwardRPC("Operator.AutopilotState", args, reply); done {
|
2020-09-25 17:46:38 +00:00
|
|
|
return err
|
2017-03-30 19:35:50 +00:00
|
|
|
}
|
|
|
|
|
2020-09-25 17:46:38 +00:00
|
|
|
// This action requires operator read access.
|
2021-08-04 21:51:19 +00:00
|
|
|
identity, authz, err := op.srv.acls.ResolveTokenToIdentityAndAuthorizer(args.Token)
|
2017-03-30 19:35:50 +00:00
|
|
|
if err != nil {
|
2020-09-25 17:46:38 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := op.srv.validateEnterpriseToken(identity); err != nil {
|
|
|
|
return err
|
2017-03-30 19:35:50 +00:00
|
|
|
}
|
2021-08-04 21:51:19 +00:00
|
|
|
if authz.OperatorRead(nil) != acl.Allow {
|
2020-09-25 17:46:38 +00:00
|
|
|
return acl.PermissionDenied("Missing operator:read permissions")
|
2017-03-30 19:35:50 +00:00
|
|
|
}
|
|
|
|
|
2020-09-25 17:46:38 +00:00
|
|
|
state := op.srv.autopilot.GetState()
|
|
|
|
if state == nil {
|
|
|
|
return fmt.Errorf("Failed to get autopilot state: no state found")
|
|
|
|
}
|
2017-03-30 19:35:50 +00:00
|
|
|
|
2020-09-25 17:46:38 +00:00
|
|
|
*reply = *state
|
2017-03-30 19:35:50 +00:00
|
|
|
return nil
|
|
|
|
}
|