Skip to content

feat(infra): add node drain, uncordon, restart and reboot #5128

Description

@dan-petty

Problem or Motivation

dan-petty/homelab has a Makefile target for each host and operation: drain-hawk, uncordon-hawk, restart-hawk and the same for 4 more hosts, plus rolling-restart. Each needs a person, has no health gate, and restart-* uses a play that drains inside Ansible. The play of rolling-restart (tasks/k3s_node_reboot.yaml) skips a node that is already cordoned, so it cannot be combined with a drain from outside. Agents may drain, restart k3s and reboot one node at a time (owner decision 2026-11-12, homelab at level full).

Proposed Solution & Architectural Design

devops infra node drain|uncordon|restart|reboot <repo> <host>... (design sections 5 and 7 in tracker #5120), one host at a time in the given order.

  • drain: health gate, record the cordon state, kubectl drain --ignore-daemonsets --delete-emptydir-data --timeout (no --force unless a person gives it).
  • uncordon: kubectl uncordon, then the health gate.
  • restart: drain, restart devops_infra_node_service with an ansible-runner ad-hoc ansible.builtin.systemd_service, wait for the unit and Ready, check GPU allocatable, uncordon only when the node was schedulable before, health gate.
  • reboot: the same with ansible.builtin.reboot, and the kernel before and after in the output.
  • Access: uncordon needs agent level safe; the others full. Audit record and devops_cli_infra_runs_total as in feat(infra): apply a saved plan one node at a time #5125. The first failure stops the run and leaves the node cordoned. --dry-run makes zero requests.

These commands replace the per-host Makefile targets and rolling-restart; dan-petty/homelab#22 removes them.

Target Subsystem

OpenTofu / Infrastructure as Code

Existing Tool Considered

kubectl and the Ansible modules systemd_service and reboot (check mode support full) do the work; the guards of #5125 are shared. kured reboots nodes with a lock and alert gates, but only when a reboot sentinel exists and with no plan or restart; a later item can add it for unattended kernel reboots. Cost: about 2.5M tokens to build, 0.3M a quarter to keep, 3 hours.

Acceptance Criteria

  • Tests with a fake kubectl and recorded events: order of steps for each operation; a cordoned node stays cordoned; a failed Ready wait stops before the next host; restart refuses a host with no devops_infra_node_service; level refusals.
  • A test: --dry-run starts no process.
  • Live run at level full by an agent, recorded in a PR comment with the audit records: after the crash-capture apply of feat(infra): apply a saved plan one node at a time #5125 (dan-petty/homelab#20), node reboot dan-petty/homelab hawk.lan drains hawk, reboots it, shows the kernel before and after, checks 1 GPU allocatable, and hawk stays cordoned. After the boot, kdump has reserved its memory (/sys/kernel/kexec_crash_size is above 0).
  • Live run: node restart dan-petty/homelab hawk.lan restarts k3s-agent; hawk is Ready and stays cordoned.

Dependencies

#5125; dan-petty/homelab#21 (the node service variable).

Proposed design

Problem statement

dan-petty/homelab keeps one Makefile target for each host and operation. Each target needs a person, has no health gate, and the restart play drains inside Ansible. The rolling-restart play skips a node that is already cordoned, so an outside drain cannot combine with it. This item adds devops infra node drain|uncordon|restart|reboot <repo> <host>... with one host at a time in the given order. Each run has a health gate, a recorded cordon state and an audit record, and it stops at the first failure.

Acceptance criteria

Requirement: drain-order

The node drain command SHALL run the node health check, the server-side drain check, and then kubectl drain --ignore-daemonsets --delete-emptydir-data --timeout <bound> with the drain bound.

Scenario: Drain checks before it cordons

  • WHEN A test runs node drain on one host with the default settings and a fake kubectl that records each call.
  • THEN The recorded calls show the health check, then kubectl drain --dry-run=server, then kubectl drain --ignore-daemonsets --delete-emptydir-data --timeout=180s, in that order.

Requirement: drain-bound

The node drain command SHALL bound the drain by infra.drain_timeout_seconds, or by the longest grace period of a pod that the drain evicts plus 60 s when that is longer.

Scenario: A long grace period extends the bound

  • WHEN A test runs node drain with infra.drain_timeout_seconds set to 180 and a fake kubectl that returns one pod on the node with a grace period of 900 s.
  • THEN The recorded kubectl drain call has --timeout=960s.

Requirement: drain-force-needs-the-flag

The drain MUST NOT pass --force to kubectl drain unless the person gives --force.

Scenario: Force needs the flag

  • WHEN A test runs node drain on one host with no --force, and again with --force.
  • THEN The first recorded kubectl drain call has no --force, and the second recorded call has --force.

Requirement: uncordon-order

The node uncordon command SHALL run kubectl uncordon and then the node health check.

Scenario: Uncordon comes before the health check

  • WHEN A test runs node uncordon on one host with a fake kubectl.
  • THEN The recorded calls show kubectl uncordon and then the health check.

Requirement: restart-steps

The node restart command SHALL drain, restart the devops_infra_node_service unit, wait for the unit and Ready, check the allocatable GPU count, uncordon only when the node was schedulable before, and run the health check.

Scenario: Restart follows the drain

  • WHEN A test runs node restart on a schedulable host with a fake kubectl and a fake ansible-runner.
  • THEN The recorded calls are in that order, and the health check follows kubectl uncordon.

Requirement: restart-keeps-cordon

The node restart command MUST NOT uncordon a node that was not schedulable before the run.

Scenario: A cordoned node stays cordoned

  • WHEN A test runs node restart on a host that is already cordoned.
  • THEN The recorded calls hold no kubectl uncordon, and the command exits 0.

Requirement: restart-needs-a-unit

The node restart command MUST refuse a host that sets no devops_infra_node_service.

Scenario: No unit, no restart

  • WHEN A test runs node restart on a host with no devops_infra_node_service in its host variables.
  • THEN The command exits non-zero, names devops_infra_node_service, and the fake kubectl and ansible-runner record no call.

Requirement: reboot-steps

The node reboot command SHALL drain, reboot with ansible.builtin.reboot, print the kernel release before and after, wait for Ready, check the allocatable GPU count, uncordon only when the node was schedulable before, and run the health check.

Scenario: Reboot shows both kernels

  • WHEN A test runs node reboot on one schedulable host with a fake kubectl and a fake ansible-runner.
  • THEN The output holds the kernel release of the fake host before and after the reboot, and the health check follows kubectl uncordon.

Requirement: agent-levels

The command MUST need agent level safe for uncordon and level full for drain, restart and reboot.

Scenario: The safe level allows only the uncordon

  • WHEN A test runs each of the four operations on a repository at level safe.
  • THEN uncordon exits 0 and records kubectl uncordon; drain, restart and reboot exit non-zero with the level refusal and start no process.

Requirement: dry-run-does-nothing

The command MUST start no process and send no request when the person gives --dry-run.

Scenario: Dry run makes no request

  • WHEN A test runs each of the four operations with --dry-run, a fake kubectl and a fake ansible-runner.
  • THEN No process starts and the fake runners record no call.

Requirement: one-host-at-a-time

The command SHALL run one host at a time in the given order and MUST stop at the first failure.

Scenario: The second host waits

  • WHEN A test runs node drain on two hosts and the first host fails.
  • THEN The command exits non-zero and the fake kubectl records no call for the second host.

Requirement: failure-keeps-the-cordon

The command MUST leave the node cordoned when a step after the drain fails.

Scenario: A failed wait keeps the cordon

  • WHEN A test runs node restart and the Ready wait fails after the drain.
  • THEN The recorded calls hold no kubectl uncordon, and the command exits non-zero.

Requirement: audit-record-and-counter

Each run SHALL write one audit record with the operation, the host, the caller and the level, and add one to devops_cli_infra_runs_total with the command and the result.

Scenario: One record and one count for each run

  • WHEN A test runs a node drain that succeeds and one that stops.
  • THEN The audit log holds two records with results ok and stopped, and the counter adds one for each with the same command.

Existing Tool Considered

kubectl and ansible-core 2.22.0's ansible.builtin.systemd_service and ansible.builtin.reboot do the low-level work. The Kubernetes documentation gives kubectl drain the --ignore-daemonsets, --delete-emptydir-data, --timeout and --force flags (https://kubernetes.io/docs/reference/kubectl/generated/kubectl_drain/, fetched 2026-11-17), and ansible-core 2.22.0 is the pinned tool (src/devops_cli/tools_lock/ansible-core.in:1). The tools lack the health gate, the order, the recorded cordon state, the audit record and the first-failure stop; this change adds them around the tools, and the guards of #5125 are shared. kured reboots nodes with a lock and alert gates, but it acts only when a reboot sentinel exists and it does no plan and no restart, so a later item can add it for unattended kernel reboots. The written cost to build the orchestration instead is about 2.5M tokens, 0.3M tokens a quarter to keep it, and 3 hours.

Key questions

  • Which existing code and tools do the drain and the uncordon of a node?: src/devops_cli/infra/nodes.py already holds the drain and the uncordon of a node. src/devops_cli/infra/apply.py calls them with the drain check and the uncordon rule. The new commands call the same code and add no second drain. [sources: src/devops_cli/infra/nodes.py:360, src/devops_cli/infra/nodes.py:366, src/devops_cli/infra/apply.py, CONTEXT.md]
  • Where does the unit name of the node service come from?: From the inventory host variable devops_infra_node_service. The constant NODE_SERVICE_VARIABLE names it in src/devops_cli/infra/onboard.py:62, and the homelab fixtures set it for each node. In homelab the value is a template, which only Ansible renders, so the command reads it through an Ansible ad-hoc ansible.builtin.debug task and the runner event reader, not from the raw ansible-inventory --list value. The real homelab repo must set it too, and dan-petty/homelab#21 adds it. [sources: src/devops_cli/infra/onboard.py:62, tests/fixtures/infra/homelab_shape/ansible/inventory/host_vars/node-1.example.com.yaml:5, openspec/changes/archive/2026-11-12-5125-apply-a-saved-plan-one-node/design.md:13, openspec/changes/archive/2026-11-12-5121-onboard-an-ansible-infra-repo/design.md:30]
  • Which agent access level does each operation need?: uncordon needs level safe. drain, restart and reboot need level full, because each one takes a host out of service. The owner decision of 2026-11-12 gives agents full on dan-petty/homelab. (best practice) [sources: src/devops_cli/config/settings.py:2304, SECURITY.md:68, CONTEXT.md]
    • Chosen: uncordon at safe, the others at full. The setting description gives safe the uncordon and full the drain, restart and reboot, so an uncordon that an agent may do is not refused.
    • Rejected: all four at full. It refuses an uncordon that an agent may do, and the owner decision names the level for each operation.
    • Rejected: all four at safe. A drain takes a host out of service, and level safe is too low for that.
  • How does the command reach a host for the restart and the reboot?: With an ansible-runner ad-hoc task: ansible.builtin.systemd_service for the restart and ansible.builtin.reboot for the reboot. The command reads the inventory of the infra repo at a commit that origin holds. (best practice) [sources: CONTEXT.md, src/devops_cli/infra/plan.py, src/devops_cli/infra/apply.py, src/devops_cli/infra/ansible_facts.py:124]
    • Chosen: ad-hoc ansible-runner task. One task needs no playbook in the infra repo, and the two modules support check mode.
    • Rejected: a generated playbook through the infra plan and apply. A plan records a check-mode run of a playbook that the infra repo holds. These commands change one unit or reboot one host.
    • Rejected: ssh and systemctl from the CLI. It bypasses the inventory and the Ansible connection of the infra repo.
  • What bounds the drain in time, and when does the command pass --force?: infra.drain_timeout_seconds (default 180 s, DEVOPS_CLI_INFRA_DRAIN_TIMEOUT_SECONDS) bounds the drain. The bound is the longer of that setting and the longest grace period of a pod that the drain evicts plus 60 s, as Cluster.drain_bound already computes it. The command passes --force only when the person gives --force; drain_args adds no --force, because --force continues past a pod that no controller owns and the apply's drain has no --force. (best practice) [sources: src/devops_cli/config/defaults.py:58, docs/CONFIGURATION.md:361, src/devops_cli/infra/nodes.py:351, src/devops_cli/infra/nodes.py:84, openspec/changes/archive/2026-11-12-5125-apply-a-saved-plan-one-node/design.md:9, https://kubernetes.io/docs/reference/kubectl/generated/kubectl_drain/]
    • Chosen: reuse infra.drain_timeout_seconds and extend it to the longest evicted pod grace period plus 60 s. It is the project's existing bound, drain_bound already implements it, and it keeps the standalone drain equal to the apply's drain.
    • Rejected: fixed --timeout 300s. It duplicates infra.drain_timeout_seconds, ignores the configured value, and differs from the apply's drain.
    • Rejected: kubectl's default of no timeout. A PodDisruptionBudget can block a drain forever, so the first-failure rule never fires.
    • Rejected: require --timeout on each run. It adds a step to each run, and the setting already gives a safe default.
  • Which audit record and which metric does a node run write?: One audit record with the operation, the host, the caller and the level, and one addition to devops_cli_infra_runs_total with the command and the result. The counter already divides runs by command, so the node commands add their own command values; the description of the counter names only apply at src/devops_cli/telemetry/instruments.py:873 and docs/TELEMETRY.md:205, so the change adds the node command values there. (best practice) [sources: openspec/specs/infra-apply/spec.md:146, src/devops_cli/telemetry/instruments.py:870, docs/TELEMETRY.md:205]
    • Chosen: reuse the audit record and devops_cli_infra_runs_total. The spec and the dashboard already divide the runs by command, and a second counter duplicates the alert rules.
    • Rejected: a new counter devops_cli_infra_node_runs_total. It doubles the dashboard and the alert rules for the same question.
  • Which order of steps does a restart use?: The drain, then the unit restart, then the wait for the unit and Ready, then the GPU allocatable check, then an uncordon only when the node was schedulable before, then the health check. (best practice) [sources: CONTEXT.md, openspec/specs/infra-apply/spec.md:90]
    • Chosen: drain, restart, wait, GPU check, conditional uncordon, health check. It matches the apply order: drain check, drain, Ready, the health of the nodes, and an uncordon only when the node was schedulable before.
    • Rejected: restart the unit before the drain. A unit restart on a node that still runs pods risks a second disruption, and the apply drains first.
    • Rejected: always uncordon after the restart. A node that a person cordoned stays cordoned, as the apply does.
  • What does --dry-run do?: It starts no process and sends no request. The term in CONTEXT.md says a dry run makes no request, and ARCHITECTURE.md says a dry run sends no external request and prints the requests that a real run would send. (best practice) [sources: CONTEXT.md, ARCHITECTURE.md:170]
    • Chosen: no process and no request. The term in CONTEXT.md says a dry run makes no request, and the issue requires that --dry-run starts no process.
    • Rejected: run the health check read-only. A health check sends a request, and the term says a dry run makes none.

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

#5125

Activity

  1. added
    type/feature
    source/agent
    scope/infra
    status/new
    value/high
    effort/medium
    on Nov 12, 2026
  2. dan-petty commented on Nov 12, 2026

    @dan-petty
    OwnerAuthor
  3. added
    priority/p2-medium
    on Nov 12, 2026
  4. devops-cli-service commented on Nov 17, 2026

    @devops-cli-service
    Contributor

    Waiting for the alternatives check to run. The next refine round looks at the item again after the check records a verdict.

  5. added
    owner/agent
    status/in-progress
    and removed
    status/new
    on Nov 21, 2026
  6. added
    status/in-review
    and removed
    status/in-progress
    on Nov 21, 2026
  7. removed
    status/in-review
    owner/agent
    on Nov 22, 2026
  8. devops-cli-service commented on Nov 22, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-22 01:30 UTC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    effort/mediumpriority/p2-mediumscope/infrasource/agenttype/featurevalue/high

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions