From 9d74288243759880374e20bdcfaae4654f6cf756 Mon Sep 17 00:00:00 2001 From: thesayyn Date: Fri, 16 Feb 2024 22:25:12 -0800 Subject: [PATCH] artifact --- pkg/v1/remote/artifact.go | 23 +++++++++++++++++++++++ pkg/v1/remote/descriptor.go | 6 +++++- pkg/v1/remote/image.go | 2 ++ pkg/v1/remote/index_test.go | 3 ++- pkg/v1/remote/puller.go | 4 ++-- pkg/v1/remote/pusher.go | 2 +- 6 files changed, 35 insertions(+), 5 deletions(-) diff --git a/pkg/v1/remote/artifact.go b/pkg/v1/remote/artifact.go index 7aa06c01a..83a554651 100644 --- a/pkg/v1/remote/artifact.go +++ b/pkg/v1/remote/artifact.go @@ -16,10 +16,33 @@ package remote import ( "github.com/google/go-containerregistry/pkg/name" + v1 "github.com/google/go-containerregistry/pkg/v1" "github.com/google/go-containerregistry/pkg/v1/partial" "github.com/google/go-containerregistry/pkg/v1/types" ) +type descriptorArtifact struct { + desc Descriptor +} + +func (d *descriptorArtifact) Digest() (v1.Hash, error) { + return d.desc.Digest, nil +} + +func (d *descriptorArtifact) MediaType() (types.MediaType, error) { + return d.desc.MediaType, nil +} + +func (d *descriptorArtifact) RawManifest() ([]byte, error) { + return d.desc.RawManifest() +} + +func (d *descriptorArtifact) Size() (int64, error) { + return d.desc.Size, nil +} + +var _ partial.Artifact = (*descriptorArtifact)(nil) + // Get returns a partial.Artifact for the given reference. // // See Head if you don't need the response body. diff --git a/pkg/v1/remote/descriptor.go b/pkg/v1/remote/descriptor.go index 39f4a8ae9..35f518259 100644 --- a/pkg/v1/remote/descriptor.go +++ b/pkg/v1/remote/descriptor.go @@ -55,7 +55,11 @@ type Descriptor struct { platform v1.Platform } -func (d *Descriptor) ToDescriptor() v1.Descriptor { +func (d *Descriptor) ToArtifact() partial.Artifact { + return &descriptorArtifact{desc: *d} +} + +func (d *Descriptor) toDesc() v1.Descriptor { return d.Descriptor } diff --git a/pkg/v1/remote/image.go b/pkg/v1/remote/image.go index 94a16d7d9..6d7ecd079 100644 --- a/pkg/v1/remote/image.go +++ b/pkg/v1/remote/image.go @@ -18,6 +18,7 @@ import ( "bytes" "context" "errors" + "fmt" "io" "net/http" "net/url" @@ -70,6 +71,7 @@ func Image(ref name.Reference, options ...Option) (v1.Image, error) { if img, ok := desc.(v1.Image); ok { return img, nil } + fmt.Println(desc.MediaType()) return nil, errors.New("is not a image") } diff --git a/pkg/v1/remote/index_test.go b/pkg/v1/remote/index_test.go index 1b4b5cf1e..87fa519aa 100644 --- a/pkg/v1/remote/index_test.go +++ b/pkg/v1/remote/index_test.go @@ -25,6 +25,7 @@ import ( "github.com/google/go-cmp/cmp" v1 "github.com/google/go-containerregistry/pkg/v1" + "github.com/google/go-containerregistry/pkg/v1/partial" "github.com/google/go-containerregistry/pkg/v1/random" "github.com/google/go-containerregistry/pkg/v1/types" ) @@ -53,7 +54,7 @@ func mustChild(t *testing.T, idx v1.ImageIndex, h v1.Hash) v1.Image { return img } -func mustMediaType(t *testing.T, tag withMediaType) types.MediaType { +func mustMediaType(t *testing.T, tag partial.WithMediaType) types.MediaType { mt, err := tag.MediaType() if err != nil { t.Fatalf("MediaType() = %v", err) diff --git a/pkg/v1/remote/puller.go b/pkg/v1/remote/puller.go index af6e0ebc0..52ade4b8f 100644 --- a/pkg/v1/remote/puller.go +++ b/pkg/v1/remote/puller.go @@ -16,7 +16,6 @@ package remote import ( "context" - "errors" "sync" "github.com/google/go-containerregistry/pkg/name" @@ -144,7 +143,8 @@ func (p *puller) artifact(ctx context.Context, ref name.Reference, acceptable [] } else if desc.MediaType.IsSchema1() { return desc.Schema1() } - return nil, errors.New("TODO: ???") + // TODO: is this the right thing? + return desc.ToArtifact(), nil } // Layer is like remote.Layer, but avoids re-authenticating when possible. diff --git a/pkg/v1/remote/pusher.go b/pkg/v1/remote/pusher.go index 6ad565954..8ae5e1789 100644 --- a/pkg/v1/remote/pusher.go +++ b/pkg/v1/remote/pusher.go @@ -250,7 +250,7 @@ func TaggableToManifest(t Taggable) (manifest, error) { return d.Schema1() } - return tagManifest{t, describable{d.ToDescriptor()}}, nil + return tagManifest{t, describable{d.toDesc()}}, nil } desc := v1.Descriptor{