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 int8

View on GitHub (pinned to 482b49bf1a)

Solutions

  1. Clamp the weight to the allowed range, e.g. weight = 100 for maximum preference.
  2. Cap generated weights at ±100 before rendering the job spec.
  3. 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

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


AI-assisted analysis of hashicorp/nomad@482b49bf1a (2026-09-04). Data as JSON: /api/errors/12bbb57c4d37ce98. Report an issue: GitHub.