Skip to content

perf(k8s): put the dependencies in their own image layers #4550

Closed

Description

@dan-petty

Problem or Motivation

Dockerfile:16-17 copies src/ before uv sync (Dockerfile:20), and the Service stage copies the whole venv in one layer (Dockerfile:154). So each release changes about 0.5 GB of layers, and each node pulls them again. About 15 releases a day.

Measured on 2026-11-11: 5 of 7 runner Jobs pulled the image for 51 to 56 s. The first uv child of an implement Job takes 30 to 101 s with an empty uv cache. The median from runner Job create to the first gateway call is 204 s. More short pods come with the move of the roadmap jobs, so the pull time grows in importance.

Proposed Solution & Architectural Design

Target Subsystem

Kubernetes & Minikube Automation (container image: Dockerfile).

Existing Tool Considered

uv's documented intermediate-layer pattern and --find-links. A pre-pull DaemonSet treats the symptom (about 50 GB a day from the registry for 2 images, 3 nodes and 15 releases); it comes only if a pull stays over 10 s after this item. Cost: about 40,000 tokens and 2 hours to build, about 2,000 tokens a month to keep, about 5,000 tokens to fix.

Acceptance Criteria

  • Two image builds with a change in src/ only differ in layers of under 50 MB together (crane manifest layer sizes, or the build output).
  • The image runs devops --version and the test suite as before; the entry points still work.
  • A live runner Job on a node that holds the previous release starts its container in 10 s or less.
  • Measure after 7 days: runner Job create to container start, median, 10 s or less; first uv child of an implement Job under 30 s.

Dependencies

None.

Source: #4517

Proposed design

Problem statement

Dockerfile:16-17 copies pyproject.toml, uv.lock and src/ before uv sync at Dockerfile:20, and the service stage copies the whole virtual environment in one layer (Dockerfile:154). Each release therefore changes about 0.5 GB of layers, and each node pulls them again at about 15 releases a day. The first uv child of an implement Job also starts with an empty uv cache, because the Job's cache is on its home volume (Dockerfile:100). This change puts the dependencies and the project in separate layers, and keeps the locked wheels in the runner image, so a source-only release changes only a small project layer and the first uv child reads local files.

Acceptance criteria

Requirement: Builder dependency layer

The builder stage SHALL install the locked runtime dependencies in a layer before it copies src/, and SHALL install the project in a later layer.

Scenario: Builder instruction order

  • WHEN a test reads the builder stage of Dockerfile
  • THEN uv sync --locked --no-dev --no-install-project precedes COPY src/, and uv sync --locked --no-dev --no-editable follows it.

Requirement: Service dependency layer

The service stage SHALL copy the dependency-only virtual environment from the deps stage in one layer.

Scenario: Service dependency source

  • WHEN a test reads the service stage of Dockerfile
  • THEN the COPY of /app/.venv names --from=deps, and no COPY of the whole /app/.venv names --from=builder.

Requirement: Service project layer

The service stage SHALL copy the project's package, its distribution metadata and its console script from the builder stage in layers after the dependency layer.

Scenario: Service project files

  • WHEN a test reads the service stage of Dockerfile
  • THEN a COPY from builder puts devops_cli* under /app/.venv/lib/python3.14/site-packages and devops under /app/.venv/bin, after the /app/.venv copy from deps.

Requirement: Runner project layer

The runner stage SHALL install the dev dependencies with --no-install-project in one layer and SHALL install the project with --no-editable in a later layer after COPY src/.

Scenario: Runner instruction order

  • WHEN a test reads the runner stage of Dockerfile
  • THEN the uv sync that holds --no-install-project precedes COPY src/, and the uv sync --locked --no-editable follows it.

Requirement: Runner wheel directory

The runner stage SHALL keep the wheels of the locked runtime and dev dependencies in one directory and SHALL set UV_FIND_LINKS to that directory.

Scenario: Local wheel lookup

  • WHEN a test reads the runner stage of Dockerfile
  • THEN the stage writes the wheels to a directory, and an ENV instruction sets UV_FIND_LINKS to it.

Requirement: Image checks

The change SHALL keep the image smoke tests, the tool check and the Service test suite in the image jobs of ci.yml, with the 3000000000-byte bound of the runner image.

Scenario: Image job steps

  • WHEN a test reads .github/workflows/ci.yml
  • THEN the service-image, service-image-test and runner-image jobs still run devops --version, devops_cli.roadmap.runner_tools and the test suite, and the runner size check still fails above 3000000000 bytes.

Existing Tool Considered

No alternatives check has run. The open, maintained tool is uv's documented Docker integration: intermediate layers separate the dependency install from the project install, and UV_FIND_LINKS gives uv a local wheel directory. It lacks a pre-pull image cache. A pre-pull DaemonSet is out of this item and comes only if a live pull stays over 10 s; its written cost is about 40,000 tokens and 2 hours to build, about 2,000 tokens a month to keep, and about 5,000 tokens to fix. The wheel directory needs uv export plus pip's pip download, because pip is in the builder base image and the runner deletes it (Dockerfile:84-85).

Key questions

  • Which Docker layer pattern should the builder stage use?: Use uv's documented intermediate-layer pattern (guide fetched 2026-11-11): copy pyproject.toml and uv.lock, run uv sync --locked --no-dev --no-install-project, copy src/, then run uv sync --locked --no-dev --no-editable. (best practice) [sources: https://docs.astral.sh/uv/guides/integration/docker/#intermediate-layers]
    • Chosen: uv intermediate layers. The documented pattern keeps the dependency layer stable when only src/ changes.
    • Rejected: One uv sync after COPY src/. It invalidates the dependency layer on every source change, which is the current Dockerfile:17-20.
    • Rejected: Pre-pull DaemonSet. It treats the symptom and waits on a live pull over 10 s.
  • How should the service and runner stages split dependency and project layers?: Add a deps stage that copies pyproject.toml and uv.lock and runs uv sync --locked --no-dev --no-install-project; the builder stage builds on it and installs the project with uv sync --locked --no-dev --no-editable. The service stage copies /app/.venv from deps, then copies devops_cli* (package and dist-info) and bin/devops from builder; the project stays under /app/.venv because the Service test asserts that devops_cli.__file__ starts with /app/.venv/ (ci.yml:653) and the Service image holds no uv (ci.yml:516). The runner stage builds on builder (Dockerfile:65) and has no venv COPY: it installs the dev dependencies with uv sync --locked --no-install-project, then copies src/ and runs uv sync --locked --no-editable in a later layer, so a source change does not change the dev-dependency layer. The tiktoken cache stays with the dependency layer. (best practice) [sources: .github/workflows/ci.yml:516, .github/workflows/ci.yml:653, src/devops_cli/config/metadata.py:89, https://docs.astral.sh/uv/guides/integration/docker/#intermediate-layers]
    • Chosen: Deps stage and project copies. The dependency layer holds no project file, so a source change changes only the small project layer; .devcontainer/Dockerfile:306-324 already uses the same split for the devcontainer image.
    • Rejected: Whole virtual environment in one layer. It is the current layer of Dockerfile:154, and it changes about 0.5 GB on each source change.
    • Rejected: Install the project in each stage. The Service image must hold no uv (ci.yml:516), so the service cannot install the project itself.
    • Rejected: Put the source tree on PYTHONPATH. The Service test requires the package under /app/.venv/ (ci.yml:653), and --version reads the dist-info (src/devops_cli/config/metadata.py:89).
  • Should the runner image keep wheels and set UV_FIND_LINKS?: Yes. Keep the wheels of both dependency groups in one directory and set UV_FIND_LINKS to it, so the first uv child of an implement Job reads local files. The Job's uv cache is on its home volume (Dockerfile:100), so it starts empty. Build the directory with uv export --locked --no-emit-project --format requirements-txt and pip download --require-hashes -d <dir> before the stage deletes pip (Dockerfile:84). The image growth must stay under the 3000000000-byte bound: the measured runner image was 2.51 GB (k8s/README.md:716), and the pull request states the growth. (best practice) [sources: k8s/README.md:716, .github/workflows/ci.yml:738]
    • Chosen: Keep wheels and set UV_FIND_LINKS. It removes a network fetch from the first uv child, and the directory comes from the same lock that the image installs.
    • Rejected: Fetch from the network on first use. It keeps the 30 to 101 s cold-cache delay that the issue measures.
    • Rejected: Pre-pull DaemonSet. It is a later remedy only if a live pull stays over 10 s.
  • How should this issue coordinate with build(docker): build the runner image FROM devtools with no tool install #4361?: Do not wait. Implement on the current Dockerfile and rebase on build(docker): build the runner image FROM devtools with no tool install #4361 if it merges first. The body records no dependency, and the parent issue lists this item as start-now. build(docker): build the runner image FROM devtools with no tool install #4361 removes the two devops install-tools calls (Dockerfile:52, Dockerfile:76) that need the project in the builder before the tool steps; after it, the project install can move after the tool steps in each stage. (best practice) [sources: openspec/changes/archive/2026-11-10-4359-the-image-holds-the-locked-tools/tasks.md:19]
  • How should the changed layer sizes be measured?: Build the image twice, from main and with a source-only change, and sum the sizes of the layers whose IDs differ in docker history --no-trunc --format '{{.ID}} {{.Size}}'. Docker and Buildx are already in the image jobs (ci.yml:484-486). crane is not pinned in the repository: the archived perf(k8s): use tag_digest HEAD for image pins #2610 change rejected a crane pin, and the project's registry client reads only a tag digest (src/devops_cli/docker/registry.py:5). (best practice) [sources: .github/workflows/ci.yml:484, openspec/changes/archive/2026-11-06-2610-devops-k8s-image-pins-resolves-with/design.md:5, src/devops_cli/docker/registry.py:5]
    • Chosen: docker history on two local builds. Docker and Buildx are in the image jobs, and the differing layer IDs are the layers that a node pulls again.
    • Rejected: crane manifest. crane is not in tools.lock, and the project rejected a new pin for it in perf(k8s): use tag_digest HEAD for image pins #2610.
    • Rejected: docker buildx imagetools inspect. It reads a pushed manifest, and the pull-request build pushes nothing (ci.yml:495).

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

  1. added
    source/agent
    on Nov 11, 2026
  2. added
    type/infra
    scope/k8s
    status/new
    value/high
    effort/medium
    on Nov 11, 2026
  3. dan-petty commented on Nov 11, 2026

    @dan-petty
    OwnerAuthor

    Intake placed this item to the backlog: intake puts every new item in the backlog. A person plans the milestones.

    • Type: type/infra. The perf prefix of the title gives the type.
    • Scope: scope/k8s. The (k8s) scope of the title gives the scope.
    • Priority: P2-Medium. Fifteen releases a day each pull 0.5 GB of changed layers, but the system still functions today.
    • Value: High. Every CI run and every node pull benefits, and the impact grows as more runner pods are added.
    • Effort: Medium. Reordering COPY and uv sync steps plus a separate site-packages layer is a contained Dockerfile change with a coordination dependency on #4361.
    • Related: feat(k8s): the implement job runs as a cluster Job on a runner image with uv, the dev dependencies and the pinned tools, submitted by the Service #2667, which asks for a different change. Both modify the Dockerfile runner stage, but #2667 adds dev dependencies and pinned tools while the candidate reorders COPY and uv sync steps for layer caching.
  4. added
    priority/p2-medium
    on Nov 11, 2026
  5. added
    status/ready
    and removed
    status/new
    on Nov 11, 2026
  6. devops-cli-service commented on Nov 11, 2026

    @devops-cli-service
    Contributor

    Status set to Ready at 1db29f7 (see proposed design in issue body).

    Moved to new items: #4616.

  7. dan-petty commented on Nov 11, 2026

    @dan-petty
    OwnerAuthor

    Scope from the CI audit (2026-11-11)

    Another workflow builds this issue now. This comment only adds the facts that the audit measured on the CI side.

    • The builder stage copies src/ before uv sync (Dockerfile:16-20), so each src/ change installs the dependencies again.
    • Each release changes pyproject.toml and uv.lock (4-9 releases per day), so the dependency layer also builds again after each release.
    • The uv Docker guide gives the order: uv sync --locked --no-install-project, then COPY src/, then uv sync --locked. The OpenSpec install stays after COPY src/, because it runs the installed devops.
    • perf(ci): stop the cache export of pull request image builds #4646 stops the cache export of PR image builds and keeps one scoped cache on main. After it, count the CACHED layers for one day.
    • Added acceptance: between two releases, a push that changes only src/ shows the dependency layer as CACHED.
  8. dan-petty commented on Nov 11, 2026

    @dan-petty
    OwnerAuthor

    Scope from the iteration lane plan (2026-11-11)

    This issue stays a child of #4517. It is also child 8 of #4667, which gives its order in the iteration lane. The plan adds one criterion.

    • The cluster gate (feat(k8s): add the CI gate WorkflowTemplate #4691) needs a fresh venv of the change with no PyPI download. The runner root is read-only, and the image venv holds the released devops-cli (the uv sync --locked --no-editable step of Dockerfile).
    • Put the wheels of uv.lock in the image (UV_FIND_LINKS), so that uv sync --locked --offline builds the venv of a checkout.

    Acceptance added: in the runner image, uv sync --locked --offline of a main checkout into an empty venv ends in at most 60 s with no network.

  9. added
    owner/agent
    on Nov 11, 2026
  10. dan-petty commented on Nov 11, 2026

    @dan-petty
    OwnerAuthor

    Scope check of the wheel directory (2026-11-11)

    The pull request of this item puts the dependencies in their own image layers. It does not add the wheel directory with UV_FIND_LINKS, because that part cannot work as written. Measured with uv 0.13.6 in python:3.14-slim-trixie, with RUN --network=none:

    • UV_FIND_LINKS=<wheel dir> uv sync --locked --offline stops: "Network connectivity is disabled, but the requested data wasn't found in the cache: https://files.pythonhosted.org/...". uv installs a locked package from the URL in uv.lock and does not search the find-links for it. A uv run in a worktree runs the same sync, so the first uv child of a Job would not read the directory either.
    • --no-index --find-links <wheel dir> resolves again for every platform and fails on pywin32.
    • A read-only uv cache in the image fails: "Could not acquire lock ... Read-only file system (os error 30)".
    • A uv cache in the image that the pod copies into its writable cache works: copy 9 s, then uv sync --locked --offline 2 s, with no network. The cache is 852 MB.

    The two criteria for the offline venv (the first uv child under 30 s, and uv sync --locked --offline of a checkout in at most 60 s) move to #4814 with this evidence. This item keeps the layer criteria.

  11. added
    status/in-progress
    and removed
    status/ready
    on Nov 12, 2026
  12. removed
    status/in-progress
    owner/agent
    on Nov 12, 2026
  13. devops-cli-service commented on Nov 12, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-12 05:27 UTC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    effort/mediumpriority/p2-mediumscope/k8ssource/agenttype/infravalue/high

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions