Skip to content

feat(infra): report drift of an infra repo #5127

Done

Description

@dan-petty

Problem or Motivation

Nothing shows when a host of dan-petty/homelab no longer matches the repo, for example after a hand edit during a crash, or a merged PR that nobody applied. Argo CD shows drift for the cluster, but the hosts under it have no such check.

Proposed Solution & Architectural Design

devops infra drift <repo> [--playbook] (design section 5 in tracker #5120). It runs the plan of #5124 on the default-branch head for every in-service host (default playbook site.yaml). Exit 0 with no drift, 2 with drift, 1 on a failure. It sets the gauge devops_cli_infra_drift_hosts{repo} and counts the run in devops_cli_infra_runs_total. With drift it opens or updates one issue in the infra repo that holds a marker comment, the hosts, the changed and unchecked tasks and the plan id. When a later run finds no drift, it closes that issue. A second run with the same result sends no write. Agent level plan. --dry-run makes zero requests. A schedule comes later in the cluster (a separate item).

Target Subsystem

OpenTofu / Infrastructure as Code

Existing Tool Considered

The plan of #5124 is the check; devops gh writes the issue. devops tf drift is the model for the exit codes and the output. Cost: about 2M tokens to build, 0.3M a quarter to keep, 3 hours.

Acceptance Criteria

  • Tests with recorded events and an HTTP fake: drift opens 1 issue; the same drift again sends no write; no drift closes the issue; a failed plan exits 1 and writes no issue.
  • A test: the gauge is set for drift and for no drift.
  • A test: --dry-run sends no request.
  • Live run on dan-petty/homelab, recorded in a PR comment: the exit code, the hosts with drift, and the issue that the run opened or the fact that none was needed.

Dependencies

#5124

Alternatives check of 2026-11-19: in-use.
The repository already uses this tool.
Tool: ansible/ansible (https://github.com/ansible/ansible), GPL-3.0, last activity 2026-11-19, maintained.
Reason: Searches found driftctl (cloud/IaC drift only, not Ansible hosts) and Ansible itself. Ansible check mode detects host drift from the repo playbooks, covering the check, but nothing joins it with the GitHub issue lifecycle, gauges, exit codes or dry-run.

Covers:

  • Check-mode run lists changed tasks for each host
  • Uses the repo playbook and inventory at a commit
  • Reports changed and unchecked tasks per host

Lacks:

  • No GitHub issue open, update or close with marker
  • No Prometheus drift gauge or run counter
  • No exit code 2 on drift and no dry-run mode

Proposed design

Problem statement

Nothing shows when a host of dan-petty/homelab no longer matches the repo, after a hand edit during a crash or a merged PR that nobody applied. Argo CD shows the drift of the cluster, but the hosts under it have no such check. This item adds devops infra drift <repo> [--playbook]: it plans the infra repo at the head of its default branch for each in-service host, reports the hosts that drift with the changed and unchecked tasks, exits 0, 2 or 1, sends a gauge and a counter, and keeps one marker issue in the infra repo.

Acceptance criteria

Requirement: Drift exit codes

The command SHALL exit 0 with no drift, 2 with drift on at least one host, and 1 when a plan fails.

Scenario: One host drifts

  • WHEN a test runs devops infra drift on recorded plan events where one host has a changed task
  • THEN the command exits 2 and prints the host name and the changed task

Requirement: Drift host gauge

Each run SHALL set the gauge devops_cli_infra_drift_hosts with the label repo to the number of hosts with drift.

Scenario: Gauge with drift and with no drift

  • WHEN a test runs the command on recorded plan events with two hosts in drift and then with none
  • THEN the gauge sample for the repo reads 2 after the first run and 0 after the second

Requirement: Run counter

Each run SHALL add one to devops_cli_infra_runs_total with the repo, the command drift and the result ok, drift or failed.

Scenario: Counter result with drift

  • WHEN a test runs the command with drift and reads the counter samples from the registry
  • THEN one sample has the labels command=drift and result=drift with the value 1

Requirement: Drift issue

A run with drift SHALL open one issue in the infra repo, or update its open marker issue, with the hosts, the changed and unchecked tasks and the plan id.

Scenario: First run with drift

  • WHEN a test runs the command with drift and an HTTP fake that records each request
  • THEN the fake records one issue create whose body holds the marker, the host, the changed task, the unchecked task and the plan id

Requirement: No write for the same drift

A run whose plan id equals the plan id in the marker issue SHALL send no write request.

Scenario: Second run with the same plan id

  • WHEN a test runs the command twice on the same recorded plan events
  • THEN the HTTP fake records no create, update or comment request on the second run

Requirement: Close when drift clears

A run with no drift SHALL close the open marker issue of the infra repo.

Scenario: Drift clears

  • WHEN a test runs the command with no drift and an open marker issue
  • THEN the HTTP fake records one close request for that issue

Requirement: Failed plan

A run whose plan ends failed SHALL exit 1 and SHALL write no issue.

Scenario: Plan fails

  • WHEN a test runs the command on recorded plan events that end failed
  • THEN the command exits 1 and the HTTP fake records no issue create or update

Requirement: Dry run

devops infra drift --dry-run SHALL send no HTTP request.

Scenario: Dry run with drift

  • WHEN a test runs the command with --dry-run on recorded plan events with drift
  • THEN the HTTP fake records zero requests and the command exits 2

Requirement: Access level gate

The command MUST refuse to run when the agent access level of the infra repo is below plan.

Scenario: Repo at level none

  • WHEN a test sets the agent access level of the infra repo to none and runs the command
  • THEN the command exits 1, prints the level and the required level, and the HTTP fake records no request

Requirement: Task names in the report

The report SHALL name each changed task and each unchecked task of each host with drift.

Scenario: A task writes the kubelet config file

  • WHEN a test runs the command on recorded plan events where the task that writes the kubelet config file is changed
  • THEN the issue body names that task and the plan id

Requirement: Playbook and commit

The command SHALL plan the playbook that --playbook names, site.yaml by default, at the head of the default branch of the infra repo.

Scenario: Explicit playbook

  • WHEN a test runs the command with --playbook harden.yaml and a recorded head commit
  • THEN the recorded plan call names harden.yaml and that commit

Requirement: Live drift check on the homelab repo

The item MUST record a live run of uv run devops infra drift dan-petty/homelab in a PR comment with the exit code, the hosts with drift and the issue that the run opened or the fact that none was needed.

Scenario: Live run on dan-petty/homelab

  • WHEN a person runs uv run devops infra drift dan-petty/homelab at the head of the default branch
  • THEN the command exits 0 or 2, prints each host with drift, and names the issue that it opened or updated

Existing Tool Considered

Ansible (https://github.com/ansible/ansible) - the alternataves check in the issue body records the verdict in-use: the repository already uses this tool. The check block in the issue body gives what Ansible covers and what it lacks. This item adds only the parts that Ansible lacks: it runs the check-mode plan of #5124 for each in-service host, maps the plan facts to the exit codes 0, 2 and 1, sends the gauge devops_cli_infra_drift_hosts and the counter devops_cli_infra_runs_total, and keeps one marker issue in the infra repo. Cost of the code that the item builds and keeps: about 2M tokens to build, 0.3M tokens a quarter to keep, and 3 hours.

Key questions

  • Which counter holds a drift run?: Extend devops_cli_infra_runs_total with the command drift and the results ok, drift and failed. One counter holds each run of devops infra that reads or changes hosts, and its help, docs/TELEMETRY.md and the infra-apply spec delta say so. (best practice) [sources: src/devops_cli/infra/apply.py, src/devops_cli/telemetry/instruments.py, docs/TELEMETRY.md]
    • Chosen: Extend devops_cli_infra_runs_total. The body names this counter, and one counter holds each run of devops infra.
    • Rejected: Add devops_cli_infra_drift_runs_total. A second counter needs its own documentation and dashboard row for the same family of runs.
  • What makes a second run with the same drift send no write?: The command compares the plan id of the run with the plan id that the marker issue holds. The plan id is the SHA-256 of the plan facts without the time, so the same facts give the same id and the run sends no write. (best practice) [sources: CONTEXT.md]
    • Chosen: Compare the plan id. The plan id is the SHA-256 of the plan facts without the time, so the same facts give the same id.
    • Rejected: Compare the rendered issue body. A changed order or a time in the text causes a write with no change of the drift.
    • Rejected: Write a comment on each run. It writes on each run and breaks the no-write rule.
  • Which agent access level does the drift run need?: plan. The run reads the repo and runs a check-mode plan, and changes no host. Below plan the command refuses and exits 1. (best practice) [sources: CONTEXT.md, openspec/specs/infra-apply/spec.md]
    • Chosen: Require plan. The run reads the repo and runs a check-mode plan, and changes no host.
    • Rejected: Require safe. It adds apply rights that the check does not use.
    • Rejected: Run at any level. A repo at none gives an agent no access to its hosts.
  • Where does the command read the hosts that it plans?: From the Ansible inventory and the playbook of the infra repo, at the head of its default branch. devops infra plan reads the same inventory and writes the plan facts for each host. [sources: CONTEXT.md, src/devops_cli/infra/apply.py]
  • How does the report show a change of a kubelet setting?: The plan holds the changed tasks and the unchecked tasks of each host. A hand edit of the kubelet config file shows as the task that writes that file. The report names each changed task, so the command needs no kubelet-specific code. [sources: CONTEXT.md, src/devops_cli/infra/apply.py]
  • How does the command find and keep the marker issue?: The command searches the open issues of the infra repo for a hidden HTML marker comment that holds the repo and the plan id. It creates one issue when no open issue holds the marker, updates it when the plan id differs, and closes it when the drift clears. It reads and writes the issue through devops gh. (best practice) [sources: CONTEXT.md, src/devops_cli/commands/gh.py]
    • Chosen: Hidden marker comment. A hidden comment survives a rename and an edit of the visible text, and the issue search finds it.
    • Rejected: A label on the issue. A label write on each run breaks the no-write rule, and a person can remove it.
    • Rejected: A title match. A person can rename the title, and the search then opens a second issue.
  • Which value does the repo label hold?: The casefolded owner/name of the infra repo, as the devops_cli_project_* series carry it. [sources: docs/TELEMETRY.md]

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

#5124

Activity

  1. added
    source/agent
    type/feature
    scope/infra
    status/new
    value/medium
    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. dan-petty commented on Nov 13, 2026

    @dan-petty
    OwnerAuthor

    Scope from the spec split (2026-11-13)

    The kubelet settings of the Kubernetes spec (Table 4 in PR #5492) are part of the host drift. The split removes the table from the spec (owner directions of 2026-11-13), so this command must show a change of these settings:

    • evictionHard (memory.available, nodefs.available, imagefs.available) and mergeDefaultEvictionSettings;
    • kubeReserved and systemReserved;
    • podPidsLimit and shutdownGracePeriod;
    • serializeImagePulls and maxParallelImagePulls;
    • imageGCHighThresholdPercent and imageGCLowThresholdPercent;
    • cpuManagerPolicy, memoryManagerPolicy, topologyManagerPolicy, cgroupDriver and enforceNodeAllocatable.

    The kubelet config file that the infra repo writes is the source, so its playbook must manage each setting above, and the check-mode plan then shows a hand change on a node. The live value of a node is at /api/v1/nodes/<node>/proxy/configz. The live run of this item can compare it with the repo for one node.

  5. 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.

  6. devops-cli-service commented on Nov 19, 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:

    • ansible check mode drift report exit code issue, results: 5
    • open source tool detect configuration drift ansible hosts, results: 5
    • driftctl infrastructure drift detection tool, results: 5
    • driftctl, results: 5
    • ansible drift detection github, results: 5
    • ansible-playbook --check --diff drift, results: 5
    • Ansible Semaphore drift, results: 5
    • check mode ansible drift monitor tool, results: 5
    • open source ansible configuration drift detection report, results: 5
    • goss server validation tool, results: 5

    Sources:

  7. added
    status/ready
    and removed
    status/new
    on Nov 19, 2026
  8. devops-cli-service commented on Nov 19, 2026

    @devops-cli-service
    Contributor

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

  9. added
    owner/agent
    status/in-progress
    and removed
    status/ready
    on Nov 24, 2026
  10. added
    status/in-review
    and removed
    status/in-progress
    on Nov 24, 2026
  11. removed
    status/in-review
    owner/agent
    on Nov 24, 2026
  12. devops-cli-service commented on Nov 24, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-24 13:45 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/medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions