when / or conditions

4 min read

Conditions control whether a resource template field is written during reconciliation. Used inside operatorBox and autoscale.

operatorBox:
  when:
    - field: spec.replicas
      operator: gt
      value: "1"
      valueType: int

  or:
    - field: spec.mode
      equals: production
    - field: spec.mode
      notEquals: staging

Semantics

BlockBehaviour
whenAND — all conditions must be true
orOR — at least one condition must be true

Both can be combined. The overall result is: when AND or.


Condition kinds

Field conditions

Compare a dot-notation path into the CR against a value.

FieldRequiredDescription
fieldyesDot-notation path into the CR (e.g. spec.replicas, spec.config.mode)
operatoryes*Comparison operator (see below)
valueyes*Value to compare against
valueTypenostring (default), int, float, bool

*Use either the operator+value form or a shorthand form — not both.

Operators

OperatorShorthandDescription
equalsequalsField equals value
notEqualsnotEqualsField does not equal value
containscontainsField contains the substring
notContainsnotContainsField does not contain the substring
prefixprefixField starts with the value
notPrefixnotPrefixField does not start with the value
suffixsuffixField ends with the value
notSuffixnotSuffixField does not end with the value
regexregexField matches the value as an RE2 regular expression (Go’s regexp syntax)
existsexists: trueField is present and non-empty
notExistsnotExists: trueField is absent or empty
gtgreaterThanField is numerically greater than value — strict
ltlessThanField is numerically less than value — strict
gtemin, greaterThanOrEqualField is numerically greater than or equal to value
ltemax, lessThanOrEqualField is numerically less than or equal to value
betweenbetweenField is numerically within an inclusive range. Value is "min,max"
notBetweennotBetweenField is numerically outside an inclusive range. Value is "min,max"
ininField is one of a comma-separated list
notInnotInField is none of a comma-separated list
uniqueField value must be unique across all existing instances of this CRD. Works in both validation.rules and when:/or:, enforced at both reconcile time (a live, authoritative check) and admission time (a fast best-effort check against the runtime’s cache) — see unique for the difference between the two
typeOf / typeMap / typeList / typeString / typeNumber / typeBool / typeNullCheck the field’s YAML type rather than its value. No shorthand — use operator: explicitly.

gt/lt are strict (exclusive); use gte/lte (or the min/max shorthand) for an inclusive bound. min/max and greaterThanOrEqual/lessThanOrEqual resolve to the same gte/lte operators — min/max read better for a bound on a quantity (min: "1"), greaterThanOrEqual/lessThanOrEqual for a direct comparison. Same operators and shorthand as validation.rules — the Condition type is shared by both.

Shorthand form

when:
  - field: spec.engine
    equals: postgres         # shorthand for operator: eq, value: postgres

  - field: spec.replicas
    greaterThan: "0"         # shorthand for operator: gt
    valueType: int

Explicit form

when:
  - field: spec.replicas
    operator: gt
    value: "3"
    valueType: int

between, in, and regex

when:
  - field: spec.replicas
    between: "1,10"          # inclusive range — 1 and 10 both pass

  - field: spec.tier
    in: "standard,premium"   # comma-separated list

  - field: spec.name
    regex: "^app-[a-z0-9-]+$"

Time conditions

Evaluate against the current wall clock (UTC). No field: key — mutually exclusive with field conditions.

time:

Active when the current clock time falls within the declared window.

when:
  - time:
      after: "09:00"
      before: "18:00"
FieldRequiredDescription
afternoActive at or after this time. Format: HH:MM (24h, UTC).
beforenoActive at or before this time. Format: HH:MM (24h, UTC).

Both after and before may be set together. Either may be omitted.

dayOfWeek:

Active on the declared set of days.

when:
  - dayOfWeek:
      weekday: true          # Mon–Fri shorthand
when:
  - dayOfWeek:
      weekend: true          # Sat–Sun shorthand
when:
  - dayOfWeek:
      in: [Monday, Wednesday, Friday]
when:
  - dayOfWeek:
      notIn: [Saturday, Sunday]
FieldMutually exclusive withDescription
weekdayweekend, in, notIntrue → active Mon–Fri; false → active Sat–Sun
weekendweekday, in, notIntrue → active Sat–Sun; false → active Mon–Fri
inweekday, weekend, notInActive on these days (full English names)
notInweekday, weekend, inActive on all days except these

Exactly one field must be set. Day names are case-insensitive (monday, Monday, and MONDAY are equivalent).

Combined example — business hours only (weekday AND time window):

when:
  - time:
      after: "09:00"
      before: "18:00"
  - dayOfWeek:
      weekday: true

cron:

Active when a cron-defined window is open. The window opens at each cron fire and stays open for duration.

when:
  - cron: "0 2 * * 0"
    duration: 2h
FieldRequiredDescription
cronyesStandard cron expression (5-field: min hour dom month dow)
durationnoHow long the window stays open after each fire. Default: 60s.

negate

Inverts the result of any condition. Applies to field conditions, time:, dayOfWeek:, and cron:.

when:
  - dayOfWeek:
      weekday: true
    negate: true        # passes on weekends

  - time:
      after: "09:00"
      before: "18:00"
    negate: true        # passes outside the window

negate is a top-level field on the condition — it inverts whatever the condition evaluates to, regardless of kind.