-
Notifications
You must be signed in to change notification settings - Fork 3
feat: pre-cache base images and serve via BuildKit mirror #91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3c19cb6
feat: pre-cache base images and serve via BuildKit mirror
hiroTamada 4a010a5
fix: update tests for mirror fallback behavior
hiroTamada f66696c
Merge remote-tracking branch 'origin/main' into feat/buildkit-mirror-…
hiroTamada ba5ca18
fix: strip digest before tag when extracting repo name for mirror token
hiroTamada File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| package builds | ||
|
|
||
| import ( | ||
| "archive/tar" | ||
| "compress/gzip" | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| "github.com/google/go-containerregistry/pkg/name" | ||
| ) | ||
|
|
||
| // ParseDockerfileFROMs extracts and deduplicates base image references from | ||
| // Dockerfile content. It reuses the same parsing logic as the builder agent's | ||
| // rewriteDockerfileFROMs: split lines, find FROM, skip flags/comments/scratch, | ||
| // normalize refs. Inter-stage references (FROM builder) and variable references | ||
| // (${VAR}) are skipped since they can't be resolved at parse time. | ||
| func ParseDockerfileFROMs(content string) []string { | ||
| lines := strings.Split(content, "\n") | ||
|
|
||
| // Track stage names so we can skip inter-stage FROM references | ||
| stageNames := make(map[string]bool) | ||
| seen := make(map[string]bool) | ||
| var refs []string | ||
|
|
||
| for _, line := range lines { | ||
| trimmed := strings.TrimSpace(line) | ||
|
|
||
| // Skip empty lines and comments | ||
| if trimmed == "" || strings.HasPrefix(trimmed, "#") { | ||
| continue | ||
| } | ||
|
|
||
| // Check for FROM instruction (case insensitive) | ||
| upper := strings.ToUpper(trimmed) | ||
| if !strings.HasPrefix(upper, "FROM ") { | ||
| continue | ||
| } | ||
|
|
||
| parts := strings.Fields(trimmed) | ||
| if len(parts) < 2 { | ||
| continue | ||
| } | ||
|
|
||
| // Find the image reference (skip FROM and any flags like --platform) | ||
| imageIdx := 1 | ||
| for imageIdx < len(parts) && strings.HasPrefix(parts[imageIdx], "--") { | ||
| imageIdx++ | ||
| } | ||
| if imageIdx >= len(parts) { | ||
| continue | ||
| } | ||
|
|
||
| imageRef := parts[imageIdx] | ||
|
|
||
| // Record AS alias if present | ||
| for j := imageIdx + 1; j < len(parts)-1; j++ { | ||
| if strings.EqualFold(parts[j], "AS") { | ||
| stageNames[strings.ToLower(parts[j+1])] = true | ||
| break | ||
| } | ||
| } | ||
|
|
||
| // Skip scratch | ||
| if imageRef == "scratch" { | ||
| continue | ||
| } | ||
|
|
||
| // Skip inter-stage references (e.g. FROM builder) | ||
| if stageNames[strings.ToLower(imageRef)] { | ||
| continue | ||
| } | ||
|
|
||
| // Skip variable references that can't be resolved | ||
| if strings.Contains(imageRef, "${") { | ||
| continue | ||
| } | ||
|
|
||
| // Normalize the image reference (same logic as builder agent) | ||
| normalized := normalizeImageRef(imageRef) | ||
|
|
||
| if !seen[normalized] { | ||
| seen[normalized] = true | ||
| refs = append(refs, normalized) | ||
| } | ||
| } | ||
|
|
||
| return refs | ||
| } | ||
|
|
||
| // normalizeImageRef normalizes a Docker image reference to match the local | ||
| // registry path that BuildKit mirror requests will use. Official Docker Hub | ||
| // images keep the library/ prefix (e.g. "node:20-alpine" → "library/node:20-alpine") | ||
| // because BuildKit requests them as /v2/library/node/manifests/.... | ||
| // Non-Docker Hub images keep the full registry path. | ||
| // | ||
| // This is consistent with normalizeToLocalRef in lib/images/mirror.go, which | ||
| // controls where mirrored images are pushed. | ||
| func normalizeImageRef(ref string) string { | ||
| parsed, err := name.ParseReference(ref) | ||
| if err != nil { | ||
| // Fall back to basic normalization if parsing fails | ||
| return strings.TrimPrefix(ref, "docker.io/") | ||
| } | ||
|
|
||
| // Get canonicalized repository (e.g. "index.docker.io/library/node") | ||
| repo := parsed.Context().String() | ||
|
|
||
| // Strip index.docker.io/ prefix (canonical form of docker.io) | ||
| repo = strings.TrimPrefix(repo, "index.docker.io/") | ||
| repo = strings.TrimPrefix(repo, "docker.io/") | ||
|
|
||
| // Keep library/ prefix — BuildKit mirror requests use it for official images | ||
|
|
||
| // Build the tag or digest suffix | ||
| var suffix string | ||
| if tag, ok := parsed.(name.Tag); ok { | ||
| suffix = ":" + tag.TagStr() | ||
| } else if dig, ok := parsed.(name.Digest); ok { | ||
| suffix = "@" + dig.DigestStr() | ||
| } | ||
|
|
||
| return repo + suffix | ||
| } | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // ExtractDockerfileFromTarball reads just the Dockerfile entry from a .tar.gz | ||
| // archive and returns its content as a string. It looks for entries named | ||
| // "Dockerfile" or "./Dockerfile" at the root of the archive. | ||
| func ExtractDockerfileFromTarball(tarballPath string) (string, error) { | ||
| f, err := os.Open(tarballPath) | ||
| if err != nil { | ||
| return "", fmt.Errorf("open tarball: %w", err) | ||
| } | ||
| defer f.Close() | ||
|
|
||
| gz, err := gzip.NewReader(f) | ||
| if err != nil { | ||
| return "", fmt.Errorf("create gzip reader: %w", err) | ||
| } | ||
| defer gz.Close() | ||
|
|
||
| tr := tar.NewReader(gz) | ||
| for { | ||
| hdr, err := tr.Next() | ||
| if err == io.EOF { | ||
| break | ||
| } | ||
| if err != nil { | ||
| return "", fmt.Errorf("read tar entry: %w", err) | ||
| } | ||
|
|
||
| // Match Dockerfile at root (with or without ./ prefix) | ||
| name := filepath.Clean(hdr.Name) | ||
| if name == "Dockerfile" { | ||
| data, err := io.ReadAll(tr) | ||
| if err != nil { | ||
| return "", fmt.Errorf("read Dockerfile from tarball: %w", err) | ||
| } | ||
| return string(data), nil | ||
| } | ||
| } | ||
|
|
||
| return "", fmt.Errorf("Dockerfile not found in tarball") | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Variable references without braces not skipped in parsing
Low Severity
The variable-reference check only matches
${VAR}syntax but Docker also supports$VAR(without braces) inFROMinstructions. A line likeFROM $BASE_IMAGEpasses through, gets treated as a literal image name, and produces a spurious mirror attempt and an invalid token permission entry. The check could usestrings.Contains(imageRef, "$")to catch both forms.