Skip to content

Commit

Permalink
Use a types.ImageReference instead of (transport, name) strings in pu…
Browse files Browse the repository at this point in the history
…llImage etc.

Use a typed value, to hopefully decrease further temptation to process strings
manually, and to avoid the unnecessary alltransports.ParseImageName which
resolveImage has already called.

This may change the strings used in some error/debug messages, which
now use transports.ImageName instead of the original input; the strings
should by definition have the same semantics.

Signed-off-by: Miloslav Trmač <[email protected]>

Closes: #1361
Approved by: rhatdan
  • Loading branch information
mtrmac authored and rh-atomic-bot committed Feb 27, 2019
1 parent f5f2cb1 commit 5946d06
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 24 deletions.
12 changes: 6 additions & 6 deletions new.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ const (
BaseImageFakeName = imagebuilder.NoBaseImageSpecifier
)

func pullAndFindImage(ctx context.Context, store storage.Store, transport string, imageName string, options BuilderOptions, sc *types.SystemContext) (*storage.Image, types.ImageReference, error) {
func pullAndFindImage(ctx context.Context, store storage.Store, srcRef types.ImageReference, options BuilderOptions, sc *types.SystemContext) (*storage.Image, types.ImageReference, error) {
pullOptions := PullOptions{
ReportWriter: options.ReportWriter,
Store: store,
SystemContext: options.SystemContext,
BlobDirectory: options.PullBlobDirectory,
}
ref, err := pullImage(ctx, store, transport, imageName, pullOptions, sc)
ref, err := pullImage(ctx, store, srcRef, pullOptions, sc)
if err != nil {
logrus.Debugf("error pulling image %q: %v", imageName, err)
logrus.Debugf("error pulling image %q: %v", transports.ImageName(srcRef), err)
return nil, nil, err
}
img, err := is.Transport.GetStoreImage(store, ref)
if err != nil {
logrus.Debugf("error reading pulled image %q: %v", imageName, err)
logrus.Debugf("error reading pulled image %q: %v", transports.ImageName(srcRef), err)
return nil, nil, errors.Wrapf(err, "error locating image %q in local storage", transports.ImageName(ref))
}
return img, ref, nil
Expand Down Expand Up @@ -137,7 +137,7 @@ func resolveImage(ctx context.Context, systemContext *types.SystemContext, store
}

if options.PullPolicy == PullAlways {
pulledImg, pulledReference, err := pullAndFindImage(ctx, store, transport, image, options, systemContext)
pulledImg, pulledReference, err := pullAndFindImage(ctx, store, srcRef, options, systemContext)
if err != nil {
logrus.Debugf("unable to pull and read image %q: %v", image, err)
failures = append(failures, failure{resolvedImageName: image, err: err})
Expand Down Expand Up @@ -172,7 +172,7 @@ func resolveImage(ctx context.Context, systemContext *types.SystemContext, store
continue
}

pulledImg, pulledReference, err := pullAndFindImage(ctx, store, transport, image, options, systemContext)
pulledImg, pulledReference, err := pullAndFindImage(ctx, store, srcRef, options, systemContext)
if err != nil {
logrus.Debugf("unable to pull and read image %q: %v", image, err)
failures = append(failures, failure{resolvedImageName: image, err: err})
Expand Down
26 changes: 8 additions & 18 deletions pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,14 @@ func Pull(ctx context.Context, imageName string, options PullOptions) error {
errs = multierror.Append(errs, err)
continue
}
taggedRef, err := docker.NewReference(tagged)
if err != nil {
return errors.Wrapf(err, "internal error creating docker.Transport reference for %s", tagged.String())
}
if options.ReportWriter != nil {
options.ReportWriter.Write([]byte("Pulling " + tagged.String() + "\n"))
}
ref, err := pullImage(ctx, options.Store, transport, tagged.String(), options, systemContext)
ref, err := pullImage(ctx, options.Store, taggedRef, options, systemContext)
if err != nil {
errs = multierror.Append(errs, err)
continue
Expand All @@ -212,21 +216,7 @@ func Pull(ctx context.Context, imageName string, options PullOptions) error {
return errs.ErrorOrNil()
}

func pullImage(ctx context.Context, store storage.Store, transport string, imageName string, options PullOptions, sc *types.SystemContext) (types.ImageReference, error) {
if transport == "" {
transport = util.DefaultTransport
} else {
if transport != util.DefaultTransport {
transport = transport + ":"
}
}
spec := transport + imageName
srcRef, err := alltransports.ParseImageName(spec)
if err != nil {
return nil, errors.Wrapf(err, "error parsing image name %q", spec)
}
logrus.Debugf("parsed image name %q", spec)

func pullImage(ctx context.Context, store storage.Store, srcRef types.ImageReference, options PullOptions, sc *types.SystemContext) (types.ImageReference, error) {
blocked, err := isReferenceBlocked(srcRef, sc)
if err != nil {
return nil, errors.Wrapf(err, "error checking if pulling from registry for %q is blocked", transports.ImageName(srcRef))
Expand Down Expand Up @@ -272,9 +262,9 @@ func pullImage(ctx context.Context, store storage.Store, transport string, image
}
}()

logrus.Debugf("copying %q to %q", spec, destName)
logrus.Debugf("copying %q to %q", transports.ImageName(srcRef), destName)
if _, err := cp.Image(ctx, policyContext, maybeCachedDestRef, srcRef, getCopyOptions(options.ReportWriter, srcRef, sc, maybeCachedDestRef, nil, "")); err != nil {
logrus.Debugf("error copying src image [%q] to dest image [%q] err: %v", spec, destName, err)
logrus.Debugf("error copying src image [%q] to dest image [%q] err: %v", transports.ImageName(srcRef), destName, err)
return nil, err
}
return destRef, nil
Expand Down

0 comments on commit 5946d06

Please sign in to comment.