Workflow controller

The Workflow controller runs a Workflow's graph. It creates a node's Task when the node's needs are settled, and it reads each Task's result to settle the node. It decides from the Workflow, the Tasks that carry its uid label and the time. The manager performs each create, each delete and each Board append.

Read Answers
Workflow What do I apply, and what does the status hold?
Node decisions When does a node start, and when is it settled?
Interface What does the controller read, and what does the manager do?
Task controller Who runs each Task that a Workflow creates?

One run #

  1. You apply a Workflow. The store gives it the finalizer fleet.dev/workflow-tasks.
  2. In each sweep, the manager reconciles each Workflow that is not settled. It also reconciles a Workflow that has no status, whose spec generation changed, that has events to publish, or that is being deleted.
  3. The controller adds a Waiting entry for each node to status.nodes.
  4. It reads the Task of each node that is not settled. A Task with a result settles the node.
  5. It settles the Waiting nodes that cannot run, and starts the nodes that can run, up to parallelism.
  6. It writes the status: the intent of each create, the node phases, the Workflow's phase and the events of each change. Then the manager creates the Tasks and publishes the events.
  7. When every node is settled, the Workflow is Succeeded or Failed. The manager does not reconcile it again until you delete it.

Implementation and evidence #

Responsibility Source Tests
Pure decisions: node phases, creates, retires, events, recovery WorkflowController Controller passes, Workflow E2E
Resource, status, graph check, the brief section and the events Workflow types, validation Unit tests in workflow.rs; Workflow E2E
Sweep, intent before effect, fenced create and retire, receipts, Board appends, the finalizer Manager, owned Tasks Workflow E2E
The workflow.fleet.dev/ labels, retire_workflow_task, the finalizer Store Store contract
fleet status workflow CLI Workflow E2E
Crate edges and the field manager architecture.toml Architecture suite

The E2E suite runs the manager on the file store with a test clock and the fake runtime. It covers a node that starts when its needs ship and reads what they left, a failed need that skips a node and fails the Workflow, an always node, parallelism, a Task subscribed to fleet.workflow.phase, a delete that retires the Tasks before the Workflow goes, and a second apply that changes nothing. Through the fleet binary, it covers the refusals of a bad graph and of a spec change, and fleet status workflow.

Node decisions #

Workflow node: a node waits for its needs, then is Skipped or runs a Task, and its Task's outcome makes it Succeeded or Failed

In each reconcile, the controller applies these steps in order:

  1. Adopt: when a create has an intent and no Created receipt, and its Task exists with this Workflow's uid, the create gets the receipt Created, with adopted true.
  2. Observe: for each node that is not settled:
    • Its Task exists: the node is Running. When the Task has a result, the node is Succeeded for the outcome shipped, and Failed for any other outcome.
    • Its Task does not exist, the node is Running, and the store refused the create three times: the node is Failed, with reason CreateFailed: <why>.
    • Its Task does not exist, the node is Running, and the controller had recorded its uid: the node is Failed, with reason TaskGone.
  3. Skip: a Waiting node whose needs are all settled, whose when is succeeded and that has a need that is not Succeeded is Skipped. The reason names the need and its phase. The controller repeats this step until no node changes, so a skip goes down the graph in one reconcile.
  4. Start: a Waiting node whose needs are all settled, and that is not skipped, is ready. The controller starts the ready nodes in the order of spec.nodes, while fewer than parallelism nodes are Running. A start writes the create's intent under create/<node>, and the node is Running.
  5. Create: for each Running node whose Task does not exist, whose create has no Created receipt and is not given up, the controller plans the create.
  6. Phase: the controller computes the Workflow's phase from the node phases. See Lifecycle.
  7. Events: for each phase change, of the Workflow or of a node, the controller adds an event to pendingEvents.

A settled node never changes again. A Task that carries a Workflow's uid label and has a result is locked in the store:

  • The store refuses an amendment to it, from fleet amend or a plugin's amends[].
  • The store refuses fleet send TASK --retry KEY for an amendment that was queued before the result and given up on. fleet send TASK --drop KEY stays allowed.
  • The store refuses a status write that changes its result.

So the result that settles a node is always its Task's first result.

The Task of a started node has:

  • the name <workflow>-<node>;
  • the labels of template.metadata.labels, and workflow.fleet.dev/name and workflow.fleet.dev/uid;
  • the spec template.spec, with each field of the node's spec in place of the field with the same name;
  • for a node with needs, a brief with the section that quotes what each need left. See The brief of a node with needs.

Conditions #

Type Status Reason Meaning
NeedsAttention True CreateUnknown The store could not say, through three attempts, whether a node's Task was created. The message names the Task.
NeedsAttention False Resolved No create is in doubt any more.

Workflow controller interface #

Controller API #

WorkflowController is in fleet-controllers. It is pure.

Item Meaning
WorkflowInput The Workflow, the Tasks whose workflow.fleet.dev/uid label is its uid, and now.
WorkflowController::due(obj) True when the Workflow has no status, is being deleted, its spec generation is not observedGeneration, pendingEvents is not empty, or its phase is not settled.
WorkflowController::reconcile(input) Returns a Plan: the status, a Create for each node Task to create, a RetireWorkflowTask effect for each Task to delete, and remove_finalizer when a deleted Workflow has no Task left.

Manager execution protocol #

The manager reconciles the due Workflows after the CronTasks in each sweep. When no Workflow is due, it reads nothing more. Otherwise it lists the Tasks once for all due Workflows. When it cannot list the Tasks, it reconciles no Workflow: an Unknown listing is never read as no Task.

For each due Workflow the manager does these steps:

  1. It writes the planned status, compare-and-swap under the lease. The status holds the intent of each create and delete, and the events to publish.
  2. It performs each create with Store::create_fenced. Then it writes the receipt under the key.
  3. It performs each delete with Store::retire_workflow_task, fenced on the Task's uid. Then it writes the receipt under the key.
  4. It appends each event in pendingEvents to the Board fleet, as controller/workflow-controller. Then it removes the appended events from pendingEvents.
  5. When the plan says so, it removes the finalizer fleet.dev/workflow-tasks.

A delete only marks the Task. The Task controller releases its session. The Workflow keeps its finalizer until the Task listing has no Task of this Workflow.

Recovery #

An intent with no receipt means that the manager stopped between the two writes. The controller answers it from the Task listing:

  • The Task exists and carries this Workflow's uid: the controller records the receipt Created, with adopted true.
  • The Task does not exist: the controller makes the create again, under the same key and the same name.
  • A create that finds a Task with the same name but without this uid gets the receipt Failed. The controller never takes another Workflow's Task.
  • A create that the store refused is tried again in the next reconcile. The record counts the earlier refusals in refusals. The latest one is in receipt. Attempts whose outcome was Unknown do not count. After three refusals the create is given up, and the node is Failed.
  • A create whose outcome is Unknown is tried again in each reconcile, with no limit: Fleet never reads Unknown as absence. The record counts the attempts in doubt in unknowns. Only a Known outcome clears the count. After three attempts in doubt, NeedsAttention is True with reason CreateUnknown.
  • A delete is planned again in each reconcile while the Task exists and is not marked for deletion.
  • An event that the manager did not append stays in pendingEvents, and the next reconcile appends it. Each event has a key, so the Board does not keep a second copy of an event that the manager appended twice.
  • When the Board refuses an event, such as when the Board fleet does not exist, the manager drops that event. It is not published again. Run fleet init to make a missing Board fleet.

Store port and write ownership #

Write Who Rule
Workflow status workflow-controller It owns every field.
A Task, created workflow-controller Only through create_fenced, under the lease.
The workflow.fleet.dev/name and -uid labels workflow-controller Set at create. fleet apply refuses them, the store refuses them from any other writer, and nobody changes them later.
A Task, deleted workflow-controller Only through retire_workflow_task, under the lease: the Task's uid must match, and the Task must carry this Workflow's uid. The Task may be running.
The finalizer fleet.dev/workflow-tasks workflow-controller Removed only when no Task of the Workflow is left.
Events of type fleet.workflow.* on the Board fleet workflow-controller Appended under the lease, with the source controller/workflow-controller.