Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "feat: replace docker with oras (#904)" #1005

Merged
merged 5 commits into from
Oct 24, 2024
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
109 changes: 109 additions & 0 deletions api/oci/extensions/repositories/ocireg/blobs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package ocireg

import (
"sync"

"github.com/containerd/containerd/remotes"
"github.com/mandelsoft/goutils/errors"
"github.com/opencontainers/go-digest"
"github.com/sirupsen/logrus"

"ocm.software/ocm/api/oci/cpi"
"ocm.software/ocm/api/oci/extensions/attrs/cacheattr"
"ocm.software/ocm/api/tech/docker/resolve"
"ocm.software/ocm/api/utils/accessio"
"ocm.software/ocm/api/utils/blobaccess/blobaccess"
)

type BlobContainer interface {
GetBlobData(digest digest.Digest) (int64, cpi.DataAccess, error)
AddBlob(blob cpi.BlobAccess) (int64, digest.Digest, error)
Unref() error
}

type blobContainer struct {
accessio.StaticAllocatable
fetcher resolve.Fetcher
pusher resolve.Pusher
mime string
}

type BlobContainers struct {
lock sync.Mutex
cache accessio.BlobCache
fetcher resolve.Fetcher
pusher resolve.Pusher
mimes map[string]BlobContainer
}

func NewBlobContainers(ctx cpi.Context, fetcher remotes.Fetcher, pusher resolve.Pusher) *BlobContainers {
return &BlobContainers{
cache: cacheattr.Get(ctx),
fetcher: fetcher,
pusher: pusher,
mimes: map[string]BlobContainer{},
}
}

func (c *BlobContainers) Get(mime string) (BlobContainer, error) {
c.lock.Lock()
defer c.lock.Unlock()

found := c.mimes[mime]
if found == nil {
container, err := NewBlobContainer(c.cache, mime, c.fetcher, c.pusher)
if err != nil {
return nil, err
}
c.mimes[mime] = container

return container, nil
}

return found, nil
}

func (c *BlobContainers) Release() error {
c.lock.Lock()
defer c.lock.Unlock()
list := errors.ErrListf("releasing mime block caches")
for _, b := range c.mimes {
list.Add(b.Unref())
}
return list.Result()
}

func newBlobContainer(mime string, fetcher resolve.Fetcher, pusher resolve.Pusher) *blobContainer {
return &blobContainer{
mime: mime,
fetcher: fetcher,
pusher: pusher,
}
}

func NewBlobContainer(cache accessio.BlobCache, mime string, fetcher resolve.Fetcher, pusher resolve.Pusher) (BlobContainer, error) {
c := newBlobContainer(mime, fetcher, pusher)

if cache == nil {
return c, nil
}
r, err := accessio.CachedAccess(c, c, cache)
if err != nil {
return nil, err
}
return r, nil
}

func (n *blobContainer) GetBlobData(digest digest.Digest) (int64, cpi.DataAccess, error) {
logrus.Debugf("orig get %s %s\n", n.mime, digest)
acc, err := NewDataAccess(n.fetcher, digest, n.mime, false)
return blobaccess.BLOB_UNKNOWN_SIZE, acc, err
}

func (n *blobContainer) AddBlob(blob cpi.BlobAccess) (int64, digest.Digest, error) {
err := push(dummyContext, n.pusher, blob)
if err != nil {
return blobaccess.BLOB_UNKNOWN_SIZE, blobaccess.BLOB_UNKNOWN_DIGEST, err
}
return blob.Size(), blob.Digest(), err
}
Loading