Skip to content

Commit

Permalink
renamed the createRepository function and fixed digest resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
Skarlso committed Dec 16, 2024
1 parent f7ce457 commit c9e2830
Showing 1 changed file with 19 additions and 20 deletions.
39 changes: 19 additions & 20 deletions api/tech/oras/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,20 @@ func New(opts ClientOptions) *Client {
}

func (c *Client) Resolve(ctx context.Context, ref string) (string, ociv1.Descriptor, error) {
src, err := c.resolveRef(ref)
src, err := c.createRepository(ref)
if err != nil {
return "", ociv1.Descriptor{}, err
}

// We try to first resolve a manifest.
// _Note_: If there is an error like not found, but we know that the digest exists
// we can add src.Blobs().Resolve in here. If we do that, note that
// for Blobs().Resolve `not found` is actually `invalid checksum digest format`.
// 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") {
// Then we use the blob store to resolve.
desc, err := src.Blobs().Resolve(ctx, ref)
if err != nil {
if strings.Contains(err.Error(), "not found") {
return "", ociv1.Descriptor{}, errdefs.ErrNotFound
}

return "", ociv1.Descriptor{}, fmt.Errorf("failed to resolve blob: %w", err)
}

return ref, desc, nil
return "", ociv1.Descriptor{}, errdefs.ErrNotFound
}

return "", ociv1.Descriptor{}, fmt.Errorf("failed to resolve manifest %q: %w", ref, err)
Expand Down Expand Up @@ -100,15 +94,16 @@ func (c *Client) Push(ctx context.Context, d ociv1.Descriptor, src regclient.Sou
return nil, err
}

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

if split := strings.Split(c.Ref, ":"); len(split) == 2 {
// Once we get a reference that contains a tag, we need to re-push that
// layer with the reference included. PushReference pushes a blob or a
// manifest.
// layer with the reference included. PushReference then will tag
// 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)
}
Expand All @@ -126,14 +121,17 @@ func (c *Client) Push(ctx context.Context, d ociv1.Descriptor, src regclient.Sou
}

func (c *Client) Fetch(ctx context.Context, desc ociv1.Descriptor) (io.ReadCloser, error) {
src, err := c.resolveRef(c.Ref)
src, err := c.createRepository(c.Ref)
if err != nil {
return nil, fmt.Errorf("failed to resolve ref %q: %w", c.Ref, err)
}

// oras requires a Resolve to happen before a fetch because
// -1 is an invalid size.
// manifest is not set in the descriptor
// src.Resolve is a manifest().resolve
rdesc, err := src.Resolve(ctx, desc.Digest.String())
// We explicitly call resolve on manifest first because it might be
// 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") {
rdesc, err = src.Blobs().Resolve(ctx, desc.Digest.String())
Expand All @@ -150,6 +148,7 @@ func (c *Client) Fetch(ctx context.Context, desc ociv1.Descriptor) (io.ReadClose
return nil, fmt.Errorf("failed to resolve fetch manifest %q: %w", desc.Digest.String(), err)
}

// lastly, try a manifest fetch.
fetch, err := src.Fetch(ctx, rdesc)
if err != nil {
return nil, fmt.Errorf("failed to fetch manifest: %w", err)
Expand All @@ -160,7 +159,7 @@ func (c *Client) Fetch(ctx context.Context, desc ociv1.Descriptor) (io.ReadClose

func (c *Client) List(ctx context.Context) ([]string, error) {
var result []string
src, err := c.resolveRef(c.Ref)
src, err := c.createRepository(c.Ref)
if err != nil {
return nil, fmt.Errorf("failed to resolve ref %q: %w", c.Ref, err)
}
Expand All @@ -175,7 +174,7 @@ func (c *Client) List(ctx context.Context) ([]string, error) {
return result, nil
}

func (c *Client) resolveRef(ref string) (*remote.Repository, error) {
func (c *Client) createRepository(ref string) (*remote.Repository, error) {
src, err := remote.NewRepository(ref)
if err != nil {
return nil, fmt.Errorf("failed to create new repository: %w", err)
Expand Down

0 comments on commit c9e2830

Please sign in to comment.