Recurring task in k8s charms


We have a k8s charm that we’re working on which requires a recurring task to be run and it seems there are a few different options (and probably more we haven’t thought of):

  • Installing and configuring cron in the workload container to run the recurring task.
  • Talking to the k8s API to configure a kubernetes cronjob (although that restricts us to tasks that would be executed in separate pods from our workload).
  • Using pebble to start a long running process that runs the task with the frequency required.
  • Triggering off an update-status hook and running the task based on this (would mean you could only do so as frequently as update-status is configured for the model in question).

Has anyone else come across this already and have any recommendations/best practices for approaching the problem?

Thanks, Tom

1 Like

Hey Tom!

Those options will mostly work - for containers specifically, I’ve had quite a lot of luck using supercronic which is a nice statically compiled cron daemon with no external dependencies.

I’d probably use this by creating an additional sidecar container that just has supercronic in it, but depending on what the cronjob actually is, you could also add it to your workload container and just kick it off alongside the workload in your Pebble plan :slight_smile:

One other option is to utilise Pebble checks. These are obviously used traditionally for liveness/readiness, but at their core, they are essentially a way to execute some code on an interval - and you could use them to trigger a simple bash/python/whatever script in your workload relatively easily :slight_smile:

Cheers, Jon

Thanks for the suggestions. I think we’ll avoid abusing pebble health checks for now, although that’s a cute trick. Would it be worth us filing a bug against pebble asking for support for recurring tasks that aren’t checks? That does sort of feel like it might be the right place for this to live long term but we can install and use cron in the meantime so it’s not a blocker for sure.

We’re actually tracking that at the moment anyway in Pebble - we’re cooking the ability for Pebble to raise events in the operator in response to various intervals/stimulus, so this should be easier in the future.

(@hpidcock / @benhoyt)