Skip to content

Commit

Permalink
fix the matching logic for the imagePullSecret in ImagePullJob (openk…
Browse files Browse the repository at this point in the history
…ruise#1241)

Signed-off-by: liheng.zms <[email protected]>
  • Loading branch information
zmberg authored and diannaowa committed Jun 2, 2023
1 parent 7843f73 commit 54e6399
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 4 deletions.
11 changes: 7 additions & 4 deletions pkg/daemon/criruntime/imageruntime/cri.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ import (

appsv1alpha1 "github.com/openkruise/kruise/apis/apps/v1alpha1"

daemonutil "github.com/openkruise/kruise/pkg/daemon/util"
"github.com/pkg/errors"
"google.golang.org/grpc"
v1 "k8s.io/api/core/v1"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
"k8s.io/klog/v2"
"k8s.io/kubernetes/pkg/kubelet/cri/remote/util"

daemonutil "github.com/openkruise/kruise/pkg/daemon/util"
"k8s.io/kubernetes/pkg/util/parsers"
)

const maxMsgSize = 1024 * 1024 * 16
Expand Down Expand Up @@ -72,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 @@ -90,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")
}
})
}
}

0 comments on commit 54e6399

Please sign in to comment.