Skip to content

Commit

Permalink
simplified the interface and removed the unused status and commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Skarlso committed Dec 16, 2024
1 parent 78d2bc8 commit 024a606
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 42 deletions.
5 changes: 2 additions & 3 deletions api/oci/extensions/repositories/ocireg/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ func pushData(ctx context.Context, p oras.Pusher, desc artdesc.Descriptor, data
}

logging.Logger().Debug("*** push blob", "mediatype", desc.MediaType, "digest", desc.Digest, "key", key)
req, err := p.Push(ctx, desc, data)
if err != nil {
if err := p.Push(ctx, desc, data); err != nil {
if errdefs.IsAlreadyExists(err) {
logging.Logger().Debug("blob already exists", "mediatype", desc.MediaType, "digest", desc.Digest)

Expand All @@ -104,7 +103,7 @@ func pushData(ctx context.Context, p oras.Pusher, desc artdesc.Descriptor, data
return fmt.Errorf("failed to push: %w", err)
}

return req.Commit(ctx, desc.Size, desc.Digest)
return nil
}

var dummyContext = nologger()
Expand Down
34 changes: 11 additions & 23 deletions api/tech/oras/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package oras

import (
"context"
"errors"
"fmt"
"io"
"strings"

"github.com/containerd/containerd/content"
"github.com/containerd/containerd/errdefs"
"github.com/opencontainers/go-digest"
ociv1 "github.com/opencontainers/image-spec/specs-go/v1"
oraserr "oras.land/oras-go/v2/errdef"
"oras.land/oras-go/v2/registry/remote"
"oras.land/oras-go/v2/registry/remote/auth"
)
Expand All @@ -25,18 +25,6 @@ type Client struct {
Ref string
}

type pushRequest struct{}

func (p *pushRequest) Commit(ctx context.Context, size int64, expected digest.Digest, opts ...content.Opt) error {
return nil
}

func (p *pushRequest) Status() (content.Status, error) {
return content.Status{}, nil
}

var _ PushRequest = &pushRequest{}

var (
_ Resolver = &Client{}
_ Fetcher = &Client{}
Expand All @@ -61,7 +49,7 @@ func (c *Client) Resolve(ctx context.Context, ref string) (string, ociv1.Descrip
// Meaning it will throw that error instead of not found.
desc, err := src.Resolve(ctx, ref)
if err != nil {
if strings.Contains(err.Error(), "not found") {
if errors.Is(err, oraserr.ErrNotFound) {
return "", ociv1.Descriptor{}, errdefs.ErrNotFound
}

Expand All @@ -86,15 +74,15 @@ func (c *Client) Lister(ctx context.Context, ref string) (Lister, error) {
return c, nil
}

func (c *Client) Push(ctx context.Context, d ociv1.Descriptor, src Source) (PushRequest, error) {
func (c *Client) Push(ctx context.Context, d ociv1.Descriptor, src Source) error {
reader, err := src.Reader()
if err != nil {
return nil, err
return err
}

repository, err := c.createRepository(c.Ref)
if err != nil {
return nil, err
return err
}

if split := strings.Split(c.Ref, ":"); len(split) == 2 {
Expand All @@ -103,19 +91,19 @@ func (c *Client) Push(ctx context.Context, d ociv1.Descriptor, src Source) (Push
// that layer resulting in the created tag pointing to the right
// blob data.
if err := repository.PushReference(ctx, d, reader, c.Ref); err != nil {
return nil, fmt.Errorf("failed to push tag: %w", err)
return fmt.Errorf("failed to push tag: %w", err)
}

return &pushRequest{}, nil
return nil
}

// We have a digest, so we push use plain push for the digest.
// Push here decides if it's a Manifest or a Blob.
if err := repository.Push(ctx, d, reader); err != nil {
return nil, fmt.Errorf("failed to push: %w, %s", err, c.Ref)
return fmt.Errorf("failed to push: %w, %s", err, c.Ref)
}

return &pushRequest{}, nil
return nil
}

func (c *Client) Fetch(ctx context.Context, desc ociv1.Descriptor) (io.ReadCloser, error) {
Expand All @@ -131,7 +119,7 @@ func (c *Client) Fetch(ctx context.Context, desc ociv1.Descriptor) (io.ReadClose
// that the mediatype is not set at this point.
rdesc, err := src.Manifests().Resolve(ctx, desc.Digest.String())
if err != nil {
if strings.Contains(err.Error(), "not found") {
if errors.Is(err, oraserr.ErrNotFound) {
rdesc, err = src.Blobs().Resolve(ctx, desc.Digest.String())
if err != nil {
return nil, fmt.Errorf("failed to resolve fetch blob %q: %w", desc.Digest.String(), err)
Expand Down
17 changes: 1 addition & 16 deletions api/tech/oras/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"context"
"io"

"github.com/containerd/containerd/content"
"github.com/opencontainers/go-digest"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
)

Expand Down Expand Up @@ -54,22 +52,9 @@ type Fetcher interface {
type Pusher interface {
// Push returns a push request for the given resource identified
// by the descriptor and the given data source.
Push(ctx context.Context, d ocispec.Descriptor, src Source) (PushRequest, error)
Push(ctx context.Context, d ocispec.Descriptor, src Source) error
}

type Lister interface {
List(context.Context) ([]string, error)
}

// PushRequest handles the result of a push request
// replaces containerd content.Writer.
type PushRequest interface {
// Commit commits the blob (but no roll-back is guaranteed on an error).
// size and expected can be zero-value when unknown.
// Commit always closes the writer, even on error.
// ErrAlreadyExists aborts the writer.
Commit(ctx context.Context, size int64, expected digest.Digest, opts ...content.Opt) error

// Status returns the current state of write
Status() (content.Status, error)
}

0 comments on commit 024a606

Please sign in to comment.