VirtusLab/besom

Add resource lifecycle hooks

lbialy opened this issue · 0 comments

lbialy commented

For example:

  extension [A <: besom.internal.Resource](outputOfResource: Output[A])
    def afterCreate(callback: A => Output[Unit]): Output[A] =
      for
        resource <- outputOfResource
        // make sure we run the callback after URN is resolved on a separate fork
        // CHECK THAT TASK TRACKER IS TRACKING THIS FORK
        _        <- (resource.urn *> callback(resource)).getData.fork 
      yield resource

This allows user to run code after a resource was actually created (by hijacking URN Promise resolution). Useful for execution of postconditions and unrelated tasks that have to happen ONLY for said resource, integrations for instance.

Separate fork is necessary to prevent pollution of main thread (that declares resources) with dynamic Output that will short-circuit on empty during dry-run and remove subsequent resources from the plan.

Use cases:

  • grafana instance can be configured post start with a hook (without init containers)
  • interact with a kubernetes service through a port-forward