Skip to content

projects: Cache pip downloads across Python function builds - #611

Merged
haarchri merged 3 commits into
crossplane:mainfrom
ytsarev:python-pip-cache
Nov 25, 2026
Merged

haarchri merged 3 commits into
crossplane:mainfrom
ytsarev:python-pip-cache

Conversation

@ytsarev

@ytsarev ytsarev commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Description of your changes

Python function builds start with an empty pip cache, so every build downloads the function's dependency wheels from PyPI again, once per arch. #588 took the toolchain install out of the build with a prebuilt build image. This does the same for dependency downloads.

This PR mounts a named volume, crossplane-python-pip-cache, at pip's cache dir in the build container, so downloads are reused across builds, functions, and projects.

It also adds a comment explaining why StartWithVolumeMount uses NoCopy, plus a test for it. With this change, concurrent function builds share one fresh volume, and the build image ships 36MB of pip cache at that same path. Without NoCopy, every container would try to seed the new volume from the image at once and fail with mkdir ...: file exists.

Timings for two generated Python functions × amd64/arm64, with images already pulled (arm64 Mac, Docker 29.5.2):

crossplane project build main this PR
warm 18.1s / 18.9s / 19.3s 10.4s / 10.3s / 10.5s
first build, empty volume n/a 14.4s

Every layer and config blob in the built package is identical across main, a cold-cache build, and a warm-cache build. TestPythonBuildPipCache covers this: it builds a function with a PyPI dependency twice at once on an empty volume, then once with the cache warm, and requires identical images.

I have:

  • Read and followed Crossplane's contribution process.
  • Read and followed Crossplane's AI policy and confirmed a human stands behind this PR.
  • Run ./nix.sh flake check to ensure this PR is ready for review.
  • Added or updated unit tests.
  • Linked a PR or a docs tracking issue to document this change.
  • Added backport release-x.y labels to auto-backport this PR.

Every Python function build starts with an empty pip cache, so it downloads
the function's dependency wheels from PyPI again, for each architecture, on
every build. Mount a shared named volume at pip's cache directory so those
downloads are reused across builds, functions, and projects.

The cache only saves downloads. pip still revalidates index pages on every
resolve, so the built venv doesn't depend on whether the cache was warm.

Signed-off-by: Yury Tsarev <yury@upbound.io>
…tent

StartWithVolumeMount mounts with NoCopy so that containers mounting the same
new volume at once don't all seed it from the image, which fails with
"mkdir ...: file exists". Concurrent Python function builds now do exactly
that with the shared pip cache, and the Python build image ships a pip cache
at the same path. Explain why NoCopy is set, and add a test so it isn't
dropped.

Signed-off-by: Yury Tsarev <yury@upbound.io>
@ytsarev
ytsarev requested review from a team, jcogilvie and tampakrap as code owners November 25, 2026 09:36
@ytsarev
ytsarev requested review from adamwg and a balanced review from Copilot and removed request for a team November 25, 2026 09:36

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The tests do not verify pip cache reuse with real dependencies, cold-versus-warm cache output comparisons, or concurrent builds sharing an initially empty cache volume.

1 open issue
What changed in this PR

Adds persistent pip download caching to Python function builds, complementing the prebuilt Python build image and benefiting both CLI users and embedded callers.

Changes:

  • Mounts crossplane-python-pip-cache at pip’s cache directory to reuse downloads across builds, functions, and projects.
  • Documents the existing NoCopy behavior and adds a regression test for the volume mount configuration.
File Description
internal/​project/​functions/​python.go Defines and mounts the shared pip cache volume.
internal/​docker/​docker.go Explains why existing NoCopy behavior prevents concurrent volume-seeding races.
internal/​docker/​docker_test.go Tests named-volume configuration, including NoCopy.

Review effort: Balanced

Comment thread internal/project/functions/python.go Resolved
Build a function that depends on a package from PyPI twice at once on an
empty pip cache volume, then once more with the cache warm, and require all
three images to be identical. In between, check that pip actually wrote to
the volume, so the last build really is a warm one.

Give pythonBuilder a pipCacheVolume field so tests run against a private,
initially empty volume instead of the user's cache.

Signed-off-by: Yury Tsarev <yury@upbound.io>
Copilot AI balanced review requested due to automatic review settings November 25, 2026 09:46

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Project-controlled Hatch build hooks can rewrite the shared dependency cache, allowing a malicious build to poison dependency data consumed by unrelated projects on the same Docker daemon.

1 open issue
1 resolved since last review

Review effort: Balanced

),
docker.StartWithCommand([]string{"sh", "-c", pythonBuildScript}),
docker.StartWithWorkingDirectory("/" + filepath.ToSlash(c.FunctionPath)),
docker.StartWithVolumeMount(b.pipCacheVolume, pythonPipCacheDir),

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point that the build container can write to the cache, and you're right that revalidation isn't an integrity boundary: on a 304, pip reuses the cached body. I'll fix that wording in the description.

I'd still keep one shared cache. It's the same trust model as pip's per-user cache on a host, which any pip install running a build backend can write to, and as BuildKit cache mounts, which builds on a builder share by default. Building a project already means running its code, and the build container is there to keep the build environment consistent, not to isolate projects from each other. On ephemeral CI runners every job gets a fresh daemon, so nothing carries over between repos.

If maintainers would rather scope it per project, I'm happy to key the volume on the project's directory in a follow-up.

CC @haarchri

@codecov

codecov Bot commented Nov 25, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 72.42%. Comparing base (d800af4) to head (e36e887).

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@haarchri
haarchri merged commit df6937b into crossplane:main Nov 25, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants