@@ -45,6 +45,7 @@ import (
4545 "github.com/containerd/containerd/log"
4646 "github.com/containerd/containerd/pkg/cri/annotations"
4747 criconfig "github.com/containerd/containerd/pkg/cri/config"
48+ crilabels "github.com/containerd/containerd/pkg/cri/labels"
4849 snpkg "github.com/containerd/containerd/pkg/snapshotters"
4950 distribution "github.com/containerd/containerd/reference/docker"
5051 "github.com/containerd/containerd/remotes/docker"
@@ -155,12 +156,14 @@ func (c *CRIImageService) PullImage(ctx context.Context, r *runtime.PullImageReq
155156 tracing .Attribute ("snapshotter.name" , snapshotter ),
156157 )
157158
159+ labels := c .getLabels (ctx , ref )
160+
158161 pullOpts := []containerd.RemoteOpt {
159162 containerd .WithSchema1Conversion , //nolint:staticcheck // Ignore SA1019. Need to keep deprecated package for compatibility.
160163 containerd .WithResolver (resolver ),
161164 containerd .WithPullSnapshotter (snapshotter ),
162165 containerd .WithPullUnpack ,
163- containerd .WithPullLabel ( imageLabelKey , imageLabelValue ),
166+ containerd .WithPullLabels ( labels ),
164167 containerd .WithMaxConcurrentDownloads (c .config .MaxConcurrentDownloads ),
165168 containerd .WithImageHandler (imageHandler ),
166169 containerd .WithUnpackOpts ([]containerd.UnpackOpt {
@@ -199,7 +202,7 @@ func (c *CRIImageService) PullImage(ctx context.Context, r *runtime.PullImageReq
199202 if r == "" {
200203 continue
201204 }
202- if err := c .createImageReference (ctx , r , image .Target ()); err != nil {
205+ if err := c .createImageReference (ctx , r , image .Target (), labels ); err != nil {
203206 return nil , fmt .Errorf ("failed to create image reference %q: %w" , r , err )
204207 }
205208 // Update image store to reflect the newest state in containerd.
@@ -283,50 +286,69 @@ func ParseAuth(auth *runtime.AuthConfig, host string) (string, string, error) {
283286// Note that because create and update are not finished in one transaction, there could be race. E.g.
284287// the image reference is deleted by someone else after create returns already exists, but before update
285288// happens.
286- func (c * CRIImageService ) createImageReference (ctx context.Context , name string , desc imagespec.Descriptor ) error {
289+ func (c * CRIImageService ) createImageReference (ctx context.Context , name string , desc imagespec.Descriptor , labels map [ string ] string ) error {
287290 img := containerdimages.Image {
288291 Name : name ,
289292 Target : desc ,
290293 // Add a label to indicate that the image is managed by the cri plugin.
291- Labels : map [ string ] string { imageLabelKey : imageLabelValue } ,
294+ Labels : labels ,
292295 }
293296 // TODO(random-liu): Figure out which is the more performant sequence create then update or
294297 // update then create.
295298 oldImg , err := c .client .ImageService ().Create (ctx , img )
296299 if err == nil || ! errdefs .IsAlreadyExists (err ) {
297300 return err
298301 }
299- if oldImg .Target .Digest == img .Target .Digest && oldImg .Labels [imageLabelKey ] == imageLabelValue {
302+ if oldImg .Target .Digest == img .Target .Digest && oldImg .Labels [crilabels . ImageLabelKey ] == labels [ crilabels . ImageLabelKey ] {
300303 return nil
301304 }
302- _ , err = c .client .ImageService ().Update (ctx , img , "target" , "labels." + imageLabelKey )
305+ _ , err = c .client .ImageService ().Update (ctx , img , "target" , "labels." + crilabels . ImageLabelKey )
303306 return err
304307}
305308
306- // UpdateImage updates image store to reflect the newest state of an image reference
309+ // getLabels get image labels to be added on CRI image
310+ func (c * CRIImageService ) getLabels (ctx context.Context , name string ) map [string ]string {
311+ labels := map [string ]string {crilabels .ImageLabelKey : crilabels .ImageLabelValue }
312+ configSandboxImage := c .config .SandboxImage
313+ // parse sandbox image
314+ sandboxNamedRef , err := distribution .ParseDockerRef (configSandboxImage )
315+ if err != nil {
316+ log .G (ctx ).Errorf ("failed to parse sandbox image from config %s" , sandboxNamedRef )
317+ return nil
318+ }
319+ sandboxRef := sandboxNamedRef .String ()
320+ // Adding pinned image label to sandbox image
321+ if sandboxRef == name {
322+ labels [crilabels .PinnedImageLabelKey ] = crilabels .PinnedImageLabelValue
323+ }
324+ return labels
325+ }
326+
327+ // updateImage updates image store to reflect the newest state of an image reference
307328// in containerd. If the reference is not managed by the cri plugin, the function also
308329// generates necessary metadata for the image and make it managed.
309330func (c * CRIImageService ) UpdateImage (ctx context.Context , r string ) error {
310331 img , err := c .client .GetImage (ctx , r )
311332 if err != nil && ! errdefs .IsNotFound (err ) {
312333 return fmt .Errorf ("get image by reference: %w" , err )
313334 }
314- if err == nil && img .Labels ()[imageLabelKey ] != imageLabelValue {
335+ if err == nil && img .Labels ()[crilabels . ImageLabelKey ] != crilabels . ImageLabelValue {
315336 // Make sure the image has the image id as its unique
316337 // identifier that references the image in its lifetime.
317338 configDesc , err := img .Config (ctx )
318339 if err != nil {
319340 return fmt .Errorf ("get image id: %w" , err )
320341 }
321342 id := configDesc .Digest .String ()
322- if err := c .createImageReference (ctx , id , img .Target ()); err != nil {
343+ labels := c .getLabels (ctx , id )
344+ if err := c .createImageReference (ctx , id , img .Target (), labels ); err != nil {
323345 return fmt .Errorf ("create image id reference %q: %w" , id , err )
324346 }
325347 if err := c .imageStore .Update (ctx , id ); err != nil {
326348 return fmt .Errorf ("update image store for %q: %w" , id , err )
327349 }
328350 // The image id is ready, add the label to mark the image as managed.
329- if err := c .createImageReference (ctx , r , img .Target ()); err != nil {
351+ if err := c .createImageReference (ctx , r , img .Target (), labels ); err != nil {
330352 return fmt .Errorf ("create managed label: %w" , err )
331353 }
332354 }
0 commit comments