Skip to content

Commit

Permalink
TestShouldPullImage: run test cases with feature gate enabled and dis…
Browse files Browse the repository at this point in the history
…abled

Signed-off-by: Paco Xu <paco.xu@daocloud.io>
  • Loading branch information
pacoxu committed Jan 5, 2023
1 parent 88d7159 commit 3dbb9d7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
5 changes: 2 additions & 3 deletions pkg/kubelet/container/testing/runtime_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions pkg/kubelet/images/image_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"k8s.io/client-go/util/flowcontrol"
"k8s.io/klog/v2"

utilfeature "k8s.io/apiserver/pkg/util/feature"
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1"
"k8s.io/kubernetes/pkg/credentialprovider"
credentialprovidersecrets "k8s.io/kubernetes/pkg/credentialprovider/secrets"
Expand Down Expand Up @@ -225,8 +226,8 @@ func (m *imageManager) EnsureImageExists(ctx context.Context, pod *v1.Pod, conta
} else {
// store/create hashMatch map entry for auth config hash key used to pull the image
// for this imageref (digest)
digest := m.ensureSecretPulledImage[imageRef]
if digest == nil {
digest, ok := m.ensureSecretPulledImage[imageRef]
if !ok {
digest = &ensureSecretPulledImageDigest{HashMatch: make(map[string]bool)}
m.ensureSecretPulledImage[imageRef] = digest
}
Expand Down Expand Up @@ -301,8 +302,8 @@ func (m *imageManager) isEnsuredBySecret(imageRef string, image kubecontainer.Im

hash := kubecontainer.HashAuth(auth)
if hash != "" {
digest := m.ensureSecretPulledImage[imageRef]
if digest != nil {
digest, ok := m.ensureSecretPulledImage[imageRef]
if ok {
if digest.HashMatch[hash] {
ensuredBySecret = true
return
Expand Down
21 changes: 13 additions & 8 deletions pkg/kubelet/images/image_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
utilfeature "k8s.io/apiserver/pkg/util/feature"
"k8s.io/client-go/tools/record"
"k8s.io/client-go/util/flowcontrol"
featuregatetesting "k8s.io/component-base/featuregate/testing"
Expand Down Expand Up @@ -432,14 +433,18 @@ func TestShouldPullImage(t *testing.T) {
},
}

defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.KubeletEnsureSecretPulledImages, true)()

for i, test := range tests {
sp := shouldPullImage(test.container, test.imagePresent, test.pulledBySecret, test.ensuredBySecret)
if utilfeature.DefaultFeatureGate.Enabled(features.KubeletEnsureSecretPulledImages) {
assert.Equal(t, test.expectedWithFGOn, sp, "TestCase[%d]: %s ensured image fg enabled", i, test.description)
} else {
t.Run("disabled_features", func(t *testing.T) {
defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.KubeletEnsureSecretPulledImages, false)()
for i, test := range tests {
sp := shouldPullImage(test.container, test.imagePresent, test.pulledBySecret, test.ensuredBySecret)
assert.Equal(t, test.expectedWithFGOff, sp, "TestCase[%d]: %s ensured image fg disabled", i, test.description)
}
}
})
t.Run("enabled_features", func(t *testing.T) {
defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.KubeletEnsureSecretPulledImages, true)()
for i, test := range tests {
sp := shouldPullImage(test.container, test.imagePresent, test.pulledBySecret, test.ensuredBySecret)
assert.Equal(t, test.expectedWithFGOn, sp, "TestCase[%d]: %s ensured image fg enabled", i, test.description)
}
})
}

0 comments on commit 3dbb9d7

Please sign in to comment.