@@ -319,16 +319,30 @@ func (c *CRIImageService) createImageReference(ctx context.Context, name string,
319
319
// TODO(random-liu): Figure out which is the more performant sequence create then update or
320
320
// update then create.
321
321
// TODO: Call CRIImageService directly
322
- oldImg , err := c .images .Create (ctx , img )
322
+ _ , err := c .images .Create (ctx , img )
323
323
if err == nil {
324
324
return nil
325
325
} else if ! errdefs .IsAlreadyExists (err ) {
326
326
return err
327
327
}
328
- if oldImg .Target .Digest == img .Target .Digest && oldImg .Labels [crilabels .ImageLabelKey ] == labels [crilabels .ImageLabelKey ] {
328
+ // Retrieve oldImg from image store here because Create routine returns an
329
+ // empty image on ErrAlreadyExists
330
+ oldImg , err := c .images .Get (ctx , name )
331
+ if err != nil {
332
+ return err
333
+ }
334
+ fieldpaths := []string {"target" }
335
+ if oldImg .Labels [crilabels .ImageLabelKey ] != labels [crilabels .ImageLabelKey ] {
336
+ fieldpaths = append (fieldpaths , "labels." + crilabels .ImageLabelKey )
337
+ }
338
+ if oldImg .Labels [crilabels .PinnedImageLabelKey ] != labels [crilabels .PinnedImageLabelKey ] &&
339
+ labels [crilabels .PinnedImageLabelKey ] == crilabels .PinnedImageLabelValue {
340
+ fieldpaths = append (fieldpaths , "labels." + crilabels .PinnedImageLabelKey )
341
+ }
342
+ if oldImg .Target .Digest == img .Target .Digest && len (fieldpaths ) < 2 {
329
343
return nil
330
344
}
331
- _ , err = c .images .Update (ctx , img , "target" , "labels." + crilabels . ImageLabelKey )
345
+ _ , err = c .images .Update (ctx , img , fieldpaths ... )
332
346
return err
333
347
}
334
348
@@ -360,7 +374,7 @@ func (c *CRIImageService) UpdateImage(ctx context.Context, r string) error {
360
374
return fmt .Errorf ("get image id: %w" , err )
361
375
}
362
376
id := configDesc .Digest .String ()
363
- labels := c .getLabels (ctx , id )
377
+ labels := c .getLabels (ctx , r )
364
378
if err := c .createImageReference (ctx , id , img .Target (), labels ); err != nil {
365
379
return fmt .Errorf ("create image id reference %q: %w" , id , err )
366
380
}
0 commit comments