-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
oci exporter: support exporting directories #3162
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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 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 |
---|---|---|
|
@@ -2,6 +2,7 @@ package client | |
|
||
import ( | ||
"context" | ||
"encoding/base64" | ||
"encoding/json" | ||
"io" | ||
"os" | ||
|
@@ -14,6 +15,7 @@ import ( | |
controlapi "github.com/moby/buildkit/api/services/control" | ||
"github.com/moby/buildkit/client/llb" | ||
"github.com/moby/buildkit/client/ociindex" | ||
"github.com/moby/buildkit/exporter/containerimage/exptypes" | ||
"github.com/moby/buildkit/identity" | ||
"github.com/moby/buildkit/session" | ||
sessioncontent "github.com/moby/buildkit/session/content" | ||
|
@@ -124,6 +126,8 @@ func (c *Client) solve(ctx context.Context, def *llb.Definition, runGateway runG | |
ex = opt.Exports[0] | ||
} | ||
|
||
indicesToUpdate := []string{} | ||
|
||
if !opt.SessionPreInitialized { | ||
if len(syncedDirs) > 0 { | ||
s.Allow(filesync.NewFSSyncProvider(syncedDirs)) | ||
|
@@ -133,49 +137,64 @@ func (c *Client) solve(ctx context.Context, def *llb.Definition, runGateway runG | |
s.Allow(a) | ||
} | ||
|
||
contentStores := map[string]content.Store{} | ||
for key, store := range cacheOpt.contentStores { | ||
contentStores[key] = store | ||
} | ||
for key, store := range opt.OCIStores { | ||
key2 := "oci:" + key | ||
if _, ok := contentStores[key2]; ok { | ||
return nil, errors.Errorf("oci store key %q already exists", key) | ||
} | ||
contentStores[key2] = store | ||
} | ||
|
||
var supportFile bool | ||
var supportDir bool | ||
switch ex.Type { | ||
case ExporterLocal: | ||
if ex.Output != nil { | ||
return nil, errors.New("output file writer is not supported by local exporter") | ||
} | ||
if ex.OutputDir == "" { | ||
return nil, errors.New("output directory is required for local exporter") | ||
} | ||
s.Allow(filesync.NewFSSyncTargetDir(ex.OutputDir)) | ||
case ExporterOCI, ExporterDocker, ExporterTar: | ||
if ex.OutputDir != "" { | ||
return nil, errors.Errorf("output directory %s is not supported by %s exporter", ex.OutputDir, ex.Type) | ||
} | ||
supportDir = true | ||
case ExporterTar: | ||
supportFile = true | ||
case ExporterOCI, ExporterDocker: | ||
supportDir = ex.OutputDir != "" | ||
supportFile = ex.Output != nil | ||
} | ||
|
||
if supportFile && supportDir { | ||
return nil, errors.Errorf("both file and directory output is not support by %s exporter", ex.Type) | ||
} | ||
if !supportFile && ex.Output != nil { | ||
return nil, errors.Errorf("output file writer is not supported by %s exporter", ex.Type) | ||
} | ||
if !supportDir && ex.OutputDir != "" { | ||
return nil, errors.Errorf("output directory is not supported by %s exporter", ex.Type) | ||
} | ||
|
||
if supportFile { | ||
if ex.Output == nil { | ||
return nil, errors.Errorf("output file writer is required for %s exporter", ex.Type) | ||
} | ||
s.Allow(filesync.NewFSSyncTarget(ex.Output)) | ||
default: | ||
if ex.Output != nil { | ||
return nil, errors.Errorf("output file writer is not supported by %s exporter", ex.Type) | ||
} | ||
if ex.OutputDir != "" { | ||
return nil, errors.Errorf("output directory %s is not supported by %s exporter", ex.OutputDir, ex.Type) | ||
} | ||
} | ||
|
||
// this is a new map that contains both cacheOpt stores and OCILayout stores | ||
contentStores := make(map[string]content.Store, len(cacheOpt.contentStores)+len(opt.OCIStores)) | ||
// copy over the stores references from cacheOpt | ||
for key, store := range cacheOpt.contentStores { | ||
contentStores[key] = store | ||
} | ||
// copy over the stores references from ociLayout opts | ||
for key, store := range opt.OCIStores { | ||
// conflicts are not allowed | ||
if _, ok := contentStores[key]; ok { | ||
// we probably should check if the store is identical, but given that | ||
// https://pkg.go.dev/github.com/containerd/containerd/content#Store | ||
// is just an interface, composing 4 others, that is rather hard to do. | ||
// For a future iteration. | ||
return nil, errors.Errorf("contentStore key %s exists in both cache and OCI layouts", key) | ||
if supportDir { | ||
if ex.OutputDir == "" { | ||
return nil, errors.Errorf("output directory is required for %s exporter", ex.Type) | ||
} | ||
switch ex.Type { | ||
case ExporterOCI, ExporterDocker: | ||
if err := os.MkdirAll(ex.OutputDir, 0755); err != nil { | ||
return nil, err | ||
} | ||
cs, err := contentlocal.NewStore(ex.OutputDir) | ||
if err != nil { | ||
return nil, err | ||
} | ||
contentStores["export"] = cs | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you want to make sure this does not collide then add a random suffix and send the full contentstore id in exporter attributes. |
||
indicesToUpdate = append(indicesToUpdate, filepath.Join(ex.OutputDir, "index.json")) | ||
default: | ||
s.Allow(filesync.NewFSSyncTargetDir(ex.OutputDir)) | ||
} | ||
contentStores[key] = store | ||
} | ||
|
||
if len(contentStores) > 0 { | ||
|
@@ -352,6 +371,25 @@ func (c *Client) solve(ctx context.Context, def *llb.Definition, runGateway runG | |
} | ||
} | ||
} | ||
if manifestDescDt := res.ExporterResponse[exptypes.ExporterImageDescriptorKey]; manifestDescDt != "" { | ||
manifestDescDt, err := base64.StdEncoding.DecodeString(manifestDescDt) | ||
if err != nil { | ||
return nil, err | ||
} | ||
var manifestDesc ocispecs.Descriptor | ||
if err = json.Unmarshal([]byte(manifestDescDt), &manifestDesc); err != nil { | ||
return nil, err | ||
} | ||
for _, indexJSONPath := range indicesToUpdate { | ||
tag := "latest" | ||
if t, ok := res.ExporterResponse["image.name"]; ok { | ||
tag = t | ||
} | ||
if err = ociindex.PutDescToIndexJSONFileLocked(indexJSONPath, manifestDesc, tag); err != nil { | ||
return nil, err | ||
} | ||
} | ||
} | ||
return res, nil | ||
} | ||
|
||
|
This file contains 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 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
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.
Is this a different behavior from
local
where directory is created early even for broken builds(not a big issue).Also looks like
local
defaults to0700
? https://github.com/moby/buildkit/blob/master/session/filesync/diffcopy.go#L102There 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.
As written the
oci
unpack=true
is more similar to--export-cache type=local
than it is to--output type=local
(since it uses a content store).See https://github.com/jedevc/buildkit/blob/oci-unpack-tar/client/solve.go#L466-L472 for how we create the directory using
0755
- maybe it's worth consistentizing all of these?I don't think we can avoid not making the directory here, we'd otherwise have to wait until the first write to the content store by creating a new interface to defer the creation of the directory.