@@ -18,22 +18,19 @@ package render
1818
1919import (
2020 "context"
21+ "encoding/base64"
2122 "fmt"
2223 "io"
2324 "net"
24- "os"
2525
26- "github.com/distribution/reference"
27- "github.com/docker/cli/cli/config"
28- "github.com/docker/cli/cli/config/configfile"
2926 "github.com/docker/docker/api/types/container"
3027 "github.com/docker/docker/api/types/filters"
3128 typesimage "github.com/docker/docker/api/types/image"
32- registrytypes "github.com/docker/docker/api/types/registry"
3329 "github.com/docker/docker/client"
3430 "github.com/docker/docker/errdefs"
35- "github.com/docker/docker/registry"
3631 "github.com/docker/go-connections/nat"
32+ "github.com/google/go-containerregistry/pkg/authn"
33+ "github.com/google/go-containerregistry/pkg/name"
3734
3835 "github.com/crossplane/crossplane-runtime/pkg/errors"
3936 "github.com/crossplane/crossplane-runtime/pkg/logging"
@@ -112,12 +109,12 @@ type RuntimeDocker struct {
112109 // Cleanup controls how the containers are handled after rendering.
113110 Cleanup DockerCleanup
114111
115- // ConfigFile contains information like credentials for each registry, default to ~/.docker/config.json
116- ConfigFile * configfile.ConfigFile
117-
118112 // PullPolicy controls how the runtime image is pulled.
119113 PullPolicy DockerPullPolicy
120114
115+ // Keychain to use for pulling images from private registry.
116+ Keychain authn.Keychain
117+
121118 // log is the logger for this runtime.
122119 log logging.Logger
123120}
@@ -162,15 +159,12 @@ func GetRuntimeDocker(fn pkgv1.Function, log logging.Logger) (*RuntimeDocker, er
162159 return nil , errors .Wrapf (err , "cannot get pull policy for Function %q" , fn .GetName ())
163160 }
164161
165- // Initial ConfigFile
166- configFile := config .LoadDefaultConfigFile (os .Stderr )
167-
168162 r := & RuntimeDocker {
169163 Image : fn .Spec .Package ,
170164 Name : "" ,
171165 Cleanup : cleanup ,
172- ConfigFile : configFile ,
173166 PullPolicy : pullPolicy ,
167+ Keychain : authn .DefaultKeychain ,
174168 log : log ,
175169 }
176170
@@ -301,33 +295,29 @@ func (r *RuntimeDocker) createContainer(ctx context.Context, cli *client.Client)
301295}
302296
303297func (r * RuntimeDocker ) getPullOptions () (typesimage.PullOptions , error ) {
304- // Resolve auth token by looking into config file
305- named , err := reference . ParseNormalizedNamed (r .Image )
298+ // Resolve auth token by looking into keychain
299+ ref , err := name . ParseReference (r .Image )
306300 if err != nil {
307301 return typesimage.PullOptions {}, errors .Wrapf (err , "Image is not a valid reference %s" , r .Image )
308302 }
309303
310- repoInfo , err := registry . ParseRepositoryInfo ( named )
304+ auth , err := r . Keychain . Resolve ( ref . Context (). Registry )
311305 if err != nil {
312- return typesimage.PullOptions {}, errors .Wrapf (err , "Cannot parse repository info: %s" , named . String ())
306+ return typesimage.PullOptions {}, errors .Wrapf (err , "Cannot resolve auth for %s" , ref . Context (). RegistryStr ())
313307 }
314308
315- configKey := repoInfo .Index .Name
316- if repoInfo .Index .Official {
317- configKey = registry .IndexServer
318- }
319- authConfig , err := r .ConfigFile .GetAuthConfig (configKey )
309+ authConfig , err := auth .Authorization ()
320310 if err != nil {
321- return typesimage.PullOptions {}, errors .Wrapf (err , "Cannot get auth config info with configKey: %s" , configKey )
311+ return typesimage.PullOptions {}, errors .Wrapf (err , "Cannot get auth config for %s" , ref . Context (). RegistryStr () )
322312 }
323313
324- encodedAuth , err := registrytypes . EncodeAuthConfig ( registrytypes . AuthConfig ( authConfig ) )
314+ token , err := authConfig . MarshalJSON ( )
325315 if err != nil {
326- return typesimage.PullOptions {}, errors .Wrapf (err , "Cannot encode auth config with configKey: %s" , configKey )
316+ return typesimage.PullOptions {}, errors .Wrapf (err , "Cannot marshal auth config for %s" , ref . Context (). RegistryStr () )
327317 }
328318
329319 return typesimage.PullOptions {
330- RegistryAuth : encodedAuth ,
320+ RegistryAuth : base64 . URLEncoding . EncodeToString ( token ) ,
331321 }, nil
332322}
333323
0 commit comments