-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new
registry.Lookup
wrapper to handle generic Reference
lookups
This allows us to update `cmd/lookup` to use this new wrapper and thus let us easily/correctly test more edge cases / lookups. 🚀
- Loading branch information
Showing
5 changed files
with
232 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package registry | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"strings" | ||
|
||
"cuelabs.dev/go/oci/ociregistry" | ||
"cuelabs.dev/go/oci/ociregistry/ocimem" | ||
) | ||
|
||
// see `LookupType*` consts for possible values for this type | ||
type LookupType string | ||
|
||
const ( | ||
LookupTypeManifest LookupType = "manifest" | ||
LookupTypeBlob LookupType = "blob" | ||
) | ||
|
||
type LookupOptions struct { | ||
// unspecified implies [LookupTypeManifest] | ||
Type LookupType | ||
|
||
// whether or not to do a HEAD instead of a GET (will still return an [ociregistry.BlobReader], but with an empty body / zero bytes) | ||
Head bool | ||
} | ||
|
||
// a wrapper around [ociregistry.Interface.GetManifest] (and `GetTag`, `GetBlob`, and the `Resolve*` versions of the above) that accepts a [Reference] and always returns a [ociregistry.BlobReader] (in the case of a HEAD request, it will be a zero-length reader with just a valid descriptor) | ||
func Lookup(ctx context.Context, ref Reference, opts *LookupOptions) (ociregistry.BlobReader, error) { | ||
client, err := Client(ref.Host, nil) | ||
if err != nil { | ||
return nil, fmt.Errorf("%s: failed getting client: %w", ref, err) | ||
} | ||
|
||
var o LookupOptions | ||
if opts != nil { | ||
o = *opts | ||
} | ||
|
||
var ( | ||
r ociregistry.BlobReader | ||
desc ociregistry.Descriptor | ||
) | ||
switch o.Type { | ||
case LookupTypeManifest, "": | ||
if ref.Digest != "" { | ||
if o.Head { | ||
desc, err = client.ResolveManifest(ctx, ref.Repository, ref.Digest) | ||
} else { | ||
r, err = client.GetManifest(ctx, ref.Repository, ref.Digest) | ||
} | ||
} else { | ||
tag := ref.Tag | ||
if tag == "" { | ||
tag = "latest" | ||
} | ||
if o.Head { | ||
desc, err = client.ResolveTag(ctx, ref.Repository, tag) | ||
} else { | ||
r, err = client.GetTag(ctx, ref.Repository, tag) | ||
} | ||
} | ||
|
||
case LookupTypeBlob: | ||
// TODO error if Digest == "" ? (ociclient already does for us, so we can probably just pass it through here without much worry) | ||
if o.Head { | ||
desc, err = client.ResolveBlob(ctx, ref.Repository, ref.Digest) | ||
} else { | ||
r, err = client.GetBlob(ctx, ref.Repository, ref.Digest) | ||
} | ||
|
||
default: | ||
return nil, fmt.Errorf("unknown LookupType: %q", o.Type) | ||
} | ||
|
||
// normalize 404 and 404-like to nil return (so it's easier to detect) | ||
if err != nil { | ||
if errors.Is(err, ociregistry.ErrBlobUnknown) || | ||
errors.Is(err, ociregistry.ErrManifestUnknown) || | ||
errors.Is(err, ociregistry.ErrNameUnknown) { | ||
// obvious 404 cases | ||
return nil, nil | ||
} | ||
// https://github.com/cue-labs/oci/issues/26 | ||
if errStr := strings.TrimPrefix(err.Error(), "error response: "); strings.HasPrefix(errStr, "404 ") || | ||
// 401 often means "repository not found" (due to the nature of public/private mixing on Hub and the fact that ociauth definitely handled any possible authentication for us, so if we're still getting 401 it's unavoidable and might as well be 404) | ||
strings.HasPrefix(errStr, "401 ") { | ||
return nil, nil | ||
} | ||
return r, err | ||
} | ||
|
||
if o.Head { | ||
r = ocimem.NewBytesReader(nil, desc) | ||
} | ||
|
||
return r, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters