2017-02-10 01:58:20 +00:00
|
|
|
package api
|
|
|
|
|
2018-09-28 04:27:38 +00:00
|
|
|
import "strconv"
|
|
|
|
|
2017-02-10 01:58:20 +00:00
|
|
|
// Operator can be used to perform low-level operator tasks for Nomad.
|
|
|
|
type Operator struct {
|
|
|
|
c *Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// Operator returns a handle to the operator endpoints.
|
|
|
|
func (c *Client) Operator() *Operator {
|
|
|
|
return &Operator{c}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RaftServer has information about a server in the Raft configuration.
|
|
|
|
type RaftServer struct {
|
|
|
|
// ID is the unique ID for the server. These are currently the same
|
|
|
|
// as the address, but they will be changed to a real GUID in a future
|
|
|
|
// release of Nomad.
|
|
|
|
ID string
|
|
|
|
|
|
|
|
// Node is the node name of the server, as known by Nomad, or this
|
|
|
|
// will be set to "(unknown)" otherwise.
|
|
|
|
Node string
|
|
|
|
|
|
|
|
// Address is the IP:port of the server, used for Raft communications.
|
|
|
|
Address string
|
|
|
|
|
|
|
|
// Leader is true if this server is the current cluster leader.
|
|
|
|
Leader bool
|
|
|
|
|
|
|
|
// Voter is true if this server has a vote in the cluster. This might
|
|
|
|
// be false if the server is staging and still coming online, or if
|
|
|
|
// it's a non-voting server, which will be added in a future release of
|
|
|
|
// Nomad.
|
|
|
|
Voter bool
|
2017-11-22 00:29:11 +00:00
|
|
|
|
|
|
|
// RaftProtocol is the version of the Raft protocol spoken by this server.
|
|
|
|
RaftProtocol string
|
2017-02-10 01:58:20 +00:00
|
|
|
}
|
|
|
|
|
2018-03-11 17:48:04 +00:00
|
|
|
// RaftConfiguration is returned when querying for the current Raft configuration.
|
2017-02-10 01:58:20 +00:00
|
|
|
type RaftConfiguration struct {
|
|
|
|
// Servers has the list of servers in the Raft configuration.
|
|
|
|
Servers []*RaftServer
|
|
|
|
|
|
|
|
// Index has the Raft index of this configuration.
|
|
|
|
Index uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
// RaftGetConfiguration is used to query the current Raft peer set.
|
|
|
|
func (op *Operator) RaftGetConfiguration(q *QueryOptions) (*RaftConfiguration, error) {
|
2017-02-20 19:12:34 +00:00
|
|
|
r, err := op.c.newRequest("GET", "/v1/operator/raft/configuration")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-02-10 01:58:20 +00:00
|
|
|
r.setQueryOptions(q)
|
|
|
|
_, resp, err := requireOK(op.c.doRequest(r))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
var out RaftConfiguration
|
|
|
|
if err := decodeBody(resp, &out); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RaftRemovePeerByAddress is used to kick a stale peer (one that it in the Raft
|
|
|
|
// quorum but no longer known to Serf or the catalog) by address in the form of
|
|
|
|
// "IP:port".
|
|
|
|
func (op *Operator) RaftRemovePeerByAddress(address string, q *WriteOptions) error {
|
2017-02-20 19:12:34 +00:00
|
|
|
r, err := op.c.newRequest("DELETE", "/v1/operator/raft/peer")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-10 01:58:20 +00:00
|
|
|
r.setWriteOptions(q)
|
|
|
|
|
2017-09-26 22:26:33 +00:00
|
|
|
r.params.Set("address", address)
|
2017-02-10 01:58:20 +00:00
|
|
|
|
|
|
|
_, resp, err := requireOK(op.c.doRequest(r))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.Body.Close()
|
|
|
|
return nil
|
|
|
|
}
|
2018-01-16 21:35:32 +00:00
|
|
|
|
|
|
|
// RaftRemovePeerByID is used to kick a stale peer (one that is in the Raft
|
|
|
|
// quorum but no longer known to Serf or the catalog) by ID.
|
|
|
|
func (op *Operator) RaftRemovePeerByID(id string, q *WriteOptions) error {
|
|
|
|
r, err := op.c.newRequest("DELETE", "/v1/operator/raft/peer")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
r.setWriteOptions(q)
|
|
|
|
|
|
|
|
r.params.Set("id", id)
|
|
|
|
|
|
|
|
_, resp, err := requireOK(op.c.doRequest(r))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.Body.Close()
|
|
|
|
return nil
|
|
|
|
}
|
2018-09-28 04:27:38 +00:00
|
|
|
|
|
|
|
type SchedulerConfiguration struct {
|
2018-10-01 14:26:52 +00:00
|
|
|
// PreemptionConfig specifies whether to enable eviction of lower
|
2018-09-28 04:27:38 +00:00
|
|
|
// priority jobs to place higher priority jobs.
|
2018-10-01 14:26:52 +00:00
|
|
|
PreemptionConfig PreemptionConfig
|
2018-09-28 04:27:38 +00:00
|
|
|
|
|
|
|
// CreateIndex/ModifyIndex store the create/modify indexes of this configuration.
|
|
|
|
CreateIndex uint64
|
|
|
|
ModifyIndex uint64
|
|
|
|
}
|
|
|
|
|
2018-10-29 18:10:43 +00:00
|
|
|
// SchedulerConfigurationResponse is the response object that wraps SchedulerConfiguration
|
|
|
|
type SchedulerConfigurationResponse struct {
|
|
|
|
// SchedulerConfig contains scheduler config options
|
|
|
|
SchedulerConfig SchedulerConfiguration
|
|
|
|
|
|
|
|
// CreateIndex/ModifyIndex store the create/modify indexes of this configuration.
|
|
|
|
CreateIndex uint64
|
|
|
|
ModifyIndex uint64
|
|
|
|
}
|
|
|
|
|
2018-10-01 14:26:52 +00:00
|
|
|
// PreemptionConfig specifies whether preemption is enabled based on scheduler type
|
|
|
|
type PreemptionConfig struct {
|
|
|
|
SystemSchedulerEnabled bool
|
|
|
|
}
|
|
|
|
|
2018-09-28 04:27:38 +00:00
|
|
|
// SchedulerGetConfiguration is used to query the current Scheduler configuration.
|
2018-10-29 18:10:43 +00:00
|
|
|
func (op *Operator) SchedulerGetConfiguration(q *QueryOptions) (*SchedulerConfigurationResponse, *QueryMeta, error) {
|
|
|
|
var resp SchedulerConfigurationResponse
|
2018-09-28 04:27:38 +00:00
|
|
|
qm, err := op.c.query("/v1/operator/scheduler/config", &resp, q)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return &resp, qm, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SchedulerSetConfiguration is used to set the current Scheduler configuration.
|
|
|
|
func (op *Operator) SchedulerSetConfiguration(conf *SchedulerConfiguration, q *WriteOptions) (*WriteMeta, error) {
|
|
|
|
var out bool
|
|
|
|
wm, err := op.c.write("/v1/operator/scheduler/config", conf, &out, q)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return wm, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SchedulerCASConfiguration is used to perform a Check-And-Set update on the
|
|
|
|
// Scheduler configuration. The ModifyIndex value will be respected. Returns
|
|
|
|
// true on success or false on failures.
|
|
|
|
func (op *Operator) SchedulerCASConfiguration(conf *SchedulerConfiguration, q *WriteOptions) (bool, *WriteMeta, error) {
|
|
|
|
var out bool
|
|
|
|
wm, err := op.c.write("/v1/operator/scheduler/config?cas="+strconv.FormatUint(conf.ModifyIndex, 10), conf, &out, q)
|
|
|
|
if err != nil {
|
|
|
|
return false, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, wm, nil
|
|
|
|
}
|