-
-
Notifications
You must be signed in to change notification settings - Fork 326
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #540 from kazk/polish-tls-support
- Loading branch information
Showing
37 changed files
with
1,054 additions
and
619 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Minimal custom client example. | ||
use k8s_openapi::api::core::v1::ConfigMap; | ||
use tower::ServiceBuilder; | ||
|
||
use kube::{ | ||
api::{Api, ListParams}, | ||
client::ConfigExt, | ||
Client, Config, | ||
}; | ||
|
||
#[tokio::main] | ||
async fn main() -> anyhow::Result<()> { | ||
std::env::set_var("RUST_LOG", "info,kube=debug"); | ||
tracing_subscriber::fmt::init(); | ||
|
||
let config = Config::infer().await?; | ||
let https = config.native_tls_https_connector()?; | ||
let client = Client::new( | ||
ServiceBuilder::new() | ||
.layer(config.base_uri_layer()) | ||
.option_layer(config.auth_layer()?) | ||
.service(hyper::Client::builder().build(https)), | ||
); | ||
|
||
let cms: Api<ConfigMap> = Api::namespaced(client, "default"); | ||
for cm in cms.list(&ListParams::default()).await? { | ||
println!("{:?}", cm); | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// 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 tower::ServiceBuilder; | ||
|
||
use kube::{ | ||
api::{Api, ListParams}, | ||
client::ConfigExt, | ||
Client, Config, | ||
}; | ||
|
||
#[tokio::main] | ||
async fn main() -> anyhow::Result<()> { | ||
std::env::set_var("RUST_LOG", "info,kube=debug"); | ||
tracing_subscriber::fmt::init(); | ||
|
||
let config = Config::infer().await?; | ||
|
||
// 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 https = config.rustls_https_connector()?; | ||
Client::new( | ||
ServiceBuilder::new() | ||
.layer(config.base_uri_layer()) | ||
.service(hyper::Client::builder().build(https)), | ||
) | ||
} else { | ||
let https = config.native_tls_https_connector()?; | ||
Client::new( | ||
ServiceBuilder::new() | ||
.layer(config.base_uri_layer()) | ||
.service(hyper::Client::builder().build(https)), | ||
) | ||
}; | ||
|
||
let cms: Api<ConfigMap> = Api::namespaced(client, "default"); | ||
for cm in cms.list(&ListParams::default()).await? { | ||
println!("{:?}", cm); | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Custom client example with TraceLayer. | ||
use std::time::Duration; | ||
|
||
use http::{Request, Response}; | ||
use hyper::Body; | ||
use k8s_openapi::api::core::v1::ConfigMap; | ||
use tower::ServiceBuilder; | ||
use tower_http::{decompression::DecompressionLayer, trace::TraceLayer}; | ||
use tracing::Span; | ||
|
||
use kube::{ | ||
api::{Api, ListParams}, | ||
client::ConfigExt, | ||
Client, Config, | ||
}; | ||
|
||
#[tokio::main] | ||
async fn main() -> anyhow::Result<()> { | ||
std::env::set_var("RUST_LOG", "info,kube=debug,custom_client_trace=debug"); | ||
tracing_subscriber::fmt::init(); | ||
|
||
let config = Config::infer().await?; | ||
let https = config.native_tls_https_connector()?; | ||
let client = Client::new( | ||
ServiceBuilder::new() | ||
.layer(config.base_uri_layer()) | ||
// Add `DecompressionLayer` to make request headers interesting. | ||
.layer(DecompressionLayer::new()) | ||
.layer( | ||
// Attribute names follow [Semantic Conventions]. | ||
// [Semantic Conventions]: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/http.md#http-client | ||
TraceLayer::new_for_http() | ||
.make_span_with(|request: &Request<Body>| { | ||
tracing::debug_span!( | ||
"HTTP", | ||
http.method = %request.method(), | ||
http.url = %request.uri(), | ||
http.status_code = tracing::field::Empty, | ||
otel.name = %format!("HTTP {}", request.method()), | ||
otel.kind = "client", | ||
otel.status_code = tracing::field::Empty, | ||
) | ||
}) | ||
.on_request(|request: &Request<Body>, _span: &Span| { | ||
tracing::debug!("payload: {:?} headers: {:?}", request.body(), request.headers()) | ||
}) | ||
.on_response(|response: &Response<Body>, latency: Duration, span: &Span| { | ||
let status = response.status(); | ||
span.record("http.status_code", &status.as_u16()); | ||
if status.is_client_error() || status.is_server_error() { | ||
span.record("otel.status_code", &"ERROR"); | ||
} | ||
tracing::debug!("finished in {}ms", latency.as_millis()) | ||
}), | ||
) | ||
.service(hyper::Client::builder().build(https)), | ||
); | ||
|
||
let cms: Api<ConfigMap> = Api::namespaced(client, "default"); | ||
for cm in cms.list(&ListParams::default()).await? { | ||
println!("{:?}", cm); | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.