@@ -223,7 +223,7 @@ func (c *CRIImageService) PullImage(ctx context.Context, name string, credential
223
223
if r == "" {
224
224
continue
225
225
}
226
- if err := c .createImageReference (ctx , r , image .Target (), labels ); err != nil {
226
+ if err := c .createOrUpdateImageReference (ctx , r , image .Target (), labels ); err != nil {
227
227
return "" , fmt .Errorf ("failed to create image reference %q: %w" , r , err )
228
228
}
229
229
// Update image store to reflect the newest state in containerd.
@@ -304,11 +304,11 @@ func ParseAuth(auth *runtime.AuthConfig, host string) (string, string, error) {
304
304
return "" , "" , nil
305
305
}
306
306
307
- // createImageReference creates image reference inside containerd image store.
307
+ // createOrUpdateImageReference creates or updates image reference inside containerd image store.
308
308
// Note that because create and update are not finished in one transaction, there could be race. E.g.
309
309
// the image reference is deleted by someone else after create returns already exists, but before update
310
310
// happens.
311
- func (c * CRIImageService ) createImageReference (ctx context.Context , name string , desc imagespec.Descriptor , labels map [string ]string ) error {
311
+ func (c * CRIImageService ) createOrUpdateImageReference (ctx context.Context , name string , desc imagespec.Descriptor , labels map [string ]string ) error {
312
312
img := containerdimages.Image {
313
313
Name : name ,
314
314
Target : desc ,
@@ -362,31 +362,42 @@ func (c *CRIImageService) getLabels(ctx context.Context, name string) map[string
362
362
func (c * CRIImageService ) UpdateImage (ctx context.Context , r string ) error {
363
363
// TODO: Use image service
364
364
img , err := c .client .GetImage (ctx , r )
365
- if err != nil && ! errdefs .IsNotFound (err ) {
366
- return fmt .Errorf ("get image by reference: %w" , err )
367
- }
368
- if err == nil && img .Labels ()[crilabels .ImageLabelKey ] != crilabels .ImageLabelValue {
369
- // Make sure the image has the image id as its unique
370
- // identifier that references the image in its lifetime.
371
- configDesc , err := img .Config (ctx )
372
- if err != nil {
373
- return fmt .Errorf ("get image id: %w" , err )
374
- }
375
- id := configDesc .Digest .String ()
376
- labels := c .getLabels (ctx , r )
377
- if err := c .createImageReference (ctx , id , img .Target (), labels ); err != nil {
378
- return fmt .Errorf ("create image id reference %q: %w" , id , err )
365
+ if err != nil {
366
+ if ! errdefs .IsNotFound (err ) {
367
+ return fmt .Errorf ("get image by reference: %w" , err )
379
368
}
380
- if err := c .imageStore .Update (ctx , id ); err != nil {
381
- return fmt .Errorf ("update image store for %q: %w" , id , err )
369
+ // If the image is not found, we should continue updating the cache,
370
+ // so that the image can be removed from the cache.
371
+ if err := c .imageStore .Update (ctx , r ); err != nil {
372
+ return fmt .Errorf ("update image store for %q: %w" , r , err )
382
373
}
383
- // The image id is ready, add the label to mark the image as managed.
384
- if err := c .createImageReference (ctx , r , img .Target (), labels ); err != nil {
385
- return fmt .Errorf ("create managed label: %w" , err )
374
+ return nil
375
+ }
376
+
377
+ labels := img .Labels ()
378
+ criLabels := c .getLabels (ctx , r )
379
+ for key , value := range criLabels {
380
+ if labels [key ] != value {
381
+ // Make sure the image has the image id as its unique
382
+ // identifier that references the image in its lifetime.
383
+ configDesc , err := img .Config (ctx )
384
+ if err != nil {
385
+ return fmt .Errorf ("get image id: %w" , err )
386
+ }
387
+ id := configDesc .Digest .String ()
388
+ if err := c .createOrUpdateImageReference (ctx , id , img .Target (), criLabels ); err != nil {
389
+ return fmt .Errorf ("create image id reference %q: %w" , id , err )
390
+ }
391
+ if err := c .imageStore .Update (ctx , id ); err != nil {
392
+ return fmt .Errorf ("update image store for %q: %w" , id , err )
393
+ }
394
+ // The image id is ready, add the label to mark the image as managed.
395
+ if err := c .createOrUpdateImageReference (ctx , r , img .Target (), criLabels ); err != nil {
396
+ return fmt .Errorf ("create managed label: %w" , err )
397
+ }
398
+ break
386
399
}
387
400
}
388
- // If the image is not found, we should continue updating the cache,
389
- // so that the image can be removed from the cache.
390
401
if err := c .imageStore .Update (ctx , r ); err != nil {
391
402
return fmt .Errorf ("update image store for %q: %w" , r , err )
392
403
}
0 commit comments