Skip to content

Run rolling maintenance hooks on plain host maintenance - #14777

Open
bhouse-nexthop wants to merge 1 commit into
apache:mainfrom
bhouse-nexthop:kvm-maintenance-hooks
Open

bhouse-nexthop wants to merge 1 commit into
apache:mainfrom
bhouse-nexthop:kvm-maintenance-hooks

Conversation

@bhouse-nexthop

Copy link
Copy Markdown
Collaborator

Description

This PR adds an opt-in setting, kvm.host.maintenance.run.hooks (cluster scope, off by default). When it is on, plain host maintenance runs the existing rolling maintenance hook scripts. It also adds a PostMigrate stage that runs once the host is empty. The PR reuses RollingMaintenanceCommand and rolling.maintenance.hooks.dir and needs no agent change: a stage with no script is skipped, so older agents still work.

Path Stage When On failure
prepareHostForMaintenance PreFlight, PreMaintenance before VMs move maintenance refused (also on exit 70, or if the host is not Up)
host reaches Maintenance PostMigrate (new) host is empty, runs in the background alert, state unchanged
cancelHostMaintenance PostMaintenance before the host is enabled again alert, cancel goes on
startRollingMaintenance PostMigrate (new) between entering maintenance and the Maintenance stage same as the Maintenance stage
  • Maintenance stage: plain maintenance never runs it. The operator does that work.
  • Marker: a host detail, maintenance.hooks, records the maintenance whose PreMaintenance ran. PostMigrate and PostMaintenance only fire for that maintenance, and PostMaintenance removes the marker.
  • Locking: a cluster-wide lock per host is held while any stage runs, rolling or not, so two stages never overlap on one agent.
  • Prepare checks: the existing prepare checks run again after the hooks, because the hooks can take minutes. If prepare then fails, PostMaintenance undoes PreMaintenance.
  • Agent down on cancel: cancel first restarts a disconnected agent as it already does (kvm.ssh.to.agent), then waits for it. If the agent stays down, cancel is refused while the setting is on. With the setting off, cancel goes through without the hook.
  • No double runs: rolling maintenance calls maintain() without these hooks.
  • Docs: the stage order is documented in the setting description and in agent.properties.

Fixes: #14776

Types of changes

  • Breaking change (fix or feature that would cause existing functionality to change)
  • New feature (non-breaking change which adds functionality)
  • Bug fix (non-breaking change which fixes an issue)
  • Enhancement (improves an existing feature and functionality)
  • Cleanup (Code refactoring and cleanup, that may add test cases)
  • Build/CI
  • Test (unit or integration test code)

Feature/Enhancement Scale or Bug Severity

Feature/Enhancement Scale

  • Major
  • Minor

How Has This Been Tested?

  • Unit tests: RollingMaintenanceManagerImplTest (30) and ResourceManagerImplTest (80) pass. The new cases cover:
    • hooks off and on, success, failure and exit 70, host not Up, lock busy;
    • PostMigrate failure and its skip cases;
    • PostMaintenance running, refusing and skipping;
    • cancel ordering, undo after a failed prepare, maintain(cmd, false) skipping hooks, the Disabled host pre-check, and the stage order.
  • Not yet run on a live KVM cluster.

How did you try to break this feature and the system with this change?

Case Result
cancel while PostMigrate runs cancel waits for the lock; PostMigrate is skipped if the maintenance has gone
PostMigrate queued across a cancel and re-prepare skipped, because the marker id differs
cancel with the agent down agent restarted or waited for, else refused, or skipped with the setting off
prepare fails after the hooks PostMaintenance undo, or an alert if the agent is gone
rolling and plain maintenance on one host stages serialised by the lock, and rolling skips the plain hooks
setting off no hooks run; a stale marker is dropped at the next prepare

Opt-in: kvm.host.maintenance.run.hooks (cluster, default false). Reuses
the existing RollingMaintenanceCommand and rolling.maintenance.hooks.dir
scripts on the KVM agent; no agent change.

| Path | Stage | When | On failure |
|---|---|---|---|
| prepareHostForMaintenance | PreFlight, PreMaintenance | before VMs move | maintenance refused (also on exit 70 or host not Up) |
| host reaches Maintenance | PostMigrate (new) | host is empty, runs in background | alert, state unchanged |
| cancelHostMaintenance | PostMaintenance | before the host is enabled | alert, cancel goes on |

- The Maintenance stage never runs on this path; the operator does the work.
- Host detail `maintenance.hooks` holds an id of the maintenance whose
  PreMaintenance ran. PostMigrate and PostMaintenance only fire for that
  maintenance; PostMaintenance drops the marker.
- A cluster-wide lock per host is held while any stage runs (rolling or
  not), so stages never overlap.
- Prepare is refused before any hook when the host state can't enter
  maintenance (e.g. Disabled) or VMs are running with VM HA off. The usual
  prepare checks run again after the hooks, since they can take minutes;
  if prepare then fails, PostMaintenance undoes PreMaintenance.
- Before PostMaintenance, cancel restarts a disconnected agent as it
  already does (kvm.ssh.to.agent) and waits for it; if it stays down the
  cancel is refused while the setting is on, and goes through without
  the hook once the setting is off.
- A stale marker (host left maintenance some other way) is dropped when
  the next maintenance starts.
- startRollingMaintenance gets the PostMigrate stage between entering
  maintenance and Maintenance, and calls maintain() without these hooks,
  so no stage runs twice.
- Stage order documented in agent.properties.

Signed-off-by: Brad House <bhouse@nexthop.ai>
@codecov

codecov Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 63.46154% with 95 lines in your changes missing coverage. Please review.
✅ Project coverage is 20.83%. Comparing base (2737087) to head (ad77a3f).

Files with missing lines Patch % Lines
.../cloud/resource/RollingMaintenanceManagerImpl.java 66.48% 52 Missing and 9 partials ⚠️
...n/java/com/cloud/resource/ResourceManagerImpl.java 55.40% 24 Missing and 9 partials ⚠️
.../com/cloud/resource/RollingMaintenanceManager.java 75.00% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               main   #14777      +/-   ##
============================================
+ Coverage     20.80%   20.83%   +0.02%     
- Complexity    20919    20962      +43     
============================================
  Files          6053     6053              
  Lines        548451   548687     +236     
  Branches      78719    73315    -5404     
============================================
+ Hits         114128   114323     +195     
- Misses       420958   420966       +8     
- Partials      13365    13398      +33     
Flag Coverage Δ
uitests 3.86% <ø> (ø)
unittests 22.27% <63.46%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@bhouse-nexthop

Copy link
Copy Markdown
Collaborator Author

Test report: maintenance hooks on plain host maintenance (KVM)

Environment

  • CloudStack 24.0 (main) build with this PR, plus some of my other open PRs.
  • Three KVM hosts in one cluster, Ceph RBD primary storage, one management server.
  • kvm.host.maintenance.run.hooks=true set at cluster scope. On one host, rolling.maintenance.hooks.dir pointed at a directory with trivial PreFlight, PreMaintenance, PostMigrate and PostMaintenance scripts. Each one appends a timestamp and its stage name to a log file and exits 0, unless a per-stage file sets the exit code. The agent uses the default systemd service executor.
  • A watcher sampled the host state and the number of VMs on the host every 2-3 s.

Results

Step Result Evidence
Enable maintenance with one user VM on the host pass hooks: PreFlight 13:56:39, PreMaintenance 13:56:49; then Enabled -> PrepareForMaintenance 13:57:00 and the VM's migration scheduled 13:57:00, done 13:57:04
PostMigrate after Maintenance is reached pass PrepareForMaintenance -> Maintenance 13:57:38.675, "Running maintenance hook PostMigrate" 13:57:38.678 (background thread), finished 13:57:48
Cancel maintenance pass "Running maintenance hook PostMaintenance" 13:58:01, finished 13:58:11.851; Maintenance -> Enabled 13:58:11.960, after the hook
PreMaintenance exits 70 pass prepare fails: "Maintenance hook PreMaintenance on Host ... refused maintenance: ..."; host stays Up/Enabled; the VM on it is not migrated; PreFlight and PreMaintenance ran, nothing after them
startRollingMaintenance on that host (a Maintenance script added for this run, since rolling maintenance skips a host without one) pass success=true, hostsupdated=[host]; hook log: PreFlight, PreMaintenance, PostMigrate, Maintenance, PostMaintenance, once each; no extra run from the plain-maintenance path
PostMaintenance hook failure on cancel (by accident, see below) pass cancel waited for the stage until the stage timeout, then raised the alert "Maintenance hook PostMaintenance failed on host ..." and enabled the host anyway
Cleanup done scripts and rolling.maintenance.hooks.dir removed, agent restarted, setting reset to false

Hook log (plain maintenance, then cancel)

13:56:39.767 PreFlight rc=0
13:56:49.918 PreMaintenance rc=0
13:57:38.750 PostMigrate rc=0
13:58:01.841 PostMaintenance rc=0

Hook log (one rolling maintenance run)

14:33:05.393 PreFlight rc=0
14:33:15.601 PreMaintenance rc=0
14:33:56.108 PostMigrate rc=0
14:34:06.238 Maintenance rc=0
14:34:16.425 PostMaintenance rc=0

Notes. Neither is caused by this PR, but both are worth knowing.

  • My first run used a longer hooks directory (/etc/cloudstack/agent/r3-hooks). The service executor puts the stage, script path, timeout and both result-file paths into the systemd instance name. For PostMaintenance (the longest stage name) that name came to 256 characters, over systemd's 255 limit, so systemctl start refused it and the stage never ran. PreFlight, PreMaintenance and PostMigrate were under the limit and ran. The cancel then polled until kvm.rolling.maintenance.stage.timeout (1800 s), alerted, and enabled the host. A shorter directory (/etc/cloudstack/agent/hooks, 238 characters) fixed it. Rolling maintenance has the same limit; this PR just makes the PostMaintenance path more common.
  • With a user VM on the host and a single cluster, startRollingMaintenance could not migrate it. Rolling maintenance disables the cluster, and the planner then avoids it ("Deploy avoids clusters: [1]"), so the host ended in ErrorInMaintenance after 5 attempts. The rolling run above was done with the host empty.

@bhouse-nexthop

Copy link
Copy Markdown
Collaborator Author

Tested the 4.22 backport of this change on a nested KVM lab running 4.22.1.1-nexthop (a 4.22.1.1-based build). The lab has 3 KVM hosts, and the hooks dir is set to /etc/cloudstack/agent/hooks. kvm.host.maintenance.run.hooks=true is set at cluster scope. One stub script per stage logs the time and its exit code.

# Scenario Result Evidence
1 prepareHostForMaintenance on a host with a user VM and the SSVM/CPVM, then cancelHostMaintenance Pass PreFlight, then PreMaintenance (10s later), then the host went to PrepareForMaintenance and its 3 VMs migrated off. Maintenance was reached after 27s, and PostMigrate ran right after with the host empty. On cancel, PostMaintenance ran first and the host was Enabled about 11s later. Each hook ran once
2 PreMaintenance exits 70 Pass Prepare was refused ("Maintenance hook PreMaintenance on Host ..."). The host stayed Up/Enabled and no VM moved. Only PreFlight and PreMaintenance ran
3 startRollingMaintenance on the empty host (one cluster, so it can't migrate VMs away) Pass hostsupdated: [host]. The stages ran PreFlight, PreMaintenance, PostMigrate, Maintenance, PostMaintenance, each exactly once, with PostMigrate after entering maintenance and before Maintenance
4 startRollingMaintenance without a Maintenance script Pass Skipped with "There is no maintenance script on the host", as before

This branch has not been deployed

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

KVM: run the rolling maintenance hooks on plain host maintenance, plus a stage once the host is empty

1 participant