@@ -27,6 +27,7 @@ import (
2727"k8s.io/client-go/kubernetes"
2828"k8s.io/client-go/rest"
2929"k8s.io/client-go/util/retry"
30+ "k8s.io/kubernetes/pkg/credentialprovider"
3031"oras.land/oras-go/v2"
3132"oras.land/oras-go/v2/content"
3233"oras.land/oras-go/v2/content/file"
@@ -420,6 +421,8 @@ func (w *worker) pullOCIImage(
420421return nil , errors .New ("k8s environment is not set" )
421422}
422423
424+ log .Info ("Getting OCI credentials from secret" , "SecretNamespace" , secretRef .Namespace , "SecretName" , secretRef .Name )
425+
423426secret , err := w .k8s .CoreV1 ().Secrets (string (secretRef .Namespace )).Get (ctx , string (secretRef .Name ), metav1.GetOptions {})
424427if err != nil {
425428return nil , fmt .Errorf ("failed to get secret: %w" , err )
@@ -428,15 +431,25 @@ func (w *worker) pullOCIImage(
428431return nil , fmt .Errorf ("invalid secret type %q, expected %q" , secret .Type , "kubernetes.io/dockerconfigjson" )
429432}
430433encodedToken := secret .Data [".dockerconfigjson" ]
431- var dockerConfig struct {
432- Auths map [ string ]auth. Credential `json:"auths"`
434+ if len ( encodedToken ) == 0 {
435+ return nil , fmt . Errorf ( "no .dockerconfigjson data found in secret" )
433436}
437+ var dockerConfig credentialprovider.DockerConfigJSON
434438if err := json .Unmarshal (encodedToken , & dockerConfig ); err != nil {
435439return nil , fmt .Errorf ("failed to parse dockerconfigjson: %w" , err )
436440}
437441
438442credsFunc = func (_ context.Context , _ string ) (auth.Credential , error ) {
439- return dockerConfig .Auths [repo .Reference .Registry ], nil
443+ authKey := filepath .Join (repo .Reference .Registry , repo .Reference .Repository )
444+ r , ok := dockerConfig .Auths [authKey ]
445+ if ! ok {
446+ return auth .EmptyCredential , fmt .Errorf ("no credentials found for registry %s" , authKey )
447+ }
448+ log .Info ("Found credentials for registry" , "registry" , authKey , "username" , r .Username )
449+ return auth.Credential {
450+ Username : r .Username ,
451+ Password : r .Password ,
452+ }, nil
440453}
441454} else {
442455// TODO(dilyevsky): Support other kinds of secrets for non-k8s environments.
0 commit comments