Skip to content

refactor(k8s): read the environment Secret rows from homelab #6685

Done

Description

@dan-petty

Problem

  • After dan-petty/homelab#96, the environment Secret rows are both in homelab k8s/cluster-secrets.toml and in src/devops_cli/k8s/cluster_secrets.py.
  • Readers of the table: commands/k8s/cluster_secret_push.py, k8s/secret_push.py, commands/config.py (config set --generate), commands/k8s/stack_lifecycle.py (deploy-stack), mattermost_bootstrap.py and roadmap_key_bootstrap.py.
  • The rows are frozen dataclasses (cluster_secrets.py:55-200); no pydantic model of them exists.
  • src/devops_cli/k8s/environment.py resolves the environment tree (--k8s-dir, the profile fact kubernetes.dir, infra.repos).

Proposed remedy

  • A pydantic TypeAdapter over a file model reads cluster-secrets.toml and builds the existing dataclasses, with one tagged form for each source kind (keyring, setting, literal, derived) and the generator and adopt_from tables. No second row model.
  • push-secrets and the other readers load the file from the environment tree and add the product rows.
  • Delete the moved rows from cluster_secrets.py.
  • Write the tests first, with a fixture file under tests/fixtures/.

Acceptance criteria

  • A new test fails on main and passes on the branch: a fixture file with one row of each source kind gives the same ClusterSecret objects as equal Python rows. uv run pytest tests/k8s -q -k cluster_secret passes.
  • git grep -n -E 'stack=(BASE_STACK|"(chat|gitea|infra)")' src/ finds nothing, and the only llm rows left in cluster_secrets.py are valkey-devops-auth and valkey-runs-auth.
  • uv run devops ci --check --only test,lint,typecheck passes.

Person steps

  • Run devops k8s push-secrets --plan with an unlocked keyring against the live cluster and post the count of changes (expected: 0). Post no value.

Dependencies

  • dan-petty/homelab#96.

Alternatives check of 2026-11-21: not-applicable.
Class: refactor, no search.
Tool: none found.

Proposed design

Problem statement

The environment Secret rows live in homelab k8s/cluster-secrets.toml and in src/devops_cli/k8s/cluster_secrets.py. The duplicate rows make two sources of truth. The change reads the homelab file into the existing dataclasses. It keeps only the product rows in code. The readers load the file from the environment tree.

Acceptance criteria

Requirement: File rows load into existing dataclasses

The loader SHALL build the existing ClusterSecret dataclasses from k8s/cluster-secrets.toml.

Scenario: One row of each source kind

  • WHEN the test tests/k8s/test_cluster_secrets_file.py loads tests/fixtures/cluster-secrets.toml with one keyring, setting, literal and derived row
  • THEN each loaded object equals the equal ClusterSecret Python row and uv run pytest tests/k8s -q -k cluster_secret passes.

Requirement: Generator and adopt_from tables load

The file model SHALL read the generator and adopt_from tables for each row.

Scenario: A generated row and an adopted row

  • WHEN the fixture file holds a generator table and an adopt_from table
  • THEN the loaded SecretEntry has the same SecretGenerator and SecretKeyRef as the equal Python row, and uv run pytest tests/k8s -q -k cluster_secret passes.

Requirement: Environment rows removed from code

The module src/devops_cli/k8s/cluster_secrets.py MUST contain no environment Secret rows after the change.

Scenario: No moved stack rows remain

  • WHEN the test tests/k8s/test_cluster_secret_source.py runs git grep -n -E 'stack=(BASE_STACK|"(chat|gitea|infra)")' src/
  • THEN the command finds no match, and git grep -n 'name="valkey-' src/devops_cli/k8s/cluster_secrets.py finds only valkey-devops-auth and valkey-runs-auth.

Requirement: Push-secrets uses the environment file

devops k8s push-secrets MUST load the environment rows from cluster-secrets.toml and append the product rows.

Scenario: A fixture environment tree

  • WHEN the test tests/k8s/test_cluster_secret_push.py runs with --k8s-dir pointing at a fixture environment tree
  • THEN the plan contains each fixture row and each product row, and uv run pytest tests/k8s -q -k cluster_secret passes.

Requirement: CI checks pass

The change MUST pass the project test, lint and typecheck checks.

Scenario: The scoped CI check

  • WHEN uv run devops ci --check --only test,lint,typecheck runs
  • THEN the command exits 0.

Requirement: Live plan shows no change

The refactor MUST keep the live cluster Secret plan unchanged.

Scenario: The live push plan

  • WHEN a person runs uv run devops k8s push-secrets --plan with an unlocked keyring against the live cluster
  • THEN the command reports 0 changes and posts no Secret value.

Existing Tool Considered

The alternatives check in the issue body records not-applicable for a refactor with no search and Tool: none found. The change uses the existing pydantic TypeAdapter and the standard library tomllib; it adds no new tool. The loader glue costs about 20k tokens and 2 hours to build, keep and fix.

Key questions

  • How does the loader read cluster-secrets.toml into the existing frozen dataclasses?: Use tomllib.load to read the file. Use a pydantic TypeAdapter over a file model to validate the data. The file model has one tagged form for each source kind, plus the generator and adopt_from tables. The loader builds the existing ClusterSecret dataclasses. It adds no second row model. (best practice) [sources: src/devops_cli/k8s/cluster_secrets.py, src/devops_cli/config/settings.py]
    • Chosen: pydantic TypeAdapter over a file model. It reuses the project's existing pydantic dependency and keeps the dataclasses as the row model.
    • Rejected: hand-written TOML parser. It adds code to keep and does not validate the file schema.
    • Rejected: second pydantic row model. It duplicates the dataclasses and makes two row models.
  • Where do the readers find cluster-secrets.toml?: The readers call src/devops_cli/k8s/environment.py to resolve the environment tree. The resolver reads --k8s-dir, the profile fact kubernetes.dir, or infra.repos. The readers do not hardcode a homelab path. (best practice) [sources: src/devops_cli/k8s/environment.py, src/devops_cli/commands/k8s/cluster_secret_push.py, src/devops_cli/k8s/secret_push.py]
    • Chosen: environment tree resolver. It follows the existing --k8s-dir and profile rules.
    • Rejected: hardcoded homelab path. It breaks other environment trees and the product base.
    • Rejected: current working directory. It does not follow the environment tree rules.
  • Which rows stay in src/devops_cli/k8s/cluster_secrets.py?: Only the product rows stay in src/devops_cli/k8s/cluster_secrets.py. The environment rows move to k8s/cluster-secrets.toml. The only llm rows left are valkey-devops-auth and valkey-runs-auth. (best practice) [sources: src/devops_cli/k8s/cluster_secrets.py]
    • Chosen: product rows only. It removes the duplicate environment rows and matches the issue's grep criterion.
    • Rejected: keep all rows. It keeps two sources of truth.
    • Rejected: load all rows from the file. The product base k8s/devops is not part of the environment tree.
  • How does the file model carry a generator and an adopt_from reference?: Each SecretEntry table gets an optional generator table and an optional adopt_from table. The loader maps them to SecretGenerator and SecretKeyRef. A plain source kind has no such table. (best practice) [sources: src/devops_cli/k8s/cluster_secrets.py, src/devops_cli/k8s/secret_push.py]
    • Chosen: optional nested tables. It keeps the existing dataclass fields and reads them from the file.
    • Rejected: flat string fields. It needs parsing and loses the typed SecretGenerator and SecretKeyRef.
    • Rejected: leave generator and adopt_from in Python. It keeps part of the row in code and splits the row across two files.
  • How does the change prove the file rows equal the Python rows?: Add a fixture tests/fixtures/cluster-secrets.toml with one row of each source kind. A test compares each loaded ClusterSecret with an equal Python row. The test runs offline. (best practice) [sources: tests/k8s/test_cluster_secret_plan.py, tests/k8s/test_cluster_secret_push.py]
    • Chosen: fixture and equality test. It tests each source kind offline and catches a wrong field.
    • Rejected: live cluster only. It is not offline and shows no field detail.

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

  1. added
    type/refactor
    scope/k8s
    source/agent
    on Nov 21, 2026
  2. dan-petty commented on Nov 21, 2026

    @dan-petty
    OwnerAuthor
  3. added
    status/new
    value/medium
    effort/medium
    priority/p2-medium
    on Nov 21, 2026
  4. added
    status/ready
    and removed
    status/new
    on Nov 22, 2026
  5. devops-cli-service commented on Nov 22, 2026

    @devops-cli-service
    Contributor

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

  6. added
    owner/agent
    status/in-progress
    and removed
    status/ready
    on Nov 23, 2026
  7. added
    status/in-review
    and removed
    status/in-progress
    on Nov 23, 2026
  8. removed
    status/in-review
    owner/agent
    on Nov 23, 2026
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/k8ssource/agenttype/refactorvalue/medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions