Fix ambiguity in adding localhost to podman save

...and some naming decisions.

This change ensures podman save doesn't incorrectly prepend localhost when saving an image.

Signed-off-by: haircommander <pehunt@redhat.com>

Closes: #1140
Approved by: rhatdan
This commit is contained in:
haircommander
2018-07-23 12:56:24 -04:00
committed by Atomic Bot
parent 9bd991f477
commit 879453eaf1
4 changed files with 48 additions and 20 deletions

View File

@ -145,7 +145,6 @@ func saveCmd(c *cli.Context) error {
return err return err
} }
} }
if err := newImage.PushImageToReference(getContext(), destRef, manifestType, "", "", writer, c.Bool("compress"), libpodImage.SigningOptions{}, &libpodImage.DockerRegistryOptions{}, false, additionaltags); err != nil { if err := newImage.PushImageToReference(getContext(), destRef, manifestType, "", "", writer, c.Bool("compress"), libpodImage.SigningOptions{}, &libpodImage.DockerRegistryOptions{}, false, additionaltags); err != nil {
if err2 := os.Remove(output); err2 != nil { if err2 := os.Remove(output); err2 != nil {
logrus.Errorf("error deleting %q: %v", output, err) logrus.Errorf("error deleting %q: %v", output, err)
@ -165,12 +164,15 @@ func imageNameForSaveDestination(img *libpodImage.Image, imgUserInput string) st
} }
prepend := "" prepend := ""
if !strings.Contains(imgUserInput, libpodImage.DefaultLocalRepo) { localRegistryPrefix := fmt.Sprintf("%s/", libpodImage.DefaultLocalRegistry)
if !strings.HasPrefix(imgUserInput, localRegistryPrefix) {
// we need to check if localhost was added to the image name in NewFromLocal // we need to check if localhost was added to the image name in NewFromLocal
for _, name := range img.Names() { for _, name := range img.Names() {
// if the user searched for the image whose tag was prepended with localhost, we'll need to prepend localhost to successfully search // If the user is saving an image in the localhost registry, getLocalImage need
if strings.Contains(name, libpodImage.DefaultLocalRepo) && strings.Contains(name, imgUserInput) { // a name that matches the format localhost/<tag1>:<tag2> or localhost/<tag>:latest to correctly
prepend = fmt.Sprintf("%s/", libpodImage.DefaultLocalRepo) // set up the manifest and save.
if strings.HasPrefix(name, localRegistryPrefix) && (strings.HasSuffix(name, imgUserInput) || strings.HasSuffix(name, fmt.Sprintf("%s:latest", imgUserInput))) {
prepend = localRegistryPrefix
break break
} }
} }

View File

@ -258,7 +258,7 @@ func (i *Image) getLocalImage() (*storage.Image, error) {
} }
// if the image is saved with the repository localhost, searching with localhost prepended is necessary // 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 // We don't need to strip the sha because we have already determined it is not an ID
img, err = i.imageruntime.getImage(DefaultLocalRepo + "/" + i.InputName) img, err = i.imageruntime.getImage(fmt.Sprintf("%s/%s", DefaultLocalRegistry, i.InputName))
if err == nil { if err == nil {
return img.image, err return img.image, err
} }
@ -465,7 +465,7 @@ func normalizeTag(tag string) (string, error) {
} }
// If the input doesn't specify a registry, set the registry to localhost // If the input doesn't specify a registry, set the registry to localhost
if !decomposedTag.hasRegistry { if !decomposedTag.hasRegistry {
tag = fmt.Sprintf("%s/%s", DefaultLocalRepo, tag) tag = fmt.Sprintf("%s/%s", DefaultLocalRegistry, tag)
} }
return tag, nil return tag, nil
} }

View File

@ -43,9 +43,9 @@ var (
// and because syntaxes of image names are transport-dependent, the prefix is not really interchangeable; // and because syntaxes of image names are transport-dependent, the prefix is not really interchangeable;
// each user implicitly assumes the appended string is a Docker-like reference. // each user implicitly assumes the appended string is a Docker-like reference.
DefaultTransport = DockerTransport + "://" DefaultTransport = DockerTransport + "://"
// DefaultLocalRepo is the default local repository for local image operations // DefaultLocalRegistry is the default local registry for local image operations
// Remote pulls will still use defined registries // Remote pulls will still use defined registries
DefaultLocalRepo = "localhost" DefaultLocalRegistry = "localhost"
) )
// pullRefPair records a pair of prepared image references to pull. // pullRefPair records a pair of prepared image references to pull.
@ -74,12 +74,12 @@ func singlePullRefPairGoal(rp pullRefPair) *pullGoal {
} }
func (ir *Runtime) getPullRefPair(srcRef types.ImageReference, destName string) (pullRefPair, error) { func (ir *Runtime) getPullRefPair(srcRef types.ImageReference, destName string) (pullRefPair, error) {
imgPart, err := decompose(destName) decomposedDest, err := decompose(destName)
if err == nil && !imgPart.hasRegistry { if err == nil && !decomposedDest.hasRegistry {
// If the image doesn't have a registry, set it as the default repo // If the image doesn't have a registry, set it as the default repo
imgPart.registry = DefaultLocalRepo decomposedDest.registry = DefaultLocalRegistry
imgPart.hasRegistry = true decomposedDest.hasRegistry = true
destName = imgPart.assemble() destName = decomposedDest.assemble()
} }
reference := destName reference := destName
@ -179,11 +179,9 @@ func (ir *Runtime) pullGoalFromImageReference(ctx context.Context, srcRef types.
case DirTransport: case DirTransport:
path := srcRef.StringWithinTransport() path := srcRef.StringWithinTransport()
image := path image := path
// remove leading "/"
if image[:1] == "/" { if image[:1] == "/" {
// Instead of removing the leading /, set localhost as the registry // Set localhost as the registry so docker.io isn't prepended, and the path becomes the repository
// so docker.io isn't prepended, and the path becomes the repository image = DefaultLocalRegistry + image
image = DefaultLocalRepo + image
} }
return ir.getSinglePullRefPairGoal(srcRef, image) return ir.getSinglePullRefPairGoal(srcRef, image)

View File

@ -166,7 +166,7 @@ var _ = Describe("Podman load", func() {
Expect(result.ExitCode()).To(Equal(0)) Expect(result.ExitCode()).To(Equal(0))
}) })
It("podman load localhost repo from scratch", func() { It("podman load localhost registry from scratch", func() {
outfile := filepath.Join(podmanTest.TempDir, "load_test.tar.gz") outfile := filepath.Join(podmanTest.TempDir, "load_test.tar.gz")
setup := podmanTest.Podman([]string{"tag", ALPINE, "hello:world"}) setup := podmanTest.Podman([]string{"tag", ALPINE, "hello:world"})
@ -191,7 +191,35 @@ var _ = Describe("Podman load", func() {
Expect(result.LineInOutputContains("localhost")).To(BeTrue()) Expect(result.LineInOutputContains("localhost")).To(BeTrue())
}) })
It("podman load localhost repo from dir", func() { It("podman load localhost registry from scratch and :latest", func() {
outfile := filepath.Join(podmanTest.TempDir, "load_test.tar.gz")
setup := podmanTest.Podman([]string{"pull", fedoraMinimal})
setup.WaitWithDefaultTimeout()
Expect(setup.ExitCode()).To(Equal(0))
setup = podmanTest.Podman([]string{"tag", "fedora-minimal", "hello"})
setup.WaitWithDefaultTimeout()
Expect(setup.ExitCode()).To(Equal(0))
setup = podmanTest.Podman([]string{"save", "-o", outfile, "--format", "oci-archive", "hello"})
setup.WaitWithDefaultTimeout()
Expect(setup.ExitCode()).To(Equal(0))
setup = podmanTest.Podman([]string{"rmi", "hello"})
setup.WaitWithDefaultTimeout()
Expect(setup.ExitCode()).To(Equal(0))
load := podmanTest.Podman([]string{"load", "-i", outfile})
load.WaitWithDefaultTimeout()
Expect(load.ExitCode()).To(Equal(0))
result := podmanTest.Podman([]string{"images", "-f", "label", "hello:latest"})
result.WaitWithDefaultTimeout()
Expect(result.LineInOutputContains("docker")).To(Not(BeTrue()))
Expect(result.LineInOutputContains("localhost")).To(BeTrue())
})
It("podman load localhost registry from dir", func() {
outfile := filepath.Join(podmanTest.TempDir, "load") outfile := filepath.Join(podmanTest.TempDir, "load")
setup := podmanTest.Podman([]string{"tag", BB, "hello:world"}) setup := podmanTest.Podman([]string{"tag", BB, "hello:world"})