Skip to content

feat(oci): add pull-through cache for OCI registries - #441

Merged
worstell merged 4 commits into
mainfrom
aat/oci-strategy
Nov 9, 2026
Merged

worstell merged 4 commits into
mainfrom
aat/oci-strategy

Conversation

@alecthomas

Copy link
Copy Markdown
Collaborator

Image pulls from CI and workstations fetch the same layers from Docker Hub and other registries over and over. That is slow, and it runs into pull rate limits. This adds an oci strategy that serves the OCI Distribution API from cachew, so those pulls hit the local and tiered caches instead. It builds on the handler groundwork from #438 and the request coalescing from #440.

The changes, one commit each:

  • The oci strategy, configured with one block per registry.
    • Routes: clients reach a registry at /oci/<registry>/v2/, which suits containerd with override_path, or on a dedicated hostname at /v2/, which Docker and Podman need because they cannot use a path prefix. /v2/ is answered locally. Pushes and any other write method get a 405.
    • Paths are parsed from the end, because repository names can contain slashes. Configured rewrites, such as Docker Hub's implicit library/ namespace, are applied before the name and reference are validated.
    • Blobs are keyed on registry plus digest, verified against their digest before caching, and never revalidated. Large blob fills use ParallelFetch, with configurable chunk size and concurrency (32 MB × 4 by default). Registry redirects to a CDN are followed on the server side.
    • Digest-addressed manifests are verified and treated as immutable. Tag manifests are keyed on a normalised Accept header. After tag-ttl (default 5m) they are revalidated with a HEAD request on Docker-Content-Digest, since the cache replaces upstream ETags. Docker Hub does not count HEADs against rate limits. A stale tag is served when the upstream is unavailable or rate limiting.
    • Tag lists and referrers pass through uncached.
    • Upstream auth uses go-containerregistry's token transport with the strategy's own username/password, or anonymous access when they are unset. One transport is kept per repository scope. Client Authorization and Cookie headers are removed and never forwarded.
    • The README has an oci section with a Docker Hub example and setup notes for containerd and Docker.
  • An integration test against the reference registry (registry:3, Distribution 3.1.2) running in Docker. It pulls through cachew with go-containerregistry's client and checks every blob digest. It covers anonymous and htpasswd upstreams, tag updates, and serving from cache after the registry container is removed. It runs under the integration tag in CI and fails rather than skips when Docker is missing.

New dependencies:

  • go-containerregistry v0.22.1 provides the token exchange. It also brings in docker/cli's config code, docker-credential-helpers and logrus.
  • go-digest checks digests.
  • golang.org/x/crypto was already an indirect dependency and is now direct, because the integration test uses bcrypt to create the htpasswd file.
  • Module version selection also raised klauspost/compress, x/mod, x/sync and x/sys.

Known limitations:

  • A manifest fetched only by tag is not also cached under its digest. A later pull by digest misses the cache, and fails if the upstream is down.
  • In tags/list responses, the Link header for the next page still uses the upstream path. A path-prefixed client that follows it will go around cachew.
  • Blob keys do not include the repository. A cached blob can therefore be fetched under any repository name on the same registry. That is consistent with every client sharing the upstream credentials.

🤖 Generated with Claude Code

alecthomas and others added 2 commits November 9, 2026 16:16
Image pulls from CI and workstations repeatedly fetch the same layers
from Docker Hub and other registries, which is slow and runs into pull
rate limits. The oci strategy serves the Distribution API from cachew
so those pulls hit the local and tiered caches instead.

Blobs and digest-addressed manifests are immutable, so they are checked
against their digest before caching and never revalidated. Tag
manifests are mutable and vary on Accept, so they are keyed on a
normalised Accept header and revalidated with a HEAD request on
Docker-Content-Digest after tag-ttl. Docker Hub does not count HEADs
against rate limits. A stale tag is served when the upstream is
unavailable or rate limiting.

Clients reach a registry either at /oci/<registry>/v2/ (containerd with
override_path) or on a dedicated hostname at /v2/, because Docker and
Podman cannot use a path prefix. Upstream auth uses go-containerregistry's
token transport with the strategy's own credentials, one per repository
scope. Client credentials are stripped and never forwarded.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The unit tests use a hand-written fake registry, which only proves
cachew agrees with our own reading of the Distribution spec. This test
runs the reference registry in Docker and pulls through cachew with
go-containerregistry's client, checking every blob digest. It covers
anonymous and htpasswd upstreams, tag updates, and serving from cache
after the registry is gone.

It requires Docker and fails without it, so CI cannot pass it silently.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@alecthomas
alecthomas marked this pull request as ready for review November 9, 2026 05:29
@alecthomas
alecthomas requested a review from a team as a code owner November 9, 2026 05:29
@alecthomas
alecthomas requested review from joshfriend and removed request for a team November 9, 2026 05:29
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-09T21:05:22.593458Z 716ae88 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 653763df1d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread internal/strategy/oci.go Resolved
Comment thread internal/strategy/oci.go Resolved
passThrough now returns a failed response copy, and ServeHTTP logs it.
Remove the field comments on ociRequest.

Co-authored-by: Chain <492cad42391b4a5c84d831885f468e6f1699c0a8c39241f1c002235ece37ee69@buzz>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Elizabeth Worstell <worstell@squareup.com>

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f5f0fad1a8

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread internal/strategy/oci.go Resolved
Comment thread internal/strategy/oci.go Resolved
Co-authored-by: Chain <492cad42391b4a5c84d831885f468e6f1699c0a8c39241f1c002235ece37ee69@buzz>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Elizabeth Worstell <worstell@squareup.com>
@worstell
worstell enabled auto-merge (squash) November 9, 2026 21:03

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 716ae885b1

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread internal/strategy/oci.go
CacheKey(o.blobKey).
Transform(o.transform).
Verify(o.verifier).
ParallelFetch(int64(config.BlobChunkSizeMB)<<20, int(config.BlobConcurrency))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Handle zero-length blobs before parallel range fetching

When a valid OCI blob has size zero, enabling ParallelFetch makes the cold miss request Range: bytes=0-...; a compliant registry answers that unsatisfiable range with 416 and Content-Range: bytes */0. The handler only invokes the parallel reader for a 206 response, so it forwards the 416 instead of returning and caching the valid empty blob, making images or artifacts containing zero-length descriptors impossible to pull through this strategy.

Useful? React with 👍 / 👎.

@worstell
worstell merged commit 7366f6e into main Nov 9, 2026
7 checks passed
@worstell
worstell deleted the aat/oci-strategy branch November 9, 2026 21:05
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.

2 participants