Skip to content

fix(infra): drain check refuses each Longhorn storage node #7376

Done

Description

@dan-petty

Problem Description

The drain check of devops infra stops each run for a node that holds Longhorn storage. devops infra node drain, node restart, node reboot and the drain of devops infra apply first run kubectl drain <node> --ignore-daemonsets --delete-emptydir-data --timeout=30s --dry-run=server (drain_check in src/devops_cli/infra/nodes.py) and stop when it exits with an error (_drain in src/devops_cli/infra/node_run.py and src/devops_cli/infra/apply.py).

Longhorn keeps one PodDisruptionBudget instance-manager-<id> for each storage node while the node is schedulable, with 0 allowed disruptions. It deletes the budget only after the node is cordoned, when its node drain policy allows the drain (syncInstanceManagerPDB and canDeleteInstanceManagerPDB in controller/instance_manager_controller.go of longhorn-manager v1.13.0). So a server dry run before the cordon always names the instance-manager pod, and the check refuses a drain that would end.

The homelab cluster has three storage nodes since Longhorn was installed (dan-petty/homelab#412). By the code, no node command and no apply with a drain can pass the check for these three hosts.

Reproduction Steps

  1. kubectl get pdb -n longhorn-system: three budgets instance-manager-<id>, ALLOWED DISRUPTIONS 0. The cluster has no Longhorn volume.
  2. kubectl drain <storage node> --ignore-daemonsets --delete-emptydir-data --force --timeout=30s --dry-run=server (read-only, run on 2026-11-27 with kubectl v1.37.3 for the build of dan-petty/homelab#429): exit code 1 after 33 seconds. The last line is error when evicting pods/"instance-manager-<id>" -n "longhorn-system": global timeout reached: 30s. No node is cordoned and no pod changes.
  3. The same dry run for a node with no Longhorn pod exits 0 in under a second.

Not reproduced with devops infra node drain itself: no real drain ran. The evidence is the dry run and the code path.

Expected vs Actual Behavior

Expected: the drain check stops a run only for a pod that the real drain cannot evict.

Actual: the check stops each run for a Longhorn storage node, because the dry run comes before the cordon and Longhorn frees its pod only after the cordon.

The same limit holds for a CloudNativePG cluster with more than one instance: the operator moves the primary to another instance only when the node of the primary is cordoned (document "Kubernetes upgrade and maintenance" of CloudNativePG 1.30). The homelab cluster has no such database now.

Environment Diagnostics

kubectl v1.37.3, cluster v1.37.2+k3s1, Longhorn v1.13.0 with the node drain policy block-if-contains-last-replica, devops-cli main of 2026-11-27.

Logs, Traces, or Terminal Output

error when evicting pods/"instance-manager-<id>" -n "longhorn-system" (will retry after 5s): Cannot evict pod as it would violate the pod's disruption budget.
error: unable to drain node "<storage node>" due to error: error when evicting pods/"instance-manager-<id>" -n "longhorn-system": global timeout reached: 30s, continuing command...
There are pending nodes to be drained:
 <storage node>
error when evicting pods/"instance-manager-<id>" -n "longhorn-system": global timeout reached: 30s

Acceptance criteria

  • A node command and an apply pass the drain check for a Longhorn storage node whose dry run names only the instance-manager pod of Longhorn, or the check runs in a state in which Longhorn has freed that pod.
  • The check still stops a run for a pod that a budget with no allowed disruption covers, for example the one instance of a CloudNativePG cluster.
  • A test covers a dry run that names only the instance-manager pod, and a dry run that names that pod and one more pod.

The upgrade play of the homelab repository leaves the instance-manager pod out of its own check (dan-petty/homelab#429).

Source

Source: https://github.com/dan-petty/homelab/issues/429

Alternatives check of 2026-11-27: none.
Tool: none found.
Reason: Searches surfaced only node-reboot daemons (kured, system-upgrade-controller) that cordon and then drain, and Longhorn's own cordon-dependent PDB; no open tool offers a reusable drain preflight check, so drain_check must be fixed in devops-cli.

Lacks:

  • No reusable drain preflight check in any found tool
  • No way to tell a PDB that clears after the cordon
  • Daemons replace the node workflow instead of adding a check
  • No dry-run option that accounts for Longhorn instance-manager budgets

Proposed design

Problem statement

Fix the drain check to ignore only the Longhorn instance-manager block. The drain check runs a server dry run before the cordon. Longhorn keeps an instance-manager PodDisruptionBudget with zero allowed disruptions until the node is cordoned. The dry run therefore fails for each Longhorn storage node. Node commands and applies stop before a drain that would finish. The fix keeps every other block and keeps the check read-only.

Acceptance criteria

Requirement: Drain check ignores the Longhorn instance-manager block

The drain check SHALL return no problem when the server dry run fails only for Longhorn instance-manager pods in the longhorn-system namespace.

Scenario: Dry run names only the Longhorn instance-manager pod

  • WHEN The server dry run exits 1 and its error output names only a pod in longhorn-system whose name starts with instance-manager-.
  • THEN Cluster.drain_check returns None, and the unit test with the recorded dry-run output passes.

Requirement: Drain check stops for a second blocked pod

The drain check SHALL return the dry-run problem when the server dry run fails for a pod other than a Longhorn instance-manager pod.

Scenario: Dry run names the Longhorn instance-manager pod and one more pod

  • WHEN The server dry-run error output names a longhorn-system/instance-manager-* pod and a pod in another namespace whose PodDisruptionBudget allows no disruption.
  • THEN Cluster.drain_check returns a problem that names the other pod, and the node command and the apply stop before the drain.

Requirement: Drain check stops for a single CloudNativePG instance

The drain check SHALL stop a run when the server dry run fails for the only instance of a CloudNativePG cluster.

Scenario: Dry run names the single CloudNativePG instance

  • WHEN The server dry-run error output names a pod of a CloudNativePG cluster with one instance and no Longhorn instance-manager pod.
  • THEN Cluster.drain_check returns the problem, and the node command and the apply stop before the drain.

Requirement: Drain check reads every eviction error

The drain check MUST inspect every error when evicting pods/... line in the server dry-run output.

Scenario: Longhorn error comes before another blocked pod

  • WHEN The server dry-run output has an instance-manager-* eviction error line before an eviction error line for another pod.
  • THEN Cluster.drain_check returns a problem for the other pod, and the unit test with the recorded output passes.

Requirement: Node command and apply reach the drain

The node command and the apply SHALL reach the drain step for a Longhorn storage node whose dry run names only the instance-manager pod.

Scenario: Node command and apply use the corrected drain check

  • WHEN A node command or an apply runs the drain check for a Longhorn storage node and the dry run names only the instance-manager pod.
  • THEN The run calls the drain step and does not stop at the drain check.

Existing Tool Considered

The alternatives check in the issue body records verdict none and no tool. No open, maintained tool or library offers a reusable drain preflight check. The fix stays in drain_check in src/devops_cli/infra/nodes.py. Cost to build, keep and fix the parser: about 3,000 tokens and 1 hour.

Key questions

  • How should the drain check treat the Longhorn instance-manager PodDisruptionBudget?: Ignore a dry-run failure only when every blocked pod is a Longhorn instance-manager pod in longhorn-system. Return the problem when any other pod is blocked. (best practice) [sources: src/devops_cli/infra/nodes.py:421, src/devops_cli/infra/node_run.py, src/devops_cli/infra/apply.py]
    • Chosen: Ignore only Longhorn instance-manager errors. It keeps the check read-only and keeps every other PodDisruptionBudget block.
    • Rejected: Cordon the node before the dry run. It changes the node before the check and leaves the node cordoned when the check fails.
    • Rejected: Exclude Longhorn pods with --pod-selector. It hides the Longhorn pod from the dry run and does not test the recorded failure output.
  • How should the drain check find every blocked pod in the dry-run output?: Read every error when evicting pods/... line in stderr. Do not use only the last line. (best practice) [sources: src/devops_cli/infra/nodes.py:421]
    • Chosen: Read every eviction error line. A second blocked pod can appear before the last line, and the check must stop for it.
    • Rejected: Use only the last stderr line. It hides a second blocked pod and lets the drain check pass.
  • Should the check query Longhorn to decide whether the PodDisruptionBudget will be deleted after the cordon?: No. The fix ignores the Longhorn instance-manager block and keeps every other block. The real drain remains the final guard if Longhorn keeps the budget for a last replica. (best practice) [sources: src/devops_cli/infra/nodes.py:421]
    • Chosen: Ignore the Longhorn instance-manager block. It is a small, read-only fix and matches the acceptance tests.
    • Rejected: Query Longhorn custom resources for the node drain policy. It copies version-specific Longhorn logic and adds a new API dependency.

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

  1. added
    type/bug
    scope/infra
    source/agent
    status/new
    on Nov 27, 2026
  2. dan-petty commented on Nov 27, 2026

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

    @devops-cli-service
    Contributor

    The result in the alternatives block of the issue body changed. Before this check: no check. A person decides what the new result means for the item.

    Searches (10)

    Tools considered:

    Searches run:

    • kubectl drain dry-run Longhorn instance-manager PodDisruptionBudget blocks drain before cordon, results: 5
    • draino kured node drain respects PodDisruptionBudget blocking pod selector, results: 5
    • tool node drain preflight check ignores PDB pods evicted after cordon, results: 5
    • longhorn node drain policy block-if-contains-last-replica instance-manager PDB, results: 5
    • kured blocking-pod-selector drain, results: 5
    • kubectl drain pod-selector flag documentation, results: 5
    • longhorn kubectl drain stuck instance-manager pod disruption budget github issue, results: 5
    • kubectl drain dry-run server eviction pdb instance-manager longhorn workaround cordon first, results: 5
    • kubernetes node drain simulator preflight check tool github, results: 5
    • longhorn documentation node drain policy instance-manager PDB cordon, results: 5

    Sources:

  5. added
    status/ready
    and removed
    status/new
    on Nov 28, 2026
  6. devops-cli-service commented on Nov 28, 2026

    @devops-cli-service
    Contributor

    Next step: the stage job writes the plan of the item. See the section Proposed design of the body.

  7. added
    owner/agent
    status/in-progress
    status/in-review
    and removed
    status/ready
    status/in-progress
    on Nov 28, 2026
  8. removed
    status/in-review
    owner/agent
    on Nov 28, 2026
  9. devops-cli-service commented on Nov 29, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-29 00:41 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/bugvalue/medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions