@@ -54,37 +54,39 @@ func Pull(ctx context.Context, imageName string, opts ...PullOption) error {
5454 defer pullOpts .pullClient .Close ()
5555 }
5656
57+ if pullOpts .credentialsFn == nil {
58+ if err := WithCredentialsFromConfig ()(pullOpts ); err != nil {
59+ return fmt .Errorf ("set credentials for pull option: %w" , err )
60+ }
61+ }
62+
5763 if imageName == "" {
5864 return errors .New ("image name is not set" )
5965 }
6066
61- authConfigs , err := config .AuthConfigs (imageName )
62- if err != nil {
63- pullOpts .pullClient .Logger ().Warn ("failed to get image auth, setting empty credentials for the image" , "image" , imageName , "error" , err )
64- } else {
65- // there must be only one auth config for the image
66- if len (authConfigs ) > 1 {
67- return fmt .Errorf ("multiple auth configs found for image %s, expected only one" , imageName )
68- }
69-
70- var tmp config.AuthConfig
71- for _ , ac := range authConfigs {
72- tmp = ac
73- }
74-
75- authConfig := config.AuthConfig {
76- Username : tmp .Username ,
77- Password : tmp .Password ,
78- }
79- encodedJSON , err := json .Marshal (authConfig )
67+ if pullOpts .credentialsFn == nil {
68+ username , password , err := pullOpts .credentialsFn (imageName )
8069 if err != nil {
81- pullOpts .pullClient .Logger ().Warn ("failed to marshal image auth, setting empty credentials for the image" , "image" , imageName , "error" , err )
82- } else {
83- pullOpts .pullOptions .RegistryAuth = base64 .URLEncoding .EncodeToString (encodedJSON )
70+ return fmt .Errorf ("failed to retrieve credentials for image %s: %w" , imageName , err )
71+ }
72+ if username != "" && password != "" {
73+ authConfig := config.AuthConfig {
74+ Username : username ,
75+ Password : password ,
76+ }
77+ encodedJSON , err := json .Marshal (authConfig )
78+ if err != nil {
79+ pullOpts .pullClient .Logger ().Warn ("failed to marshal image auth, setting empty credentials for the image" , "image" , imageName , "error" , err )
80+ } else {
81+ pullOpts .pullOptions .RegistryAuth = base64 .URLEncoding .EncodeToString (encodedJSON )
82+ }
8483 }
8584 }
8685
87- var pull io.ReadCloser
86+ var (
87+ pull io.ReadCloser
88+ err error
89+ )
8890 err = backoff .RetryNotify (
8991 func () error {
9092 pull , err = pullOpts .pullClient .ImagePull (ctx , imageName , pullOpts .pullOptions )
0 commit comments