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 26, 2024
1 parent 9913b92 commit 141bb45
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 8 deletions.
51 changes: 49 additions & 2 deletions pkg/daemon/criruntime/imageruntime/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ limitations under the License.
package imageruntime

import (
"context"
"testing"

"github.com/openkruise/kruise/pkg/util/secret"

v1 "k8s.io/api/core/v1"
"k8s.io/kubernetes/pkg/util/parsers"
)
Expand Down 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 Expand Up @@ -192,3 +206,36 @@ func TestContainsImage(t *testing.T) {
})
}
}

func TestAuthInfos(t *testing.T) {
cases := []struct {
name string
Image []string
GetSecrets func() []v1.Secret
Expect int
}{
{
name: "test1",
Image: []string{"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,
},
}

for _, cs := range cases {
t.Run(cs.name, func(t *testing.T) {
infos := secret.AuthInfos(context.TODO(), cs.Image[0], cs.Image[1], cs.GetSecrets())
if len(infos) != cs.Expect {
t.Fatalf("AuthInfos failed")
}
})
}
}
10 changes: 4 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,15 +20,14 @@ 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
Expand Down

0 comments on commit 141bb45

Please sign in to comment.