Skip to content

Commit

Permalink
add moving with_default_ns Client setter - fixes #543
Browse files Browse the repository at this point in the history
  • Loading branch information
clux committed Jun 6, 2021
1 parent 0339b85 commit 17647e1
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 23 deletions.
19 changes: 9 additions & 10 deletions examples/custom_client.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
// Minimal custom client example.
use k8s_openapi::api::core::v1::ConfigMap;
use tower::ServiceBuilder;
use k8s_openapi::api::core::v1::Pod;

use kube::{
api::{Api, ListParams},
Api, ResourceExt,
client::ConfigExt,
Client, Config,
};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
std::env::set_var("RUST_LOG", "info,kube=debug");
std::env::set_var("RUST_LOG", "info,kube=trace");
tracing_subscriber::fmt::init();

let config = Config::infer().await?;
let https = config.native_tls_https_connector()?;
let client = Client::new(
ServiceBuilder::new()
tower::ServiceBuilder::new()
.layer(config.base_uri_layer())
.option_layer(config.auth_layer()?)
.service(hyper::Client::builder().build(https)),
);
.service(hyper::Client::builder().build(https))
).with_default_namespace(config.default_ns);

let cms: Api<ConfigMap> = Api::namespaced(client, "default");
for cm in cms.list(&ListParams::default()).await? {
println!("{:?}", cm);
let pods: Api<Pod> = Api::default_namespaced(client);
for p in pods.list(&Default::default()).await? {
println!("{}", p.name());
}

Ok(())
Expand Down
14 changes: 7 additions & 7 deletions examples/custom_client_tls.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Custom client supporting both native-tls and rustls-tls
// Must enable `rustls-tls` feature to run this.
// Run with `USE_RUSTLS=1` to pick rustls.
use k8s_openapi::api::core::v1::ConfigMap;
use k8s_openapi::api::core::v1::Pod;
use tower::ServiceBuilder;

use kube::{
api::{Api, ListParams},
Api, ResourceExt,
client::ConfigExt,
Client, Config,
};
Expand All @@ -19,7 +19,7 @@ async fn main() -> anyhow::Result<()> {

// Pick TLS at runtime
let use_rustls = std::env::var("USE_RUSTLS").map(|s| s == "1").unwrap_or(false);
let client = if use_rustls {
let client = (if use_rustls {
let https = config.rustls_https_connector()?;
Client::new(
ServiceBuilder::new()
Expand All @@ -33,11 +33,11 @@ async fn main() -> anyhow::Result<()> {
.layer(config.base_uri_layer())
.service(hyper::Client::builder().build(https)),
)
};
}).with_default_namespace(config.default_ns);

let cms: Api<ConfigMap> = Api::namespaced(client, "default");
for cm in cms.list(&ListParams::default()).await? {
println!("{:?}", cm);
let pods: Api<Pod> = Api::default_namespaced(client);
for p in pods.list(&Default::default()).await? {
println!("{}", p.name());
}

Ok(())
Expand Down
12 changes: 6 additions & 6 deletions examples/custom_client_trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ use std::time::Duration;

use http::{Request, Response};
use hyper::Body;
use k8s_openapi::api::core::v1::ConfigMap;
use k8s_openapi::api::core::v1::Pod;
use tower::ServiceBuilder;
use tower_http::{decompression::DecompressionLayer, trace::TraceLayer};
use tracing::Span;

use kube::{
api::{Api, ListParams},
Api, ResourceExt,
client::ConfigExt,
Client, Config,
};
Expand Down Expand Up @@ -54,11 +54,11 @@ async fn main() -> anyhow::Result<()> {
}),
)
.service(hyper::Client::builder().build(https)),
);
).with_default_namespace(config.default_ns);

let cms: Api<ConfigMap> = Api::namespaced(client, "default");
for cm in cms.list(&ListParams::default()).await? {
println!("{:?}", cm);
let pods: Api<Pod> = Api::default_namespaced(client);
for p in pods.list(&Default::default()).await? {
println!("{}", p.name());
}

Ok(())
Expand Down
7 changes: 7 additions & 0 deletions kube/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@ impl Client {
pub(crate) fn default_ns(&self) -> &str {
&self.default_ns
}
/// Set the default namespace on a [`Client`]
///
/// This is done by default in [`Client::try_default`], but must be done manually with custom clients.
pub fn with_default_namespace<T: Into<String>>(mut self, ns: T) -> Self {
self.default_ns = ns.into();
self
}

async fn send(&self, request: Request<Body>) -> Result<Response<Body>> {
let mut svc = self.inner.clone();
Expand Down

0 comments on commit 17647e1

Please sign in to comment.