davit 0.8.0

Kubernetes management utilities
# Product Requirement Document: Davit

| Metadata | Details |
| --- | --- |
| **Project Name** | **Davit** |
| **Version** | 1.0 |
| **Status** | Approved Draft |
| **Target Stack** | Rust (Linux/macOS) |
| **Core Function** | Safe Kubernetes Deployment Wrapper & TUI |

## 1. Executive Summary

**Concept:** In maritime terms, a *davit* is a crane-like device used to safely lower lifeboats or anchors into the water. In our context, **Davit** is a Rust-based CLI tool designed to safely "lower" new container versions into our Kubernetes clusters.

**Problem:** Our current deployment workflow relies on disparate manual steps: checking Google Artifact Registry (GAR), editing YAMLs by hand, switching `kubectl` contexts, and monitoring rollouts via raw terminal output. This "human glue" is prone to context errors (e.g., deploying to Prod instead of Staging) and lacks a standardized audit trail.

**Solution:** Davit acts as a mechanical safety layer. It standardizes the path from Artifact Registry to running Pod across legacy manifests and Helm/ArgoCD releases. It validates inputs, visualizes source and rendered changes via a TUI (Text User Interface), preserves Git as the desired-state authority, and provides a rich dashboard to monitor the specific moment of "handoff" between old and new pods.

---

## 2. User Personas

* **The Backend Developer:** "I built a feature and want to test it in Staging. I don't remember the exact `kubectl` context or the long image hash. I just want to select 'MyService' and the latest version."
* **The DevOps Engineer:** "I need to release a hotfix to Production. I need absolute certainty that the YAML diff is correct and that the previous pod shuts down gracefully while the new one starts."

---

## 3. Functional Requirements

### 3.1 Global Configuration

Davit must respect the XDG Base Directory specification.

* **Location:** `$XDG_CONFIG_HOME/davit/config.toml` (defaulting to `~/.config/davit/config.toml`).
* **Structure:** Defines available environments, root paths for local IaC repositories, and strict Kubernetes contexts.

**Example Schema:**

```toml
[[environments]]
name = "staging"
kubectl_context = "gke_europe-west1_staging"

[[environments.sources]]
name = "legacy"
type = "manifest"
repo_root = "/home/user/git/infra-repo/k8s/staging"

[[environments.sources]]
name = "helm"
type = "argo-cd"
repo_root = "/home/user/git/helm"
helm_cluster = "ccs-staging"

[[environments]]
name = "production"
kubectl_context = "gke_europe-west1_prod"
protected = true  # Forces an extra "type the environment name to confirm" step

[[environments.sources]]
name = "legacy"
type = "manifest"
repo_root = "/home/user/git/infra-repo/k8s/prod"

```

### 3.2 Progressive Disclosure & Input Resolution

The tool supports a hybrid workflow: "CLI for speed, TUI for discovery."

**Input Logic:**

**Smart Disambiguation Strategy:**

* **YAML-based Discovery:** Instead of assuming a fixed directory structure, Davit recursively scans the `repo_root` for YAML files and identifies microservices by searching for GCR/Artifact Registry images within container specs.
* **Exact Match:** User inputs `user-api`. Found 1 folder/service. -> **Select.**
* **Partial Match (Unique):** User inputs `pay`. Only `payment-service` exists. -> **Prompt:** *"Did you mean 'payment-service'? (Y/n)"*
* **Ambiguous Match:** User inputs `data`. Matches `data-ingest` and `database-proxy`. -> **Menu:** Show list of these two for selection.
* **Namespace Conflict:** User inputs `redis`. Exists in both `cache` and `session` namespaces. -> **Menu:** *"Which 'redis'?"*

### 3.3 Artifact Registry Integration

* **Source:** Google Artifact Registry (`pkg.dev`) and legacy Google Container Registry (`gcr.io`) via `gcloud` wrapper.
* **Sorting:** Reverse chronological (Newest `updateTime` at top). Supports robust timestamp parsing for various `gcloud` output formats.
* **Visuals:** The list must display:
* **Tag** (e.g., `v1.0.4-fix`)
* **Age** (e.g., `2 hours ago` - colored Green for recent, Grey for old)
* **Hash** (Short SHA)



### 3.4 The "Dry Run" (Preview Phase)

Before touching the cluster, Davit modifies the YAML in memory or a temp file.

* **Unified Diff:** A context-aware diff view showing specifically what is changing in the discovered `yaml_path`.
* **Interactive Selection:** After showing the diff, provide a menu with:
    * **Apply:** Save change and proceed to deployment.
    * **Show Full Diff:** Toggle between unified and complete file view.
    * **Dismiss:** Abort the deployment.
* **Context Check:** Prominently display: *"WARNING: You are targeting PRODUCTION"*.

### 3.5 The "Lowering" (Execution & Watch Phase)

Once confirmed, Davit applies the changes and enters **Dashboard Mode**.

* **Technology:** Uses `kubectl apply` for the change, but switches to `kube-rs` (or parsed `kubectl get -w`) for monitoring.
* **TUI Layout (Split Screen):**
* **Top Pane (Rollout Status):** Real-time table of ReplicaSets.
* *New Pods:* `ContainerCreating` -> `Running` -> `Ready`.
* *Old Pods:* `Running` -> `Terminating`.


* **Bottom Pane (Live Log Stream):**
* **Left Column:** Tailing logs of the **terminating** pod (to catch graceful shutdown errors).
* **Right Column:** Tailing logs of the **new** pod (to catch startup crashes/boot loops).





### 3.6 The "Anchor" (Git Operations)

Upon successful rollout (New Pod is Ready, Old Pod is Gone):

1. `git add <modified_file>`
2. `git commit -m "feat(deploy): update <service> to <tag> in <env>"`
3. `git push`
4. When release tagging is enabled for the environment and rollout completion has been confirmed,
   create and push an annotated tag for the release commit. The configured `strftime` format is the
   base name; existing local or remote names receive an increment such as `_01`.
4. Display: *"Deployment Successful & Config Saved."*

For direct Helm releases, Davit validates and renders the updated environment values, runs
`helm upgrade --install --atomic --wait` alongside the live log dashboard, and commits/pushes
only after both Helm and rollout monitoring confirm success. Closing logs does not cancel Helm;
Davit waits for its result, including any automatic rollback, and leaves values uncommitted.
Failed Helm upgrades restore the original local values without creating a Git commit. If Git
commit/push fails after deployment, updated values are retained and the recovery need is reported.
ArgoCD releases commit and push the desired state before syncing that exact Git revision.
Any configured release tag is created only after rollout completion and successful commit/push.
Rollback is performed through Helm history or a Git revert plus ArgoCD sync, never by applying a
rendered manifest directly.

### 3.7 Helm and ArgoCD Sources

* A Helm environment points at the repository root and a `clusters/<environment>` identifier.
* An environment may aggregate multiple repositories and deployment drivers during migration;
  discovery and deployment retain the selected service's source and driver.
* Services are discovered from ArgoCD `Application` resources and their local chart and
  `$values/` references.
* The primary repository is resolved from merged chart defaults and environment overrides; an
  Application may provide `davit.io/image-tag-path` when automatic resolution is ambiguous.
* The preview must include both the values change and the resulting `helm template` change.
* Davit must not use `kubectl apply` for Helm-backed services.
* Every distinct source repository is refreshed with a fast-forward pull even when unrelated local
  changes exist. Pull conflicts and branch divergence abort visibly; stale local state requires the
  explicit `--no-fetch` option.

---

### 3.8 Applying Manual YAML Changes

* `davit apply` discovers modified, staged, and new YAML files within the chosen environment's
  manifest and direct Helm sources. ArgoCD sources are skipped; deleted files do not trigger cluster deletion.
* A multiple-selection terminal screen provides `d` for a diff of the highlighted file against
  Git HEAD (rendered Kubernetes changes for Helm), Space for selection, J/K for application order,
  and Enter for confirmation. The final Helm preview combines all selected inputs.
* Secret payloads and annotations are hidden in local previews; live diffs containing Secrets
  are suppressed. Files retain their original formatting on disk.
* The user supplies a commit message. All selected snapshots receive server-side validation and
  a live cluster diff before final approval, then are applied sequentially with workload rollout
  monitoring. Dependencies requiring a new Namespace or CRD must be applied in separate invocations.
* After ConfigMap/Secret changes, the user can select referencing Deployments, StatefulSets, or
  DaemonSets for a rollout restart, followed by rollout monitoring. A workload whose pod template
  already changed and completed rollout after configuration application is not restarted again.
* After success, selected files are committed and pushed once per repository, preserving other
  staged changes. This flow creates no release tags. Non-interactive execution requires explicit
  file selection, message, approval, and a configuration restart policy; dry-run makes no changes.
* Direct Helm changes are grouped into one upgrade per affected release, including all references
  to a shared chart or values file. Release order follows the first selected input. Helm metadata
  discovery does not require a registry image, and all declared values files and the release namespace
  are retained. Image tags are not rewritten.
* Helm charts and values are snapshotted from Git HEAD plus selected files only, linted, rendered,
  validated, and upgraded with `--atomic --wait`. Unselected working-copy changes remain excluded.
  Chart dependencies must exist in Git HEAD; Application routing changes, inline overrides, and
  uncommitted non-YAML chart inputs are outside this flow. Helm dry-run renders locally.
* Processing stops on the first error and reports partial application, retaining local edits.
  Failed Helm upgrades request automatic rollback; successful earlier operations remain applied.
  Separate repositories are not a single
  Git transaction. Edits made after selection abort the sequence before committing unapplied content.

---

## 4. Non-Functional Requirements

### 4.1 System

* **Language:** Rust.
* **Distribution:** Single static binary.
* **OS Support:** Linux (primary), macOS (secondary).

### 4.2 Reliability & Safety

* **Atomic Revert:** If `kubectl apply` fails or the user aborts during the watch phase (Ctrl+C), Davit should offer to revert the local YAML file changes to the previous state.
* **Targeted Tag Replacement:** Deployment logic must escape image names to ensure only the intended microservice container is updated, leaving sidecars (e.g. `haproxy`) untouched.
* **Dependency Minimalist:** Should rely only on `kubectl`, `gcloud`, and `git` being present in `$PATH`.

---

## 5. Technical Architecture

### 5.1 Tech Stack (Rust Crates)

| Component | Crate | Usage |
| --- | --- | --- |
| **CLI Parser** | `clap` | Parsing arguments (`--env`, `--tag`). |
| **Config** | `config`, `serde`, `directories` | Loading TOML from XDG paths. |
| **Wizard TUI** | `inquire` | Selection lists, confirmation prompts, fuzzy search. |
| **Dashboard TUI** | `ratatui` | The complex split-screen view during rollout. |
| **K8s Interaction** | `kube` (kube-rs) | Monitoring Pod events and streaming logs programmatically. |
| **Process** | `std::process::Command` | Invoking `kubectl apply` and `git` commands. |

### 5.2 Application Flow

```mermaid
graph TD
    A[Start Davit] --> B{Config Loaded?}
    B -->|Yes| C[Parse CLI Args]
    C --> D{Missing Args?}
    D -->|Yes| E[Inquire Wizard: Env -> Service]
    E --> F[Inquire Wizard: Fetch & Select Image]
    D -->|No| F
    F --> G[Modify YAML (InMemory)]
    G --> H[Show Visual Diff]
    H --> I{User Confirm?}
    I -->|No| J[Exit / Revert]
    I -->|Yes| K[kubectl apply]
    K --> L[Ratatui Dashboard]
    L --> M{Rollout Success?}
    M -->|No| N[Show Error Logs]
    M -->|Yes| O[Git Commit & Push]
    O --> P[Exit Success]

```

---

## 6. Development Phasing

### Phase 1: The "Rigging" (MVP)

* Setup Rust project structure.
* Implement XDG Config loading.
* Implement `clap` for basic args.
* Implement `inquire` for Environment and Service selection (handling the disambiguation logic).
* Mock the Image Selection list.
* Output: Prints the `kubectl` command it *would* run.

### Phase 2: The "Winch" (Execution)

* Connect to Artifact Registry (via `gcloud` JSON output).
* Implement the YAML modification logic.
* Implement the Visual Diff.
* Execute `kubectl apply`.

### Phase 3: The "View" (Dashboard)

* Build the `ratatui` interface.
* Implement `kube-rs` watchers for Pod status.
* Implement dual-log streaming.
* Add Git automation.