Skip to content

Waiting for Job completion in InitContainer (or before main container starts) #106802

@collimarco

Description

@collimarco

What would you like to be added?

A common pattern is to include the release phase (e.g. database migrations) inside a Job.
Then you need to wait until the migration is completed before starting the new pods: you can use an InitContainer for that.

For example if you use Rails you can:

  1. Create a Job (e.g. migration-job) with rails db:migrate command
  2. Inside the Deployment (e.g. web app), add an InitContainer that waits for the condition rake db:abort_if_pending_migrations (this command returns success only when the migrations are complete)

The problem is that not all commands (in step 1) have a corresponding "check" command (in step 2). It can be extremely hard for some commands to find a separate command in step 2 that verifies their completion.

It would be much easier if we could simply wait for a Job to become Complete before starting the pods (in step 2).

Why is this needed?

InitContainers are a great way to wait for a general condition to be met, but it is difficult to check a condition for a Kubernetes component (like a Job).

One of the most common conditions is a Job completion, but currently there's no way to express that in Kubernetes (e.g. waitFor: migration-job). This could be something similar to what kubectl wait does on the client side, but inside the spec.

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/featureCategorizes issue or PR as related to a new feature.lifecycle/rottenDenotes an issue or PR that has aged beyond stale and will be auto-closed.needs-triageIndicates an issue or PR lacks a `triage/foo` label and requires one.sig/appsCategorizes an issue or PR as relevant to SIG Apps.sig/nodeCategorizes an issue or PR as relevant to SIG Node.

    Type

    No type

    Projects

    Status

    Closed

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions