Skip to content

Commit

Permalink
optimize func ConvertToRegistryAuths
Browse files Browse the repository at this point in the history
Signed-off-by: liheng.zms <[email protected]>
  • Loading branch information
zmberg committed Jan 25, 2024
1 parent 9913b92 commit bcc6f25
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
16 changes: 15 additions & 1 deletion pkg/daemon/criruntime/imageruntime/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func TestMatchRegistryAuths(t *testing.T) {
}
return []v1.Secret{demo}
},
Expect: 2,
Expect: 1,
},
{
name: "test5",
Expand All @@ -102,6 +102,20 @@ func TestMatchRegistryAuths(t *testing.T) {
},
Expect: 0,
},
{
name: "test6",
Image: "registry.private.com/app/echoserver:v1",
GetSecrets: func() []v1.Secret {
demo := v1.Secret{
Data: map[string][]byte{
"data": []byte("not docker config json"),
},
Type: v1.SecretTypeOpaque,
}
return []v1.Secret{demo}
},
Expect: 0,
},
}
for _, cs := range cases {
t.Run(cs.name, func(t *testing.T) {
Expand Down
13 changes: 7 additions & 6 deletions pkg/util/secret/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

daemonutil "github.com/openkruise/kruise/pkg/daemon/util"
corev1 "k8s.io/api/core/v1"
"k8s.io/kubernetes/pkg/credentialprovider"
credentialprovidersecrets "k8s.io/kubernetes/pkg/credentialprovider/secrets"
)

Expand All @@ -21,20 +20,22 @@ func AuthInfos(ctx context.Context, imageName, tag string, pullSecrets []corev1.
return authInfos
}

var (
keyring = credentialprovider.NewDockerKeyring()
)

func ConvertToRegistryAuths(pullSecrets []corev1.Secret, repo string) (infos []daemonutil.AuthInfo, err error) {
keyring, err := credentialprovidersecrets.MakeDockerKeyring(pullSecrets, keyring)
keyring, err := credentialprovidersecrets.MakeDockerKeyring(pullSecrets, nil)
if err != nil {
return nil, err
}
if keyring == nil {
return nil, nil
}
creds, withCredentials := keyring.Lookup(repo)
if !withCredentials {
return nil, nil
}
for _, c := range creds {
if c.Username == "" {
continue
}
infos = append(infos, daemonutil.AuthInfo{
Username: c.Username,
Password: c.Password,
Expand Down

0 comments on commit bcc6f25

Please sign in to comment.