2017-12-18 21:16:23 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2018-01-30 03:53:34 +00:00
|
|
|
"encoding/json"
|
2017-12-18 21:16:23 +00:00
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// AutopilotConfiguration is used for querying/setting the Autopilot configuration.
|
2018-01-16 21:35:32 +00:00
|
|
|
// Autopilot helps manage operator tasks related to Nomad servers like removing
|
2017-12-18 21:16:23 +00:00
|
|
|
// failed servers from the Raft quorum.
|
|
|
|
type AutopilotConfiguration struct {
|
|
|
|
// CleanupDeadServers controls whether to remove dead servers from the Raft
|
|
|
|
// peer list when a new server joins
|
|
|
|
CleanupDeadServers bool
|
|
|
|
|
|
|
|
// LastContactThreshold is the limit on the amount of time a server can go
|
|
|
|
// without leader contact before being considered unhealthy.
|
2018-01-30 03:53:34 +00:00
|
|
|
LastContactThreshold time.Duration
|
2017-12-18 21:16:23 +00:00
|
|
|
|
|
|
|
// MaxTrailingLogs is the amount of entries in the Raft Log that a server can
|
|
|
|
// be behind before being considered unhealthy.
|
|
|
|
MaxTrailingLogs uint64
|
|
|
|
|
2020-02-16 21:23:20 +00:00
|
|
|
// MinQuorum sets the minimum number of servers allowed in a cluster before
|
|
|
|
// autopilot can prune dead servers.
|
|
|
|
MinQuorum uint
|
|
|
|
|
2017-12-18 21:16:23 +00:00
|
|
|
// ServerStabilizationTime is the minimum amount of time a server must be
|
|
|
|
// in a stable, healthy state before it can be added to the cluster. Only
|
|
|
|
// applicable with Raft protocol version 3 or higher.
|
2018-01-30 03:53:34 +00:00
|
|
|
ServerStabilizationTime time.Duration
|
2017-12-18 21:16:23 +00:00
|
|
|
|
2018-01-30 03:53:34 +00:00
|
|
|
// (Enterprise-only) EnableRedundancyZones specifies whether to enable redundancy zones.
|
|
|
|
EnableRedundancyZones bool
|
2017-12-18 21:16:23 +00:00
|
|
|
|
|
|
|
// (Enterprise-only) DisableUpgradeMigration will disable Autopilot's upgrade migration
|
|
|
|
// strategy of waiting until enough newer-versioned servers have been added to the
|
|
|
|
// cluster before promoting them to voters.
|
|
|
|
DisableUpgradeMigration bool
|
|
|
|
|
2018-01-30 03:53:34 +00:00
|
|
|
// (Enterprise-only) EnableCustomUpgrades specifies whether to enable using custom
|
|
|
|
// upgrade versions when performing migrations.
|
|
|
|
EnableCustomUpgrades bool
|
2017-12-18 21:16:23 +00:00
|
|
|
|
|
|
|
// CreateIndex holds the index corresponding the creation of this configuration.
|
|
|
|
// This is a read-only field.
|
|
|
|
CreateIndex uint64
|
|
|
|
|
|
|
|
// ModifyIndex will be set to the index of the last update when retrieving the
|
|
|
|
// Autopilot configuration. Resubmitting a configuration with
|
|
|
|
// AutopilotCASConfiguration will perform a check-and-set operation which ensures
|
|
|
|
// there hasn't been a subsequent update since the configuration was retrieved.
|
|
|
|
ModifyIndex uint64
|
|
|
|
}
|
|
|
|
|
2018-01-30 03:53:34 +00:00
|
|
|
func (u *AutopilotConfiguration) MarshalJSON() ([]byte, error) {
|
|
|
|
type Alias AutopilotConfiguration
|
|
|
|
return json.Marshal(&struct {
|
|
|
|
LastContactThreshold string
|
|
|
|
ServerStabilizationTime string
|
|
|
|
*Alias
|
|
|
|
}{
|
|
|
|
LastContactThreshold: u.LastContactThreshold.String(),
|
|
|
|
ServerStabilizationTime: u.ServerStabilizationTime.String(),
|
2018-09-04 23:03:52 +00:00
|
|
|
Alias: (*Alias)(u),
|
2018-01-30 03:53:34 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *AutopilotConfiguration) UnmarshalJSON(data []byte) error {
|
|
|
|
type Alias AutopilotConfiguration
|
|
|
|
aux := &struct {
|
|
|
|
LastContactThreshold string
|
|
|
|
ServerStabilizationTime string
|
|
|
|
*Alias
|
|
|
|
}{
|
|
|
|
Alias: (*Alias)(u),
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(data, &aux); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
if aux.LastContactThreshold != "" {
|
|
|
|
if u.LastContactThreshold, err = time.ParseDuration(aux.LastContactThreshold); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if aux.ServerStabilizationTime != "" {
|
|
|
|
if u.ServerStabilizationTime, err = time.ParseDuration(aux.ServerStabilizationTime); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-18 21:16:23 +00:00
|
|
|
// ServerHealth is the health (from the leader's point of view) of a server.
|
|
|
|
type ServerHealth struct {
|
|
|
|
// ID is the raft ID of the server.
|
|
|
|
ID string
|
|
|
|
|
|
|
|
// Name is the node name of the server.
|
|
|
|
Name string
|
|
|
|
|
|
|
|
// Address is the address of the server.
|
|
|
|
Address string
|
|
|
|
|
|
|
|
// The status of the SerfHealth check for the server.
|
|
|
|
SerfStatus string
|
|
|
|
|
2018-01-16 21:35:32 +00:00
|
|
|
// Version is the Nomad version of the server.
|
2017-12-18 21:16:23 +00:00
|
|
|
Version string
|
|
|
|
|
|
|
|
// Leader is whether this server is currently the leader.
|
|
|
|
Leader bool
|
|
|
|
|
|
|
|
// LastContact is the time since this node's last contact with the leader.
|
2018-01-30 03:53:34 +00:00
|
|
|
LastContact time.Duration
|
2017-12-18 21:16:23 +00:00
|
|
|
|
|
|
|
// LastTerm is the highest leader term this server has a record of in its Raft log.
|
|
|
|
LastTerm uint64
|
|
|
|
|
|
|
|
// LastIndex is the last log index this server has a record of in its Raft log.
|
|
|
|
LastIndex uint64
|
|
|
|
|
|
|
|
// Healthy is whether or not the server is healthy according to the current
|
|
|
|
// Autopilot config.
|
|
|
|
Healthy bool
|
|
|
|
|
|
|
|
// Voter is whether this is a voting server.
|
|
|
|
Voter bool
|
|
|
|
|
|
|
|
// StableSince is the last time this server's Healthy value changed.
|
|
|
|
StableSince time.Time
|
|
|
|
}
|
|
|
|
|
2018-01-30 03:53:34 +00:00
|
|
|
func (u *ServerHealth) MarshalJSON() ([]byte, error) {
|
|
|
|
type Alias ServerHealth
|
|
|
|
return json.Marshal(&struct {
|
|
|
|
LastContact string
|
|
|
|
*Alias
|
|
|
|
}{
|
|
|
|
LastContact: u.LastContact.String(),
|
|
|
|
Alias: (*Alias)(u),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *ServerHealth) UnmarshalJSON(data []byte) error {
|
|
|
|
type Alias ServerHealth
|
|
|
|
aux := &struct {
|
|
|
|
LastContact string
|
|
|
|
*Alias
|
|
|
|
}{
|
|
|
|
Alias: (*Alias)(u),
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(data, &aux); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
if aux.LastContact != "" {
|
|
|
|
if u.LastContact, err = time.ParseDuration(aux.LastContact); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-18 21:16:23 +00:00
|
|
|
// OperatorHealthReply is a representation of the overall health of the cluster
|
|
|
|
type OperatorHealthReply struct {
|
|
|
|
// Healthy is true if all the servers in the cluster are healthy.
|
|
|
|
Healthy bool
|
|
|
|
|
|
|
|
// FailureTolerance is the number of healthy servers that could be lost without
|
|
|
|
// an outage occurring.
|
|
|
|
FailureTolerance int
|
|
|
|
|
|
|
|
// Servers holds the health of each server.
|
|
|
|
Servers []ServerHealth
|
|
|
|
}
|
|
|
|
|
|
|
|
// AutopilotGetConfiguration is used to query the current Autopilot configuration.
|
2018-02-08 00:47:44 +00:00
|
|
|
func (op *Operator) AutopilotGetConfiguration(q *QueryOptions) (*AutopilotConfiguration, *QueryMeta, error) {
|
|
|
|
var resp AutopilotConfiguration
|
|
|
|
qm, err := op.c.query("/v1/operator/autopilot/configuration", &resp, q)
|
2018-01-18 22:28:58 +00:00
|
|
|
if err != nil {
|
2018-02-08 00:47:44 +00:00
|
|
|
return nil, nil, err
|
2017-12-18 21:16:23 +00:00
|
|
|
}
|
2018-02-08 00:47:44 +00:00
|
|
|
return &resp, qm, nil
|
2017-12-18 21:16:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AutopilotSetConfiguration is used to set the current Autopilot configuration.
|
2018-02-08 00:47:44 +00:00
|
|
|
func (op *Operator) AutopilotSetConfiguration(conf *AutopilotConfiguration, q *WriteOptions) (*WriteMeta, error) {
|
|
|
|
var out bool
|
2023-02-07 22:42:25 +00:00
|
|
|
wm, err := op.c.put("/v1/operator/autopilot/configuration", conf, &out, q)
|
2017-12-18 21:16:23 +00:00
|
|
|
if err != nil {
|
2018-02-08 00:47:44 +00:00
|
|
|
return nil, err
|
2017-12-18 21:16:23 +00:00
|
|
|
}
|
2018-02-08 00:47:44 +00:00
|
|
|
return wm, nil
|
2017-12-18 21:16:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AutopilotCASConfiguration is used to perform a Check-And-Set update on the
|
|
|
|
// Autopilot configuration. The ModifyIndex value will be respected. Returns
|
|
|
|
// true on success or false on failures.
|
2018-02-08 00:47:44 +00:00
|
|
|
func (op *Operator) AutopilotCASConfiguration(conf *AutopilotConfiguration, q *WriteOptions) (bool, *WriteMeta, error) {
|
|
|
|
var out bool
|
2023-02-07 22:42:25 +00:00
|
|
|
wm, err := op.c.put("/v1/operator/autopilot/configuration?cas="+strconv.FormatUint(conf.ModifyIndex, 10), conf, &out, q)
|
2017-12-18 21:16:23 +00:00
|
|
|
if err != nil {
|
2018-02-08 00:47:44 +00:00
|
|
|
return false, nil, err
|
2017-12-18 21:16:23 +00:00
|
|
|
}
|
|
|
|
|
2018-02-08 00:47:44 +00:00
|
|
|
return out, wm, nil
|
2017-12-18 21:16:23 +00:00
|
|
|
}
|
|
|
|
|
2018-01-16 21:35:32 +00:00
|
|
|
// AutopilotServerHealth is used to query Autopilot's top-level view of the health
|
|
|
|
// of each Nomad server.
|
2018-02-08 00:47:44 +00:00
|
|
|
func (op *Operator) AutopilotServerHealth(q *QueryOptions) (*OperatorHealthReply, *QueryMeta, error) {
|
2017-12-18 21:16:23 +00:00
|
|
|
var out OperatorHealthReply
|
2018-02-08 00:47:44 +00:00
|
|
|
qm, err := op.c.query("/v1/operator/autopilot/health", &out, q)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
2017-12-18 21:16:23 +00:00
|
|
|
}
|
2018-02-08 00:47:44 +00:00
|
|
|
return &out, qm, nil
|
2017-12-18 21:16:23 +00:00
|
|
|
}
|