Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add partial namespace matching #245

Merged
merged 2 commits into from
Sep 11, 2024
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
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,15 @@ prompt:

# Behavior
behavior:
# Make sure the namespace exists with `kubectl get namespaces` when switching
# namespaces. If you do not have the right to list namespaces, disable this.
# Namespace validation and switching behavior. Set to "false" if you do not have
# the right to list namespaces.
# Valid values:
# true: Make sure the namespace exists with `kubectl get namespaces`.
# false: Switch namespaces without validation.
# partial: Check for partial matches when running `kubie ns <namespace>`
# and no exact match is found:
# - if exactly one namespace partially matches, switch to that namespace
# - if multiple namespaces partially match, select from those
# Default: true
validate_namespaces: true

Expand Down
2 changes: 1 addition & 1 deletion src/cmd/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn enter_context(
kubeconfig.contexts[0].context.namespace.as_deref(),
);

if settings.behavior.validate_namespaces {
if settings.behavior.validate_namespaces.can_list_namespaces() {
if let Some(namespace_name) = namespace_name {
let namespaces = kubectl::get_namespaces(Some(&kubeconfig))?;
if !namespaces.iter().any(|x| x == namespace_name) {
Expand Down
5 changes: 3 additions & 2 deletions src/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ pub fn select_or_list_context(skim_options: &SkimOptions, installed: &mut Instal
}
}

pub fn select_or_list_namespace(skim_options: &SkimOptions) -> Result<SelectResult> {
let mut namespaces = kubectl::get_namespaces(None)?;
pub fn select_or_list_namespace(skim_options: &SkimOptions, namespaces: Option<Vec<String>>) -> Result<SelectResult> {
let mut namespaces = namespaces.unwrap_or_else(|| kubectl::get_namespaces(None).expect("could not get namespaces"));

namespaces.sort();

if namespaces.is_empty() {
Expand Down
32 changes: 25 additions & 7 deletions src/cmd/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::cmd::{select_or_list_namespace, SelectResult};
use crate::kubeconfig;
use crate::kubectl;
use crate::session::Session;
use crate::settings::Settings;
use crate::settings::{Settings, ValidateNamespacesBehavior};
use crate::shell::spawn_shell;
use crate::state::State;
use crate::vars;
Expand All @@ -32,16 +32,34 @@ pub fn namespace(
.context("There is not previous namespace to switch to")?
.to_string(),
),
Some(s) => {
if settings.behavior.validate_namespaces {
Some(s) => match settings.behavior.validate_namespaces {
ValidateNamespacesBehavior::False => Some(s),
ValidateNamespacesBehavior::True => {
let namespaces = kubectl::get_namespaces(None)?;
if !namespaces.contains(&s) {
return Err(anyhow!("'{}' is not a valid namespace for the context", s))
return Err(anyhow!("'{}' is not a valid namespace for the context", s));
}
Some(s)
}
Some(s)
}
None => match select_or_list_namespace(skim_options)? {
ValidateNamespacesBehavior::Partial => {
let namespaces = kubectl::get_namespaces(None)?;
if namespaces.contains(&s) {
Some(s)
} else {
let ns_partial_matches: Vec<String> =
namespaces.iter().filter(|&ns| ns.contains(&s)).cloned().collect();
match ns_partial_matches.len() {
0 => return Err(anyhow!("'{}' is not a valid namespace for the context", s)),
1 => Some(ns_partial_matches[0].clone()),
_ => match select_or_list_namespace(skim_options, Some(ns_partial_matches))? {
SelectResult::Selected(s) => Some(s),
_ => return Ok(()),
},
}
}
}
},
None => match select_or_list_namespace(skim_options, None)? {
SelectResult::Selected(s) => Some(s),
_ => return Ok(()),
},
Expand Down
25 changes: 17 additions & 8 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,19 +166,28 @@ impl ContextHeaderBehavior {
}
}

#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, Default)]
pub struct Behavior {
#[serde(default = "def_bool_true")]
pub validate_namespaces: bool,
#[serde(default)]
pub validate_namespaces: ValidateNamespacesBehavior,
#[serde(default)]
pub print_context_in_exec: ContextHeaderBehavior,
}

impl Default for Behavior {
fn default() -> Self {
Behavior {
validate_namespaces: true,
print_context_in_exec: Default::default(),
#[derive(Debug, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
pub enum ValidateNamespacesBehavior {
#[default]
True,
False,
Partial,
}

impl ValidateNamespacesBehavior {
pub fn can_list_namespaces(&self) -> bool {
match self {
ValidateNamespacesBehavior::True | ValidateNamespacesBehavior::Partial => true,
ValidateNamespacesBehavior::False => false,
}
}
}
Expand Down
Loading