Introduce struct pullGoal

The eventual goal is to cleanly capture semantics like "pull all images
for DockerArchive" and "did a search through $registries" without
hard-coding it through; and to allow a pullImage variant where
the caller can pass an imageReference directly.

For now, this just wraps []pullRefPair and should not change behavior.

Signed-off-by: Miloslav Trmač <mitr@redhat.com>

Closes: #1176
Approved by: rhatdan
This commit is contained in:
Miloslav Trmač 2018-07-28 05:36:47 +02:00 committed by Atomic Bot
parent bf0ab88eac
commit fadb143399
1 changed files with 22 additions and 17 deletions

View File

@ -55,6 +55,11 @@ type pullRefPair struct {
dstRef types.ImageReference dstRef types.ImageReference
} }
// pullGoal represents the prepared image references and decided behavior to be executed by imagePull
type pullGoal struct {
refPairs []pullRefPair
}
// pullRefName records a prepared source reference and a destination name to try to pull (if not DockerArchive) or to pull all (if DockerArchive) // pullRefName records a prepared source reference and a destination name to try to pull (if not DockerArchive) or to pull all (if DockerArchive)
type pullRefName struct { type pullRefName struct {
image string image string
@ -164,14 +169,14 @@ func refNamesFromImageReference(ctx context.Context, srcRef types.ImageReference
} }
} }
// refPairsFromImageReference returns a list of pullRefPair for a single ImageReference, depending on the used transport. // pullGoalFromImageReference returns a pull goal for a single ImageReference, depending on the used transport.
func (ir *Runtime) refPairsFromImageReference(ctx context.Context, srcRef types.ImageReference, imgName string, sc *types.SystemContext) ([]pullRefPair, error) { func (ir *Runtime) pullGoalFromImageReference(ctx context.Context, srcRef types.ImageReference, imgName string, sc *types.SystemContext) (pullGoal, error) {
refNames, err := refNamesFromImageReference(ctx, srcRef, imgName, sc) refNames, err := refNamesFromImageReference(ctx, srcRef, imgName, sc)
if err != nil { if err != nil {
return nil, err return pullGoal{}, err
} }
return ir.pullRefPairsFromRefNames(refNames) return ir.pullGoalFromRefNames(refNames)
} }
// pullImage pulls an image from configured registries // pullImage pulls an image from configured registries
@ -179,19 +184,19 @@ func (ir *Runtime) refPairsFromImageReference(ctx context.Context, srcRef types.
// pulled. // pulled.
func (i *Image) pullImage(ctx context.Context, writer io.Writer, authfile, signaturePolicyPath string, signingOptions SigningOptions, dockerOptions *DockerRegistryOptions, forceSecure bool) ([]string, error) { func (i *Image) pullImage(ctx context.Context, writer io.Writer, authfile, signaturePolicyPath string, signingOptions SigningOptions, dockerOptions *DockerRegistryOptions, forceSecure bool) ([]string, error) {
// pullImage copies the image from the source to the destination // pullImage copies the image from the source to the destination
var pullRefPairs []pullRefPair var goal pullGoal
sc := GetSystemContext(signaturePolicyPath, authfile, false) sc := GetSystemContext(signaturePolicyPath, authfile, false)
srcRef, err := alltransports.ParseImageName(i.InputName) srcRef, err := alltransports.ParseImageName(i.InputName)
if err != nil { if err != nil {
// could be trying to pull from registry with short name // could be trying to pull from registry with short name
pullRefPairs, err = i.refPairsFromPossiblyUnqualifiedName() goal, err = i.pullGoalFromPossiblyUnqualifiedName()
if err != nil { if err != nil {
return nil, errors.Wrap(err, "error getting default registries to try") return nil, errors.Wrap(err, "error getting default registries to try")
} }
} else { } else {
pullRefPairs, err = i.imageruntime.refPairsFromImageReference(ctx, srcRef, i.InputName, sc) goal, err = i.imageruntime.pullGoalFromImageReference(ctx, srcRef, i.InputName, sc)
if err != nil { if err != nil {
return nil, errors.Wrapf(err, "error getting pullRefPair info to pull image %q", i.InputName) return nil, errors.Wrapf(err, "error determining pull goal for image %q", i.InputName)
} }
} }
policyContext, err := getPolicyContext(sc) policyContext, err := getPolicyContext(sc)
@ -205,7 +210,7 @@ func (i *Image) pullImage(ctx context.Context, writer io.Writer, authfile, signa
return nil, err return nil, err
} }
var images []string var images []string
for _, imageInfo := range pullRefPairs { for _, imageInfo := range goal.refPairs {
copyOptions := getCopyOptions(writer, signaturePolicyPath, dockerOptions, nil, signingOptions, authfile, "", false, nil) copyOptions := getCopyOptions(writer, signaturePolicyPath, dockerOptions, nil, signingOptions, authfile, "", false, nil)
if imageInfo.srcRef.Transport().Name() == DockerTransport { if imageInfo.srcRef.Transport().Name() == DockerTransport {
imgRef := imageInfo.srcRef.DockerReference() imgRef := imageInfo.srcRef.DockerReference()
@ -315,24 +320,24 @@ func refNamesFromPossiblyUnqualifiedName(inputName string) ([]pullRefName, error
return pullNames, nil return pullNames, nil
} }
// refPairsFromPossiblyUnqualifiedName looks at a decomposed image and determines the possible // pullGoalFromPossiblyUnqualifiedName looks at a decomposed image and determines the possible
// image references to try pulling in combination with the registries.conf file as well // image references to try pulling in combination with the registries.conf file as well
func (i *Image) refPairsFromPossiblyUnqualifiedName() ([]pullRefPair, error) { func (i *Image) pullGoalFromPossiblyUnqualifiedName() (pullGoal, error) {
refNames, err := refNamesFromPossiblyUnqualifiedName(i.InputName) refNames, err := refNamesFromPossiblyUnqualifiedName(i.InputName)
if err != nil { if err != nil {
return nil, err return pullGoal{}, err
} }
return i.imageruntime.pullRefPairsFromRefNames(refNames) return i.imageruntime.pullGoalFromRefNames(refNames)
} }
// pullRefPairsFromNames converts a []pullRefName to []pullRefPair // pullRefPairsFromNames converts a []pullRefName to a pullGoal
func (ir *Runtime) pullRefPairsFromRefNames(refNames []pullRefName) ([]pullRefPair, error) { func (ir *Runtime) pullGoalFromRefNames(refNames []pullRefName) (pullGoal, error) {
// Here we construct the destination references // Here we construct the destination references
res := make([]pullRefPair, len(refNames)) res := make([]pullRefPair, len(refNames))
for i, rn := range refNames { for i, rn := range refNames {
destRef, err := is.Transport.ParseStoreReference(ir.store, rn.dstName) destRef, err := is.Transport.ParseStoreReference(ir.store, rn.dstName)
if err != nil { if err != nil {
return nil, errors.Wrapf(err, "error parsing dest reference name %#v", rn.dstName) return pullGoal{}, errors.Wrapf(err, "error parsing dest reference name %#v", rn.dstName)
} }
res[i] = pullRefPair{ res[i] = pullRefPair{
image: rn.image, image: rn.image,
@ -340,5 +345,5 @@ func (ir *Runtime) pullRefPairsFromRefNames(refNames []pullRefName) ([]pullRefPa
dstRef: destRef, dstRef: destRef,
} }
} }
return res, nil return pullGoal{refPairs: res}, nil
} }