mirror of
https://github.com/kemko/nomad.git
synced 2026-01-04 17:35:43 +03:00
api: add constraints generators
This commit is contained in:
33
api/jobs.go
33
api/jobs.go
@@ -82,6 +82,7 @@ type Job struct {
|
||||
Priority int
|
||||
AllAtOnce bool
|
||||
Datacenters []string
|
||||
Constraints []*Constraint
|
||||
Meta map[string]string
|
||||
Status string
|
||||
StatusDescription string
|
||||
@@ -96,3 +97,35 @@ type registerJobRequest struct {
|
||||
type registerJobResponse struct {
|
||||
EvalID string
|
||||
}
|
||||
|
||||
// Constraint is used to serialize a job placement constraint.
|
||||
type Constraint struct {
|
||||
Hard bool
|
||||
LTarget string
|
||||
RTarget string
|
||||
Operand string
|
||||
Weight int
|
||||
}
|
||||
|
||||
// HardConstraint is used to create a new hard constraint.
|
||||
func HardConstraint(left, operand, right string) *Constraint {
|
||||
return constraint(left, operand, right, true, 0)
|
||||
}
|
||||
|
||||
// SoftConstraint is used to create a new soft constraint. It
|
||||
// takes an additional weight parameter to allow balancing
|
||||
// multiple soft constraints amongst eachother.
|
||||
func SoftConstraint(left, operand, right string, weight int) *Constraint {
|
||||
return constraint(left, operand, right, false, weight)
|
||||
}
|
||||
|
||||
// constraint generates a new job placement constraint.
|
||||
func constraint(left, operand, right string, hard bool, weight int) *Constraint {
|
||||
return &Constraint{
|
||||
Hard: hard,
|
||||
LTarget: left,
|
||||
RTarget: right,
|
||||
Operand: operand,
|
||||
Weight: weight,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -178,3 +178,33 @@ func TestJobs_Delete(t *testing.T) {
|
||||
t.Fatalf("expected 0 jobs, got: %d", n)
|
||||
}
|
||||
}
|
||||
|
||||
func TestJobs_Constraints(t *testing.T) {
|
||||
{
|
||||
c := HardConstraint("kernel.name", "=", "darwin")
|
||||
expect := &Constraint{
|
||||
Hard: true,
|
||||
LTarget: "kernel.name",
|
||||
RTarget: "darwin",
|
||||
Operand: "=",
|
||||
Weight: 0,
|
||||
}
|
||||
if !reflect.DeepEqual(c, expect) {
|
||||
t.Fatalf("expect: %#v, got: %#v", expect, c)
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
c := SoftConstraint("memory.totalbytes", ">=", "250000000", 5)
|
||||
expect := &Constraint{
|
||||
Hard: false,
|
||||
LTarget: "memory.totalbytes",
|
||||
RTarget: "250000000",
|
||||
Operand: ">=",
|
||||
Weight: 5,
|
||||
}
|
||||
if !reflect.DeepEqual(c, expect) {
|
||||
t.Fatalf("expect: %#v, got: %#v", expect, c)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user