Kubernetes: discover service health check ports from the image

What does this MR do?

The Kubernetes executor only waits for a service if the job sets HEALTHCHECK_TCP_PORT on it. The Docker executor waits without configuration, on the ports the image exposes. Jobs that worked on Docker start before their services are ready once they move to Kubernetes.

With FF_KUBERNETES_HEALTH_CHECK_INSPECT_IMAGES enabled, services without HEALTHCHECK_TCP_PORT are waited for on the TCP ports their image exposes, any one of which has to accept a connection.

Design

Kubernetes has no equivalent of docker inspect on a running container. The exposed ports come from the image config in the registry instead. The helper container fetches it as part of the existing health check, once the pod is running, for each service without an explicit port.

%%{init: {"themeCSS": "rect.rect { stroke: #bbb !important; stroke-dasharray: 6 4; }"}}%%
sequenceDiagram
    participant R as runner manager
    box rgb(250,250,250) job pod
    participant S as services
    participant H as helper
    end
    participant G as registry

    R->>R: create pod, credentials secret
    R->>S: start
    R->>H: start
    Note over H: job credentials mounted
    R->>H: health-check
    H->>G: image config at pulled digest
    G-->>H: exposed ports
    H->>S: dial any port, 30s
    H-->>R: ready or warning

Credentials

Job hooks run in the helper container. Anything mounted there is readable by the job. By default the helper gets only the credentials the job already has: the job token for the project registry and DOCKER_AUTH_CONFIG. The runner already writes these into the pod's credentials secret for the kubelet, together with any Docker credentials from the runner host. The secret now has a second key with the job's subset, and that key is what the helper mounts.

Operators whose private service images are pulled with image_pull_secrets or the runner host's Docker credentials can set image_pull_secrets_readable_by_jobs in [runners.kubernetes]. The helper then mounts the same secrets the pod pulls with, and the documentation for the option states that jobs can read them. Without it, such images are skipped with a warning in the job log and the job continues.

Secrets attached to the service account are not mounted in either mode, since the runner does not know their names.

Which image is inspected

The helper inspects the configured repository at the digest the kubelet reports for the running container, A tag that moved between pod creation and the health check therefore does not matter. If the registry does not know that digest, which happens when the node already had the same content from another repository, the helper falls back to the tag. The platform is the helper's own, since it runs on the same node as the services.

Failures and timeouts

An image that cannot be inspected or exposes no TCP port is skipped with a message and the job continues, which is what happens today without the flag. Inspection has a 45 second budget for all services together. An image can also expose a port the service was told not to listen on. The wait on inferred ports therefore ends after 30 seconds per service, the Docker executor's default, with a warning. Explicit HEALTHCHECK_TCP_PORT values wait until the job is cancelled, as before.

Each job does one manifest fetch per inferred service, including when the node already has the image. Docker Hub counts that as a pull, and the flag description states this.

Also in this MR

A service alias that collides with a fixed container name such as helper now results in a generated container name, with the alias still resolving as a hostname. Before, the API server rejected such a pod. The health check is executed as an argument list instead of a shell string. -p remains an alias of --port, without the extra element urfave/cli added when both forms shared a field.

Not covered

Native steps jobs. Their health check only takes explicit ports, and the job log warns when the flag is on and a service has no HEALTHCHECK_TCP_PORT.

What's the best way to test this MR?

I ran this on GKE with the flag enabled and jobs whose scripts connect to the services on the first line: redis and postgres without ports, a mix of explicit and inferred ports, a sidecar with no exposed ports, an image given by digest, a named port, an image in the project's own registry, services aliased with fixed container names, and a hook reading the mounted credentials. A second runner with image_pull_secrets for a private project and image_pull_secrets_readable_by_jobs inspected an image only that secret can pull. The first runner skipped the same image with a warning.

What are the relevant issue numbers?

Edited by Igor

Merge request reports

Loading
Loading