diff --git a/api/operator.go b/api/operator.go index d215326f4..f46216f31 100644 --- a/api/operator.go +++ b/api/operator.go @@ -112,7 +112,11 @@ func (op *Operator) RaftRemovePeerByID(id string, q *WriteOptions) error { return nil } +// SchedulerConfiguration is the config for controlling scheduler behavior type SchedulerConfiguration struct { + // SchedulerAlgorithm lets you select between available scheduling algorithms. + SchedulerAlgorithm string + // PreemptionConfig specifies whether to enable eviction of lower // priority jobs to place higher priority jobs. PreemptionConfig PreemptionConfig @@ -140,6 +144,11 @@ type SchedulerSetConfigurationResponse struct { WriteMeta } +// SchedulerAlgorithm is an enum string that encapsulates the valid options for a +// SchedulerConfiguration stanza's SchedulerAlgorithm. These modes will allow the +// scheduler to be user-selectable. +type SchedulerAlgorithm string + // PreemptionConfig specifies whether preemption is enabled based on scheduler type type PreemptionConfig struct { SystemSchedulerEnabled bool diff --git a/command/agent/operator_endpoint.go b/command/agent/operator_endpoint.go index 7ac48c0fd..8abe7cdf5 100644 --- a/command/agent/operator_endpoint.go +++ b/command/agent/operator_endpoint.go @@ -14,6 +14,8 @@ import ( "github.com/hashicorp/raft" ) +// OperatorRequest is used route operator/raft API requests to the implementing +// functions. func (s *HTTPServer) OperatorRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) { path := strings.TrimPrefix(req.URL.Path, "/v1/operator/raft/") switch { @@ -251,7 +253,12 @@ func (s *HTTPServer) schedulerUpdateConfig(resp http.ResponseWriter, req *http.R return nil, CodedError(http.StatusBadRequest, fmt.Sprintf("Error parsing scheduler config: %v", err)) } + if !structs.SchedulerAlgorithmIsValid(conf.SchedulerAlgorithm) { + return nil, CodedError(http.StatusBadRequest, fmt.Sprintf("Invalid scheduler algorithm selected.")) + } + args.Config = structs.SchedulerConfiguration{ + SchedulerAlgorithm: conf.SchedulerAlgorithm, PreemptionConfig: structs.PreemptionConfig{ SystemSchedulerEnabled: conf.PreemptionConfig.SystemSchedulerEnabled, BatchSchedulerEnabled: conf.PreemptionConfig.BatchSchedulerEnabled, diff --git a/nomad/config.go b/nomad/config.go index 8a0fb7a18..8b4fc1c25 100644 --- a/nomad/config.go +++ b/nomad/config.go @@ -403,6 +403,7 @@ func DefaultConfig() *Config { ServerHealthInterval: 2 * time.Second, AutopilotInterval: 10 * time.Second, DefaultSchedulerConfig: structs.SchedulerConfiguration{ + SchedulerAlgorithm: structs.SchedulerAlgorithmBinpack, PreemptionConfig: structs.PreemptionConfig{ SystemSchedulerEnabled: true, BatchSchedulerEnabled: false, diff --git a/nomad/structs/operator.go b/nomad/structs/operator.go index 03d8caedb..101ce6351 100644 --- a/nomad/structs/operator.go +++ b/nomad/structs/operator.go @@ -124,8 +124,37 @@ type AutopilotConfig struct { ModifyIndex uint64 } +// SchedulerAlgorithm is an enum string that encapsulates the valid options for a +// SchedulerConfiguration stanza's SchedulerAlgorithm. These modes will allow the +// scheduler to be user-selectable. +type SchedulerAlgorithm string + +const ( + // SchedulerAlgorithmBinpack indicates that the scheduler should spread + // allocations as evenly as possible over the available hardware. + SchedulerAlgorithmBinpack string = "binpack" + + // SchedulerAlgorithmSpread indicates that the scheduler should spread + // allocations as evenly as possible over the available hardware. + SchedulerAlgorithmSpread string = "spread" +) + +// SchedulerAlgorithmIsValid validates the given SchedulerAlgorithm string and +// returns true only when a correct algorithm is specified. +func SchedulerAlgorithmIsValid(alg string) bool { + switch alg { + case SchedulerAlgorithmBinpack, SchedulerAlgorithmSpread: + return true + default: + return false + } +} + // SchedulerConfiguration is the config for controlling scheduler behavior type SchedulerConfiguration struct { + // SchedulerAlgorithm lets you select between available scheduling algorithms. + SchedulerAlgorithm string `hcl:"scheduler_algorithm"` + // PreemptionConfig specifies whether to enable eviction of lower // priority jobs to place higher priority jobs. PreemptionConfig PreemptionConfig `hcl:"preemption_config"`