kube icon indicating copy to clipboard operation
kube copied to clipboard

Consider adding a `RuntimeApiExt` trait to `kube-runtime`

Open clux opened this issue 3 years ago • 1 comments

I.e. add a trait that allows (after importing) the following changes (as an MVP):

-let stream = watcher(api, lp).applied_objects();
+let stream = api.watcher(lp).applied_objects();
-let cond = await_condition(api, "mycm", my_condition_fn);
+let cond = api.condition("mycm", my_condition_fn);

It should be fairly innocuous to do; it's unlikely we are ever going to kill off Api (unless we want to re-open any bigger discussions on neokubism) so we should make them as nice to work with as possible.

Having chaining here is simpler, allows simpler fn names, easier LTR scannability, and reduces the chance of passing in the wrong api (although unlikely if given further constraints but still possible).

Other potentials for inclusion in such an ext trait would be; delete_and_finalize, finalizer.

clux avatar May 21 '22 21:05 clux

Well, turns out this is incredibly complicated to do as a trait. Was trying something along the lines of:

pub trait RuntimeApiExt
{
    type R: Resource + Clone + Debug + DeserializeOwned + Send + 'static;
    fn watcher(&self, lp: &ListParams) -> impl Stream<Item = Result<Event<Self::R>>> + Send
    {
        watcher(self.clone(), lp)
    }

    async fn condition(&self, name: &str, cond: impl Condition<Self::R>) -> Result<Option<Self::R>, WaitErr> {
        await_condition(self, name, cond).await
    }
}

impl<K: ?Sized> RuntimeApiExt for Api<K>
where K: Resource + Clone + DeserializeOwned + Debug + Send + 'static
{
    type R = K;
}

but that has so many complications:

  • pulls in async_trait crate
  • can't use impl Stream in return of trait signatures (so have to convert watcher into a concrete Stream type)
  • cannot make this generic over K because there's no trait for Api<K> so the signatures doesn't actually match anyway

we also cannot make this an impl Api<K> where K:... because it would have to be a cross-crate impl.

...maybe this isn't a good idea with the current async rust setup.

clux avatar Jun 05 '22 20:06 clux