Skip to content

Commit

Permalink
[s3 cache] Fix upload of downloaded layer
Browse files Browse the repository at this point in the history
see #5584.

Seems this is a regression related to #4551, which happen when buildkit need to export a S3 layer directly from a downloaded S3 layer.

With the new wrapper, there is no exception, and buildkit download and re upload them without any issue. Checksum and size of layers are identical.

Signed-off-by: Bertrand Paquet <[email protected]>
  • Loading branch information
bpaquet committed Dec 15, 2024
1 parent f209225 commit f04b18a
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions cache/remotecache/s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,20 @@ func ResolveCacheExporterFunc() remotecache.ResolveCacheExporterFunc {
}
}

type readerFromReaderAt struct {
ra io.ReaderAt
off int64
}

func (r *readerFromReaderAt) Read(p []byte) (n int, err error) {
n, err = r.ra.ReadAt(p, r.off)
if err != nil {
return n, err
}
r.off += int64(n)
return n, nil
}

type exporter struct {
solver.CacheExporterTarget
chains *v1.CacheChains
Expand All @@ -192,12 +206,6 @@ 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 @@ -256,7 +264,7 @@ func (e *exporter) Finalize(ctx context.Context) (map[string]string, error) {
return layerDone(errors.Wrap(err, "error reading layer blob from provider"))
}
defer ra.Close()
if err := e.s3Client.saveMutableAt(groupCtx, key, &nopCloserSectionReader{io.NewSectionReader(ra, 0, ra.Size())}); err != nil {
if err := e.s3Client.saveMutableAt(groupCtx, key, &readerFromReaderAt{ra, 0}); err != nil {
return layerDone(errors.Wrap(err, "error writing layer blob"))
}
layerDone(nil)
Expand Down

0 comments on commit f04b18a

Please sign in to comment.