-
Notifications
You must be signed in to change notification settings - Fork 202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[wip] Add option to push all tags #1099
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -2,17 +2,20 @@ package libimage | |||||||||||
|
||||||||||||
import ( | ||||||||||||
"context" | ||||||||||||
"fmt" | ||||||||||||
"time" | ||||||||||||
|
||||||||||||
dockerArchiveTransport "github.com/containers/image/v5/docker/archive" | ||||||||||||
"github.com/containers/image/v5/docker/reference" | ||||||||||||
"github.com/containers/image/v5/transports" | ||||||||||||
"github.com/containers/image/v5/transports/alltransports" | ||||||||||||
"github.com/sirupsen/logrus" | ||||||||||||
) | ||||||||||||
|
||||||||||||
// PushOptions allows for custommizing image pushes. | ||||||||||||
type PushOptions struct { | ||||||||||||
CopyOptions | ||||||||||||
AllTags bool | ||||||||||||
} | ||||||||||||
|
||||||||||||
// Push pushes the specified source which must refer to an image in the local | ||||||||||||
|
@@ -29,28 +32,79 @@ func (r *Runtime) Push(ctx context.Context, source, destination string, options | |||||||||||
options = &PushOptions{} | ||||||||||||
} | ||||||||||||
|
||||||||||||
// Look up the local image. Note that we need to ignore the platform | ||||||||||||
// and push what the user specified (containers/podman/issues/10344). | ||||||||||||
image, resolvedSource, err := r.LookupImage(source, nil) | ||||||||||||
// Push the single image | ||||||||||||
if !options.AllTags { | ||||||||||||
|
||||||||||||
// Look up the local image. Note that we need to ignore the platform | ||||||||||||
// and push what the user specified (containers/podman/issues/10344). | ||||||||||||
image, resolvedSource, err := r.LookupImage(source, nil) | ||||||||||||
if err != nil { | ||||||||||||
return nil, err | ||||||||||||
} | ||||||||||||
|
||||||||||||
// Make sure we have a proper destination, and parse it into an image | ||||||||||||
// reference for copying. | ||||||||||||
if destination == "" { | ||||||||||||
// Doing an ID check here is tempting but false positives (due | ||||||||||||
// to a short partial IDs) are more painful than false | ||||||||||||
// negatives. | ||||||||||||
destination = resolvedSource | ||||||||||||
} | ||||||||||||
|
||||||||||||
return pushImage(ctx, image, destination, options, resolvedSource, r) | ||||||||||||
} | ||||||||||||
|
||||||||||||
// Below handles the AllTags option, for which we have to build a list of | ||||||||||||
// all the local images that match the provided repository and then push them. | ||||||||||||
// | ||||||||||||
// Start by making sure a destination was not specified, since we'll get that from the source | ||||||||||||
if len(destination) != 0 { | ||||||||||||
return nil, fmt.Errorf("`destination` should not be specified if using AllTags") | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is that a fundamental design decision, or a missing feature to be possibly added later? (I’m perfectly fine with not implementing all possible options at first. I just want future maintainers to know what the intent was.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it could be a new feature to be added later. But for right now, I didn't have anything to base that feature off of, so it was simpler to be strict about it. |
||||||||||||
} | ||||||||||||
|
||||||||||||
// Make sure the source repository does not have a tag | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
srcNamed, err := reference.ParseNormalizedNamed(source) | ||||||||||||
if err != nil { | ||||||||||||
return nil, err | ||||||||||||
} | ||||||||||||
if _, hasTag := srcNamed.(reference.NamedTagged); hasTag { | ||||||||||||
byarbrough marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
return nil, fmt.Errorf("can't push with AllTags if source tag is specified") | ||||||||||||
} | ||||||||||||
|
||||||||||||
// Now list every image of that source repository | ||||||||||||
listOptions := &ListImagesOptions{} | ||||||||||||
listOptions.Filters = []string{fmt.Sprintf("reference=%s:*", source)} | ||||||||||||
logrus.Debugf("Finding all images for source %s", source) | ||||||||||||
srcImages, _ := r.ListImages(ctx, nil, listOptions) | ||||||||||||
|
||||||||||||
// Push each tag for every image in the list | ||||||||||||
var byteManifest []byte | ||||||||||||
for _, img := range srcImages { | ||||||||||||
namedTagged, err := img.NamedTaggedRepoTags() | ||||||||||||
byarbrough marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
if err != nil { | ||||||||||||
return nil, err | ||||||||||||
} | ||||||||||||
for _, n := range namedTagged { | ||||||||||||
destWithTag := fmt.Sprintf("%s:%s", source, n.Tag()) | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
But I suspect the whole tag listing thing might need reworking and then this becomes unnecessary. (Hypothetically: List all images, obtain their Alternatively, it’s of course possible to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I looked at the WithTags option, but that returns a different type, so didn't use it. I looked through most of the libimage functions and nothing caught my eye as a better option. I've been having a difficult time juggling the different ways to refer to a single image and its tag. |
||||||||||||
b, err := pushImage(ctx, img, destWithTag, options, "", r) | ||||||||||||
if err != nil { | ||||||||||||
return byteManifest, err | ||||||||||||
} | ||||||||||||
byteManifest = append(byteManifest, b...) | ||||||||||||
byarbrough marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
return byteManifest, nil | ||||||||||||
} | ||||||||||||
|
||||||||||||
// pushImage sends a single image to be copied to the destination | ||||||||||||
func pushImage(ctx context.Context, image *Image, destination string, options *PushOptions, resolvedSource string, r *Runtime) ([]byte, error) { | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I’d prefer this to be a method on |
||||||||||||
srcRef, err := image.StorageReference() | ||||||||||||
if err != nil { | ||||||||||||
return nil, err | ||||||||||||
} | ||||||||||||
|
||||||||||||
// Make sure we have a proper destination, and parse it into an image | ||||||||||||
// reference for copying. | ||||||||||||
if destination == "" { | ||||||||||||
// Doing an ID check here is tempting but false positives (due | ||||||||||||
// to a short partial IDs) are more painful than false | ||||||||||||
// negatives. | ||||||||||||
destination = resolvedSource | ||||||||||||
} | ||||||||||||
|
||||||||||||
logrus.Debugf("Pushing image %s to %s", source, destination) | ||||||||||||
logrus.Debugf("Pushing image %s to %s", transports.ImageName(srcRef), destination) | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would now print a fairly large c/storage reference. I’m honestly completely unsure about this — on one hand the output will be less readable, OTOH actually listing the precise image we are pushing is quite valuable. So I’m fine with this change, just highlighting this for @vrothberg . |
||||||||||||
|
||||||||||||
destRef, err := alltransports.ParseImageName(destination) | ||||||||||||
if err != nil { | ||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,6 +67,59 @@ func TestPush(t *testing.T) { | |
} | ||
} | ||
|
||
func TestPushAllTags(t *testing.T) { | ||
runtime, cleanup := testNewRuntime(t) | ||
defer cleanup() | ||
ctx := context.Background() | ||
|
||
// Prefetch two different alpine images and make some tags | ||
pullOptions := &PullOptions{} | ||
pullOptions.Writer = os.Stdout | ||
_, err := runtime.Pull(ctx, "docker.io/library/alpine:3.15", config.PullPolicyAlways, pullOptions) | ||
require.NoError(t, err) | ||
lookupOptions := &LookupImageOptions{} | ||
img, _, err := runtime.LookupImage("docker.io/library/alpine:3.15", lookupOptions) | ||
require.NoError(t, err) | ||
img.Tag("docker.io/library/alpine") // imply latest | ||
img.Tag("docker.io/library/alpine:3.15alpha") | ||
_, err = runtime.Pull(ctx, "docker.io/library/alpine:3.14", config.PullPolicyAlways, pullOptions) | ||
require.NoError(t, err) | ||
|
||
pushOptions := &PushOptions{} | ||
pushOptions.AllTags = true // primary thing being tested here | ||
pushOptions.Writer = os.Stdout | ||
|
||
workdir, err := ioutil.TempDir("", "libimagepush") | ||
require.NoError(t, err) | ||
defer os.RemoveAll(workdir) | ||
|
||
for _, test := range []struct { | ||
source string | ||
destination string | ||
expectError bool | ||
}{ | ||
{"alpine", "docker.io/library/alpine", true}, // fail for destination | ||
{"docker://docker.io/library/alpine", "", true}, // fail for transport | ||
{"docker.io/library/alpine:latest", "", true}, // fail for tag | ||
{"alpine:latest", "", true}, // fail for tag | ||
Comment on lines
+101
to
+104
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Non-blocking? The comments are better than nothing but not really recording the intent — I hand to refer back to the code to see what conditions are imposed on the destination. Something like “non-empty destination is currently rejected” / “transport:image-name references are rejected because they refer to an image, not a repo” / “source must not contain a tag” … would be nice. |
||
// These two tests require authentication to a real registry to work | ||
// {"myregistry/alpine", "", false}, | ||
// {"example.com/myregistry/alpine", "", false}, | ||
} { | ||
_, err := runtime.Push(ctx, test.source, test.destination, pushOptions) | ||
if test.expectError { | ||
require.Error(t, err, "%v", test) | ||
continue | ||
} | ||
require.NoError(t, err, "%v", test) | ||
} | ||
|
||
// And now remove all of them. | ||
rmReports, rmErrors := runtime.RemoveImages(ctx, nil, nil) | ||
require.Len(t, rmErrors, 0) | ||
require.Len(t, rmReports, 2) | ||
} | ||
|
||
func TestPushOtherPlatform(t *testing.T) { | ||
runtime, cleanup := testNewRuntime(t) | ||
defer cleanup() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a comment to the new field.