mirror of
https://github.com/containers/podman.git
synced 2025-09-14 03:21:12 +08:00
Merge pull request #1103 from haircommander/load_dockerless
Podman load/tag/save prepends localhost when no registry is present
This commit is contained in:
@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
@ -118,14 +119,26 @@ func saveCmd(c *cli.Context) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
newImage, err := runtime.ImageRuntime().NewFromLocal(args[0])
|
source := args[0]
|
||||||
|
newImage, err := runtime.ImageRuntime().NewFromLocal(source)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
dest := dst
|
dest := dst
|
||||||
// need dest to be in the format transport:path:reference for the following transports
|
// need dest to be in the format transport:path:reference for the following transports
|
||||||
if (strings.Contains(dst, libpod.OCIArchive) || strings.Contains(dst, libpod.DockerArchive)) && !strings.Contains(newImage.ID(), args[0]) {
|
if (strings.Contains(dst, libpod.OCIArchive) || strings.Contains(dst, libpod.DockerArchive)) && !strings.Contains(newImage.ID(), source) {
|
||||||
dest = dst + ":" + args[0]
|
prepend := ""
|
||||||
|
if !strings.Contains(source, libpodImage.DefaultLocalRepo) {
|
||||||
|
// we need to check if localhost was added to the image name in NewFromLocal
|
||||||
|
for _, name := range newImage.Names() {
|
||||||
|
// if the user searched for the image whose tag was prepended with localhost, we'll need to prepend localhost to successfully search
|
||||||
|
if strings.Contains(name, libpodImage.DefaultLocalRepo) && strings.Contains(name, source) {
|
||||||
|
prepend = fmt.Sprintf("%s/", libpodImage.DefaultLocalRepo)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
dest = fmt.Sprintf("%s:%s%s", dst, prepend, source)
|
||||||
}
|
}
|
||||||
if err := newImage.PushImage(getContext(), dest, manifestType, "", "", writer, c.Bool("compress"), libpodImage.SigningOptions{}, &libpodImage.DockerRegistryOptions{}, false, additionaltags); err != nil {
|
if err := newImage.PushImage(getContext(), dest, 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 {
|
||||||
|
@ -260,6 +260,12 @@ func (i *Image) getLocalImage() (*storage.Image, error) {
|
|||||||
if hasReg {
|
if hasReg {
|
||||||
return nil, errors.Errorf("%s", imageError)
|
return nil, errors.Errorf("%s", 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
|
||||||
|
img, err = i.imageruntime.getImage(DefaultLocalRepo + "/" + i.InputName)
|
||||||
|
if err == nil {
|
||||||
|
return img.image, err
|
||||||
|
}
|
||||||
|
|
||||||
// grab all the local images
|
// grab all the local images
|
||||||
images, err := i.imageruntime.GetImages()
|
images, err := i.imageruntime.GetImages()
|
||||||
@ -462,6 +468,10 @@ func (i *Image) TagImage(tag string) error {
|
|||||||
if !decomposedTag.isTagged {
|
if !decomposedTag.isTagged {
|
||||||
tag = fmt.Sprintf("%s:%s", tag, decomposedTag.tag)
|
tag = fmt.Sprintf("%s:%s", tag, decomposedTag.tag)
|
||||||
}
|
}
|
||||||
|
// If the input doesn't specify a registry, set the registry to localhost
|
||||||
|
if !decomposedTag.hasRegistry {
|
||||||
|
tag = fmt.Sprintf("%s/%s", DefaultLocalRepo, tag)
|
||||||
|
}
|
||||||
tags := i.Names()
|
tags := i.Names()
|
||||||
if util.StringInSlice(tag, tags) {
|
if util.StringInSlice(tag, tags) {
|
||||||
return nil
|
return nil
|
||||||
|
@ -170,12 +170,12 @@ func TestImage_MatchRepoTag(t *testing.T) {
|
|||||||
// foo should resolve to foo:latest
|
// foo should resolve to foo:latest
|
||||||
repoTag, err := newImage.MatchRepoTag("foo")
|
repoTag, err := newImage.MatchRepoTag("foo")
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, "foo:latest", repoTag)
|
assert.Equal(t, "localhost/foo:latest", repoTag)
|
||||||
|
|
||||||
// foo:bar should resolve to foo:bar
|
// foo:bar should resolve to foo:bar
|
||||||
repoTag, err = newImage.MatchRepoTag("foo:bar")
|
repoTag, err = newImage.MatchRepoTag("foo:bar")
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, "foo:bar", repoTag)
|
assert.Equal(t, "localhost/foo:bar", repoTag)
|
||||||
// Shutdown the runtime and remove the temporary storage
|
// Shutdown the runtime and remove the temporary storage
|
||||||
cleanup(workdir, ir)
|
cleanup(workdir, ir)
|
||||||
}
|
}
|
||||||
|
@ -45,6 +45,9 @@ var (
|
|||||||
AtomicTransport = "atomic"
|
AtomicTransport = "atomic"
|
||||||
// DefaultTransport is a prefix that we apply to an image name
|
// DefaultTransport is a prefix that we apply to an image name
|
||||||
DefaultTransport = DockerTransport
|
DefaultTransport = DockerTransport
|
||||||
|
// DefaultLocalRepo is the default local repository for local image operations
|
||||||
|
// Remote pulls will still use defined registries
|
||||||
|
DefaultLocalRepo = "localhost"
|
||||||
)
|
)
|
||||||
|
|
||||||
type pullStruct struct {
|
type pullStruct struct {
|
||||||
@ -55,6 +58,14 @@ type pullStruct struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ir *Runtime) getPullStruct(srcRef types.ImageReference, destName string) (*pullStruct, error) {
|
func (ir *Runtime) getPullStruct(srcRef types.ImageReference, destName string) (*pullStruct, error) {
|
||||||
|
imgPart, err := decompose(destName)
|
||||||
|
if err == nil && !imgPart.hasRegistry {
|
||||||
|
// If the image doesn't have a registry, set it as the default repo
|
||||||
|
imgPart.registry = DefaultLocalRepo
|
||||||
|
imgPart.hasRegistry = true
|
||||||
|
destName = imgPart.assemble()
|
||||||
|
}
|
||||||
|
|
||||||
reference := destName
|
reference := destName
|
||||||
if srcRef.DockerReference() != nil {
|
if srcRef.DockerReference() != nil {
|
||||||
reference = srcRef.DockerReference().String()
|
reference = srcRef.DockerReference().String()
|
||||||
@ -148,7 +159,9 @@ func (ir *Runtime) getPullListFromRef(ctx context.Context, srcRef types.ImageRef
|
|||||||
image := splitArr[1]
|
image := splitArr[1]
|
||||||
// remove leading "/"
|
// remove leading "/"
|
||||||
if image[:1] == "/" {
|
if image[:1] == "/" {
|
||||||
image = image[1:]
|
// Instead of removing the leading /, set localhost as the registry
|
||||||
|
// so docker.io isn't prepended, and the path becomes the repository
|
||||||
|
image = DefaultLocalRepo + image
|
||||||
}
|
}
|
||||||
pullInfo, err := ir.getPullStruct(srcRef, image)
|
pullInfo, err := ir.getPullStruct(srcRef, image)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -161,4 +161,60 @@ var _ = Describe("Podman load", func() {
|
|||||||
inspect.WaitWithDefaultTimeout()
|
inspect.WaitWithDefaultTimeout()
|
||||||
Expect(result.ExitCode()).To(Equal(0))
|
Expect(result.ExitCode()).To(Equal(0))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
It("podman load localhost repo from scratch", 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:world"})
|
||||||
|
setup.WaitWithDefaultTimeout()
|
||||||
|
Expect(setup.ExitCode()).To(Equal(0))
|
||||||
|
|
||||||
|
setup = podmanTest.Podman([]string{"save", "-o", outfile, "--format", "oci-archive", "hello:world"})
|
||||||
|
setup.WaitWithDefaultTimeout()
|
||||||
|
Expect(setup.ExitCode()).To(Equal(0))
|
||||||
|
|
||||||
|
setup = podmanTest.Podman([]string{"rmi", "hello:world"})
|
||||||
|
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:world"})
|
||||||
|
result.WaitWithDefaultTimeout()
|
||||||
|
Expect(result.LineInOutputContains("docker")).To(Not(BeTrue()))
|
||||||
|
Expect(result.LineInOutputContains("localhost")).To(BeTrue())
|
||||||
|
})
|
||||||
|
|
||||||
|
It("podman load localhost repo from dir", func() {
|
||||||
|
outfile := filepath.Join(podmanTest.TempDir, "load")
|
||||||
|
setup := podmanTest.Podman([]string{"pull", fedoraMinimal})
|
||||||
|
setup.WaitWithDefaultTimeout()
|
||||||
|
Expect(setup.ExitCode()).To(Equal(0))
|
||||||
|
|
||||||
|
setup = podmanTest.Podman([]string{"tag", "fedora-minimal", "hello:world"})
|
||||||
|
setup.WaitWithDefaultTimeout()
|
||||||
|
Expect(setup.ExitCode()).To(Equal(0))
|
||||||
|
|
||||||
|
setup = podmanTest.Podman([]string{"save", "-o", outfile, "--format", "oci-dir", "hello:world"})
|
||||||
|
setup.WaitWithDefaultTimeout()
|
||||||
|
Expect(setup.ExitCode()).To(Equal(0))
|
||||||
|
|
||||||
|
setup = podmanTest.Podman([]string{"rmi", "hello:world"})
|
||||||
|
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", "load:latest"})
|
||||||
|
result.WaitWithDefaultTimeout()
|
||||||
|
Expect(result.LineInOutputContains("docker")).To(Not(BeTrue()))
|
||||||
|
Expect(result.LineInOutputContains("localhost")).To(BeTrue())
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
@ -38,7 +38,7 @@ var _ = Describe("Podman tag", func() {
|
|||||||
Expect(results.ExitCode()).To(Equal(0))
|
Expect(results.ExitCode()).To(Equal(0))
|
||||||
inspectData := results.InspectImageJSON()
|
inspectData := results.InspectImageJSON()
|
||||||
Expect(StringInSlice("docker.io/library/alpine:latest", inspectData[0].RepoTags)).To(BeTrue())
|
Expect(StringInSlice("docker.io/library/alpine:latest", inspectData[0].RepoTags)).To(BeTrue())
|
||||||
Expect(StringInSlice("foobar:latest", inspectData[0].RepoTags)).To(BeTrue())
|
Expect(StringInSlice("localhost/foobar:latest", inspectData[0].RepoTags)).To(BeTrue())
|
||||||
})
|
})
|
||||||
|
|
||||||
It("podman tag shortname", func() {
|
It("podman tag shortname", func() {
|
||||||
@ -51,7 +51,7 @@ var _ = Describe("Podman tag", func() {
|
|||||||
Expect(results.ExitCode()).To(Equal(0))
|
Expect(results.ExitCode()).To(Equal(0))
|
||||||
inspectData := results.InspectImageJSON()
|
inspectData := results.InspectImageJSON()
|
||||||
Expect(StringInSlice("docker.io/library/alpine:latest", inspectData[0].RepoTags)).To(BeTrue())
|
Expect(StringInSlice("docker.io/library/alpine:latest", inspectData[0].RepoTags)).To(BeTrue())
|
||||||
Expect(StringInSlice("foobar:latest", inspectData[0].RepoTags)).To(BeTrue())
|
Expect(StringInSlice("localhost/foobar:latest", inspectData[0].RepoTags)).To(BeTrue())
|
||||||
})
|
})
|
||||||
|
|
||||||
It("podman tag shortname:tag", func() {
|
It("podman tag shortname:tag", func() {
|
||||||
@ -64,7 +64,7 @@ var _ = Describe("Podman tag", func() {
|
|||||||
Expect(results.ExitCode()).To(Equal(0))
|
Expect(results.ExitCode()).To(Equal(0))
|
||||||
inspectData := results.InspectImageJSON()
|
inspectData := results.InspectImageJSON()
|
||||||
Expect(StringInSlice("docker.io/library/alpine:latest", inspectData[0].RepoTags)).To(BeTrue())
|
Expect(StringInSlice("docker.io/library/alpine:latest", inspectData[0].RepoTags)).To(BeTrue())
|
||||||
Expect(StringInSlice("foobar:new", inspectData[0].RepoTags)).To(BeTrue())
|
Expect(StringInSlice("localhost/foobar:new", inspectData[0].RepoTags)).To(BeTrue())
|
||||||
})
|
})
|
||||||
|
|
||||||
It("podman tag shortname image no tag", func() {
|
It("podman tag shortname image no tag", func() {
|
||||||
|
Reference in New Issue
Block a user