Skip to content

Commit

Permalink
feat(plugin): add support to fetch ns from kubeconfig context
Browse files Browse the repository at this point in the history
If '--namespace-from-context' flag is set then we fetch ns from the kubeconfig
context. Its hidden to users now. Incase user has not given -n flag and
--namespace-from-context we default to "mayastor". If they have used
--namespace-from-context flag then we get ns from kubeconfig. If
kubeconfig path is specified then we use client of that kubeconfig or
else we get default kubeconfig client.

Signed-off-by: Abhilash Shetty <[email protected]>
  • Loading branch information
abhilashshetty04 committed Jan 27, 2025
1 parent c07c05c commit a63a983
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions k8s/plugin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
33 changes: 26 additions & 7 deletions k8s/plugin/src/main.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand All @@ -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<String>,

#[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
}
}
Expand All @@ -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 {
Expand Down

0 comments on commit a63a983

Please sign in to comment.