Skip to content

Commit 2597d9b

Browse files
committed
Instrument remote image pulling
1 parent 9010cd3 commit 2597d9b

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

artifact_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func TestArtifact(t *testing.T) {
1818
log.SetLevel(logrus.DebugLevel)
1919

2020
digest := "alpine@sha256:60eda2a7bc29a54fe6beae0d72312ea995eb3b8387535e8dbf6767fd1b765d34" // linux/amd64 digest
21-
img, err := image.NewFromRemote(ctx, digest, types.ImageOptions{})
21+
img, err := image.NewFromRemote(ctx, log, digest, types.ImageOptions{})
2222
r.NoError(err)
2323

2424
artifact, err := NewArtifact(img, log, mockBlockCache{}, ArtifactOption{

image/remote.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/google/go-containerregistry/pkg/name"
1414
v1 "github.com/google/go-containerregistry/pkg/v1"
1515
"github.com/google/go-containerregistry/pkg/v1/remote"
16+
"github.com/sirupsen/logrus"
1617
)
1718

1819
type DockerConfig struct {
@@ -25,7 +26,12 @@ type RegistryAuth struct {
2526
Token string `json:"auth"`
2627
}
2728

28-
func NewFromRemote(ctx context.Context, imageName string, option types.ImageOptions) (ImageWithIndex, error) {
29+
func NewFromRemote(
30+
ctx context.Context,
31+
log logrus.FieldLogger,
32+
imageName string,
33+
option types.ImageOptions,
34+
) (ImageWithIndex, error) {
2935
var nameOpts []name.Option
3036
if option.RegistryOptions.Insecure {
3137
nameOpts = append(nameOpts, name.Insecure)
@@ -35,14 +41,20 @@ func NewFromRemote(ctx context.Context, imageName string, option types.ImageOpti
3541
return nil, fmt.Errorf("failed to parse the image name: %w", err)
3642
}
3743

38-
img, err := tryRemote(ctx, imageName, ref, option)
44+
img, err := tryRemote(ctx, log, imageName, ref, option)
3945
if err != nil {
4046
return nil, err
4147
}
4248
return img, nil
4349
}
4450

45-
func tryRemote(ctx context.Context, imageName string, ref name.Reference, option types.ImageOptions) (ImageWithIndex, error) {
51+
func tryRemote(
52+
ctx context.Context,
53+
log logrus.FieldLogger,
54+
imageName string,
55+
ref name.Reference,
56+
option types.ImageOptions,
57+
) (ImageWithIndex, error) {
4658
remoteOpts := []remote.Option{
4759
remote.WithContext(ctx),
4860
}
@@ -55,20 +67,26 @@ func tryRemote(ctx context.Context, imageName string, ref name.Reference, option
5567

5668
// Username/Password based auth.
5769
if len(option.RegistryOptions.Credentials) > 0 {
70+
log.Info("using basic authentication to pull an image")
5871
for _, cred := range option.RegistryOptions.Credentials {
5972
remoteOpts = append(remoteOpts, remote.WithAuth(&authn.Basic{
6073
Username: cred.Username,
6174
Password: cred.Password,
6275
}))
6376
}
6477
} else {
78+
log.Info("using other authentication to pull an image")
6579
domain := ref.Context().RegistryStr()
6680
auth := registry.GetToken(ctx, domain, option.RegistryOptions)
6781
if auth.Username != "" && auth.Password != "" {
82+
log.Info("using cloud provider registry token to pull an image")
6883
remoteOpts = append(remoteOpts, remote.WithAuth(&auth))
6984
} else if option.RegistryOptions.RegistryToken != "" {
85+
log.Info("using bearer token to pull an image")
7086
bearer := authn.Bearer{Token: option.RegistryOptions.RegistryToken}
7187
remoteOpts = append(remoteOpts, remote.WithAuth(&bearer))
88+
} else {
89+
log.Info("not using authentication to pull an image after all")
7290
}
7391
}
7492

0 commit comments

Comments
 (0)