Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
thesayyn committed Feb 21, 2024
1 parent 7e53ae5 commit e2fd491
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 28 deletions.
Binary file removed main
Binary file not shown.
12 changes: 0 additions & 12 deletions notes.txt

This file was deleted.

4 changes: 3 additions & 1 deletion pkg/v1/layout/puller.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ func (p *puller) Artifact(ctx context.Context, ref name.Reference) (partial.Arti
return nil, err
}
return reg.ImageIndex(desc.Digest)
} else if desc.MediaType.IsSchema1() {
return nil, fmt.Errorf("layout puller does not support Schema1 images.")

Check warning on line 62 in pkg/v1/layout/puller.go

View workflow job for this annotation

GitHub Actions / Lint

error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)

Check failure on line 62 in pkg/v1/layout/puller.go

View workflow job for this annotation

GitHub Actions / Presubmit

error strings should not end with punctuation or newlines (ST1005)
}
return nil, fmt.Errorf("TODO: handle non image media types")
return nil, fmt.Errorf("unknown media type: %s", desc.MediaType)
}

// Head implements remote.Puller.
Expand Down
68 changes: 65 additions & 3 deletions pkg/v1/layout/pusher.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,77 @@ import (
"bytes"
"context"
"errors"
"fmt"
"io"

"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/remote"
"github.com/google/go-containerregistry/pkg/v1/types"
specsv1 "github.com/opencontainers/image-spec/specs-go/v1"
)

func taggableToManifest(t partial.WithRawManifest) (partial.Artifact, error) {
if a, ok := t.(partial.Artifact); ok {
return a, nil
}

desc := v1.Descriptor{
// A reasonable default if Taggable doesn't implement MediaType.
MediaType: types.DockerManifestSchema2,
}

b, err := t.RawManifest()
if err != nil {
return nil, err
}

if wmt, ok := t.(partial.WithMediaType); ok {
desc.MediaType, err = wmt.MediaType()
if err != nil {
return nil, err
}
}

desc.Digest, desc.Size, err = v1.SHA256(bytes.NewReader(b))
if err != nil {
return nil, err
}
return nil, fmt.Errorf("unknown taggable type")
}

func unpackTaggable(t partial.WithRawManifest) ([]byte, *v1.Descriptor, error) {
b, err := t.RawManifest()
if err != nil {
return nil, nil, err
}

// A reasonable default if Taggable doesn't implement MediaType.
mt := types.DockerManifestSchema2

if wmt, ok := t.(partial.WithMediaType); ok {
m, err := wmt.MediaType()
if err != nil {
return nil, nil, err
}
mt = m
}

h, sz, err := v1.SHA256(bytes.NewReader(b))
if err != nil {
return nil, nil, err
}

return b, &v1.Descriptor{
MediaType: mt,
Size: sz,
Digest: h,
}, nil
}

// Use partial.Artifact to unpack taggable.
// Duplication is not a concern here.
type pusher struct {
path Path
}
Expand Down Expand Up @@ -40,12 +102,12 @@ func (lp *pusher) writeLayer(l v1.Layer) error {
}

// Push implements remote.Pusher.
func (lp *pusher) Push(ctx context.Context, ref name.Reference, t remote.Taggable) error {
mf, err := remote.TaggableToManifest(t)
func (lp *pusher) Push(ctx context.Context, ref name.Reference, t partial.WithRawManifest) error {
mf, err := taggableToManifest(t)
if err != nil {
return err
}
b, desc, err := remote.UnpackTaggable(t)
b, desc, err := unpackTaggable(t)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/v1/remote/puller.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package remote

import (
"context"
"fmt"
"sync"

"github.com/google/go-containerregistry/pkg/name"
Expand Down Expand Up @@ -143,8 +144,7 @@ func (p *puller) artifact(ctx context.Context, ref name.Reference, acceptable []
} else if desc.MediaType.IsSchema1() {
return desc.Schema1()
}
// TODO: is this the right thing?
return desc.ToArtifact(), nil
return nil, fmt.Errorf("unknown media type: %s", desc.MediaType)
}

// Layer is like remote.Layer, but avoids re-authenticating when possible.
Expand Down
14 changes: 7 additions & 7 deletions pkg/v1/remote/pusher.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ import (
)

type manifest interface {
Taggable
partial.Describable
partial.Artifact
}

// key is either v1.Hash or v1.Layer (for stream.Layer)
Expand Down Expand Up @@ -93,7 +92,7 @@ func (w *workers) Stream(layer v1.Layer, f func() error) error {

type Pusher interface {
Delete(ctx context.Context, ref name.Reference) error
Push(ctx context.Context, ref name.Reference, t Taggable) error
Push(ctx context.Context, ref name.Reference, t partial.WithRawManifest) error
Upload(ctx context.Context, repo name.Repository, l v1.Layer) error
}

Expand Down Expand Up @@ -132,7 +131,7 @@ func (p *pusher) writer(ctx context.Context, repo name.Repository, o *options) (
return rw, rw.init(ctx)
}

func (p *pusher) Push(ctx context.Context, ref name.Reference, t Taggable) error {
func (p *pusher) Push(ctx context.Context, ref name.Reference, t partial.WithRawManifest) error {
w, err := p.writer(ctx, ref.Context(), p.o)
if err != nil {
return err
Expand Down Expand Up @@ -227,12 +226,13 @@ func (d describable) MediaType() (types.MediaType, error) {
return d.desc.MediaType, nil
}

// This is basically partia
type tagManifest struct {
Taggable
partial.Describable
}

func TaggableToManifest(t Taggable) (manifest, error) {
func taggableToManifest(t Taggable) (manifest, error) {
if m, ok := t.(manifest); ok {
return m, nil
}
Expand Down Expand Up @@ -279,7 +279,7 @@ func TaggableToManifest(t Taggable) (manifest, error) {
}

func (rw *repoWriter) writeManifest(ctx context.Context, ref name.Reference, t Taggable) error {
m, err := TaggableToManifest(t)
m, err := taggableToManifest(t)
if err != nil {
return err
}
Expand Down Expand Up @@ -402,7 +402,7 @@ func (rw *repoWriter) manifestExists(ctx context.Context, ref name.Reference, t
client: rw.w.client,
}

m, err := TaggableToManifest(t)
m, err := taggableToManifest(t)
if err != nil {
return false, err
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/v1/remote/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import (

// Taggable is an interface that enables a manifest PUT (e.g. for tagging).
type Taggable interface {
RawManifest() ([]byte, error)
partial.WithRawManifest
}

// Write pushes the provided img to the specified image reference.
Expand Down Expand Up @@ -433,7 +433,7 @@ func (w *writer) uploadOne(ctx context.Context, l v1.Layer) error {
//
// Use reflection to either pull the v1.Descriptor out of remote.Descriptor or
// create a descriptor based on the RawManifest and (optionally) MediaType.
func UnpackTaggable(t Taggable) ([]byte, *v1.Descriptor, error) {
func unpackTaggable(t Taggable) ([]byte, *v1.Descriptor, error) {
if d, ok := t.(*Descriptor); ok {
return d.Manifest, &d.Descriptor, nil
}
Expand Down Expand Up @@ -571,7 +571,7 @@ func (w *writer) commitManifest(ctx context.Context, t Taggable, ref name.Refere

tryUpload := func() error {
ctx := retry.Never(ctx)
raw, desc, err := UnpackTaggable(t)
raw, desc, err := unpackTaggable(t)
if err != nil {
return err
}
Expand Down

0 comments on commit e2fd491

Please sign in to comment.