From 5320e120d2d66fb4311d635d4c2f401474407327 Mon Sep 17 00:00:00 2001 From: clux Date: Fri, 21 May 2021 21:27:54 +0100 Subject: [PATCH] add a config feature in kube - fixes #533 --- .circleci/config.yml | 1 + kube/Cargo.toml | 4 ++-- kube/src/config/mod.rs | 37 +++++++++++++++++++++++++++++++++++++ kube/src/error.rs | 3 +++ kube/src/lib.rs | 31 +++++++++++++++++++++++++++---- 5 files changed, 70 insertions(+), 6 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index bd56ecd58..c28c69186 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -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 diff --git a/kube/Cargo.toml b/kube/Cargo.toml index 161eb5681..67371868f 100644 --- a/kube/Cargo.toml +++ b/kube/Cargo.toml @@ -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] diff --git a/kube/src/config/mod.rs b/kube/src/config/mod.rs index a5b57c877..cd46a7d93 100644 --- a/kube/src/config/mod.rs +++ b/kube/src/config/mod.rs @@ -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; @@ -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/"); + } +} diff --git a/kube/src/error.rs b/kube/src/error.rs index e0ce22655..8a43ab767 100644 --- a/kube/src/error.rs +++ b/kube/src/error.rs @@ -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), @@ -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), diff --git a/kube/src/lib.rs b/kube/src/lib.rs index ba1ef35be..d37979a0e 100644 --- a/kube/src/lib.rs +++ b/kube/src/lib.rs @@ -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` pub type Result = std::result::Result; }