@@ -88,7 +88,7 @@ func NewImageManager(recorder record.EventRecorder, imageService kubecontainer.I
8888 puller = newParallelImagePuller (imageService , maxParallelImagePulls )
8989 }
9090 var pullImageSecretCheck bool
91- if pullImageSecretRecheck != nil {
91+ if utilfeature . DefaultFeatureGate . Enabled (( features . KubeletEnsureSecretPulledImages )) && pullImageSecretRecheck != nil {
9292 pullImageSecretCheck = * pullImageSecretRecheck
9393 }
9494 return & imageManager {
@@ -106,7 +106,7 @@ func NewImageManager(recorder record.EventRecorder, imageService kubecontainer.I
106106
107107// shouldPullImage returns whether we should pull an image according to
108108// the presence and pull policy of the image.
109- func shouldPullImage (container * v1.Container , imagePresent , pulledBySecret , ensuredBySecret bool ) bool {
109+ func shouldPullImage (container * v1.Container , imagePresent , pulledBySecret , ensuredBySecret , pullImageSecretRecheck bool ) bool {
110110 switch container .ImagePullPolicy {
111111 case v1 .PullNever :
112112 return false
@@ -122,10 +122,8 @@ func shouldPullImage(container *v1.Container, imagePresent, pulledBySecret, ensu
122122 // pulled the image successfully. Otherwise pod B could use pod A's images
123123 // without auth. So in this case if pulledBySecret but not ensured by matching
124124 // secret auth for a pull again for the pod B scenario where the auth does not match
125- if utilfeature .DefaultFeatureGate .Enabled ((features .KubeletEnsureSecretPulledImages )) {
126- if pulledBySecret && ! ensuredBySecret {
127- return true // noting here that old behaviour returns false in this case indicating the image should not be pulled
128- }
125+ if pullImageSecretRecheck && pulledBySecret && ! ensuredBySecret {
126+ return true // noting here that old behaviour returns false in this case indicating the image should not be pulled
129127 }
130128 return false
131129 }
@@ -141,6 +139,7 @@ func (m *imageManager) logIt(ref *v1.ObjectReference, eventtype, event, prefix,
141139 }
142140}
143141
142+ // EnsuredInfo contains the data if an image is ensured and the last ensured time
144143type EnsuredInfo struct {
145144 // true for ensured secret
146145 Ensured bool `json:"ensured"`
@@ -197,7 +196,7 @@ func (m *imageManager) EnsureImageExists(ctx context.Context, pod *v1.Pod, conta
197196 }
198197 present := imageRef != ""
199198 var pulledBySecret , ensuredBySecret bool
200- if utilfeature . DefaultFeatureGate . Enabled ( features . KubeletEnsureSecretPulledImages ) && m .pullImageSecretRecheck {
199+ if m .pullImageSecretRecheck {
201200 m .loadEnsureSecretPulledImagesData ()
202201 if present {
203202 pulledBySecret , ensuredBySecret , err = m .isEnsuredBySecret (imageRef , spec , pullSecrets )
@@ -207,10 +206,10 @@ func (m *imageManager) EnsureImageExists(ctx context.Context, pod *v1.Pod, conta
207206 }
208207 klog .V (5 ).InfoS ("Get ensured check by secret" , "image" , image , "imageRef" , imageRef , "pulledBySecret" , pulledBySecret , "ensuredBySecret" , ensuredBySecret )
209208 }
210- if ! shouldPullImage (container , present , pulledBySecret , ensuredBySecret ) {
209+ if ! shouldPullImage (container , present , pulledBySecret , ensuredBySecret , m . pullImageSecretRecheck ) {
211210 // should not pull when pull never, or if present and correctly authenticated
212211 if present {
213- if utilfeature . DefaultFeatureGate . Enabled ( features . KubeletEnsureSecretPulledImages ) && m .pullImageSecretRecheck && ! ensuredBySecret {
212+ if pulledBySecret && m .pullImageSecretRecheck && ! ensuredBySecret {
214213 // TODO: add integration test for this thrown error message
215214 msg := fmt .Sprintf ("Container image %q is present with pull policy of %q but does not have the proper auth (image secret) that was used to pull the image" , container .Image , container .ImagePullPolicy )
216215 m .logIt (ref , v1 .EventTypeWarning , events .ErrImageNotEnsured , logPrefix , msg , klog .Warning )
@@ -253,7 +252,7 @@ func (m *imageManager) EnsureImageExists(ctx context.Context, pod *v1.Pod, conta
253252 metrics .ImagePullDuration .WithLabelValues (metrics .GetImageSizeBucket (imagePullResult .imageSize )).Observe (imagePullDuration .Seconds ())
254253 m .backOff .GC ()
255254
256- if utilfeature . DefaultFeatureGate . Enabled ( features . KubeletEnsureSecretPulledImages ) && m .pullImageSecretRecheck {
255+ if m .pullImageSecretRecheck {
257256 m .lock .Lock ()
258257 defer m .lock .Unlock ()
259258 // Get the ImageRef after the Image Pull
@@ -287,7 +286,11 @@ func (m *imageManager) storeEnsureSecretPulledImagesData() {
287286 klog .ErrorS (err , "Failed to create path" , "path" , imageStateManagerPath )
288287 return
289288 }
290- defer imageFile .Close ()
289+ defer func () {
290+ if err := imageFile .Close (); err != nil {
291+ klog .ErrorS (err , "Failed to close the file" , "file-path" , imageStateManagerPath )
292+ }
293+ }()
291294 }
292295 byteData , err := utiljson .Marshal (m .ensureSecretPulledImages )
293296 if err != nil {
@@ -306,7 +309,7 @@ func (m *imageManager) storeEnsureSecretPulledImagesData() {
306309func (m * imageManager ) loadEnsureSecretPulledImagesData () {
307310 _ , err := os .Stat (imageStateManagerPath )
308311 if err != nil && ! os .IsExist (err ) {
309- klog .ErrorS ( err , "Failed to stat file" , "file" , imageStateManagerPath )
312+ klog .InfoS ( "Failed to stat file" , "file" , imageStateManagerPath , "error" , err )
310313 return
311314 }
312315 m .fsLock .Lock ()
@@ -379,43 +382,39 @@ func applyDefaultImageTag(image string) (string, error) {
379382// 2. ensuredBySecret: true if the secret for an auth used to pull an
380383// image has already been authenticated through a successful pull request
381384// and the same auth exists for this podSandbox/image.
382- func (m * imageManager ) isEnsuredBySecret (imageRef string , image kubecontainer.ImageSpec , pullSecrets []v1.Secret ) (bool , bool , error ) {
385+ func (m * imageManager ) isEnsuredBySecret (imageRef string , image kubecontainer.ImageSpec , pullSecrets []v1.Secret ) (pulledBySecret bool , ensuredBySecret bool , _ error ) {
383386 m .lock .Lock ()
384387 defer m .lock .Unlock ()
385- if utilfeature . DefaultFeatureGate . Enabled ( features . KubeletEnsureSecretPulledImages ) && m .pullImageSecretRecheck {
388+ if m .pullImageSecretRecheck {
386389 m .refreshCache ()
387390 }
388391 if imageRef == "" {
389- return false , false , errors .New ("imageRef is empty" )
392+ return pulledBySecret , ensuredBySecret , errors .New ("imageRef is empty" )
390393 }
391394
392395 // if the image is in the ensured secret pulled image list, it is pulled by secret
393- pulledBySecret : = m .ensureSecretPulledImages [imageRef ] != nil
396+ pulledBySecret = m .ensureSecretPulledImages [imageRef ] != nil
394397
395398 img := image .Image
396399 repoToPull , _ , _ , err := parsers .ParseImageName (img )
397400 if err != nil {
398- return pulledBySecret , false , err
401+ return pulledBySecret , ensuredBySecret , err
399402 }
400403
401404 if m .keyring == nil {
402- return pulledBySecret , false , nil
405+ return pulledBySecret , ensuredBySecret , nil
403406 }
404407 keyring , err := credentialprovidersecrets .MakeDockerKeyring (pullSecrets , * m .keyring )
405408 if err != nil {
406- return pulledBySecret , false , err
409+ return pulledBySecret , ensuredBySecret , err
407410 }
408411
409412 creds , withCredentials := keyring .Lookup (repoToPull )
410413 if ! withCredentials {
411- return pulledBySecret , false , nil
412- }
413- // if the pullImageSecretRecheckPeriod is 0, we don't need to check the secret again
414- if m .pullImageSecretRecheckPeriod .Duration == 0 {
415- return pulledBySecret , true , nil
414+ return pulledBySecret , ensuredBySecret , nil
416415 }
417416
418- if utilfeature . DefaultFeatureGate . Enabled ( features . KubeletEnsureSecretPulledImages ) && m .pullImageSecretRecheck {
417+ if m .pullImageSecretRecheck {
419418 for _ , currentCreds := range creds {
420419 auth := & runtimeapi.AuthConfig {
421420 Username : currentCreds .Username ,
@@ -434,13 +433,13 @@ func (m *imageManager) isEnsuredBySecret(imageRef string, image kubecontainer.Im
434433 digest := m .ensureSecretPulledImages [imageRef ]
435434 if digest != nil {
436435 ensuredInfo := digest .Auths [hash ]
437- if ensuredInfo != nil && ensuredInfo .Ensured {
438- return pulledBySecret , true , nil
436+ if ensuredBySecret = ensuredInfo != nil && ensuredInfo .Ensured ; ensuredBySecret {
437+ return pulledBySecret , ensuredBySecret , nil
439438 }
440439 }
441440 }
442441 }
443- return pulledBySecret , false , nil
442+ return pulledBySecret , ensuredBySecret , nil
444443}
445444
446445func (m * imageManager ) refreshCache () {
0 commit comments