Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions oci/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/google/go-containerregistry/pkg/v1/remote/transport"
"github.com/google/go-containerregistry/pkg/v1/types"

"github.com/cocoonstack/cocoon-common/snapshot"
)

const maxRegistryConnsPerHost = 32
Expand Down Expand Up @@ -59,6 +61,9 @@ func (r *OCIRegistry) GetManifest(ctx context.Context, repo, tag string) ([]byte
}
desc, err := remote.Get(ref, r.callOpts(ctx)...)
if err != nil {
if isNotFound(err) {
return nil, "", fmt.Errorf("get manifest %s:%s: %w", repo, tag, snapshot.ErrManifestNotFound)
}
return nil, "", fmt.Errorf("get manifest %s:%s: %w", repo, tag, err)
}
return desc.Manifest, string(desc.MediaType), nil
Expand Down Expand Up @@ -176,13 +181,17 @@ func bulkTransport() *http.Transport {
// ignoreNotFound maps a registry 404 to a nil error (absent, not failed) and
// wraps anything else.
func ignoreNotFound(err error, action string) error {
var terr *transport.Error
if errors.As(err, &terr) && terr.StatusCode == http.StatusNotFound {
if isNotFound(err) {
return nil
}
return fmt.Errorf("%s: %w", action, err)
}

func isNotFound(err error) bool {
var terr *transport.Error
return errors.As(err, &terr) && terr.StatusCode == http.StatusNotFound
}

// streamLayer is a v1.Layer over a body with a known digest and size, so PutBlob
// streams a raw blob without buffering it (WriteLayer reads only Compressed()).
// body is single-use: a retried upload fails the digest check, not corrupts.
Expand Down
11 changes: 11 additions & 0 deletions oci/oci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,17 @@ func TestStreamResolvesIndexChildByDigest(t *testing.T) {
}
}

func TestGetManifestNotFoundIsTyped(t *testing.T) {
srv := httptest.NewServer(registry.New())
t.Cleanup(srv.Close)
r := NewOCIRegistry(strings.TrimPrefix(srv.URL, "http://")+"/cocoon", authn.DefaultKeychain)

_, _, err := r.GetManifest(t.Context(), "ghost/repo", "missing")
if !errors.Is(err, snapshot.ErrManifestNotFound) {
t.Fatalf("err = %v, want errors.Is ErrManifestNotFound", err)
}
}

func digestOf(b []byte) string {
sum := sha256.Sum256(b)
return "sha256:" + hex.EncodeToString(sum[:])
Expand Down
2 changes: 2 additions & 0 deletions snapshot/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const (
)

var (
ErrManifestNotFound = errors.New("manifest not found")

errMissingSnapshotJSON = errors.New("snapshot.json not found in export stream")

nowFunc = time.Now // tests override
Expand Down