diff --git a/Cargo.lock b/Cargo.lock index 3ef7d11cc..c2845a43e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2234,6 +2234,7 @@ dependencies = [ "console-logger", "constants", "humantime", + "kube 0.94.2", "kube-forward", "kube-proxy", "openapi", diff --git a/k8s/plugin/Cargo.toml b/k8s/plugin/Cargo.toml index e53a41748..4b4e67651 100644 --- a/k8s/plugin/Cargo.toml +++ b/k8s/plugin/Cargo.toml @@ -24,6 +24,7 @@ rest-plugin = { path = "../../dependencies/control-plane/control-plane/plugin", console-logger = { path = "../../console-logger" } supportability = { path = "../supportability" } upgrade = { path = "../upgrade" } +kube = { version = "0.94.2", features = ["derive", "runtime"] } kube-proxy = { path = "../../dependencies/control-plane/k8s/proxy" } kube-forward = { path = "../../dependencies/control-plane/k8s/forward" } tokio = { version = "1.41.0" } diff --git a/k8s/plugin/src/main.rs b/k8s/plugin/src/main.rs index c4af5c6a3..f7e7f4145 100644 --- a/k8s/plugin/src/main.rs +++ b/k8s/plugin/src/main.rs @@ -1,11 +1,11 @@ use clap::Parser; use plugin::ExecuteOperation; use resources::{init_rest, Error, Operations}; +pub mod resources; +use kube::Client; use std::{env, ops::Deref}; -pub mod resources; - #[derive(Parser, Debug)] #[clap(name = utils::package_description!(), version = utils::version_info_str!())] #[group(skip)] @@ -15,17 +15,36 @@ struct CliArgs { operations: Operations, /// Kubernetes namespace of mayastor service - #[clap(global = true, long, short = 'n', default_value = "mayastor")] - namespace: String, + #[clap(global = true, long, short = 'n')] + namespace: Option, #[clap(flatten)] args: resources::CliArgs, + + /// Use namespace from the current `kubeconfig` context. + #[clap(global = true, long, hide = true, default_value = "false")] + namespace_from_context: bool, } impl CliArgs { - fn args() -> Self { + async fn args() -> Self { let mut args = CliArgs::parse(); - args.args.namespace = args.namespace.clone(); + args.args.namespace = if let Some(namespace) = &args.namespace { + namespace.to_string() + } else if args.namespace_from_context { + let client = if let Some(path) = &args.args.kube_config_path { + kube_proxy::client_from_kubeconfig(Some(path.clone())) + .await + .expect("Couldnt create client from supplied kubeconfig") + } else { + Client::try_default() + .await + .expect("Couldnt create client from default kubeconfig") + }; + client.default_namespace().to_string() + } else { + "mayastor".to_string() + }; args } } @@ -40,7 +59,7 @@ impl Deref for CliArgs { #[tokio::main] async fn main() { - let cli_args = CliArgs::args(); + let cli_args = CliArgs::args().await; let _tracer_flusher = cli_args.init_tracing(); if let Err(error) = cli_args.execute().await {