Skip to content

Commit

Permalink
consul/connect: use additional constraints in scheduling connect tasks
Browse files Browse the repository at this point in the history
This PR adds two additional constraints on Connect sidecar and gateway tasks,
making sure Nomad schedules them only onto nodes where Connect is actually
enabled on the Consul agent.

Consul requires `connect.enabled = true` and `ports.grpc = <number>` to be
explicitly set on agent configuration before Connect APIs will work. Until
now, Nomad would only validate a minimum version of Consul, which would cause
confusion for users who try to run Connect tasks on nodes where Consul is not
yet sufficiently configured. These contstraints prevent job scheduling on nodes
where Connect is not actually use-able.

Closes #10700
  • Loading branch information
shoenig committed Jun 3, 2021
1 parent c422aec commit 0cc4338
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
20 changes: 20 additions & 0 deletions nomad/job_endpoint_hook_connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,22 @@ func connectGatewayVersionConstraint() *structs.Constraint {
}
}

func connectEnabledConstraint() *structs.Constraint {
return &structs.Constraint{
LTarget: "${attr.consul.connect}",
RTarget: "true",
Operand: "=",
}
}

func connectListenerConstraint() *structs.Constraint {
return &structs.Constraint{
LTarget: "${attr.consul.grpc}",
RTarget: "0",
Operand: ">",
}
}

// jobConnectHook implements a job Mutating and Validating admission controller
type jobConnectHook struct{}

Expand Down Expand Up @@ -414,6 +430,8 @@ func newConnectGatewayTask(prefix, service string, netHost bool) *structs.Task {
Resources: connectSidecarResources(),
Constraints: structs.Constraints{
connectGatewayVersionConstraint(),
connectEnabledConstraint(),
connectListenerConstraint(),
},
}
}
Expand All @@ -437,6 +455,8 @@ func newConnectSidecarTask(service string) *structs.Task {
},
Constraints: structs.Constraints{
connectSidecarVersionConstraint(),
connectEnabledConstraint(),
connectListenerConstraint(),
},
}
}
Expand Down
4 changes: 2 additions & 2 deletions nomad/structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8234,15 +8234,15 @@ const (
ConstraintAttributeIsNotSet = "is_not_set"
)

// Constraints are used to restrict placement options.
// A Constraint is used to restrict placement options.
type Constraint struct {
LTarget string // Left-hand target
RTarget string // Right-hand target
Operand string // Constraint operand (<=, <, =, !=, >, >=), contains, near
str string // Memoized string
}

// Equal checks if two constraints are equal
// Equals checks if two constraints are equal
func (c *Constraint) Equals(o *Constraint) bool {
return c == o ||
c.LTarget == o.LTarget &&
Expand Down

0 comments on commit 0cc4338

Please sign in to comment.