score-spec / spec

The Score Specification provides a developer-centric and platform-agnostic Workload specification to improve developer productivity and experience. It eliminates configuration inconsistencies between environments.
https://score.dev/
Apache License 2.0
7.81k stars 2.2k forks source link

[FEATURE] Ordered Container Start in Score #104

Open chrishumanitec opened 2 months ago

chrishumanitec commented 2 months ago

Detailed description

Developers should be able to express rules that can be used to require some containers to be started and run before others within a single workload.

Context

Many workloads require some form of setup before the main container can run. For example, schema migration needs to be completed, static site data needs to be generated or shared state should be initiated. This set up can involve containers running to completion before another container starts, being ready/healthy before another container starts, or just have started before another container starts. This allows for patterns such as “Sidecars” or “Init Containers” to be implemented.

Many container orchestration platforms provide for ordered container start (Init Containers in Kubernetes, depends_on with service_started, service_healthy, or service_completed_successfully in Docker Compose, run.googleapis.com/container-dependencies in Google Cloud Run.) All of them allow for specifying the start order with various levels of support for conditions on when the next container should start.

Possible implementation

Proposal

Add a new before property to the container object. It would have the following schema:

description: Defines before which other containers this container should be started
title: Before
type: object
properties:
  containers:
    description: List of containers that should start after this container.
    type: array
    items:
      description: A container ID in this workload
      type: string
        minLength: 2
        maxLength: 63
        pattern: "^[a-z0-9][a-z0-9-]{0,61}[a-z0-9]$"
  ready:
    description: The status of the container before the next container are started.
    title: Ready
    type: string
    enum:
    - started
    - healthy
    - complete

Why not use after like compose depends_on ?

This is not a dependency relationship - more a preparatory relationship

Startup order of containers is not strictly speaking a dependency graph like in compose. In the usecase of init containers, the main container is not even run at the same time as the init container. Instead, some kind of state is managed before the main container runs.

In a well factored system, the main container should not have to worry about what init containers were run - rather just that the state is correct. Only the init containers themselves “know” that they need to leave the system is a certain state.

Default state is unaffected

By specifying using before rather than after, the default behavior in score is unaffected. Adding an init container only affects the init container and not the main container.

Example Score Files

Init containers

Score

containers:
  init-one:
    ...
    before:
      containers:
      - init-two
  init-two:
    ...
    before:
      containers:
      - main
  main:
    ...

Kubernetes Pod

apiVersion: core/v1
kind: Pod
metadata:
  name: test
spec:
  containers:
  - name: main
    ...
  initContainers:
  - name: init-one
    ...
  - name: init-two
    ...

Sidecars

Score

containers:
  init:
    ...
    before:
      ready: complete # implicit default
      containers:
      - main
  sidecar:
    ...
    before:
      containers:
      - main
      ready: started
  main:
    ....

Kubernetes Pod (prior to 1.29)

apiVersion: core/v1
kind: Pod
metadata:
  name: test
spec:
  containers:
  - name: main
    ...
  - name: sidecar
    ...
  initContainers:
  - name: init
    ...

Kubernetes Pod (after 1.29)

apiVersion: core/v1
kind: Pod
metadata:
  name: test
spec:
  containers:
  - name: main
    ...
  initContainers:
  - name: init
    ...
  - name: sidecar
    restartPolicy: Always
    ...

Additional information

No response

astromechza commented 2 months ago

Edge cases to consider and mitigate

These should be caught when running score-xyz generate.