Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/ERRORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ The Exit Code column is the exception's `exit_code` attribute. Only `devops clou
| `KubernetesLoggingError` | `K8S_LOGGING_ERROR` | `1` | K8s | Raised when querying cluster logs or Loki fails. |
| `LLMInferenceError` | `LLM_INFERENCE_ERROR` | `10` | Ai | Base exception for LLM provider invocation failures. |
| `LibraryIngestionError` | `LIBRARY_INGESTION_ERROR` | `1` | Ai | Base exception for package introspection and library ingestion errors. |
| `LibraryNotFoundError` | `LIBRARY_NOT_FOUND_ERROR` | `1` | Ai | Raised when target package cannot be imported or found in the environment. |
| `LibraryNotFoundError` | `LIBRARY_NOT_FOUND_ERROR` | `1` | Ai | Raised when target package could not be loaded or found in the environment. |
| `LogfireConfigurationError` | `LOGFIRE_CONFIG_ERROR` | `1` | Telemetry | Raised when Logfire configuration fails or credentials cannot be retrieved. |
| `MemoryProfilerError` | `PROFILER_ERROR` | `1` | Devops_cli.telemetry.memory_profiler | Domain exception raised when memory profiling target resolution or execution fails. |
| `ModelBundleError` | `MODEL_BUNDLE_ERROR` | `1` | Ai | Raised when an AI model bundle cannot be located, resolved, or loaded. |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
schema: spec-driven
created: 2026-10-25
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Design

## Context

See proposal.md for motivation and problem background. DevOps CLI deploys Helm charts across multiple stacks (`infra`, `llm`, `gitea`, `logging`). Previously, conflict resolution inspected stderr strings and modified resource metadata with `kubectl`. Helm 3.17 introduced `--take-ownership` to handle existing resources natively, and `helm status -o json` provides structured machine-readable state inspection.

## Goals / Non-Goals

**Goals:**
- Eliminate regex parsing of human stderr strings during Helm operations.
- Recover pending release states (`pending-install`, `pending-upgrade`, `pending-rollback`) via Helm native lifecycle commands (`helm uninstall`, `helm rollback`) before attempting `helm upgrade --install`.
- Use `--take-ownership` on all `helm upgrade --install` commands.
- Verify Helm version >= 3.17 once prior to release deployments.
- Eliminate obsolete retry loops, constants, and manual `kubectl` labeling/annotation loops.

**Non-Goals:**
- Supporting Helm versions prior to 3.17: pre-1.0 alpha software maintains no backward compatibility or shims.
- Changing chart values or manifests.

## Decisions

### Decision: Check Helm version once with standard `packaging.version.Version`
- **Choice**: Call `helm version --template {{.Version}}` once in `deploy_stack` when Helm releases are present, parse the version string with `packaging.version.Version`, and fail with `KubernetesDeployError` if version < 3.17.
- **Rationale**: CONTRIBUTING.md mandates using standard parsers (`packaging`) for open domains like software versions. Checking once avoids spawning redundant `helm version` subprocesses for each release.
- **Alternative Considered**: Parsing version strings manually with regex or splitting on dots. Rejected because manual version splitting does not handle pre-releases or complex version schemas correctly.
- **Alternative Considered**: Checking version on every single release installation. Rejected because Helm version does not change between releases during a single CLI command run.

### Decision: Inspect status before upgrade using `helm status -o json`
- **Choice**: Execute `helm status <release> -n <namespace> -o json` prior to `helm upgrade --install`. If the release is not found, continue to upgrade. If status is `pending-install`, execute `helm uninstall <release> -n <namespace>`. If status is `pending-upgrade` or `pending-rollback`, execute `helm rollback <release> -n <namespace>`.
- **Rationale**: `helm status -o json` provides a stable schema (`info.status`) regardless of Helm's human-readable formatting or terminal localization. Rolling back restores the prior working revision rather than deleting Secret state.
- **Alternative Considered**: Deleting Helm Secret locks with `kubectl delete secret`. Rejected because it assumes the Secret storage driver and leaves Helm release history in an inconsistent state.
- **Alternative Considered**: Reacting after failure by inspecting stderr for error messages. Rejected because human error strings change across Helm releases (such as Helm 3 vs Helm 4).

### Decision: Delegate resource adoption entirely to Helm `--take-ownership`
- **Choice**: Pass `--take-ownership` on all `helm upgrade --install` commands and delete `_adopt_helm_resource_if_conflict` and `CONST_HELM_OWNERSHIP_CONFLICT_RE`.
- **Rationale**: Helm 3.17+ natively adopts existing resources that match the chart. Removing the kubectl label/annotate loop eliminates round-trips and fragile regex matching.
- **Alternative Considered**: Keeping a fallback kubectl adoption loop if `--take-ownership` fails. Rejected because pre-1.0 alpha rules mandate zero legacy shims or zombie code.

## Risks / Trade-offs

- [Risk] Helm 3.16 or older environments fail deployment. → Mitigation: Clear error message explicitly naming the installed version and the required version (3.17 or later).
- [Risk] Rollback on a release with no prior revision. → Mitigation: `pending-install` is uninstalled instead of rolled back; `pending-upgrade` and `pending-rollback` only occur when a prior revision exists.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Proposal

## Why

DevOps CLI recovered stuck Helm releases and adopted conflicting resources by parsing human error text in stderr with regular expressions and manipulating Secret locks and annotations with `kubectl` (#1438). This approach was fragile against Helm wording changes, assumed the Secret storage driver, and discarded revisions instead of rolling back.

This change fits one pull request.

## What Changes

- Verify installed Helm version before deployment and require Helm 3.17 or later; fail with an error naming the installed version when older.
- Inspect Helm release state before `helm upgrade --install` using `helm status -o json`.
- Automatically uninstall releases in `pending-install` state before upgrade.
- Automatically rollback releases in `pending-upgrade` or `pending-rollback` state to their previous revision with `helm rollback <release>` before upgrade.
- Pass `--take-ownership` on all `helm upgrade --install` command executions so Helm adopts existing resources natively.
- Delete `_recover_stuck_helm_release_if_pending`, `_adopt_helm_resource_if_conflict`, `_run_helm_with_adoption_retries`, and regular expression `CONST_HELM_OWNERSHIP_CONFLICT_RE`.
- Remove `DEFAULT_HELM_RECOVERY_MAX_RETRIES` setting and delete the `kubectl annotate` / `kubectl label` loop.
- Reword import failure messages in `src/devops_cli/ai/library/introspector.py` and `src/devops_cli/exceptions/ai.py` so obsolete strings do not match in `src`.

## Capabilities

### New Capabilities
- `k8s-helm-deploy`: Helm release deployment, version verification, state recovery, and ownership adoption for Kubernetes stacks.

### Modified Capabilities

None.

## Impact

- `src/devops_cli/commands/k8s/stack_lifecycle.py`: Helm deployment command construction, single-point version check, and JSON status recovery.
- `src/devops_cli/commands/k8s/__init__.py`: Clean up exports of removed helpers.
- `src/devops_cli/config/constants.py`: Remove `CONST_HELM_OWNERSHIP_CONFLICT_RE` and add `CONST_HELM_MIN_VERSION`.
- `src/devops_cli/config/defaults.py`: Remove `DEFAULT_HELM_RECOVERY_MAX_RETRIES`.
- `tests/test_k8s.py`: Remove obsolete stderr matching tests and add tests for status inspection, rollback, uninstall, `--take-ownership`, and version checking.

## Feasibility

The premise was verified against the real system:
- `helm version` reports `version.BuildInfo{Version:"v4.3.0", ...}` in the environment.
- `helm upgrade --help` confirms the `--take-ownership` flag is available: "if set, upgrade will ignore the check for helm annotations and take ownership of the existing resources".
- `helm status --help` confirms `-o json` output and release states: `pending-install`, `pending-upgrade`, `pending-rollback`.
- `helm rollback --help` confirms rollback to the previous revision via `helm rollback <RELEASE> [REVISION] [flags]`.
- Upstream Helm introduced `--take-ownership` in Helm 3.17.0.

## Existing Tool Considered

Helm CLI (3.17+) provides native ownership adoption through `--take-ownership` and release recovery commands (`helm rollback`, `helm uninstall`). DevOps CLI previously maintained a custom regular expression (`CONST_HELM_OWNERSHIP_CONFLICT_RE`) and custom `kubectl annotate` and `kubectl label` adoption routines. Adopting Helm's native `--take-ownership` replaces this custom code. Replaced internal code includes `_adopt_helm_resource_if_conflict`, `_recover_stuck_helm_release_if_pending`, `_run_helm_with_adoption_retries`, `CONST_HELM_OWNERSHIP_CONFLICT_RE`, and `DEFAULT_HELM_RECOVERY_MAX_RETRIES`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Spec Delta

## Purpose

Automated Helm release deployment, version verification, state recovery, and resource ownership adoption for Kubernetes stacks.

## ADDED Requirements

### Requirement: Helm version requirement
The deployment process SHALL require Helm version 3.17 or later when deploying Helm releases. If the installed Helm version is older than 3.17, deployment SHALL abort with an error message naming the installed version.

#### Scenario: Helm version meets minimum requirement
- **WHEN** the installed Helm version is 3.17.0 or later and `devops k8s deploy-stack` runs
- **THEN** the version check succeeds and deployment continues

#### Scenario: Helm version is below minimum requirement
- **WHEN** the installed Helm version is 3.16.2 and `devops k8s deploy-stack` runs
- **THEN** deployment fails with an error stating that Helm 3.17 or later is required and naming version 3.16.2

### Requirement: Release status inspection and recovery
Before executing `helm upgrade --install`, the deployment process SHALL inspect the release status with `helm status -o json`. A release in `pending-install` status SHALL be uninstalled with `helm uninstall`. A release in `pending-upgrade` or `pending-rollback` status SHALL be rolled back with `helm rollback` before proceeding.

#### Scenario: Pending install release is uninstalled
- **WHEN** `helm status -o json` reports release status `pending-install`
- **THEN** the system executes `helm uninstall` before running `helm upgrade --install`

#### Scenario: Pending upgrade release is rolled back
- **WHEN** `helm status -o json` reports release status `pending-upgrade`
- **THEN** the system executes `helm rollback` before running `helm upgrade --install`

#### Scenario: Pending rollback release is rolled back
- **WHEN** `helm status -o json` reports release status `pending-rollback`
- **THEN** the system executes `helm rollback` before running `helm upgrade --install`

### Requirement: Helm ownership adoption
Every `helm upgrade --install` invocation SHALL include `--take-ownership` to adopt existing unmanaged Kubernetes resources without manual annotation or labeling.

#### Scenario: Upgrade command includes take-ownership
- **WHEN** `devops k8s deploy-stack` executes a Helm release install or upgrade
- **THEN** the constructed `helm upgrade --install` command arguments include `--take-ownership`
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Tasks

## 1. Helm version requirement check

- [x] 1.1 Add failing unit tests asserting that Helm 3.16 fails with the version error naming the installed version, and Helm >= 3.17 passes (`uv run devops ci test tests/test_k8s.py`).
- [x] 1.2 Implement `_require_helm_version` in `src/devops_cli/commands/k8s/stack_lifecycle.py` using `packaging.version.Version`, define `CONST_HELM_MIN_VERSION` in `src/devops_cli/config/constants.py`, and invoke the check once in `deploy_stack` (`uv run devops ci test tests/test_k8s.py`).
- [x] 1.3 Regenerate documentation with `uv run devops docs generate --sync-readme`.

## 2. Release status inspection and recovery

- [x] 2.1 Add failing unit tests asserting that `pending-upgrade` or `pending-rollback` releases trigger `helm rollback` before upgrade and `pending-install` releases trigger `helm uninstall` before upgrade (`uv run devops ci test tests/test_k8s.py`).
- [x] 2.2 Implement `_recover_stuck_helm_release` reading `helm status -o json`, running `helm rollback` on `pending-upgrade`/`pending-rollback` and `helm uninstall` on `pending-install`, deleting `_recover_stuck_helm_release_if_pending` and its obsolete tests (`uv run devops ci test tests/test_k8s.py`).
- [x] 2.3 Regenerate documentation with `uv run devops docs generate --sync-readme`.

## 3. Native ownership adoption and cleanup of obsolete code

- [x] 3.1 Add failing unit tests asserting that every `helm upgrade --install` command carries `--take-ownership` (`uv run devops ci test tests/test_k8s.py`).
- [x] 3.2 Add `--take-ownership` to `_build_helm_upgrade_cmd`, delete `_adopt_helm_resource_if_conflict`, `_run_helm_with_adoption_retries`, `CONST_HELM_OWNERSHIP_CONFLICT_RE`, `DEFAULT_HELM_RECOVERY_MAX_RETRIES`, delete obsolete adoption tests in `tests/test_k8s.py`, and reword import error messages in `src/devops_cli/ai/library/introspector.py` and `src/devops_cli/exceptions/ai.py` (`uv run devops ci test tests/test_k8s.py`).
- [x] 3.3 Regenerate documentation with `uv run devops docs generate --sync-readme`.

## 4. Quality gate and acceptance criteria validation

- [x] 4.1 Verify acceptance criteria: assert `git grep -En 'another operation|invalid ownership metadata|cannot be imported|CONST_HELM_OWNERSHIP_CONFLICT_RE' src` prints nothing and run static checks (`git grep -En 'another operation|invalid ownership metadata|cannot be imported|CONST_HELM_OWNERSHIP_CONFLICT_RE' src && uv run devops ci --check --only lint,format,actionlint,zizmor`).
- [x] 4.2 Regenerate documentation with `uv run devops docs generate --sync-readme`.

## Workflow follow-up

- Pending a person: review the diff in tuicr (tuicr -r origin/main..HEAD).
39 changes: 39 additions & 0 deletions openspec/specs/k8s-helm-deploy/spec.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# k8s-helm-deploy Specification

## Purpose
Automated Helm release deployment, version verification, state recovery, and resource ownership adoption for Kubernetes stacks.

## Requirements

### Requirement: Helm version requirement
The deployment process SHALL require Helm version 3.17 or later when deploying Helm releases. If the installed Helm version is older than 3.17, deployment SHALL abort with an error message naming the installed version.

#### Scenario: Helm version meets minimum requirement
- **WHEN** the installed Helm version is 3.17.0 or later and `devops k8s deploy-stack` runs
- **THEN** the version check succeeds and deployment continues

#### Scenario: Helm version is below minimum requirement
- **WHEN** the installed Helm version is 3.16.2 and `devops k8s deploy-stack` runs
- **THEN** deployment fails with an error stating that Helm 3.17 or later is required and naming version 3.16.2

### Requirement: Release status inspection and recovery
Before executing `helm upgrade --install`, the deployment process SHALL inspect the release status with `helm status -o json`. A release in `pending-install` status SHALL be uninstalled with `helm uninstall`. A release in `pending-upgrade` or `pending-rollback` status SHALL be rolled back with `helm rollback` before proceeding.

#### Scenario: Pending install release is uninstalled
- **WHEN** `helm status -o json` reports release status `pending-install`
- **THEN** the system executes `helm uninstall` before running `helm upgrade --install`

#### Scenario: Pending upgrade release is rolled back
- **WHEN** `helm status -o json` reports release status `pending-upgrade`
- **THEN** the system executes `helm rollback` before running `helm upgrade --install`

#### Scenario: Pending rollback release is rolled back
- **WHEN** `helm status -o json` reports release status `pending-rollback`
- **THEN** the system executes `helm rollback` before running `helm upgrade --install`

### Requirement: Helm ownership adoption
Every `helm upgrade --install` invocation SHALL include `--take-ownership` to adopt existing unmanaged Kubernetes resources without manual annotation or labeling.

#### Scenario: Upgrade command includes take-ownership
- **WHEN** `devops k8s deploy-stack` executes a Helm release install or upgrade
- **THEN** the constructed `helm upgrade --install` command arguments include `--take-ownership`
2 changes: 1 addition & 1 deletion src/devops_cli/ai/library/introspector.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ def _import_root_module(self, package_name: str) -> ModuleType:
except ImportError, ModuleNotFoundError:
continue
raise LibraryNotFoundError(
f"Package '{package_name}' not found or cannot be imported in the current environment"
f"Package '{package_name}' not found or could not be loaded in the current environment"
)

def save_to_dir(self, contract: LibraryContract, output_dir: Path | str) -> Path:
Expand Down
12 changes: 6 additions & 6 deletions src/devops_cli/commands/k8s/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@
_HELM_REPOS_BY_STACK,
_MANIFESTS_BY_STACK,
VALID_STACKS,
_adopt_helm_resource_if_conflict,
_bootstrap_openwebui_account,
_recover_stuck_helm_release_if_pending,
_recover_stuck_helm_release,
_require_helm_version,
bootstrap_openwebui,
deploy_stack,
sync_secrets,
Expand Down Expand Up @@ -141,7 +141,6 @@
"_HELM_REPOS",
"_HELM_REPOS_BY_STACK",
"_MANIFESTS_BY_STACK",
"_adopt_helm_resource_if_conflict",
"_bootstrap_openwebui_account",
"_cluster_reachable",
"_detect_service_url",
Expand All @@ -150,7 +149,8 @@
"_k8s_clients",
"_minikube_running",
"_parse_minikube_service_url",
"_recover_stuck_helm_release_if_pending",
"_recover_stuck_helm_release",
"_require_helm_version",
"_resolve_accessible_url",
"_resolve_k8s_node_port_url",
"_resolve_stacks",
Expand Down Expand Up @@ -219,9 +219,9 @@
"bootstrap_openwebui",
"sync_secrets",
"teardown_stack",
"_adopt_helm_resource_if_conflict",
"_bootstrap_openwebui_account",
"_recover_stuck_helm_release_if_pending",
"_recover_stuck_helm_release",
"_require_helm_version",
}


Expand Down
Loading
Loading