@@ -94,23 +94,32 @@ import (
9494
9595// PullImage pulls an image with authentication config.
9696func (c * criService ) PullImage (ctx context.Context , r * runtime.PullImageRequest ) (* runtime.PullImageResponse , error ) {
97- inProgresImagePulling .Inc ()
98- defer inProgresImagePulling .Dec ()
97+ inProgressImagePulls .Inc ()
98+ defer inProgressImagePulls .Dec ()
9999 startTime := time .Now ()
100+ var domain string
101+ var err error
102+ defer func () {
103+ if err != nil {
104+ imagePulls .WithValues ("failed" , domain ).Inc ()
105+ } else {
106+ imagePulls .WithValues ("succeed" , domain ).Inc ()
107+ }
108+ }()
100109 imageRef := r .GetImage ().GetImage ()
101110 namedRef , err := distribution .ParseDockerRef (imageRef )
102111 if err != nil {
103- imagePullingError .WithValues (imageRef ).Inc ()
104112 return nil , fmt .Errorf ("failed to parse image reference %q: %w" , imageRef , err )
105113 }
106114 ref := namedRef .String ()
107115 if ref != imageRef {
108116 log .G (ctx ).Debugf ("PullImage using normalized image ref: %q" , ref )
109117 }
118+ imageNamed , _ := distribution .ParseNamed (ref )
119+ domain = distribution .Domain (imageNamed )
110120
111121 imagePullProgressTimeout , err := time .ParseDuration (c .config .ImagePullProgressTimeout )
112122 if err != nil {
113- imagePullingError .WithValues (imageRef ).Inc ()
114123 return nil , fmt .Errorf ("failed to parse image_pull_progress_timeout %q: %w" , c .config .ImagePullProgressTimeout , err )
115124 }
116125
@@ -136,7 +145,6 @@ func (c *criService) PullImage(ctx context.Context, r *runtime.PullImageRequest)
136145 defer pcancel ()
137146 snapshotter , err := c .snapshotterFromPodSandboxConfig (ctx , ref , r .SandboxConfig )
138147 if err != nil {
139- imagePullingError .WithValues (imageRef ).Inc ()
140148 return nil , err
141149 }
142150 log .G (ctx ).Debugf ("PullImage %q with snapshotter %s" , ref , snapshotter )
@@ -170,13 +178,11 @@ func (c *criService) PullImage(ctx context.Context, r *runtime.PullImageRequest)
170178 image , err := c .client .Pull (pctx , ref , pullOpts ... )
171179 pcancel ()
172180 if err != nil {
173- imagePullingError .WithValues (imageRef ).Inc ()
174181 return nil , fmt .Errorf ("failed to pull and unpack image %q: %w" , ref , err )
175182 }
176183
177184 configDesc , err := image .Config (ctx )
178185 if err != nil {
179- imagePullingError .WithValues (imageRef ).Inc ()
180186 return nil , fmt .Errorf ("get image config descriptor: %w" , err )
181187 }
182188 imageID := configDesc .Digest .String ()
@@ -187,25 +193,20 @@ func (c *criService) PullImage(ctx context.Context, r *runtime.PullImageRequest)
187193 continue
188194 }
189195 if err := c .createImageReference (ctx , r , image .Target ()); err != nil {
190- imagePullingError .WithValues (imageRef ).Inc ()
191196 return nil , fmt .Errorf ("failed to create image reference %q: %w" , r , err )
192197 }
193198 // Update image store to reflect the newest state in containerd.
194199 // No need to use `updateImage`, because the image reference must
195200 // have been managed by the cri plugin.
196201 if err := c .imageStore .Update (ctx , r ); err != nil {
197- imagePullingError .WithValues (imageRef ).Inc ()
198202 return nil , fmt .Errorf ("failed to update image store %q: %w" , r , err )
199203 }
200204 }
201205
202- imageNamed , _ := distribution .ParseNamed (ref )
203- domain := distribution .Domain (imageNamed )
204-
205206 size , _ := image .Size (ctx )
206207
207208 secondsPulling1MiB := time .Since (startTime ).Seconds () / float64 (size / 1.0 / 1024 / 1024 )
208- imagePullThoughput .WithValues (domain ).Update (time .Duration (secondsPulling1MiB ) * time .Second )
209+ imagePullThroughput .WithValues (domain ).Update (time .Duration (secondsPulling1MiB ) * time .Second )
209210
210211 log .G (ctx ).Debugf ("Pulled image %q with image id %q, repo tag %q, repo digest %q, size %q in %v s" , imageRef , imageID ,
211212 repoTag , repoDigest , strconv .FormatInt (size , 10 ), time .Since (startTime ).Seconds ())
0 commit comments