Skip to content

Commit

Permalink
Add connect/read/write timeouts to Config (#971)
Browse files Browse the repository at this point in the history
* add connect/read/write timeout

Signed-off-by: goenning <me@goenning.net>

* code review

Signed-off-by: goenning <me@goenning.net>

* ignore internal deprecation

Signed-off-by: goenning <me@goenning.net>

Co-authored-by: Eirik A <sszynrae@gmail.com>
  • Loading branch information
goenning and clux authored Aug 5, 2022
1 parent fb695bc commit 050bf9d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
11 changes: 8 additions & 3 deletions kube-client/src/client/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ impl TryFrom<Config> for ClientBuilder<BoxService<Request<hyper::Body>, Response
use http::header::HeaderMap;
use tracing::Span;

let timeout = config.timeout;
let default_ns = config.default_namespace.clone();

let client: hyper::Client<_, hyper::Body> = {
Expand Down Expand Up @@ -101,8 +100,14 @@ impl TryFrom<Config> for ClientBuilder<BoxService<Request<hyper::Body>, Response
));

let mut connector = TimeoutConnector::new(connector);
connector.set_connect_timeout(timeout);
connector.set_read_timeout(timeout);

// Set the timeout for the client and fallback to default deprecated timeout until it's removed
#[allow(deprecated)]
{
connector.set_connect_timeout(config.connect_timeout.or(config.timeout));
connector.set_read_timeout(config.read_timeout.or(config.timeout));
connector.set_write_timeout(config.write_timeout);
}

hyper::Client::builder().build(connector)
};
Expand Down
30 changes: 30 additions & 0 deletions kube-client/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,25 @@ pub struct Config {
pub default_namespace: String,
/// The configured root certificate
pub root_cert: Option<Vec<Vec<u8>>>,
/// Set the timeout for connecting to the Kubernetes API.
///
/// A value of `None` means no timeout
pub connect_timeout: Option<std::time::Duration>,
/// Set the timeout for the Kubernetes API response.
///
/// A value of `None` means no timeout
pub read_timeout: Option<std::time::Duration>,
/// Set the timeout for the Kubernetes API request.
///
/// A value of `None` means no timeout
pub write_timeout: Option<std::time::Duration>,
/// Timeout for calls to the Kubernetes API.
///
/// A value of `None` means no timeout
#[deprecated(
since = "0.75.0",
note = "replaced by more granular members `connect_timeout`, `read_timeout` and `write_timeout`. This member will be removed in 0.78.0."
)]
pub timeout: Option<std::time::Duration>,
/// Whether to accept invalid certificates
pub accept_invalid_certs: bool,
Expand All @@ -148,10 +164,14 @@ impl Config {
/// Most likely you want to use [`Config::infer`] to infer the config from
/// the environment.
pub fn new(cluster_url: http::Uri) -> Self {
#[allow(deprecated)]
Self {
cluster_url,
default_namespace: String::from("default"),
root_cert: None,
connect_timeout: Some(DEFAULT_CONNECT_TIMEOUT),
read_timeout: Some(DEFAULT_READ_TIMEOUT),
write_timeout: None,
timeout: Some(DEFAULT_TIMEOUT),
accept_invalid_certs: false,
auth_info: AuthInfo::default(),
Expand Down Expand Up @@ -196,10 +216,14 @@ impl Config {
let default_namespace = incluster_config::load_default_ns()?;
let root_cert = incluster_config::load_cert()?;

#[allow(deprecated)]
Ok(Self {
cluster_url,
default_namespace,
root_cert: Some(root_cert),
connect_timeout: Some(DEFAULT_CONNECT_TIMEOUT),
read_timeout: Some(DEFAULT_READ_TIMEOUT),
write_timeout: None,
timeout: Some(DEFAULT_TIMEOUT),
accept_invalid_certs: false,
auth_info: AuthInfo {
Expand Down Expand Up @@ -254,10 +278,14 @@ impl Config {
root_cert = Some(ca_bundle);
}

#[allow(deprecated)]
Ok(Self {
cluster_url,
default_namespace,
root_cert,
connect_timeout: Some(DEFAULT_CONNECT_TIMEOUT),
read_timeout: Some(DEFAULT_READ_TIMEOUT),
write_timeout: None,
timeout: Some(DEFAULT_TIMEOUT),
accept_invalid_certs,
proxy_url: loader.proxy_url()?,
Expand Down Expand Up @@ -325,6 +353,8 @@ fn certs(data: &[u8]) -> Result<Vec<Vec<u8>>, pem::PemError> {
// https://github.com/kube-rs/kube-rs/issues/146#issuecomment-590924397
/// Default Timeout
const DEFAULT_TIMEOUT: Duration = Duration::from_secs(295);
const DEFAULT_CONNECT_TIMEOUT: Duration = Duration::from_secs(30);
const DEFAULT_READ_TIMEOUT: Duration = Duration::from_secs(295);

// temporary catalina hack for openssl only
#[cfg(all(target_os = "macos", feature = "native-tls"))]
Expand Down

0 comments on commit 050bf9d

Please sign in to comment.