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

optimize func ConvertToRegistryAuths #1492

Closed
wants to merge 1 commit into from
Closed
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
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
Loading