@@ -46,37 +46,35 @@ func Pull(ctx context.Context, imageName string, opts ...PullOption) error {
46
46
pullOpts .client = sdk
47
47
}
48
48
49
+ if pullOpts .credentialsFn == nil {
50
+ if err := WithCredentialsFromConfig (pullOpts ); err != nil {
51
+ return fmt .Errorf ("set credentials for pull option: %w" , err )
52
+ }
53
+ }
54
+
49
55
if imageName == "" {
50
56
return errors .New ("image name is not set" )
51
57
}
52
58
53
- authConfigs , err := config . AuthConfigs (imageName )
59
+ username , password , err := pullOpts . credentialsFn (imageName )
54
60
if err != nil {
55
- pullOpts .client .Logger ().Warn ("failed to get image auth, setting empty credentials for the image" , "image" , imageName , "error" , err )
56
- } else {
57
- // there must be only one auth config for the image
58
- if len (authConfigs ) > 1 {
59
- return fmt .Errorf ("multiple auth configs found for image %s, expected only one" , imageName )
60
- }
61
-
62
- var tmp config.AuthConfig
63
- for _ , ac := range authConfigs {
64
- tmp = ac
65
- }
61
+ return fmt .Errorf ("failed to retrieve registry credentials for %s: %w" , imageName , err )
62
+ }
66
63
67
- authConfig := config.AuthConfig {
68
- Username : tmp .Username ,
69
- Password : tmp .Password ,
70
- }
71
- encodedJSON , err := json .Marshal (authConfig )
72
- if err != nil {
73
- pullOpts .client .Logger ().Warn ("failed to marshal image auth, setting empty credentials for the image" , "image" , imageName , "error" , err )
74
- } else {
75
- pullOpts .pullOptions .RegistryAuth = base64 .URLEncoding .EncodeToString (encodedJSON )
76
- }
64
+ authConfig := config.AuthConfig {
65
+ Username : username ,
66
+ Password : password ,
67
+ }
68
+ encodedJSON , err := json .Marshal (authConfig )
69
+ if err != nil {
70
+ pullOpts .client .Logger ().Warn ("failed to marshal image auth, setting empty credentials for the image" , "image" , imageName , "error" , err )
71
+ } else {
72
+ pullOpts .pullOptions .RegistryAuth = base64 .URLEncoding .EncodeToString (encodedJSON )
77
73
}
78
74
79
- var pull io.ReadCloser
75
+ var (
76
+ pull io.ReadCloser
77
+ )
80
78
err = backoff .RetryNotify (
81
79
func () error {
82
80
pull , err = pullOpts .client .ImagePull (ctx , imageName , pullOpts .pullOptions )
0 commit comments