Search code examples
kuberneteskubernetes-operatoroperator-sdk

How to release custom k8s operator when i need to run migration script?


I abstract my software as a k8s operator. When I want to release my software, there are two solutions, which one is better?

  1. Create a new service to detect version change whether need to run the migration script and then execute migrate.
  2. Write the logic of whether to run the migration script in the Operator. Use Operator auto detect version change.

Solution

  • Operators are designed to manage the lifecycle of application so the operator is the service that should detect and run this migration.

    To accomplish this, you have a few options:

    1. Resync on a timer. Regularly pull the image. If the sha has changed, run the migration logic. The issue here is when you start having many versions it can become very complex.
    2. Have the version of the operator tied to the version of the managed application so that migration occurs when upgrading the operator. Note in this case, the operator should be responsible for creating the CR for the managed application. You can use Helm or OLM to manage that operator upgrade for you (especially if you want to have very specific update paths: for example 1.0.0 -> 1.0.1 -> 1.0.2 but not 1.0.0 -> 1.0.2 directly).