Skip to content

Commit

Permalink
Update kubectl example to include optional context argument (#1647)
Browse files Browse the repository at this point in the history
Allow overriding current context with the named context from
the user's kubeconfig file.

Signed-off-by: Michael Sommerville <[email protected]>
  • Loading branch information
mgs255 authored Nov 23, 2024
1 parent 9f93e2f commit 3ee4ae5
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion examples/kubectl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -26,6 +27,8 @@ struct App {
selector: Option<String>,
#[arg(long, short)]
namespace: Option<String>,
#[arg(long, short)]
context: Option<String>,
#[arg(long, short = 'A')]
all: bool,
verb: Verb,
Expand Down Expand Up @@ -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?;
Expand Down

0 comments on commit 3ee4ae5

Please sign in to comment.