Skip to content

Commit ba335de

Browse files
committed
1 parent 39b0137 commit ba335de

File tree

8 files changed

+42
-41
lines changed

8 files changed

+42
-41
lines changed

pkg/features/kube_features.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,9 +369,9 @@ const (
369369
// fallback to using it's cgroupDriver option.
370370
KubeletCgroupDriverFromCRI featuregate.Feature = "KubeletCgroupDriverFromCRI"
371371

372-
// owner: @mikebrow @pacoxu
372+
// owner: @mikebrow @pacoxu @sairameshv
373373
// kep: http://kep.k8s.io/2535
374-
// alpha: v1.30
374+
// alpha: v1.31
375375
//
376376
// Enables kubelet to ensure images pulled with pod imagePullSecrets are authenticated
377377
// by other pods that do not have the same credentials.

pkg/kubelet/apis/config/scheme/testdata/KubeletConfiguration/after/v1beta1.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ oomScoreAdj: -999
7676
podLogsDir: /var/log/pods
7777
podPidsLimit: -1
7878
port: 10250
79-
pullImageSecretRecheckPeriod: 0s
8079
pullImageSecretRecheck: false
80+
pullImageSecretRecheckPeriod: 0s
8181
registerNode: true
8282
registryBurst: 10
8383
registryPullQPS: 5

pkg/kubelet/apis/config/scheme/testdata/KubeletConfiguration/roundtrip/default/v1beta1.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ oomScoreAdj: -999
7676
podLogsDir: /var/log/pods
7777
podPidsLimit: -1
7878
port: 10250
79-
pullImageSecretRecheckPeriod: 0s
8079
pullImageSecretRecheck: false
80+
pullImageSecretRecheckPeriod: 0s
8181
registerNode: true
8282
registryBurst: 10
8383
registryPullQPS: 5

pkg/kubelet/container/helpers.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,10 @@ func pickFieldsToHash(container *v1.Container) map[string]string {
127127
return retval
128128
}
129129

130-
// HashAuth - returns a hash code for a CRI pull image auth, and an error if the hash cannot be calculated.
130+
// HashAuth returns a hash code of a CRI pull image auth,
131+
// and an error if the hash cannot be calculated.
132+
// This hash code would be used as a metadata of the image pull info
133+
// to determine if an image is ensured and has to be pulled or not
131134
func HashAuth(auth *runtimeapi.AuthConfig) (string, error) {
132135
hash := fnv.New64a()
133136
authJSON, err := json.Marshal(auth)

pkg/kubelet/container/helpers_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1037,6 +1037,6 @@ func TestHashAuth(t *testing.T) {
10371037
for _, tc := range testCases {
10381038
hashVal, err := HashAuth(tc.auth)
10391039
assert.Equal(t, tc.expectedHash, hashVal, "the hash value here should not be changed.")
1040-
assert.Nil(t, err)
1040+
assert.NoError(t, err)
10411041
}
10421042
}

pkg/kubelet/images/image_manager.go

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -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
144143
type 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() {
306309
func (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

446445
func (m *imageManager) refreshCache() {

pkg/kubelet/images/image_manager_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -694,14 +694,14 @@ func TestShouldPullImage(t *testing.T) {
694694
t.Run("disable_ensure_secret_pull_image_features", func(t *testing.T) {
695695
featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.KubeletEnsureSecretPulledImages, false)
696696
for i, test := range tests {
697-
sp := shouldPullImage(test.container, test.imagePresent, test.pulledBySecret, test.ensuredBySecret)
697+
sp := shouldPullImage(test.container, test.imagePresent, test.pulledBySecret, test.ensuredBySecret, false)
698698
assert.Equal(t, test.expectedWithFGOff, sp, "TestCase[%d]: %s ensured image fg disabled", i, test.description)
699699
}
700700
})
701701
t.Run("enable_ensure_secret_pull_image_features", func(t *testing.T) {
702702
featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.KubeletEnsureSecretPulledImages, true)
703703
for i, test := range tests {
704-
sp := shouldPullImage(test.container, test.imagePresent, test.pulledBySecret, test.ensuredBySecret)
704+
sp := shouldPullImage(test.container, test.imagePresent, test.pulledBySecret, test.ensuredBySecret, true)
705705
assert.Equal(t, test.expectedWithFGOn, sp, "TestCase[%d]: %s ensured image fg enabled", i, test.description)
706706
}
707707
})

pkg/kubelet/images/puller.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,10 @@ import (
2727
)
2828

2929
type pullResult struct {
30-
imageRef string
31-
imageSize uint64
32-
err error
33-
pullDuration time.Duration
34-
// pullCredentialsHash of successful pull with auth, nil if no auth used or error
30+
imageRef string
31+
imageSize uint64
32+
err error
33+
pullDuration time.Duration
3534
pullCredentialsHash string
3635
}
3736

0 commit comments

Comments
 (0)