Skip to content

Commit

Permalink
Add kube::Api::get_metadata_opt_with (#1708)
Browse files Browse the repository at this point in the history
Adds the aforementioned method, allowing the use of get_metadata_opt()
with an explicit ResourceVersion. This follows an existing pattern, as
both get() and get_metadata() have *_with() variants.

Signed-off-by: Sebastian Soto <ssoto@redhat.com>
  • Loading branch information
sebsoto authored Mar 1, 2025
1 parent a0c043a commit 2cfd167
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions kube-client/src/api/core_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ where
}
}

/// [Get Metadata](`Api::get_metadata`) for a named resource if it exists, returns [`None`] if it doesn't exit
/// [Get Metadata](`Api::get_metadata`) for a named resource if it exists, returns [`None`] if it doesn't exist
///
/// ```no_run
/// # use kube::Api;
Expand All @@ -162,7 +162,36 @@ where
///
/// Note that [`PartialObjectMeta`] embeds the raw `ObjectMeta`.
pub async fn get_metadata_opt(&self, name: &str) -> Result<Option<PartialObjectMeta<K>>> {
match self.get_metadata(name).await {
self.get_metadata_opt_with(name, &GetParams::default()).await
}

/// [Get Metadata](`Api::get_metadata`) of an object if it exists, using an explicit `resourceVersion`.
/// Returns [`None`] if it doesn't exist.
///
/// ```no_run
/// # use kube::Api;
/// use k8s_openapi::api::core::v1::Pod;
/// use kube_core::params::GetParams;
///
/// async fn wrapper() -> Result<(), Box<dyn std::error::Error>> {
/// # let client: kube::Client = todo!();
/// let pods: Api<Pod> = Api::namespaced(client, "apps");
/// if let Some(pod) = pods.get_metadata_opt_with("blog", &GetParams::any()).await? {
/// // Pod was found
/// } else {
/// // Pod was not found
/// }
/// # Ok(())
/// # }
/// ```
///
/// Note that [`PartialObjectMeta`] embeds the raw `ObjectMeta`.
pub async fn get_metadata_opt_with(
&self,
name: &str,
gp: &GetParams,
) -> Result<Option<PartialObjectMeta<K>>> {
match self.get_metadata_with(name, gp).await {
Ok(meta) => Ok(Some(meta)),
Err(Error::Api(ErrorResponse { reason, .. })) if &reason == "NotFound" => Ok(None),
Err(err) => Err(err),
Expand Down

0 comments on commit 2cfd167

Please sign in to comment.