Skip to content

Commit 8a75274

Browse files
authored
Merge pull request crossplane#6257 from cychiang/6235-support-pull-image-from-private-registry-with-podman
feat(render): Add support to read config file for Podman
2 parents d8be746 + 0bbed02 commit 8a75274

File tree

4 files changed

+19
-83
lines changed

4 files changed

+19
-83
lines changed

cmd/crank/render/runtime_docker.go

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,19 @@ package render
1818

1919
import (
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

303297
func (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

cmd/crank/render/runtime_docker_test.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@ package render
1919
import (
2020
"context"
2121
"io"
22-
"os"
2322
"testing"
2423

25-
"github.com/docker/cli/cli/config"
2624
"github.com/docker/docker/api/types/image"
2725
"github.com/google/go-cmp/cmp"
2826
"github.com/google/go-cmp/cmp/cmpopts"
@@ -78,7 +76,6 @@ func TestGetRuntimeDocker(t *testing.T) {
7876
want: want{
7977
rd: &RuntimeDocker{
8078
Image: "test-image-from-annotation",
81-
ConfigFile: config.LoadDefaultConfigFile(os.Stderr),
8279
Cleanup: AnnotationValueRuntimeDockerCleanupOrphan,
8380
PullPolicy: AnnotationValueRuntimeDockerPullPolicyAlways,
8481
},
@@ -105,7 +102,6 @@ func TestGetRuntimeDocker(t *testing.T) {
105102
want: want{
106103
rd: &RuntimeDocker{
107104
Image: "test-image-from-annotation",
108-
ConfigFile: config.LoadDefaultConfigFile(os.Stderr),
109105
Cleanup: AnnotationValueRuntimeDockerCleanupOrphan,
110106
Name: "test-container-name-function",
111107
PullPolicy: AnnotationValueRuntimeDockerPullPolicyIfNotPresent,
@@ -129,7 +125,6 @@ func TestGetRuntimeDocker(t *testing.T) {
129125
want: want{
130126
rd: &RuntimeDocker{
131127
Image: "test-package",
132-
ConfigFile: config.LoadDefaultConfigFile(os.Stderr),
133128
Cleanup: AnnotationValueRuntimeDockerCleanupRemove,
134129
PullPolicy: AnnotationValueRuntimeDockerPullPolicyIfNotPresent,
135130
},
@@ -194,7 +189,6 @@ func TestGetRuntimeDocker(t *testing.T) {
194189
want: want{
195190
rd: &RuntimeDocker{
196191
Image: "test-package",
197-
ConfigFile: config.LoadDefaultConfigFile(os.Stderr),
198192
Cleanup: AnnotationValueRuntimeDockerCleanupStop,
199193
PullPolicy: AnnotationValueRuntimeDockerPullPolicyIfNotPresent,
200194
},
@@ -204,7 +198,7 @@ func TestGetRuntimeDocker(t *testing.T) {
204198
for name, tc := range cases {
205199
t.Run(name, func(t *testing.T) {
206200
rd, err := GetRuntimeDocker(tc.args.fn, logging.NewNopLogger())
207-
if diff := cmp.Diff(tc.want.rd, rd, cmpopts.IgnoreUnexported(RuntimeDocker{})); diff != "" {
201+
if diff := cmp.Diff(tc.want.rd, rd, cmpopts.IgnoreUnexported(RuntimeDocker{}), cmpopts.IgnoreFields(RuntimeDocker{}, "Keychain")); diff != "" {
208202
t.Errorf("\n%s\nGetRuntimeDocker(...): -want, +got:\n%s", tc.reason, diff)
209203
}
210204
if diff := cmp.Diff(tc.want.err, err, cmpopts.EquateErrors()); diff != "" {

go.mod

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ require (
88
github.com/Masterminds/semver v1.5.0
99
github.com/alecthomas/kong v0.9.0
1010
github.com/crossplane/crossplane-runtime v1.19.0-rc.0.0.20241105071456-19d95a69cc03
11-
github.com/distribution/reference v0.5.0
1211
github.com/docker/docker v27.1.1+incompatible
1312
github.com/docker/go-connections v0.5.0
1413
github.com/emicklei/dot v1.6.2
@@ -64,8 +63,7 @@ require (
6463
github.com/cyphar/filepath-securejoin v0.2.5 // indirect
6564
github.com/digitorus/pkcs7 v0.0.0-20230818184609-3a137a874352 // indirect
6665
github.com/digitorus/timestamp v0.0.0-20231217203849-220c5c2851b7 // indirect
67-
github.com/docker/go-metrics v0.0.1 // indirect
68-
github.com/docker/libtrust v0.0.0-20160708172513-aabc10ec26b7 // indirect
66+
github.com/distribution/reference v0.5.0 // indirect
6967
github.com/dustin/go-humanize v1.0.1 // indirect
7068
github.com/emirpasic/gods v1.18.1 // indirect
7169
github.com/exponent-io/jsonpath v0.0.0-20151013193312-d6023ce2651d // indirect
@@ -89,7 +87,6 @@ require (
8987
github.com/google/certificate-transparency-go v1.2.1 // indirect
9088
github.com/google/gnostic-models v0.6.9-0.20230804172637-c7be7c783f49 // indirect
9189
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
92-
github.com/gorilla/mux v1.8.1 // indirect
9390
github.com/gorilla/websocket v1.5.1 // indirect
9491
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 // indirect
9592
github.com/hashicorp/go-retryablehttp v0.7.7 // indirect
@@ -192,7 +189,7 @@ require (
192189
github.com/dave/jennifer v1.6.0 // indirect
193190
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
194191
github.com/dimchansky/utfbom v1.1.1 // indirect
195-
github.com/docker/cli v27.4.1+incompatible
192+
github.com/docker/cli v27.4.1+incompatible // indirect
196193
github.com/docker/distribution v2.8.3+incompatible // indirect
197194
github.com/docker/docker-credential-helpers v0.8.2
198195
github.com/docker/go-units v0.5.0 // indirect

0 commit comments

Comments
 (0)