@@ -150,21 +150,21 @@ func ResolveCacheExporterFunc() remotecache.ResolveCacheExporterFunc {
150150 return nil , err
151151 }
152152
153- mc , err := newMinioClient (config )
153+ minioClient , err := newMinioClient (config )
154154 if err != nil {
155155 return nil , err
156156 }
157157
158- cc := v1 .NewCacheChains ()
159- return & exporter {CacheExporterTarget : cc , chains : cc , mc : mc , config : config }, nil
158+ cacheChains := v1 .NewCacheChains ()
159+ return & exporter {CacheExporterTarget : cacheChains , chains : cacheChains , minioClient : minioClient , config : config }, nil
160160 }
161161}
162162
163163type exporter struct {
164164 solver.CacheExporterTarget
165- chains * v1.CacheChains
166- mc * minioClient
167- config Config
165+ chains * v1.CacheChains
166+ minioClient * minioClient
167+ config Config
168168}
169169
170170func (* exporter ) Name () string { return "exporting cache to S3" }
@@ -178,12 +178,12 @@ type nopCloserSectionReader struct{ *io.SectionReader }
178178func (* nopCloserSectionReader ) Close () error { return nil }
179179
180180func (e * exporter ) Finalize (ctx context.Context ) (map [string ]string , error ) {
181- cacheConfig , descs , err := e .chains .Marshal (ctx )
181+ cacheConfig , descriptors , err := e .chains .Marshal (ctx )
182182 if err != nil {
183183 return nil , err
184184 }
185185
186- eg , groupCtx := errgroup .WithContext (ctx )
186+ errorGroup , groupContext := errgroup .WithContext (ctx )
187187 tasks := make (chan int , e .config .UploadParallelism )
188188
189189 go func () {
@@ -194,80 +194,80 @@ func (e *exporter) Finalize(ctx context.Context) (map[string]string, error) {
194194 }()
195195
196196 for workerIndex := 0 ; workerIndex < e .config .UploadParallelism ; workerIndex ++ {
197- eg .Go (func () error {
197+ errorGroup .Go (func () error {
198198 for index := range tasks {
199199 blob := cacheConfig .Layers [index ].Blob
200- dpp , ok := descs [blob ]
200+ descriptorProviderPair , ok := descriptors [blob ]
201201 if ! ok {
202202 return errors .Errorf ("missing blob %s" , blob )
203203 }
204- if dpp .Descriptor .Annotations == nil {
204+ if descriptorProviderPair .Descriptor .Annotations == nil {
205205 return errors .Errorf ("invalid descriptor without annotations" )
206206 }
207- v , ok := dpp .Descriptor .Annotations [labels .LabelUncompressed ]
207+ uncompressedAnnotation , ok := descriptorProviderPair .Descriptor .Annotations [labels .LabelUncompressed ]
208208 if ! ok {
209209 return errors .Errorf ("invalid descriptor without uncompressed annotation" )
210210 }
211- diffID , err := digest .Parse (v )
211+ diffID , err := digest .Parse (uncompressedAnnotation )
212212 if err != nil {
213213 return errors .Wrapf (err , "failed to parse uncompressed annotation" )
214214 }
215215
216- key := e .mc .blobKey (dpp .Descriptor .Digest )
217- lastMod , err := e .mc .exists (groupCtx , key )
216+ key := e .minioClient .blobKey (descriptorProviderPair .Descriptor .Digest )
217+ lastMod , err := e .minioClient .exists (groupContext , key )
218218 if err != nil {
219219 return errors .Wrapf (err , "failed to check file presence in cache" )
220220 }
221221 if lastMod != nil {
222222 if time .Since (* lastMod ) > e .config .TouchRefresh {
223- if err := e .mc .touch (groupCtx , key ); err != nil {
223+ if err := e .minioClient .touch (groupContext , key ); err != nil {
224224 return errors .Wrapf (err , "failed to touch file" )
225225 }
226226 }
227227 } else {
228- layerDone := progress .OneOff (groupCtx , fmt .Sprintf ("writing layer %s" , blob ))
229- ra , err := dpp .Provider .ReaderAt (groupCtx , dpp .Descriptor )
228+ layerDone := progress .OneOff (groupContext , fmt .Sprintf ("writing layer %s" , blob ))
229+ readerAt , err := descriptorProviderPair .Provider .ReaderAt (groupContext , descriptorProviderPair .Descriptor )
230230 if err != nil {
231231 return layerDone (errors .Wrap (err , "error reading layer blob from provider" ))
232232 }
233- defer ra .Close ()
233+ defer readerAt .Close ()
234234
235- section := & nopCloserSectionReader {io .NewSectionReader (ra , 0 , ra .Size ())}
236- if err := e .mc .saveMutableAt (groupCtx , key , section , ra .Size ()); err != nil {
235+ section := & nopCloserSectionReader {io .NewSectionReader (readerAt , 0 , readerAt .Size ())}
236+ if err := e .minioClient .saveMutableAt (groupContext , key , section , readerAt .Size ()); err != nil {
237237 return layerDone (errors .Wrap (err , "error writing layer blob" ))
238238 }
239239 layerDone (nil )
240240 }
241241
242- la := & v1.LayerAnnotations {
242+ layerAnnotations := & v1.LayerAnnotations {
243243 DiffID : diffID ,
244- Size : dpp .Descriptor .Size ,
245- MediaType : dpp .Descriptor .MediaType ,
244+ Size : descriptorProviderPair .Descriptor .Size ,
245+ MediaType : descriptorProviderPair .Descriptor .MediaType ,
246246 }
247- if v , ok := dpp .Descriptor .Annotations ["buildkit/createdat" ]; ok {
248- var t time.Time
249- if err := (& t ).UnmarshalText ([]byte (v )); err != nil {
247+ if createdAt , ok := descriptorProviderPair .Descriptor .Annotations ["buildkit/createdat" ]; ok {
248+ var createdAtTime time.Time
249+ if err := (& createdAtTime ).UnmarshalText ([]byte (createdAt )); err != nil {
250250 return err
251251 }
252- la .CreatedAt = t .UTC ()
252+ layerAnnotations .CreatedAt = createdAtTime .UTC ()
253253 }
254- cacheConfig .Layers [index ].Annotations = la
254+ cacheConfig .Layers [index ].Annotations = layerAnnotations
255255 }
256256 return nil
257257 })
258258 }
259259
260- if err := eg .Wait (); err != nil {
260+ if err := errorGroup .Wait (); err != nil {
261261 return nil , err
262262 }
263263
264- dt , err := json .Marshal (cacheConfig )
264+ manifestData , err := json .Marshal (cacheConfig )
265265 if err != nil {
266266 return nil , err
267267 }
268268
269269 for _ , name := range e .config .Names {
270- if err := e .mc .saveMutableAt (ctx , e .mc .manifestKey (name ), bytes .NewReader (dt ), int64 (len (dt ))); err != nil {
270+ if err := e .minioClient .saveMutableAt (ctx , e .minioClient .manifestKey (name ), bytes .NewReader (manifestData ), int64 (len (manifestData ))); err != nil {
271271 return nil , errors .Wrapf (err , "error writing manifest: %s" , name )
272272 }
273273 }
@@ -280,48 +280,48 @@ func ResolveCacheImporterFunc() remotecache.ResolveCacheImporterFunc {
280280 if err != nil {
281281 return nil , ocispecs.Descriptor {}, err
282282 }
283- mc , err := newMinioClient (config )
283+ minioClient , err := newMinioClient (config )
284284 if err != nil {
285285 return nil , ocispecs.Descriptor {}, err
286286 }
287- return & importer {mc : mc , config : config }, ocispecs.Descriptor {}, nil
287+ return & importer {minioClient : minioClient , config : config }, ocispecs.Descriptor {}, nil
288288 }
289289}
290290
291291type importer struct {
292- mc * minioClient
293- config Config
292+ minioClient * minioClient
293+ config Config
294294}
295295
296- func (i * importer ) makeDescriptorProviderPair (l v1.CacheLayer ) (* v1.DescriptorProviderPair , error ) {
297- if l .Annotations == nil {
296+ func (i * importer ) makeDescriptorProviderPair (layer v1.CacheLayer ) (* v1.DescriptorProviderPair , error ) {
297+ if layer .Annotations == nil {
298298 return nil , errors .Errorf ("cache layer with missing annotations" )
299299 }
300- if l .Annotations .DiffID == "" {
300+ if layer .Annotations .DiffID == "" {
301301 return nil , errors .Errorf ("cache layer with missing diffid" )
302302 }
303- annotations := map [string ]string {labels .LabelUncompressed : l .Annotations .DiffID .String ()}
304- if ! l .Annotations .CreatedAt .IsZero () {
305- if txt , err := l .Annotations .CreatedAt .MarshalText (); err == nil {
306- annotations ["buildkit/createdat" ] = string (txt )
303+ annotations := map [string ]string {labels .LabelUncompressed : layer .Annotations .DiffID .String ()}
304+ if ! layer .Annotations .CreatedAt .IsZero () {
305+ if createdAtText , err := layer .Annotations .CreatedAt .MarshalText (); err == nil {
306+ annotations ["buildkit/createdat" ] = string (createdAtText )
307307 } else {
308308 return nil , err
309309 }
310310 }
311311 return & v1.DescriptorProviderPair {
312- Provider : i .mc ,
312+ Provider : i .minioClient ,
313313 Descriptor : ocispecs.Descriptor {
314- MediaType : l .Annotations .MediaType ,
315- Digest : l .Blob ,
316- Size : l .Annotations .Size ,
314+ MediaType : layer .Annotations .MediaType ,
315+ Digest : layer .Blob ,
316+ Size : layer .Annotations .Size ,
317317 Annotations : annotations ,
318318 },
319319 }, nil
320320}
321321
322322func (i * importer ) load (ctx context.Context ) (* v1.CacheChains , error ) {
323- var cfg v1.CacheConfig
324- found , err := i .mc .getManifest (ctx , i .mc .manifestKey (i .config .Names [0 ]), & cfg )
323+ var config v1.CacheConfig
324+ found , err := i .minioClient .getManifest (ctx , i .minioClient .manifestKey (i .config .Names [0 ]), & config )
325325 if err != nil {
326326 return nil , err
327327 }
@@ -330,26 +330,26 @@ func (i *importer) load(ctx context.Context) (*v1.CacheChains, error) {
330330 }
331331
332332 allLayers := v1.DescriptorProvider {}
333- for _ , l := range cfg .Layers {
334- dpp , err := i .makeDescriptorProviderPair (l )
333+ for _ , layer := range config .Layers {
334+ descriptorProviderPair , err := i .makeDescriptorProviderPair (layer )
335335 if err != nil {
336336 return nil , err
337337 }
338- allLayers [l .Blob ] = * dpp
338+ allLayers [layer .Blob ] = * descriptorProviderPair
339339 }
340- cc := v1 .NewCacheChains ()
341- if err := v1 .ParseConfig (cfg , allLayers , cc ); err != nil {
340+ cacheChains := v1 .NewCacheChains ()
341+ if err := v1 .ParseConfig (config , allLayers , cacheChains ); err != nil {
342342 return nil , err
343343 }
344- return cc , nil
344+ return cacheChains , nil
345345}
346346
347347func (i * importer ) Resolve (ctx context.Context , _ ocispecs.Descriptor , id string , w worker.Worker ) (solver.CacheManager , error ) {
348- cc , err := i .load (ctx )
348+ cacheChains , err := i .load (ctx )
349349 if err != nil {
350350 return nil , err
351351 }
352- keysStorage , resultStorage , err := v1 .NewCacheKeyStorage (cc , w )
352+ keysStorage , resultStorage , err := v1 .NewCacheKeyStorage (cacheChains , w )
353353 if err != nil {
354354 return nil , err
355355 }
@@ -361,6 +361,16 @@ type readerAt struct {
361361 size int64
362362}
363363
364+ func (r * readerAt ) ReadAt (p []byte , off int64 ) (int , error ) {
365+ if len (p ) == 0 {
366+ return 0 , nil
367+ }
368+ if off >= r .size {
369+ return 0 , io .EOF
370+ }
371+ return r .ReaderAtCloser .ReadAt (p , off )
372+ }
373+
364374func (r * readerAt ) Size () int64 { return r .size }
365375
366376type minioClient struct {
@@ -371,25 +381,25 @@ type minioClient struct {
371381 manifestsPrefix string
372382}
373383
374- func newMinioClient (cfg Config ) (* minioClient , error ) {
375- if cfg .EndpointURL == "" {
376- cfg .EndpointURL = "https://s3.amazonaws.com"
384+ func newMinioClient (config Config ) (* minioClient , error ) {
385+ if config .EndpointURL == "" {
386+ config .EndpointURL = "https://s3.amazonaws.com"
377387 }
378388
379- parsedURL , err := url .Parse (cfg .EndpointURL )
389+ parsedURL , err := url .Parse (config .EndpointURL )
380390 if err != nil {
381391 return nil , errors .Wrap (err , "invalid endpoint_url" )
382392 }
383393
384394 bucketLookup := minio .BucketLookupDNS
385- if cfg .UsePathStyle {
395+ if config .UsePathStyle {
386396 bucketLookup = minio .BucketLookupPath
387397 }
388398
389399 client , err := minio .New (parsedURL .Host , & minio.Options {
390- Creds : credentials .NewStaticV4 (cfg .AccessKeyID , cfg .SecretAccessKey , cfg .SessionToken ),
400+ Creds : credentials .NewStaticV4 (config .AccessKeyID , config .SecretAccessKey , config .SessionToken ),
391401 Secure : parsedURL .Scheme == "https" ,
392- Region : cfg .Region ,
402+ Region : config .Region ,
393403 BucketLookup : bucketLookup ,
394404 })
395405 if err != nil {
@@ -398,24 +408,24 @@ func newMinioClient(cfg Config) (*minioClient, error) {
398408
399409 return & minioClient {
400410 client : client ,
401- bucket : cfg .Bucket ,
402- prefix : cfg .Prefix ,
403- blobsPrefix : cfg .BlobsPrefix ,
404- manifestsPrefix : cfg .ManifestsPrefix ,
411+ bucket : config .Bucket ,
412+ prefix : config .Prefix ,
413+ blobsPrefix : config .BlobsPrefix ,
414+ manifestsPrefix : config .ManifestsPrefix ,
405415 }, nil
406416}
407417
408- func (m * minioClient ) getManifest (ctx context.Context , key string , cfg * v1.CacheConfig ) (bool , error ) {
409- obj , err := m .client .GetObject (ctx , m .bucket , key , minio.GetObjectOptions {})
418+ func (m * minioClient ) getManifest (ctx context.Context , key string , config * v1.CacheConfig ) (bool , error ) {
419+ object , err := m .client .GetObject (ctx , m .bucket , key , minio.GetObjectOptions {})
410420 if err != nil {
411421 if isNotFound (err ) {
412422 return false , nil
413423 }
414424 return false , err
415425 }
416- defer obj .Close ()
417- decoder := json .NewDecoder (obj )
418- if err := decoder .Decode (cfg ); err != nil {
426+ defer object .Close ()
427+ decoder := json .NewDecoder (object )
428+ if err := decoder .Decode (config ); err != nil {
419429 return false , errors .WithStack (err )
420430 }
421431 if _ , err := decoder .Token (); ! errors .Is (err , io .EOF ) {
@@ -425,17 +435,17 @@ func (m *minioClient) getManifest(ctx context.Context, key string, cfg *v1.Cache
425435}
426436
427437func (m * minioClient ) getReader (ctx context.Context , key string , offset int64 ) (io.ReadCloser , error ) {
428- opts := minio.GetObjectOptions {}
438+ getOptions := minio.GetObjectOptions {}
429439 if offset > 0 {
430- if err := opts .SetRange (offset , 0 ); err != nil {
440+ if err := getOptions .SetRange (offset , 0 ); err != nil {
431441 return nil , err
432442 }
433443 }
434- obj , err := m .client .GetObject (ctx , m .bucket , key , opts )
444+ object , err := m .client .GetObject (ctx , m .bucket , key , getOptions )
435445 if err != nil {
436446 return nil , err
437447 }
438- return obj , nil
448+ return object , nil
439449}
440450
441451func (m * minioClient ) saveMutableAt (ctx context.Context , key string , body io.Reader , size int64 ) error {
@@ -451,33 +461,34 @@ func (m *minioClient) exists(ctx context.Context, key string) (*time.Time, error
451461 }
452462 return nil , err
453463 }
454- lm := stat .LastModified
455- return & lm , nil
464+ lastModified := stat .LastModified
465+ return & lastModified , nil
456466}
457467
458468func (m * minioClient ) touch (ctx context.Context , key string ) error {
459- src := minio.CopySrcOptions {Bucket : m .bucket , Object : key }
460- dst := minio.CopyDestOptions {
469+ // Minio will internally perform multipart copy when supported
470+ copySourceOptions := minio.CopySrcOptions {Bucket : m .bucket , Object : key }
471+ copyDestinationOptions := minio.CopyDestOptions {
461472 Bucket : m .bucket ,
462473 Object : key ,
463474 ReplaceMetadata : true ,
464475 UserMetadata : map [string ]string {"updated-at" : time .Now ().UTC ().Format (time .RFC3339Nano )},
465476 }
466- _ , err := m .client .CopyObject (ctx , dst , src )
477+ _ , err := m .client .CopyObject (ctx , copyDestinationOptions , copySourceOptions )
467478 return err
468479}
469480
470- func (m * minioClient ) ReaderAt (ctx context.Context , desc ocispecs.Descriptor ) (content.ReaderAt , error ) {
481+ func (m * minioClient ) ReaderAt (ctx context.Context , descriptor ocispecs.Descriptor ) (content.ReaderAt , error ) {
471482 readerAtCloser := toReaderAtCloser (func (offset int64 ) (io.ReadCloser , error ) {
472- return m .getReader (ctx , m .blobKey (desc .Digest ), offset )
483+ return m .getReader (ctx , m .blobKey (descriptor .Digest ), offset )
473484 })
474- return & readerAt {ReaderAtCloser : readerAtCloser , size : desc .Size }, nil
485+ return & readerAt {ReaderAtCloser : readerAtCloser , size : descriptor .Size }, nil
475486}
476487
477488func (m * minioClient ) manifestKey (name string ) string { return m .prefix + m .manifestsPrefix + name }
478489
479- func (m * minioClient ) blobKey (dgst digest.Digest ) string {
480- return m .prefix + m .blobsPrefix + dgst .String ()
490+ func (m * minioClient ) blobKey (digestValue digest.Digest ) string {
491+ return m .prefix + m .blobsPrefix + digestValue .String ()
481492}
482493
483494func isNotFound (err error ) bool {
0 commit comments