Skip to content

Commit

Permalink
Add platform option to crane copy and digest (#742)
Browse files Browse the repository at this point in the history
  • Loading branch information
dvob committed Oct 20, 2020
1 parent da2eb53 commit 8e0bbb8
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
11 changes: 9 additions & 2 deletions pkg/crane/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,15 @@ func Copy(src, dst string, opt ...Option) error {
switch desc.MediaType {
case types.OCIImageIndex, types.DockerManifestList:
// Handle indexes separately.
if err := copyIndex(desc, dstRef, o); err != nil {
return fmt.Errorf("failed to copy index: %v", err)
if o.platform != nil {
// If platform is explicitly set, don't copy the whole index, just the appropriate image.
if err := copyImage(desc, dstRef, o); err != nil {
return fmt.Errorf("failed to copy image: %v", err)
}
} else {
if err := copyIndex(desc, dstRef, o); err != nil {
return fmt.Errorf("failed to copy index: %v", err)
}
}
case types.DockerManifestSchema1, types.DockerManifestSchema1Signed:
// Handle schema 1 images separately.
Expand Down
16 changes: 16 additions & 0 deletions pkg/crane/digest.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,22 @@ package crane

// Digest returns the sha256 hash of the remote image at ref.
func Digest(ref string, opt ...Option) (string, error) {
o := makeOptions(opt...)
if o.platform != nil {
desc, err := getManifest(ref, opt...)
if err != nil {
return "", err
}
img, err := desc.Image()
if err != nil {
return "", err
}
digest, err := img.Digest()
if err != nil {
return "", err
}
return digest.String(), nil
}
desc, err := head(ref, opt...)
if err != nil {
return "", err
Expand Down
8 changes: 8 additions & 0 deletions pkg/crane/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,13 @@ func Manifest(ref string, opt ...Option) ([]byte, error) {
if err != nil {
return nil, err
}
o := makeOptions(opt...)
if o.platform != nil {
img, err := desc.Image()
if err != nil {
return nil, err
}
return img.RawManifest()
}
return desc.Manifest, nil
}
14 changes: 12 additions & 2 deletions pkg/crane/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ import (

"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/remote"
)

type options struct {
name []name.Option
remote []remote.Option
name []name.Option
remote []remote.Option
platform *v1.Platform
}

func makeOptions(opts ...Option) options {
Expand Down Expand Up @@ -54,3 +56,11 @@ func WithTransport(t http.RoundTripper) Option {
func Insecure(o *options) {
o.name = append(o.name, name.Insecure)
}

// WithPlatform is an Option to specify the platform.
func WithPlatform(platform *v1.Platform) Option {
return func(o *options) {
o.remote = append(o.remote, remote.WithPlatform(*platform))
o.platform = platform
}
}

0 comments on commit 8e0bbb8

Please sign in to comment.