File tree Expand file tree Collapse file tree 1 file changed +19
-0
lines changed
crates/stackable-operator/src Expand file tree Collapse file tree 1 file changed +19
-0
lines changed Original file line number Diff line number Diff 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_opt < 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.
You can’t perform that action at this time.
0 commit comments