Skip to content

Commit 20656a6

Browse files
committed
feat(stackable-operator): Add create_if_missing method to client
1 parent c8d0621 commit 20656a6

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

crates/stackable-operator/src/client.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,25 @@ impl Client {
253253
})
254254
}
255255

256+
/// Optionally creates a resource if it does not exist yet.
257+
///
258+
/// The name used for lookup is extracted from the resource via [`ResourceExt::name_any()`].
259+
/// This function either returns the existing resource or the newly created one.
260+
pub async fn create_if_missing<T>(&self, resource: &T) -> Result<T>
261+
where
262+
T: Clone + Debug + DeserializeOwned + Resource + Serialize + GetApi,
263+
<T as Resource>::DynamicType: Default,
264+
{
265+
if let Some(r) = self
266+
.get_opt(&resource.name_any(), resource.get_namespace())
267+
.await?
268+
{
269+
return Ok(r);
270+
}
271+
272+
self.create(resource).await
273+
}
274+
256275
/// Patches a resource using the `MERGE` patch strategy described
257276
/// in [JSON Merge Patch](https://tools.ietf.org/html/rfc7386)
258277
/// This will fail for objects that do not exist yet.

0 commit comments

Comments
 (0)