Skip to main content

Agent lifecycle

osapi agents report node conditions that compare values with limits. You can also drain and cordon an agent cleanly for maintenance. The two features come from the node management of Kubernetes.

Node conditions​

A condition is a boolean that compares a value with a limit. The agent calculates the conditions at each heartbeat (10 seconds). They show if something is wrong. The operator does not need to read the raw metrics.

ConditionDefault ThresholdData Source
MemoryPressureMemory used > 90%Heartbeat memory
HighLoadLoad1 > 2x CPU countHeartbeat load
DiskPressureAny disk > 90% usedHeartbeat disk
DrainStuckA drain waits for its consumers > 1mDrain
ConsumersUnavailableA job consumer cannot be made or boundJob consumers

The agent evaluates each condition at each heartbeat, and keeps:

  • Reason: an explanation for a person, for example "memory 94% used, 15.1/16.0 GB"
  • LastTransitionTime: the last time that the condition started or stopped

DrainStuck holds while a drain waits for the consumers of the agent to stop for more than one minute. While it holds, the agent stays Draining and does not change to Cordoned. The agent logs a warning and continues to wait.

ConsumersUnavailable holds while a JetStream consumer of the agent does not run, for example when the stream has its maximum number of consumers. The reason gives the number of consumers and the error of one of them. The agent logs the error and tries again each 10 seconds. While the condition holds, the agent can miss jobs, and a broadcast can show a timeout row for it.

The API and the CLI show only the conditions that hold now. A condition that does not hold is not in the answer. The notifier uses the full list of the agent, so it also sees when a condition stops.

CLI display​

agent list shows the conditions that hold in the CONDITIONS column:

HOSTNAME MACHINE ID STATE CONDITIONS LABELS AGE LOAD OS VERSION
web-01 m-1 Ready HighLoad, MemoryPressure 3d 4h 4.12 3.90 3.71 Ubuntu 24.04 v1.4.0
web-02 m-2 Ready - 12h 0.31 0.28 0.25 Ubuntu 24.04 v1.4.0
db-01 m-3 Ready DiskPressure 5d 1.22 1.10 1.05 Ubuntu 24.04 v1.3.2

The VERSION column is the build of each agent. During an upgrade, it shows which agents run the new build. The STATE column is the scheduling state: Ready, Draining, Cordoned or Pending.

agent get shows each condition that holds, with its reason:

Conditions (2):
TYPE REASON SINCE
MemoryPressure memory 94% used (15.1/16.0 GB) 2m
HighLoad load 4.12, threshold 4.00 for 2 CPUs 5m

Configuration​

Set the limits in osapi.yaml:

KeyTypeDefaultEnvironmentDescription
agent.conditions.memory_pressure_thresholdint90OSAPI_AGENT_CONDITIONS_MEMORY_PRESSURE_THRESHOLDMemory pressure threshold (percent used).
agent.conditions.high_load_multiplierfloat2OSAPI_AGENT_CONDITIONS_HIGH_LOAD_MULTIPLIERHigh load multiplier (load1 / cpu_count).
agent.conditions.disk_pressure_thresholdint90OSAPI_AGENT_CONDITIONS_DISK_PRESSURE_THRESHOLDDisk pressure threshold (percent used).

Agent drain​

A drain removes an agent cleanly from the agents that get jobs, for maintenance. The process continues to run. If an agent stops without a drain, it goes from the registry, and that looks the same as a crash.

State machine​

Agents have an explicit scheduling state with three values:

StateMeaning
ReadyAccepts and processes jobs (default)
DrainingCompletes its running jobs, accepts no new jobs
CordonedFully drained and idle, accepts no jobs

How it works​

  1. The operator runs osapi client agent drain --hostname web-01.
  2. The API writes a drain.<machine ID> key to the state KV bucket.
  3. The agent finds the drain flag at its next heartbeat (10s).
  4. The agent changes to Draining and stops its NATS JetStream subscriptions. Thus it gets no new jobs.
  5. The running jobs continue until they are complete.
  6. When all running jobs are complete, the state changes to Cordoned.
  7. The operator runs osapi client agent undrain --hostname web-01.
  8. The API deletes the drain key. The agent subscribes again and changes to Ready.

Timeline​

The agent records each change of state as an event in the state KV bucket (agent-state, no TTL), under its machine ID. It only adds events, and does not change them. A rename keeps the timeline, and each event keeps the hostname that the agent had then. agent get shows all the changes:

Timeline:
TIMESTAMP EVENT HOSTNAME MESSAGE
2026-03-05 09:58:10 enrollment_requested web-01 asked to be admitted, fingerprint SHA256:9f86d0...
2026-03-05 09:58:10 admitted web-01 admitted by join token 1a2b3c4d
2026-03-05 09:58:11 started web-01 started, version v1.4.0
2026-03-05 10:00:00 drain web-01 Drain initiated
2026-03-05 10:05:23 cordoned web-01 All jobs completed
2026-03-05 12:00:00 undrain web-01 Resumed accepting jobs

The timeline also has the lifecycle of the machine. The controller writes each decision of admission, and the agent writes its own start and stop:

EventWritten byMessage
enrollment_requestedcontrollerthe fingerprint of the request: the first request, or one with a new key
admittedcontrollerby an operator, by auto_admit, or by join token <id>
rejectedcontrollerby an operator, with the reason
revoked, deletedcontrollerby an operator, or for deleted by delete_after with when it was seen
hostname_changedcontrollerthe old and the new hostname
started, stoppedagentits version, at a start
config_appliedagentthe version of its config and its labels, when the labels changed
labels_changedagentthe old and the new labels, from agent.labels at a start
os_changedagentthe old and the new OS and version, such as after an upgrade
address_changedagentthe old and the new IPv4 address of the interface of the default route
condition_startedagentthe condition and its reason, such as MemoryPressure: 92% used
condition_clearedagentthe condition

The agent compares each heartbeat and each facts report with the one before. After a start, the one before is the registration and the facts that the buckets still hold, so a change while the agent was down, such as an OS upgrade, is an event. An agent with no report before has no change events: its first heartbeat is where the comparison starts. A change of the labels that the controller's config makes is in config_applied, so it has no labels_changed of its own.

The state of the agent comes from the latest drain event (drain, cordoned, undrain). A lifecycle event after a drain does not end the drain.

CLI commands​

osapi client agent drain --hostname web-01 # start draining
osapi client agent undrain --hostname web-01 # resume accepting jobs

The two commands return the current state and a confirmation message.

Permissions​

The agent and admission reference pages give the permission of each method.