hashicorp/nomad · error
Affinity weight must be within the range [-100,100]
Error message
Affinity weight must be within the range [-100,100]
What it means
Affinity.Validate() bounds weight to [-100, 100]. If a.Weight is greater than 100 or less than -100, the job is rejected with 'Affinity weight must be within the range [-100,100]'. Weight controls how strongly the scheduler favors (positive) or disfavors (negative) matching nodes.
Source
Thrown at nomad/structs/structs.go:10237
if a.RTarget == "" {
mErr.Errors = append(mErr.Errors, fmt.Errorf("Operator %q requires an RTarget", a.Operand))
}
default:
mErr.Errors = append(mErr.Errors, fmt.Errorf("Unknown affinity operator %q", a.Operand))
}
// Ensure we have an LTarget
if a.LTarget == "" {
mErr.Errors = append(mErr.Errors, fmt.Errorf("No LTarget provided but is required"))
}
// Ensure that weight is between -100 and 100, and not zero
if a.Weight == 0 {
mErr.Errors = append(mErr.Errors, fmt.Errorf("Affinity weight cannot be zero"))
}
if a.Weight > 100 || a.Weight < -100 {
mErr.Errors = append(mErr.Errors, fmt.Errorf("Affinity weight must be within the range [-100,100]"))
}
return mErr.ErrorOrNil()
}
// DiffID fulfills the DiffableWithID interface.
func (a *Affinity) DiffID() string {
return a.String()
}
// Spread is used to specify desired distribution of allocations according to weight
type Spread struct {
// Attribute is the node attribute used as the spread criteria
Attribute string
// Weight is the relative weight of this spread, useful when there are multiple
// spread and affinities
Weight int8View on GitHub (pinned to 482b49bf1a)
Solutions
- Clamp the weight to the allowed range, e.g. weight = 100 for maximum preference.
- Cap generated weights at ±100 before rendering the job spec.
- If stronger preference is needed, add multiple affinities rather than exceeding the bound.
Example fix
// before
affinity {
attribute = "${node.class}"
value = "high-memory"
weight = 200
}
// after
affinity {
attribute = "${node.class}"
value = "high-memory"
weight = 100
} Defensive patterns
Strategy: validation
Validate before calling
if a.Weight > 100 || a.Weight < -100 { return fmt.Errorf("weight %d out of range [-100,100]", a.Weight) } Prevention
- Clamp weights to [-100, 100] in job generators
- Do not assume percentage scales beyond 100 are valid
- Add unit tests asserting weight bounds in spec builders
When it happens
Trigger: Submitting a job (HCL or /v1/jobs API) whose affinity weight is outside [-100, 100], e.g. weight = 200 or weight = -150.
Common situations: Misreading the docs and using percentage-like values (0-100 only), script-generated weights from unbounded computations, or porting configs from tools with different weighting scales.
Related errors
- Affinity weight cannot be zero
- Semver affinity is invalid: %v
- Unknown affinity operator %q
- No LTarget provided but is required
- Disconnect cannot be configured with both lost_after and sto
AI-assisted analysis of hashicorp/nomad@482b49bf1a (2026-09-04).
Data as JSON: /api/errors/12bbb57c4d37ce98.
Report an issue: GitHub.