Move Image.getLocalImage to Runtime.getLocalImage

Instead of the function updating image.InputName (the only reason for it
to need an image), have it return the updated value separately.

This will allow simplifying the constructors of Image further.

Should not change behavior.

Signed-off-by: Miloslav Trmač <mitr@redhat.com>
This commit is contained in:
Miloslav Trmač 2020-02-07 02:16:34 +01:00
parent 1ab4038807
commit 933edf4a37
1 changed files with 25 additions and 26 deletions

View File

@ -125,16 +125,15 @@ func (ir *Runtime) newFromStorage(img *storage.Image) *Image {
// to only deal with local images already in the store (or
// its aliases)
func (ir *Runtime) NewFromLocal(name string) (*Image, error) {
image := Image{
InputName: name,
imageruntime: ir,
}
localImage, err := image.getLocalImage()
updatedInputName, localImage, err := ir.getLocalImage(name)
if err != nil {
return nil, err
}
image.image = localImage
return &image, nil
return &Image{
InputName: updatedInputName,
imageruntime: ir,
image: localImage,
}, nil
}
// New creates a new image object where the image could be local
@ -232,60 +231,60 @@ func stripSha256(name string) string {
}
// getLocalImage resolves an unknown input describing an image and
// returns a storage.Image or an error. It is used by NewFromLocal.
func (i *Image) getLocalImage() (*storage.Image, error) {
imageError := fmt.Sprintf("unable to find '%s' in local storage", i.InputName)
if i.InputName == "" {
return nil, errors.Errorf("input name is blank")
// returns an updated input name, and a storage.Image, or an error. It is used by NewFromLocal.
func (ir *Runtime) getLocalImage(inputName string) (string, *storage.Image, error) {
imageError := fmt.Sprintf("unable to find '%s' in local storage", inputName)
if inputName == "" {
return "", nil, errors.Errorf("input name is blank")
}
// Check if the input name has a transport and if so strip it
dest, err := alltransports.ParseImageName(i.InputName)
dest, err := alltransports.ParseImageName(inputName)
if err == nil && dest.DockerReference() != nil {
i.InputName = dest.DockerReference().String()
inputName = dest.DockerReference().String()
}
img, err := i.imageruntime.getImage(stripSha256(i.InputName))
img, err := ir.getImage(stripSha256(inputName))
if err == nil {
return img.image, err
return inputName, img.image, err
}
// container-storage wasn't able to find it in its current form
// check if the input name has a tag, and if not, run it through
// again
decomposedImage, err := decompose(i.InputName)
decomposedImage, err := decompose(inputName)
if err != nil {
return nil, err
return "", nil, err
}
// The image has a registry name in it and we made sure we looked for it locally
// with a tag. It cannot be local.
if decomposedImage.hasRegistry {
return nil, errors.Wrapf(ErrNoSuchImage, imageError)
return "", nil, errors.Wrapf(ErrNoSuchImage, imageError)
}
// if the image is saved with the repository localhost, searching with localhost prepended is necessary
// We don't need to strip the sha because we have already determined it is not an ID
ref, err := decomposedImage.referenceWithRegistry(DefaultLocalRegistry)
if err != nil {
return nil, err
return "", nil, err
}
img, err = i.imageruntime.getImage(ref.String())
img, err = ir.getImage(ref.String())
if err == nil {
return img.image, err
return inputName, img.image, err
}
// grab all the local images
images, err := i.imageruntime.GetImages()
images, err := ir.GetImages()
if err != nil {
return nil, err
return "", nil, err
}
// check the repotags of all images for a match
repoImage, err := findImageInRepotags(decomposedImage, images)
if err == nil {
return repoImage, nil
return inputName, repoImage, nil
}
return nil, errors.Wrapf(ErrNoSuchImage, err.Error())
return "", nil, errors.Wrapf(ErrNoSuchImage, err.Error())
}
// ID returns the image ID as a string