hashicorp/nomad · error

Affinity %d validation failed: %s

Error message

Affinity %d validation failed: %s

What it means

Raised during TaskGroup validation when an Affinity stanza fails Affinity.Validate(); the underlying reason is wrapped as "Affinity %d validation failed: <reason>" with a 1-based index. Affinities share the constraint-like structure (attribute, operand, value) and must pass the same kind of structural checks.

Source

Thrown at nomad/structs/structs.go:3150

		_ = multierror.Append(&mErr, errors.New("device name must be given as one of the following: type, vendor/type, or vendor/type/name"))
	}

	for idx, constr := range r.Constraints {
		// Ensure that the constraint doesn't use an operand we do not allow
		switch constr.Operand {
		case ConstraintDistinctHosts, ConstraintDistinctProperty:
			outer := fmt.Errorf("Constraint %d validation failed: using unsupported operand %q", idx+1, constr.Operand)
			_ = multierror.Append(&mErr, outer)
		default:
			if err := constr.Validate(); err != nil {
				outer := fmt.Errorf("Constraint %d validation failed: %s", idx+1, err)
				_ = multierror.Append(&mErr, outer)
			}
		}
	}
	for idx, affinity := range r.Affinities {
		if err := affinity.Validate(); err != nil {
			outer := fmt.Errorf("Affinity %d validation failed: %s", idx+1, err)
			_ = multierror.Append(&mErr, outer)
		}
	}

	return mErr.ErrorOrNil()
}

// NodeResources is used to define the resources available on a client node.
type NodeResources struct {
	// Do not read from this value except for compatibility (i.e. serialization).
	//
	// Deprecated; use NodeProcessorResources instead.
	Cpu LegacyNodeCpuResources

	Processors NodeProcessorResources
	Memory     NodeMemoryResources
	Disk       NodeDiskResources
	Devices    []*NodeDeviceResource

View on GitHub (pinned to 482b49bf1a)

Solutions

  1. Inspect the inner message after the colon for the specific field problem
  2. Provide a valid operand (e.g. "=", ">", "regexp") and required attribute/value fields
  3. Validate locally with nomad job validate before submission

Example fix

// before
affinity {
  attribute = "${meta.rack}"
  weight    = 50
  # missing operand / RTarget
}
// after
affinity {
  attribute = "${meta.rack}"
  operand   = "="
  value     = "r1"
  weight    = 50
}
Defensive patterns

Strategy: validation

Validate before calling

for i, a := range tg.Affinities {
    if err := a.Validate(); err != nil {
        return fmt.Errorf("affinity %d: %w", i+1, err)
    }
}

Type guard

func hasAffinityOperands(l, op, r string) bool { return l != "" && op != "" && r != "" }

Prevention

When it happens

Trigger: Submitting a job whose task group affinity has an invalid operand or missing attribute/value, e.g. affinity { attribute = "${meta.rack}" } with no operand and weight, causing the inner validation error.

Common situations: Hand-writing affinity stanzas after the 0.11 spread/affinity feature introduction; confusing affinity syntax with constraint syntax; templated affinities emitting empty fields.

Related errors


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