Skip to content
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

s3: stream a layer to s3 if possible, instead of getting it then sending it #4551

Merged
merged 1 commit into from
May 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions cache/remotecache/s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,12 @@ func (e *exporter) Config() remotecache.Config {
}
}

type nopCloserSectionReader struct {
*io.SectionReader
}

func (*nopCloserSectionReader) Close() error { return nil }

func (e *exporter) Finalize(ctx context.Context) (map[string]string, error) {
cacheConfig, descs, err := e.chains.Marshal(ctx)
if err != nil {
Expand Down Expand Up @@ -210,11 +216,15 @@ func (e *exporter) Finalize(ctx context.Context) (map[string]string, error) {
}
} else {
layerDone := progress.OneOff(ctx, fmt.Sprintf("writing layer %s", l.Blob))
dt, err := content.ReadBlob(ctx, dgstPair.Provider, dgstPair.Descriptor)
// TODO: once buildkit uses v2, start using
// https://github.com/containerd/containerd/pull/9657
// currently inline data should never happen.
ra, err := dgstPair.Provider.ReaderAt(ctx, dgstPair.Descriptor)
if err != nil {
return nil, layerDone(err)
return nil, layerDone(errors.Wrap(err, "error reading layer blob from provider"))
}
if err := e.s3Client.saveMutable(ctx, key, dt); err != nil {
defer ra.Close()
if err := e.s3Client.saveMutableAt(ctx, key, &nopCloserSectionReader{io.NewSectionReader(ra, 0, ra.Size())}); err != nil {
return nil, layerDone(errors.Wrap(err, "error writing layer blob"))
}
layerDone(nil)
Expand Down Expand Up @@ -426,6 +436,16 @@ func (s3Client *s3Client) saveMutable(ctx context.Context, key string, value []b
return err
}

func (s3Client *s3Client) saveMutableAt(ctx context.Context, key string, body io.ReadSeekCloser) error {
input := &s3.PutObjectInput{
Bucket: &s3Client.bucket,
Key: &key,
Body: body,
}
_, err := s3Client.Upload(ctx, input)
return err
}

func (s3Client *s3Client) exists(ctx context.Context, key string) (*time.Time, error) {
input := &s3.HeadObjectInput{
Bucket: &s3Client.bucket,
Expand Down
Loading