mirror of
https://github.com/project-zot/zot.git
synced 2026-06-17 04:48:26 +08:00
refactor(digests): standardise representation of digests to digest.Digest (#898)
- Digests were represented by different ways
- We needed a uniform way to represent the digests and enforce a format
- also replace usage of github.com/google/go-containerregistry/pkg/v1
with github.com/opencontainers/image-spec/specs-go/v1
Signed-off-by: Laurentiu Niculae <niculae.laurentiu1@gmail.com>
(cherry picked from commit 96b2f29d6d57070a913ce419149cd481c0723815)
(cherry picked from commit 3d41b583daea654c98378ce3dcb78937d71538e8)
Co-authored-by: Laurentiu Niculae <niculae.laurentiu1@gmail.com>
This commit is contained in:
+19
-16
@@ -6,6 +6,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
godigest "github.com/opencontainers/go-digest"
|
||||
"go.etcd.io/bbolt"
|
||||
|
||||
"zotregistry.io/zot/errors"
|
||||
@@ -69,9 +70,9 @@ func NewCache(rootDir string, name string, useRelPaths bool, log zlog.Logger) *C
|
||||
return &Cache{rootDir: rootDir, db: cacheDB, useRelPaths: useRelPaths, log: log}
|
||||
}
|
||||
|
||||
func (c *Cache) PutBlob(digest, path string) error {
|
||||
func (c *Cache) PutBlob(digest godigest.Digest, path string) error {
|
||||
if path == "" {
|
||||
c.log.Error().Err(errors.ErrEmptyValue).Str("digest", digest).Msg("empty path provided")
|
||||
c.log.Error().Err(errors.ErrEmptyValue).Str("digest", digest.String()).Msg("empty path provided")
|
||||
|
||||
return errors.ErrEmptyValue
|
||||
}
|
||||
@@ -95,10 +96,10 @@ func (c *Cache) PutBlob(digest, path string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
bucket, err := root.CreateBucketIfNotExists([]byte(digest))
|
||||
bucket, err := root.CreateBucketIfNotExists([]byte(digest.String()))
|
||||
if err != nil {
|
||||
// this is a serious failure
|
||||
c.log.Error().Err(err).Str("bucket", digest).Msg("unable to create a bucket")
|
||||
c.log.Error().Err(err).Str("bucket", digest.String()).Msg("unable to create a bucket")
|
||||
|
||||
return err
|
||||
}
|
||||
@@ -145,7 +146,7 @@ func (c *Cache) PutBlob(digest, path string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Cache) GetBlob(digest string) (string, error) {
|
||||
func (c *Cache) GetBlob(digest godigest.Digest) (string, error) {
|
||||
var blobPath strings.Builder
|
||||
|
||||
if err := c.db.View(func(tx *bbolt.Tx) error {
|
||||
@@ -158,7 +159,7 @@ func (c *Cache) GetBlob(digest string) (string, error) {
|
||||
return err
|
||||
}
|
||||
|
||||
bucket := root.Bucket([]byte(digest))
|
||||
bucket := root.Bucket([]byte(digest.String()))
|
||||
if bucket != nil {
|
||||
origin := bucket.Bucket([]byte(OriginalBucket))
|
||||
blobPath.WriteString(string(c.getOne(origin)))
|
||||
@@ -174,7 +175,7 @@ func (c *Cache) GetBlob(digest string) (string, error) {
|
||||
return blobPath.String(), nil
|
||||
}
|
||||
|
||||
func (c *Cache) HasBlob(digest, blob string) bool {
|
||||
func (c *Cache) HasBlob(digest godigest.Digest, blob string) bool {
|
||||
if err := c.db.View(func(tx *bbolt.Tx) error {
|
||||
root := tx.Bucket([]byte(BlobsCache))
|
||||
if root == nil {
|
||||
@@ -185,7 +186,7 @@ func (c *Cache) HasBlob(digest, blob string) bool {
|
||||
return err
|
||||
}
|
||||
|
||||
bucket := root.Bucket([]byte(digest))
|
||||
bucket := root.Bucket([]byte(digest.String()))
|
||||
if bucket == nil {
|
||||
return errors.ErrCacheMiss
|
||||
}
|
||||
@@ -218,7 +219,7 @@ func (c *Cache) getOne(bucket *bbolt.Bucket) []byte {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Cache) DeleteBlob(digest, path string) error {
|
||||
func (c *Cache) DeleteBlob(digest godigest.Digest, path string) error {
|
||||
// use only relative (to rootDir) paths on blobs
|
||||
var err error
|
||||
if c.useRelPaths {
|
||||
@@ -238,7 +239,7 @@ func (c *Cache) DeleteBlob(digest, path string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
bucket := root.Bucket([]byte(digest))
|
||||
bucket := root.Bucket([]byte(digest.String()))
|
||||
if bucket == nil {
|
||||
return errors.ErrCacheMiss
|
||||
}
|
||||
@@ -249,7 +250,7 @@ func (c *Cache) DeleteBlob(digest, path string) error {
|
||||
}
|
||||
|
||||
if err := deduped.Delete([]byte(path)); err != nil {
|
||||
c.log.Error().Err(err).Str("digest", digest).Str("bucket", DuplicatesBucket).
|
||||
c.log.Error().Err(err).Str("digest", digest.String()).Str("bucket", DuplicatesBucket).
|
||||
Str("path", path).Msg("unable to delete")
|
||||
|
||||
return err
|
||||
@@ -260,7 +261,7 @@ func (c *Cache) DeleteBlob(digest, path string) error {
|
||||
originBlob := c.getOne(origin)
|
||||
if originBlob != nil {
|
||||
if err := origin.Delete([]byte(path)); err != nil {
|
||||
c.log.Error().Err(err).Str("digest", digest).Str("bucket", OriginalBucket).
|
||||
c.log.Error().Err(err).Str("digest", digest.String()).Str("bucket", OriginalBucket).
|
||||
Str("path", path).Msg("unable to delete")
|
||||
|
||||
return err
|
||||
@@ -270,7 +271,8 @@ func (c *Cache) DeleteBlob(digest, path string) error {
|
||||
dedupedBlob := c.getOne(deduped)
|
||||
if dedupedBlob != nil {
|
||||
if err := origin.Put(dedupedBlob, nil); err != nil {
|
||||
c.log.Error().Err(err).Str("digest", digest).Str("bucket", OriginalBucket).Str("path", path).Msg("unable to put")
|
||||
c.log.Error().Err(err).Str("digest", digest.String()).Str("bucket", OriginalBucket).Str("path", path).
|
||||
Msg("unable to put")
|
||||
|
||||
return err
|
||||
}
|
||||
@@ -281,9 +283,10 @@ func (c *Cache) DeleteBlob(digest, path string) error {
|
||||
// if no key in origin bucket then digest bucket is empty, remove it
|
||||
k := c.getOne(origin)
|
||||
if k == nil {
|
||||
c.log.Debug().Str("digest", digest).Str("path", path).Msg("deleting empty bucket")
|
||||
if err := root.DeleteBucket([]byte(digest)); err != nil {
|
||||
c.log.Error().Err(err).Str("digest", digest).Str("bucket", digest).Str("path", path).Msg("unable to delete")
|
||||
c.log.Debug().Str("digest", digest.String()).Str("path", path).Msg("deleting empty bucket")
|
||||
if err := root.DeleteBucket([]byte(digest.String())); err != nil {
|
||||
c.log.Error().Err(err).Str("digest", digest.String()).Str("bucket", digest.String()).Str("path", path).
|
||||
Msg("unable to delete")
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user