Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Api::default_namespaced #534

Merged
merged 2 commits into from
May 21, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions kube/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,21 @@ impl<K: Resource> Api<K> {
}
}

/// Namespaced resource within the default namespace
///
/// This function accepts `K::DynamicType` so it can be used with dynamic resources.
///
/// Unless configured explicitly, the default namespace is either "default"
/// out of cluster, or the service account's namespace in cluster.
pub fn default_namespaced_with(client: Client, dyntype: &K::DynamicType) -> Self {
let url = K::url_path(dyntype, Some(client.default_ns()));
Self {
client,
request: Request::new(url),
phantom: std::iter::empty(),
}
}

/// Consume self and return the [`Client`]
pub fn into_client(self) -> Client {
self.into()
Expand Down Expand Up @@ -110,6 +125,19 @@ where
phantom: std::iter::empty(),
}
}

/// Namespaced resource within the default namespace
///
/// Unless configured explicitly, 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 url = K::url_path(&Default::default(), Some(client.default_ns()));
Self {
client,
request: Request::new(url),
phantom: std::iter::empty(),
}
}
}

impl<K> From<Api<K>> for Client {
Expand Down
18 changes: 17 additions & 1 deletion kube/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,30 @@ pub struct Client {
// - `Buffer` for cheap clone
// - `BoxService` for dynamic response future type
inner: Buffer<BoxService<Request<Body>, Response<Body>, BoxError>, Request<Body>>,
default_ns: String,
}

impl Client {
/// Create and initialize a [`Client`] using the given `Service`.
///
/// Use [`Client::try_from`](Self::try_from) to create with a [`Config`].
pub fn new<S>(service: S) -> Self
where
S: Service<Request<Body>, Response = Response<Body>, Error = BoxError> + Send + 'static,
S::Future: Send + 'static,
{
Self::new_with_default_ns(service, "default")
}

/// Create and initialize a [`Client`] using the given `Service` and the default namespace.
pub fn new_with_default_ns<S, T: Into<String>>(service: S, default_ns: T) -> Self
where
S: Service<Request<Body>, Response = Response<Body>, Error = BoxError> + Send + 'static,
S::Future: Send + 'static,
{
Self {
inner: Buffer::new(BoxService::new(service), 1024),
default_ns: default_ns.into(),
}
}

Expand All @@ -82,6 +93,10 @@ impl Client {
Self::try_from(Config::infer().await?)
}

pub(crate) fn default_ns(&self) -> &str {
&self.default_ns
}

async fn send(&self, request: Request<Body>) -> Result<Response<Body>> {
let mut svc = self.inner.clone();
let res = svc
Expand Down Expand Up @@ -366,6 +381,7 @@ impl TryFrom<Config> for Client {
let cluster_url = config.cluster_url.clone();
let mut default_headers = config.headers.clone();
let timeout = config.timeout;
let default_ns = config.default_ns.clone();

// AuthLayer is not necessary unless `RefreshableToken`
let maybe_auth = match Authentication::try_from(&config.auth_info)? {
Expand Down Expand Up @@ -416,7 +432,7 @@ impl TryFrom<Config> for Client {
.option_layer(maybe_auth)
.layer(tower::layer::layer_fn(LogRequest::new))
.service(client);
Ok(Self::new(inner))
Ok(Self::new_with_default_ns(inner, default_ns))
}
}

Expand Down