From 3ee4ae5303d5a71e9817ea786f4ee75cd54462ae Mon Sep 17 00:00:00 2001 From: Michael Sommerville Date: Sat, 23 Nov 2024 13:12:35 +0000 Subject: [PATCH] Update kubectl example to include optional context argument (#1647) Allow overriding current context with the named context from the user's kubeconfig file. Signed-off-by: Michael Sommerville --- examples/kubectl.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/examples/kubectl.rs b/examples/kubectl.rs index 2dcb3aa60..bcb342881 100644 --- a/examples/kubectl.rs +++ b/examples/kubectl.rs @@ -6,6 +6,7 @@ use futures::{StreamExt, TryStreamExt}; use k8s_openapi::{apimachinery::pkg::apis::meta::v1::Time, chrono::Utc}; use kube::{ api::{Api, DynamicObject, ListParams, Patch, PatchParams, ResourceExt}, + config::KubeConfigOptions, core::GroupVersionKind, discovery::{ApiCapabilities, ApiResource, Discovery, Scope}, runtime::{ @@ -26,6 +27,8 @@ struct App { selector: Option, #[arg(long, short)] namespace: Option, + #[arg(long, short)] + context: Option, #[arg(long, short = 'A')] all: bool, verb: Verb, @@ -185,7 +188,14 @@ impl App { async fn main() -> Result<()> { tracing_subscriber::fmt::init(); let app: App = clap::Parser::parse(); - let client = Client::try_default().await?; + + let options = KubeConfigOptions { + context: app.context.clone(), + cluster: None, + user: None, + }; + let config = kube::Config::from_kubeconfig(&options).await?; + let client = Client::try_from(config)?; // discovery (to be able to infer apis from kind/plural only) let discovery = Discovery::new(client.clone()).run().await?;