feat(view): label-selector and inverse table filters (Phase 4.5)
The table filter only did plain substring matching, so `:<kind> -l app=foo` (which the command parser feeds in as the filter `app=foo`) silently substring-matched the literal text instead of matching labels. Parse the filter k9s-style (compiled once per projection): - `!term` inverse (exclude matches) - `key=value` / `key==value` label equals - `key!=value` label not-equals (holds if absent or different) - comma-separated requirements all must hold (`app=api,tier=backend`) - anything else case-insensitive substring (unchanged default) Label matching uses the entity's cached labels. A stray `=` in a search term won't masquerade as a selector (label keys are validated). Tests: label eq/neq/absent, multi-requirement, inverse, substring fallback. Deferred (no new deps): fuzzy + regex; detail/log search stays substring. Validation: fmt, check, test (84 passed / 4 ignored), release build all green. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
edae2ae09f
commit
adfa11c384
3 files changed
+191
-9
No files matched your search
+6
-2
@@ -167,8 +167,12 @@ Root problem: full object JSON kept for every entity.
|
||||
- [ ] **4.3 Resource-correlated events** — selecting a pod shows its events; today Events pane
|
||||
only renders Event objects directly (detail_pane.rs:170 "planned").
|
||||
- [ ] **4.4 Rendered describe** — kubectl-style human `describe` output alongside YAML/JSON.
|
||||
- [ ] **4.5 Filter parity** — fuzzy + regex + label-selector matching (k9s `/`, `-l`), beyond
|
||||
plain substring (projector.rs:46).
|
||||
- [~] **4.5 Filter parity** — table filter now parses k9s-style: `!term` inverse, and label
|
||||
selectors `key=value`/`key==value`/`key!=value` (comma-separated, all must hold) matched against
|
||||
entity labels — this also fixes `:<kind> -l app=foo`, which previously substring-matched the
|
||||
literal `app=foo` instead of labels. Plain text stays substring (predictable default).
|
||||
Compiled once per projection (`Filter`). Tests cover label eq/neq/absent, inverse, substring
|
||||
fallback. Deferred (dep-free for now): fuzzy + regex matching; detail/log search still substring.
|
||||
- [ ] **4.6 Port-forward** — readonly-adjacent inspection; decide in/out of v1 scope.
|
||||
- [ ] **4.7 k9s readonly surface audit** — enumerate k9s readonly commands/keybindings; close
|
||||
remaining gaps or explicitly mark out-of-scope (pulses/xray/popeye are stubbed,
|
||||
|
||||
@@ -79,6 +79,11 @@ Controller behavior:
|
||||
## Search and Command UX
|
||||
|
||||
- `/`: filter in list/overlay panes, search in detail/log panes
|
||||
|
||||
Table filter syntax (in the `/` prompt or via `:<kind> -l ...`):
|
||||
- plain text — case-insensitive substring over name/namespace/status/summary
|
||||
- `!term` — inverse (exclude matches)
|
||||
- `key=value` / `key==value` / `key!=value` — label selector; comma-separate requirements (`app=api,tier=backend`), all must hold
|
||||
- `?`: detail/search shortcut
|
||||
- `n` / `N`: next/previous match in detail/log panes
|
||||
- `:`: command mode
|
||||
|
||||
+180
-7
@@ -58,11 +58,9 @@ impl ViewProjector for SimpleViewProjector {
|
||||
fn project(&self, store: &StateStore, request: &ViewRequest) -> ViewModel {
|
||||
let namespace_filter = request.namespace.as_deref();
|
||||
let mut entities = store.list(&request.context, request.kind, namespace_filter);
|
||||
let needle = request.filter.to_lowercase();
|
||||
|
||||
if !needle.is_empty() {
|
||||
entities.retain(|entity| row_matches(entity, &needle));
|
||||
}
|
||||
let filter = Filter::parse(&request.filter);
|
||||
entities.retain(|entity| filter.matches(entity));
|
||||
|
||||
entities.sort_by(|a, b| {
|
||||
let ord = match request.sort {
|
||||
@@ -87,9 +85,114 @@ impl ViewProjector for SimpleViewProjector {
|
||||
}
|
||||
}
|
||||
|
||||
/// Case-insensitive substring match across the fields shown in the table, without allocating a
|
||||
/// combined haystack string per row.
|
||||
fn row_matches(entity: &crate::model::ResourceEntity, needle_lower: &str) -> bool {
|
||||
/// A parsed table filter (k9s-style). Compiled once per projection, applied per row.
|
||||
///
|
||||
/// Syntax:
|
||||
/// - empty → match everything
|
||||
/// - `!<rest>` → invert the result of `<rest>`
|
||||
/// - `k=v`, `k==v`, `k!=v` → label selector (comma-separated requirements, all must hold)
|
||||
/// - anything else → case-insensitive substring over name/namespace/status/summary
|
||||
#[derive(Debug, Clone)]
|
||||
enum Filter {
|
||||
All,
|
||||
Substring { needle: String, negate: bool },
|
||||
Labels { reqs: Vec<LabelReq>, negate: bool },
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct LabelReq {
|
||||
key: String,
|
||||
equal: bool,
|
||||
value: String,
|
||||
}
|
||||
|
||||
impl Filter {
|
||||
fn parse(raw: &str) -> Self {
|
||||
let trimmed = raw.trim();
|
||||
if trimmed.is_empty() {
|
||||
return Filter::All;
|
||||
}
|
||||
let (negate, body) = match trimmed.strip_prefix('!') {
|
||||
Some(rest) => (true, rest.trim()),
|
||||
None => (false, trimmed),
|
||||
};
|
||||
if body.is_empty() {
|
||||
// a lone "!" filters nothing useful — treat as match-all
|
||||
return Filter::All;
|
||||
}
|
||||
if let Some(reqs) = parse_label_selector(body) {
|
||||
return Filter::Labels { reqs, negate };
|
||||
}
|
||||
Filter::Substring {
|
||||
needle: body.to_lowercase(),
|
||||
negate,
|
||||
}
|
||||
}
|
||||
|
||||
fn matches(&self, entity: &crate::model::ResourceEntity) -> bool {
|
||||
match self {
|
||||
Filter::All => true,
|
||||
Filter::Substring { needle, negate } => substring_match(entity, needle) ^ negate,
|
||||
Filter::Labels { reqs, negate } => {
|
||||
reqs.iter().all(|req| req.matches(&entity.labels)) ^ negate
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl LabelReq {
|
||||
fn matches(&self, labels: &[(String, String)]) -> bool {
|
||||
let found = labels.iter().find(|(k, _)| k == &self.key);
|
||||
match (self.equal, found) {
|
||||
(true, Some((_, v))) => v == &self.value,
|
||||
(true, None) => false,
|
||||
// `!=` holds when the label is absent or has a different value (k9s semantics)
|
||||
(false, Some((_, v))) => v != &self.value,
|
||||
(false, None) => true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse `k=v,k2!=v2` into label requirements. Returns None if the text isn't a label selector
|
||||
/// (so it falls through to substring matching).
|
||||
fn parse_label_selector(body: &str) -> Option<Vec<LabelReq>> {
|
||||
let mut reqs = Vec::new();
|
||||
for part in body.split(',') {
|
||||
let part = part.trim();
|
||||
if let Some((k, v)) = part.split_once("!=") {
|
||||
reqs.push(LabelReq {
|
||||
key: valid_label_key(k)?,
|
||||
equal: false,
|
||||
value: v.trim().to_string(),
|
||||
});
|
||||
} else if let Some((k, v)) = part.split_once("==").or_else(|| part.split_once('=')) {
|
||||
reqs.push(LabelReq {
|
||||
key: valid_label_key(k)?,
|
||||
equal: true,
|
||||
value: v.trim().to_string(),
|
||||
});
|
||||
} else {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
if reqs.is_empty() { None } else { Some(reqs) }
|
||||
}
|
||||
|
||||
/// Accept a plausible Kubernetes label key (so a stray `=` in a search term doesn't masquerade
|
||||
/// as a selector). Returns the trimmed key, or None if it doesn't look like a label key.
|
||||
fn valid_label_key(raw: &str) -> Option<String> {
|
||||
let key = raw.trim();
|
||||
if key.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let ok = key
|
||||
.chars()
|
||||
.all(|c| c.is_ascii_alphanumeric() || matches!(c, '-' | '_' | '.' | '/'));
|
||||
if ok { Some(key.to_string()) } else { None }
|
||||
}
|
||||
|
||||
/// Case-insensitive substring match across the fields shown in the table.
|
||||
fn substring_match(entity: &crate::model::ResourceEntity, needle_lower: &str) -> bool {
|
||||
let contains = |field: &str| field.to_lowercase().contains(needle_lower);
|
||||
contains(&entity.key.name)
|
||||
|| entity
|
||||
@@ -183,6 +286,76 @@ mod tests {
|
||||
assert_eq!(row.name, "worker");
|
||||
}
|
||||
|
||||
fn put_labeled(store: &mut StateStore, name: &str, labels: &[(&str, &str)]) {
|
||||
store.apply(StateDelta::Upsert(ResourceEntity {
|
||||
key: ResourceKey::new("ctx", ResourceKind::Pods, Some("ns".to_string()), name),
|
||||
status: "Running".to_string(),
|
||||
age: Some(Utc::now()),
|
||||
labels: labels
|
||||
.iter()
|
||||
.map(|(k, v)| (k.to_string(), v.to_string()))
|
||||
.collect(),
|
||||
summary: String::new(),
|
||||
extracted: Default::default(),
|
||||
}));
|
||||
}
|
||||
|
||||
fn project_names(store: &StateStore, filter: &str) -> Vec<String> {
|
||||
SimpleViewProjector
|
||||
.project(
|
||||
store,
|
||||
&ViewRequest {
|
||||
context: "ctx".to_string(),
|
||||
kind: ResourceKind::Pods,
|
||||
namespace: None,
|
||||
filter: filter.to_string(),
|
||||
sort: SortColumn::Name,
|
||||
descending: false,
|
||||
},
|
||||
)
|
||||
.order
|
||||
.iter()
|
||||
.map(|key| key.name.clone())
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn filter_label_selector_matches_labels_not_substring() {
|
||||
let mut store = StateStore::default();
|
||||
put_labeled(&mut store, "api", &[("app", "api"), ("tier", "backend")]);
|
||||
put_labeled(&mut store, "web", &[("app", "web"), ("tier", "frontend")]);
|
||||
|
||||
assert_eq!(project_names(&store, "app=api"), vec!["api"]);
|
||||
assert_eq!(project_names(&store, "app==api"), vec!["api"]);
|
||||
assert_eq!(project_names(&store, "app!=api"), vec!["web"]);
|
||||
// all comma-separated requirements must hold
|
||||
assert_eq!(project_names(&store, "app=api,tier=backend"), vec!["api"]);
|
||||
assert!(project_names(&store, "app=api,tier=frontend").is_empty());
|
||||
// label key absent => `!=` holds, `=` does not
|
||||
assert_eq!(project_names(&store, "missing!=x").len(), 2);
|
||||
assert!(project_names(&store, "missing=x").is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn filter_inverse_excludes_matches() {
|
||||
let mut store = StateStore::default();
|
||||
put_labeled(&mut store, "api", &[]);
|
||||
put_labeled(&mut store, "worker", &[]);
|
||||
assert_eq!(project_names(&store, "!work"), vec!["api"]);
|
||||
assert_eq!(project_names(&store, "!app=api"), vec!["api", "worker"]); // no app label on either
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn filter_non_selector_terms_use_substring() {
|
||||
let mut store = StateStore::default();
|
||||
put_labeled(&mut store, "api-server", &[]);
|
||||
put_labeled(&mut store, "worker", &[]);
|
||||
// "ap" is not a selector (no '='), so it substring-matches the name
|
||||
assert_eq!(project_names(&store, "ap"), vec!["api-server"]);
|
||||
// empty filter matches all
|
||||
assert_eq!(project_names(&store, "").len(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "performance benchmark"]
|
||||
fn perf_project_large_active_view() {
|
||||
|
||||
Reference in new issue
Block a user