Skip to content

Commit

Permalink
add a config feature in kube - fixes #533
Browse files Browse the repository at this point in the history
  • Loading branch information
clux committed May 21, 2021
1 parent ad5247a commit 5320e12
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 6 deletions.
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ jobs:
- run: cargo test -p kube
- run: cargo test -p kube-derive
- run: cargo test -p kube-core
- run: cargo test -p kube --lib --no-default-features --features=config
- run: cargo test --lib --all -j4
- run: cargo test --doc --all -j4
- run: cargo test -j4 -p examples
Expand Down
4 changes: 2 additions & 2 deletions kube/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ rustls-tls = ["client", "hyper-rustls", "tokio-rustls"]
ws = ["client", "tokio-tungstenite", "rand", "kube-core/ws"]
oauth = ["client", "tame-oauth"]
gzip = ["client", "async-compression"]
client = ["__config", "__non_core", "hyper", "tower", "hyper-timeout", "pin-project", "chrono", "jsonpath_lib", "bytes", "futures", "tokio", "tokio-util", "either"]
client = ["config", "__non_core", "hyper", "tower", "hyper-timeout", "pin-project", "chrono", "jsonpath_lib", "bytes", "futures", "tokio", "tokio-util", "either"]
jsonpatch = ["kube-core/jsonpatch"]
admission = ["kube-core/admission"]
derive = ["kube-derive"]

# private feature sets; do not use
__config = ["pem", "dirs"]
config = ["__non_core", "pem", "dirs"]
__non_core = ["tracing", "serde_yaml", "base64"]

[package.metadata.docs.rs]
Expand Down
37 changes: 37 additions & 0 deletions kube/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod utils;
use crate::{error::ConfigError, Result};
use file_loader::ConfigLoader;
pub use file_loader::KubeConfigOptions;
#[cfg(feature = "client")]
pub(crate) use utils::read_file_to_string;

use http::header::HeaderMap;
Expand Down Expand Up @@ -212,3 +213,39 @@ pub use file_config::{
AuthInfo, AuthProviderConfig, Cluster, Context, ExecConfig, Kubeconfig, NamedAuthInfo, NamedCluster,
NamedContext, NamedExtension, Preferences,
};


#[cfg(test)]
mod tests {
#[cfg(not(feature = "client"))] // want to ensure this works without client features
#[tokio::test]
async fn config_loading_on_small_feature_set() {
use super::Config;
let cfgraw = r#"
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: aGVsbG8K
server: https://0.0.0.0:6443
name: k3d-test
contexts:
- context:
cluster: k3d-test
user: admin@k3d-test
name: k3d-test
current-context: k3d-test
kind: Config
preferences: {}
users:
- name: admin@k3d-test
user:
client-certificate-data: aGVsbG8K
client-key-data: aGVsbG8K
"#;
let file = tempfile::NamedTempFile::new().expect("create config tempfile");
std::fs::write(file.path(), cfgraw).unwrap();
std::env::set_var("KUBECONFIG", file.path());
let kubeconfig = Config::infer().await.unwrap();
assert_eq!(kubeconfig.cluster_url.into_string(), "https://0.0.0.0:6443/");
}
}
3 changes: 3 additions & 0 deletions kube/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ pub enum Error {
Connection(std::io::Error),

/// Hyper error
#[cfg(feature = "client")]
#[error("HyperError: {0}")]
HyperError(#[from] hyper::Error),
/// Service error
#[cfg(feature = "client")]
#[error("ServiceError: {0}")]
Service(tower::BoxError),

Expand Down Expand Up @@ -166,6 +168,7 @@ pub enum ConfigError {
#[error("exec-plugin response did not contain a status")]
ExecPluginFailed,

#[cfg(feature = "client")]
#[error("Malformed token expiration date: {0}")]
MalformedTokenExpirationDate(#[source] chrono::ParseError),

Expand Down
31 changes: 27 additions & 4 deletions kube/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,23 +101,46 @@ macro_rules! cfg_client {
)*
}
}
macro_rules! cfg_config {
($($item:item)*) => {
$(
#[cfg_attr(docsrs, doc(cfg(feature = "config")))]
#[cfg(feature = "config")]
$item
)*
}
}

macro_rules! cfg_error {
($($item:item)*) => {
$(
#[cfg_attr(docsrs, doc(cfg(any(feature = "config", feature = "client"))))]
#[cfg(any(feature = "config", feature = "client"))]
$item
)*
}
}

cfg_client! {
pub mod api;
pub mod client;
pub mod config;
pub(crate) mod service;

pub mod error;

#[doc(inline)]
pub use api::Api;
#[doc(inline)]
pub use client::Client;
}

cfg_config! {
pub mod config;
#[doc(inline)]
pub use config::Config;
#[doc(inline)] pub use error::Error;
}

cfg_error! {
pub mod error;
#[doc(inline)] pub use error::Error;
/// Convient alias for `Result<T, Error>`
pub type Result<T, E = Error> = std::result::Result<T, E>;
}
Expand Down

0 comments on commit 5320e12

Please sign in to comment.