@@ -223,7 +223,7 @@ func (c *CRIImageService) PullImage(ctx context.Context, name string, credential
223223 if r == "" {
224224 continue
225225 }
226- if err := c .createImageReference (ctx , r , image .Target (), labels ); err != nil {
226+ if err := c .createOrUpdateImageReference (ctx , r , image .Target (), labels ); err != nil {
227227 return "" , fmt .Errorf ("failed to create image reference %q: %w" , r , err )
228228 }
229229 // Update image store to reflect the newest state in containerd.
@@ -304,11 +304,11 @@ func ParseAuth(auth *runtime.AuthConfig, host string) (string, string, error) {
304304 return "" , "" , nil
305305}
306306
307- // createImageReference creates image reference inside containerd image store.
307+ // createOrUpdateImageReference creates or updates image reference inside containerd image store.
308308// Note that because create and update are not finished in one transaction, there could be race. E.g.
309309// the image reference is deleted by someone else after create returns already exists, but before update
310310// 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 {
312312 img := containerdimages.Image {
313313 Name : name ,
314314 Target : desc ,
@@ -362,31 +362,42 @@ func (c *CRIImageService) getLabels(ctx context.Context, name string) map[string
362362func (c * CRIImageService ) UpdateImage (ctx context.Context , r string ) error {
363363 // TODO: Use image service
364364 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 )
379368 }
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 )
382373 }
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
386399 }
387400 }
388- // If the image is not found, we should continue updating the cache,
389- // so that the image can be removed from the cache.
390401 if err := c .imageStore .Update (ctx , r ); err != nil {
391402 return fmt .Errorf ("update image store for %q: %w" , r , err )
392403 }
0 commit comments