Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix the matching logic for the imagePullSecret in ImagePullJob #1241

Merged
merged 1 commit into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions pkg/daemon/criruntime/imageruntime/cri.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
"k8s.io/klog/v2"
"k8s.io/kubernetes/pkg/kubelet/cri/remote/util"
"k8s.io/kubernetes/pkg/util/parsers"
)

const maxMsgSize = 1024 * 1024 * 16
Expand Down Expand Up @@ -71,6 +72,10 @@ type commonCRIImageService struct {
func (c *commonCRIImageService) PullImage(ctx context.Context, imageName, tag string, pullSecrets []v1.Secret, sandboxConfig *appsv1alpha1.SandboxConfig) (ImagePullStatusReader, error) {
registry := daemonutil.ParseRegistry(imageName)
fullImageName := imageName + ":" + tag
repoToPull, _, _, err := parsers.ParseImageName(fullImageName)
if err != nil {
return nil, err
}
// Reader
pipeR, pipeW := io.Pipe()
defer pipeW.Close()
Expand All @@ -89,10 +94,9 @@ func (c *commonCRIImageService) PullImage(ctx context.Context, imageName, tag st
Labels: sandboxConfig.Labels,
}
}
var err error
if len(pullSecrets) > 0 {
var authInfos []daemonutil.AuthInfo
authInfos, err = convertToRegistryAuths(pullSecrets, registry)
authInfos, err = convertToRegistryAuths(pullSecrets, repoToPull)
if err == nil {
var pullErrs []error
for _, authInfo := range authInfos {
Expand Down
119 changes: 119 additions & 0 deletions pkg/daemon/criruntime/imageruntime/helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
Copyright 2023 The Kruise Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package imageruntime

import (
"testing"

v1 "k8s.io/api/core/v1"
"k8s.io/kubernetes/pkg/util/parsers"
)

func TestMatchRegistryAuths(t *testing.T) {
cases := []struct {
name string
Image string
GetSecrets func() []v1.Secret
Expect int
}{
{
name: "test1",
Image: "registry.private.com/app/echoserver:v1",
GetSecrets: func() []v1.Secret {
demo := v1.Secret{
Data: map[string][]byte{
v1.DockerConfigJsonKey: []byte(`{"auths":{"registry.private.com":{"username":"echoserver","password":"test","auth":"ZWNob3NlcnZlcjp0ZXN0"}}}`),
},
Type: v1.SecretTypeDockerConfigJson,
}
return []v1.Secret{demo}
},
Expect: 1,
},
{
name: "test2",
Image: "registry.private.com/app/echoserver:v1",
GetSecrets: func() []v1.Secret {
demo := v1.Secret{
Data: map[string][]byte{
v1.DockerConfigJsonKey: []byte(`{"auths":{"registry.private.com/app":{"username":"echoserver","password":"test","auth":"ZWNob3NlcnZlcjp0ZXN0"}}}`),
},
Type: v1.SecretTypeDockerConfigJson,
}
return []v1.Secret{demo}
},
Expect: 1,
},
{
name: "test3",
Image: "registry.private.com/app/echoserver@sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
GetSecrets: func() []v1.Secret {
demo := v1.Secret{
Data: map[string][]byte{
v1.DockerConfigJsonKey: []byte(`{"auths":{"registry.private.com":{"username":"echoserver","password":"test","auth":"ZWNob3NlcnZlcjp0ZXN0"}}}`),
},
Type: v1.SecretTypeDockerConfigJson,
}
return []v1.Secret{demo}
},
Expect: 1,
},
{
name: "test4",
Image: "nginx:v1",
GetSecrets: func() []v1.Secret {
demo := v1.Secret{
Data: map[string][]byte{
v1.DockerConfigJsonKey: []byte(`{"auths":{"docker.io/library/nginx":{"username":"echoserver","password":"test","auth":"ZWNob3NlcnZlcjp0ZXN0"}}}`),
},
Type: v1.SecretTypeDockerConfigJson,
}
return []v1.Secret{demo}
},
Expect: 2,
},
{
name: "test5",
Image: "registry.private.com/app/echoserver:v1",
GetSecrets: func() []v1.Secret {
demo := v1.Secret{
Data: map[string][]byte{
v1.DockerConfigJsonKey: []byte(`{"auths":{"registry.private.com/nginx":{"username":"echoserver","password":"test","auth":"ZWNob3NlcnZlcjp0ZXN0"}}}`),
},
Type: v1.SecretTypeDockerConfigJson,
}
return []v1.Secret{demo}
},
Expect: 0,
},
}
for _, cs := range cases {
t.Run(cs.name, func(t *testing.T) {
repoToPull, _, _, err := parsers.ParseImageName(cs.Image)
if err != nil {
t.Fatalf("ParseImageName failed: %s", err.Error())
}
infos, err := convertToRegistryAuths(cs.GetSecrets(), repoToPull)
if err != nil {
t.Fatalf("convertToRegistryAuths failed: %s", err.Error())
}
if len(infos) != cs.Expect {
t.Fatalf("convertToRegistryAuths failed")
}
})
}
}