From d990df690cce1393e5ac1b6708a29d9bb1bb4967 Mon Sep 17 00:00:00 2001 From: Bertrand Paquet Date: Sun, 15 Dec 2024 23:44:59 +0100 Subject: [PATCH] [s3 cache] Fix upload of downloaded layer 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 --- cache/remotecache/s3/s3.go | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/cache/remotecache/s3/s3.go b/cache/remotecache/s3/s3.go index ffa2ae6c3eca..c8ba2836406a 100644 --- a/cache/remotecache/s3/s3.go +++ b/cache/remotecache/s3/s3.go @@ -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 @@ -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 { @@ -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)