Skip to content

Commit

Permalink
add default_namespaced - for #209
Browse files Browse the repository at this point in the history
based on suggestion in #394 (comment)
  • Loading branch information
clux committed Feb 11, 2021
1 parent c0c0c62 commit 676d385
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
27 changes: 27 additions & 0 deletions kube/src/api/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ where
}
}

/// Namespaced resource in the default namespace
///
/// The default namespace is either "default" out of cluster,
/// or the service account's namespace in cluster.
pub fn default_namespaced(client: Client) -> Self {
let resource = Resource::namespaced::<K>(crate::config::DEFAULT_NAMESPACE_MARKER);
Self {
resource,
client,
phantom: iter::empty(),
}
}

/// Consume self and return the [`Client`]
pub fn into_client(self) -> Client {
self.into()
Expand Down Expand Up @@ -339,3 +352,17 @@ impl<K> From<Api<K>> for Client {
api.client
}
}

#[test]
fn api_default_ns() {
use k8s_openapi::core::v1::Pod;
let mut client = Client::try_default();
client.default_ns = "dev";
let pods = Api<Pod>::default_namespaced(client);

// TODO: try to mock parts of the service to ensure the urls are corrected accordingly
assert_eq!(
final_url.as_str(),
"https://192.168.1.65:8443/api/v1/namespaces/dev/pods?hi=yes"
);
}
4 changes: 4 additions & 0 deletions kube/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ impl Config {
const DEFAULT_TIMEOUT: Duration = Duration::from_secs(295);
const IDENTITY_PASSWORD: &str = " ";

/// A namespace maker for `Config::default_ns`
/// this cannot clash with a legal namespace as it contains an underscore
pub(crate) const DEFAULT_NAMESPACE_MARKER: &str = "DEFAULT_NS";

// temporary catalina hack for openssl only
#[cfg(all(target_os = "macos", feature = "native-tls"))]
fn hacky_cert_lifetime_for_macos(ca: &[u8]) -> bool {
Expand Down
2 changes: 1 addition & 1 deletion kube/src/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl TryFrom<Config> for Service {
};

let common = ServiceBuilder::new()
.map_request(move |r| set_cluster_url(r, &cluster_url))
.map_request(move |r| set_cluster_url(r, &cluster_url, &config))
.map_request(move |r| set_default_headers(r, default_headers.clone()))
.into_inner();

Expand Down
10 changes: 7 additions & 3 deletions kube/src/service/url.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
use http::Request;
use hyper::Body;

/// Set cluster URL.
pub fn set_cluster_url(req: Request<Body>, url: &url::Url) -> Request<Body> {
/// Set cluster URL
///
/// This also propagates `Config::default_ns` if requested via `Api::default_namespaced`
pub fn set_cluster_url(req: Request<Body>, url: &url::Url, cfg: &Config) -> Request<Body> {
let (mut parts, body) = req.into_parts();
let pandq = parts.uri.path_and_query().expect("valid path+query from kube");
parts.uri = finalize_url(url, &pandq).parse().expect("valid URL");
parts.uri = finalize_url(url, &pandq)
.replace(crate::config::DEFAULT_NAMESPACE_MARKER, cfg.default_ns)
.parse().expect("valid URL");
Request::from_parts(parts, body)
}

Expand Down

0 comments on commit 676d385

Please sign in to comment.