Skip to content
Open
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
485 changes: 485 additions & 0 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ anyhow = "1"
# Serialization
serde = { version = "1", features = ["derive"] }
serde_json = "1"
reqwest = { version = "0.12", default-features = false, features = ["json", "rustls-tls"] }

# CLI args + shell completions + man page generation
clap = { version = "4", features = ["derive"] }
Expand Down
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- **Live watch** — resources appear and update in real time as the cluster changes; deleted resources show `[DELETED]`
- **Unhealthy-first ordering** — `CrashLoopBackOff`, `Error`, `ImagePullBackOff` pods surface to the top automatically
- **Color-coded status** — red for critical, yellow for warning, green for healthy, dimmed for deleted
- **Optional OpenCost column** — show per-pod / per-controller cost inline, with namespace or cluster aggregate in the header
- **Multi-select bulk actions** — `tab` to select multiple resources, then describe/delete/restart them all at once
- **Multi-cluster support** — watch all kubeconfig contexts simultaneously with `--all-contexts`, or switch contexts interactively with `ctrl-x`
- **Context persistence** — last-used context is remembered across sessions
Expand Down Expand Up @@ -202,10 +203,34 @@ kf --kubeconfig ~/alt.yaml --context staging # use an alternate kubeconfig

---

## Optional Cost Awareness

Create `~/.config/kuberift/config.toml`:

```toml
[cost]
enabled = true
opencost_endpoint = "" # optional manual override; empty = auto-discover in cluster
display_period = "daily" # hourly | daily | monthly
highlight_threshold = 10.0 # highlight resources above $/day
```

When cost mode is enabled, `kf` will:

- detect `opencost` / `kubecost` services and query `allocation/compute`
- map pod allocations onto pods and controller-backed resources
- render a cost column without changing existing actions or output format
- disable cost for the current session if the endpoint is missing, slow, or unreachable

The namespace filter (`-n production`) also shows the namespace aggregate in the header.

---

## Config & State

| File | Purpose |
|------|---------|
| `~/.config/kuberift/config.toml` | Optional settings (`[cost]`) for OpenCost integration |
| `~/.config/kuberift/last_context` | Last-used context, restored on next launch |
| `$XDG_RUNTIME_DIR/<pid>/preview-mode` | Preview mode state (0=describe, 1=yaml, 2=logs) |
| `$XDG_RUNTIME_DIR/<pid>/preview-toggle` | Shell script installed at startup for ctrl-p |
Expand Down
70 changes: 70 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use serde::Deserialize;
use std::path::{Path, PathBuf};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
pub enum CostDisplayPeriod {
Hourly,
#[default]
Daily,
Monthly,
}

#[derive(Debug, Clone, Deserialize)]
pub struct CostConfig {
#[serde(default)]
pub enabled: bool,
#[serde(default)]
pub opencost_endpoint: String,
#[serde(default)]
pub display_period: CostDisplayPeriod,
#[serde(default = "default_highlight_threshold")]
pub highlight_threshold: f64,
}

impl Default for CostConfig {
fn default() -> Self {
Self {
enabled: false,
opencost_endpoint: String::new(),
display_period: CostDisplayPeriod::Daily,
highlight_threshold: default_highlight_threshold(),
}
}
}

#[derive(Debug, Clone, Deserialize, Default)]
pub struct AppConfig {
#[serde(default)]
pub cost: CostConfig,
}

fn default_highlight_threshold() -> f64 {
10.0
}

pub fn config_file_path() -> Option<PathBuf> {
dirs::config_dir().map(|dir| dir.join("kuberift").join("config.toml"))
}

pub fn load_config() -> AppConfig {
config_file_path()
.as_deref()
.and_then(load_config_from_path)
.unwrap_or_default()
}

pub fn load_config_from_path(path: &Path) -> Option<AppConfig> {
let raw = std::fs::read_to_string(path).ok()?;
Some(parse_config(&raw, path))
}

pub fn parse_config(raw: &str, source: &Path) -> AppConfig {
toml::from_str(raw).unwrap_or_else(|err| {
eprintln!(
"[kuberift] warning: cannot parse config '{}': {err}",
source.display()
);
AppConfig::default()
})
}
Loading
Loading