Skip to content
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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 79 additions & 13 deletions libimage/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@ 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
// If true then all images and tags matching a given repository
// will be pushed. Only supported for the docker transport.
// Usage of this flag will cause Push() to return a nil []byte.
AllTags bool
Copy link
Member

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.

}

// Push pushes the specified source which must refer to an image in the local
Expand All @@ -24,33 +30,93 @@ type PushOptions struct {
//
// Return storage.ErrImageUnknown if source could not be found in the local
// containers storage.
// Returns the bytes of the copied manifest when pushing a single tag,
// which may be used for digest computation.
// When pushing with AllTags=true then the returned []byte is always nil.
func (r *Runtime) Push(ctx context.Context, source, destination string, options *PushOptions) ([]byte, error) {
if options == nil {
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.
//
// For now, make sure a destination was not specified and get it from the source.
// This could change in the future, but that gets close to the Copy() functionality.
if len(destination) != 0 {
return nil, fmt.Errorf("`destination` should not be specified if using AllTags")
Copy link
Contributor

Choose a reason for hiding this comment

The 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.)

Copy link
Author

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Make sure the source repository does not have a tag
// Make sure the source repository does not have a tag
// This intentionally does not use `alltransports.ParseImageName`, because the outcome of that
// refers to a single image, not a repo (e.g. it defaults to …:latest for docker:// references), which
// is not the semantics we want.

srcNamed, err := reference.ParseNormalizedNamed(source)
if err != nil {
return nil, err
}
if !reference.IsNameOnly(srcNamed) {
return nil, fmt.Errorf("can't push with AllTags if source tag is specified")
}

logrus.Debugf("Finding all images for source %s", srcNamed.Name())
listOptions := &ListImagesOptions{}
srcImages, _ := r.ListImages(ctx, []string{srcNamed.Name()}, listOptions)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this actually work? AFAICS this is equivalent to

srcImages = []*libimage.Image{r.LookupImage(srcNamed.Name()}

i.e. it always only finds one image. Am I missing something?


I increasingly think that the logic that chooses which images to push should have fairly robust unit tests (single image; single image with multiple tags; multiple tags pointing to different images; images with tags both in the specified repo and outside …). That’s hard to do with the current Push API, because it now doesn’t report anything about the returned images — so I guess the code to turn source into a list of (*libimage.Image, reference.Named), or some similar output data, should be turned into a separate helper function with unit tests.


(Probably irrelevant: why is it OK to ignore errors here?)


// Push each tag for every image in the list
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 {
// Filter on repo name again to avoid pushing an image that matches
// the source image ID but has a different repository than the source
currentNamed, err := reference.ParseNormalizedNamed(n.Name())
if err != nil {
return nil, err
}
if reference.Path(currentNamed) == reference.Path(srcNamed) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be currentNamed.Name() == srcNamed.Name(), because Path does not include the registry (example.com:5000/foo and quay.io/foo would match).

// Have to use Sprintf because pushImage expects a string
destWithTag := fmt.Sprintf("%s:%s", source, n.Tag())
_, err := pushImage(ctx, img, destWithTag, options, "", r)
Comment on lines +99 to +101
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Conceptually I’d prefer to move the destRef := ParseImageName etc. logic from pushImage into the !AllTags case above, and have pushImage accept a types.ImageReference; then this can use docker.NewReference instead of making another round-trip via an untyped string.

But I haven’t really examined how that would work with the writeEvent use of destination, so I’m not completely sure it would be viable.


Why is this combining source and n at all? We know the repo of currentNamed and srcNamed matches, the two only differ in the tag; so even if this had to use a string, we could use n.String(), couldn’t we?

if err != nil {
return nil, err
}
}
}
}

return nil, 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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’d prefer this to be a method on Runtime (func (r *Runtime) pushImage), but I’ll defer to @vrothberg .

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)
Copy link
Contributor

Choose a reason for hiding this comment

The 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 {
Expand Down
53 changes: 53 additions & 0 deletions libimage/push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The 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()
Expand Down