Skip to content

Commit

Permalink
Add --all-tags to push
Browse files Browse the repository at this point in the history
Signed-off-by: TomSweeneyRedHat <[email protected]>

Adds the --all-tags to the push command to emulate the one in
the push command.   Addresses containers/podman#2369
on the Buildah side, once approved similar changes will be made to Podman.

Signed-off-by: TomSweeneyRedHat <[email protected]>
  • Loading branch information
TomSweeneyRedHat committed Mar 4, 2019
1 parent 11dd219 commit 8ea4749
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 7 deletions.
74 changes: 69 additions & 5 deletions cmd/buildah/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ import (
"github.com/containers/image/manifest"
"github.com/containers/image/transports"
"github.com/containers/image/transports/alltransports"
"github.com/containers/storage"
multierror "github.com/hashicorp/go-multierror"
imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

type pushResults struct {
allTags bool
authfile string
blobCache string
certDir string
Expand Down Expand Up @@ -61,6 +64,7 @@ func init() {

flags := pushCommand.Flags()
flags.SetInterspersed(false)
flags.BoolVarP(&opts.allTags, "all-tags", "a", false, "push all tagged images to the repository")
flags.StringVar(&opts.authfile, "authfile", "", "path of the authentication file. Default is ${XDG_RUNTIME_DIR}/containers/auth.json")
flags.StringVar(&opts.blobCache, "blob-cache", "", "assume image blobs in the specified directory will be available for pushing")
flags.StringVar(&opts.certDir, "cert-dir", "", "use certificates at the specified path to access the registry")
Expand Down Expand Up @@ -98,16 +102,42 @@ func pushCmd(c *cobra.Command, args []string, iopts pushResults) error {
return errors.New("Only two arguments are necessary to push: source and destination")
}

compress := imagebuildah.Gzip
if iopts.disableCompression {
compress = imagebuildah.Uncompressed
}

store, err := getStore(c)
if err != nil {
return err
}

// Map of image tags found for the specified image.
// tags stored in the key
var imageTags []string
if iopts.allTags {
if err := getAllTags(src, &imageTags, store); err != nil {
return err
}
logrus.Debugf("For the image: [%s] found tags: %v", src, imageTags)
}

var errs *multierror.Error
imageName := src
destTarget := destSpec
if len(imageTags) > 1 {
for _, tagKey := range imageTags {
src = imageName + ":" + tagKey
destSpec = destTarget + ":" + tagKey
if err = pushImage(c, src, destSpec, store, iopts); err != nil {
errs = multierror.Append(errs, err)
}
}
} else {
if err = pushImage(c, src, destSpec, store, iopts); err != nil {
errs = multierror.Append(errs, err)
}
}

return errs.ErrorOrNil()
}

func pushImage(c *cobra.Command, src string, destSpec string, store storage.Store, iopts pushResults) error {
dest, err := alltransports.ParseImageName(destSpec)
// add the docker:// transport to see if they neglected it.
if err != nil {
Expand Down Expand Up @@ -148,6 +178,11 @@ func pushCmd(c *cobra.Command, args []string, iopts pushResults) error {
}
}

compress := imagebuildah.Gzip
if iopts.disableCompression {
compress = imagebuildah.Uncompressed
}

options := buildah.PushOptions{
Compression: compress,
ManifestType: manifestType,
Expand Down Expand Up @@ -179,3 +214,32 @@ func getListOfTransports() string {
allTransports := strings.Join(transports.ListNames(), ",")
return strings.Replace(allTransports, ",tarball", "", 1)
}

// getAllTags gets all of the locally tagged images and adds them
// to the passed in slice.
func getAllTags(imageName string, imageTags *[]string, store storage.Store) error {

imageName = "/" + imageName
images, err := store.Images()
if err != nil {
return errors.Wrapf(err, "error reading images")
}
for _, image := range images {

if len(image.Names) < 1 {
continue
}
// Name should look like: "docker.io/library/alpine:latest"
for _, name := range image.Names {
splitName := strings.Split(name, ":")
if !strings.HasSuffix(splitName[0], imageName) {
break
}
if len(splitName) > 1 {
*imageTags = append(*imageTags, splitName[1])
}
}

}
return nil
}
5 changes: 3 additions & 2 deletions docs/buildah-pull.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ The image ID of the image that was pulled. On error 1 is returned.

## OPTIONS

**--all-tags, a**
**--all-tags, -a**

All tagged images in the repository will be pulled.
All tagged images in the repository will be pulled, not just `:latest`.

**--authfile** *path*

Expand Down Expand Up @@ -100,6 +100,7 @@ buildah pull --creds=myusername:mypassword --cert-dir ~/auth myregistry/myreposi

buildah pull --authfile=/tmp/auths/myauths.json myregistry/myrepository/imagename:imagetag

buildah pull --all-tags alpine

## Files

Expand Down
7 changes: 7 additions & 0 deletions docs/buildah-push.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ If the transport part of DESTINATION is omitted, "docker://" is assumed.

## OPTIONS

**--all-tags, -a**

All tagged images in the repository will be pushed, not just `:latest`. A tag can not be included in the imageID or DESTINATION fields.

**--authfile** *path*

Path of the authentication file. Default is ${XDG\_RUNTIME\_DIR}/containers/auth.json, which is set using `podman login`.
Expand Down Expand Up @@ -113,6 +117,9 @@ This example extracts the imageID image and puts it into the registry on the loc
This example extracts the imageID image and puts it into the registry on the localhost using credentials and certificates for authentication.
`# buildah push --cert-dir ~/auth --tls-verify=true --creds=username:password imageID docker://localhost:5000/my-imageID`

This example pushes all the tagged alpine images to the quay.io registry.
`# buildah push --all-tags alpine docker://quay.io/myrepo/alpine`

## Files

**registries.conf** (`/etc/containers/registries.conf`)
Expand Down
13 changes: 13 additions & 0 deletions tests/push.bats
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,16 @@ load helpers
echo "$output" | grep -q "docker://busybox"
buildah rmi busybox
}

@test "push-all-tags" {
buildah pull --signature-policy ${TESTSDIR}/policy.json --all-tags alpine
run buildah push --signature-policy ${TESTSDIR}/policy.json --all-tags alpine dir:/my-dir
echo "$output"
[[ "$output" =~ "my-dir:latest" ]]
[ "$status" -eq 0 ]

run ls /my-dir*
echo "$output"
[ "$status" -eq 0 ]
[ $(wc -l <<< "$output") -ge 50 ]
}

0 comments on commit 8ea4749

Please sign in to comment.