From cd8281bc7fef98fecd91368c128f7b5b0418796e Mon Sep 17 00:00:00 2001 From: Gergely Brautigam <182850+Skarlso@users.noreply.github.com> Date: Thu, 12 Dec 2024 09:40:12 +0100 Subject: [PATCH] fix fetching --- .../repositories/ocireg/repository.go | 10 ++-- api/tech/regclient/client.go | 58 +++++++++---------- 2 files changed, 34 insertions(+), 34 deletions(-) diff --git a/api/oci/extensions/repositories/ocireg/repository.go b/api/oci/extensions/repositories/ocireg/repository.go index 68bdfa8835..3183f085dc 100644 --- a/api/oci/extensions/repositories/ocireg/repository.go +++ b/api/oci/extensions/repositories/ocireg/repository.go @@ -135,10 +135,12 @@ func (r *RepositoryImpl) getResolver(comp string) (regclient.Resolver, error) { } opts := regclient.ClientOptions{ - Host: ®config.Host{ - Name: "ghcr.io", //TODO: Need to figure out how to set the host. - User: username, - Pass: password, + Host: []regconfig.Host{ + { + Name: "ghcr.io", //TODO: Need to figure out how to set the host. + User: username, + Pass: password, + }, }, Version: comp, } diff --git a/api/tech/regclient/client.go b/api/tech/regclient/client.go index 42e2959ccf..d3721f0f9a 100644 --- a/api/tech/regclient/client.go +++ b/api/tech/regclient/client.go @@ -6,7 +6,6 @@ import ( "errors" "fmt" "io" - "strings" "time" "github.com/containerd/containerd/content" @@ -18,13 +17,15 @@ import ( "github.com/regclient/regclient/config" "github.com/regclient/regclient/scheme/reg" "github.com/regclient/regclient/types/descriptor" + regerr "github.com/regclient/regclient/types/errs" "github.com/regclient/regclient/types/manifest" "github.com/regclient/regclient/types/platform" regref "github.com/regclient/regclient/types/ref" ) type ClientOptions struct { - Host *config.Host + // TODO: add multiple hosts parsing + Host []config.Host Version string } @@ -63,7 +64,7 @@ var ( func New(opts ClientOptions) *Client { rc := regclient.New( - regclient.WithConfigHost(*opts.Host), + regclient.WithConfigHost(opts.Host...), regclient.WithDockerCerts(), regclient.WithDockerCreds(), regclient.WithUserAgent("containerd/"+opts.Version), @@ -138,32 +139,30 @@ func (c *Client) Resolve(ctx context.Context, ref string) (string, ociv1.Descrip return "", ociv1.Descriptor{}, err } - if r.Digest != "" { - blob, err := c.rc.BlobHead(ctx, r, descriptor.Descriptor{ - Digest: digest.Digest(r.Digest), - }) - defer blob.Close() // we can safely close it as this is not when we read it. - - if err != nil { - if strings.Contains(err.Error(), "not found") { - return "", ociv1.Descriptor{}, errdefs.ErrNotFound - } - - return "", ociv1.Descriptor{}, fmt.Errorf("failed to resolve blob head: %w", err) - } - - c.ref = r - - return ref, c.convertDescriptorToOCI(blob.GetDescriptor()), nil - } - - // if digest is set it will use that. - fmt.Println("we are in manifest") - m, err := c.rc.ManifestHead(ctx, r) + // first, try to find the manifest + m, err := c.rc.ManifestGet(ctx, r) if err != nil { - fmt.Println("we are in manifest error: ", err) - if strings.Contains(err.Error(), "not found") { - return "", ociv1.Descriptor{}, errdefs.ErrNotFound + if errors.Is(err, regerr.ErrNotFound) { + // try to find a blob + if r.Digest != "" { + blob, err := c.rc.BlobGet(ctx, r, descriptor.Descriptor{ + Digest: digest.Digest(r.Digest), + }) + defer blob.Close() // we can safely close it as this is not when we read it. + + if err != nil { + if errors.Is(err, regerr.ErrNotFound) { + return "", ociv1.Descriptor{}, errdefs.ErrNotFound + } + + return "", ociv1.Descriptor{}, fmt.Errorf("failed to resolve blob head: %w", err) + } + + // update the reference that has been resolved successfully + c.ref = r + + return ref, c.convertDescriptorToOCI(blob.GetDescriptor()), nil + } } return "", ociv1.Descriptor{}, fmt.Errorf("failed to get manifest: %w", err) @@ -172,7 +171,6 @@ func (c *Client) Resolve(ctx context.Context, ref string) (string, ociv1.Descrip // update the Ref of the client to the resolved reference. c.ref = r - fmt.Println("we returned") return ref, c.convertDescriptorToOCI(m.GetDescriptor()), nil } @@ -224,7 +222,7 @@ func (c *Client) Push(ctx context.Context, d ociv1.Descriptor, src Source) (Push } if err := c.rc.ManifestPut(ctx, c.ref, m); err != nil { - return nil, err + return nil, fmt.Errorf("failed to put manifest: %w", err) } // pushRequest closes the RC on `Commit`.