2021-07-16 22:53:05 -05:00
|
|
|
package s3
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/json"
|
2021-12-13 14:23:31 -05:00
|
|
|
"errors"
|
2021-07-16 22:53:05 -05:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"sync"
|
2021-12-21 08:19:40 -05:00
|
|
|
"time"
|
2021-07-16 22:53:05 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
// Add s3 support.
|
|
|
|
"github.com/docker/distribution/registry/storage/driver"
|
|
|
|
// Load s3 driver.
|
|
|
|
_ "github.com/docker/distribution/registry/storage/driver/s3-aws"
|
2021-07-16 22:53:05 -05:00
|
|
|
guuid "github.com/gofrs/uuid"
|
|
|
|
godigest "github.com/opencontainers/go-digest"
|
|
|
|
ispec "github.com/opencontainers/image-spec/specs-go/v1"
|
2022-01-31 16:33:07 -05:00
|
|
|
artifactspec "github.com/oras-project/artifacts-spec/specs-go/v1"
|
2021-07-16 22:53:05 -05:00
|
|
|
"github.com/rs/zerolog"
|
2022-10-20 11:39:20 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
zerr "zotregistry.io/zot/errors"
|
2023-05-10 12:15:33 -05:00
|
|
|
zcommon "zotregistry.io/zot/pkg/common"
|
2021-12-03 22:50:58 -05:00
|
|
|
"zotregistry.io/zot/pkg/extensions/monitoring"
|
|
|
|
zlog "zotregistry.io/zot/pkg/log"
|
2023-01-18 11:24:44 -05:00
|
|
|
zreg "zotregistry.io/zot/pkg/regexp"
|
2022-09-23 00:27:56 -05:00
|
|
|
"zotregistry.io/zot/pkg/scheduler"
|
2021-12-03 22:50:58 -05:00
|
|
|
"zotregistry.io/zot/pkg/storage"
|
2022-11-02 17:53:08 -05:00
|
|
|
"zotregistry.io/zot/pkg/storage/cache"
|
|
|
|
storageConstants "zotregistry.io/zot/pkg/storage/constants"
|
2022-04-12 05:01:04 -05:00
|
|
|
"zotregistry.io/zot/pkg/test"
|
2021-07-16 22:53:05 -05:00
|
|
|
)
|
|
|
|
|
2021-12-21 08:19:40 -05:00
|
|
|
const (
|
2022-04-12 05:01:04 -05:00
|
|
|
CacheDBName = "s3_cache"
|
2021-12-21 08:19:40 -05:00
|
|
|
)
|
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
// ObjectStorage provides the image storage operations.
|
|
|
|
type ObjectStorage struct {
|
|
|
|
rootDir string
|
2021-12-13 14:23:31 -05:00
|
|
|
store driver.StorageDriver
|
2021-07-16 22:53:05 -05:00
|
|
|
lock *sync.RWMutex
|
|
|
|
blobUploads map[string]storage.BlobUpload
|
|
|
|
log zerolog.Logger
|
2022-10-20 11:36:58 -05:00
|
|
|
metrics monitoring.MetricServer
|
2022-11-02 17:53:08 -05:00
|
|
|
cache cache.Cache
|
2022-10-20 11:36:58 -05:00
|
|
|
dedupe bool
|
|
|
|
linter storage.Lint
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (is *ObjectStorage) RootDir() string {
|
|
|
|
return is.rootDir
|
|
|
|
}
|
|
|
|
|
|
|
|
func (is *ObjectStorage) DirExists(d string) bool {
|
|
|
|
if fi, err := is.store.Stat(context.Background(), d); err == nil && fi.IsDir() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewObjectStorage returns a new image store backed by cloud storages.
|
|
|
|
// see https://github.com/docker/docker.github.io/tree/master/registry/storage-drivers
|
2022-11-02 17:53:08 -05:00
|
|
|
// Use the last argument to properly set a cache database, or it will default to boltDB local storage.
|
2022-04-12 05:01:04 -05:00
|
|
|
func NewImageStore(rootDir string, cacheDir string, gc bool, gcDelay time.Duration, dedupe, commit bool,
|
2022-06-24 08:08:47 -05:00
|
|
|
log zlog.Logger, metrics monitoring.MetricServer, linter storage.Lint,
|
2022-11-02 17:53:08 -05:00
|
|
|
store driver.StorageDriver, cacheDriver cache.Cache,
|
2022-03-21 12:37:23 -05:00
|
|
|
) storage.ImageStore {
|
2021-12-13 14:23:31 -05:00
|
|
|
imgStore := &ObjectStorage{
|
2022-10-20 11:36:58 -05:00
|
|
|
rootDir: rootDir,
|
|
|
|
store: store,
|
|
|
|
lock: &sync.RWMutex{},
|
|
|
|
blobUploads: make(map[string]storage.BlobUpload),
|
|
|
|
log: log.With().Caller().Logger(),
|
|
|
|
metrics: metrics,
|
|
|
|
dedupe: dedupe,
|
|
|
|
linter: linter,
|
2022-04-12 05:01:04 -05:00
|
|
|
}
|
|
|
|
|
2022-11-02 17:53:08 -05:00
|
|
|
imgStore.cache = cacheDriver
|
2021-07-16 22:53:05 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
return imgStore
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// RLock read-lock.
|
2021-12-21 08:19:40 -05:00
|
|
|
func (is *ObjectStorage) RLock(lockStart *time.Time) {
|
|
|
|
*lockStart = time.Now()
|
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
is.lock.RLock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// RUnlock read-unlock.
|
2021-12-21 08:19:40 -05:00
|
|
|
func (is *ObjectStorage) RUnlock(lockStart *time.Time) {
|
2021-07-16 22:53:05 -05:00
|
|
|
is.lock.RUnlock()
|
2021-12-21 08:19:40 -05:00
|
|
|
|
|
|
|
lockEnd := time.Now()
|
|
|
|
// includes time spent in acquiring and holding a lock
|
|
|
|
latency := lockEnd.Sub(*lockStart)
|
2022-11-02 17:53:08 -05:00
|
|
|
monitoring.ObserveStorageLockLatency(is.metrics, latency, is.RootDir(), storageConstants.RLOCK) // histogram
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lock write-lock.
|
2021-12-21 08:19:40 -05:00
|
|
|
func (is *ObjectStorage) Lock(lockStart *time.Time) {
|
|
|
|
*lockStart = time.Now()
|
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
is.lock.Lock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unlock write-unlock.
|
2021-12-21 08:19:40 -05:00
|
|
|
func (is *ObjectStorage) Unlock(lockStart *time.Time) {
|
2021-07-16 22:53:05 -05:00
|
|
|
is.lock.Unlock()
|
2021-12-21 08:19:40 -05:00
|
|
|
|
|
|
|
lockEnd := time.Now()
|
|
|
|
// includes time spent in acquiring and holding a lock
|
|
|
|
latency := lockEnd.Sub(*lockStart)
|
2022-11-02 17:53:08 -05:00
|
|
|
monitoring.ObserveStorageLockLatency(is.metrics, latency, is.RootDir(), storageConstants.RWLOCK) // histogram
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (is *ObjectStorage) initRepo(name string) error {
|
|
|
|
repoDir := path.Join(is.rootDir, name)
|
|
|
|
|
2023-01-18 11:24:44 -05:00
|
|
|
if !zreg.FullNameRegexp.MatchString(name) {
|
2023-04-27 21:44:22 -05:00
|
|
|
is.log.Error().Str("repository", name).Msg("invalid repository name")
|
2023-01-18 11:24:44 -05:00
|
|
|
|
|
|
|
return zerr.ErrInvalidRepositoryName
|
|
|
|
}
|
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
// "oci-layout" file - create if it doesn't exist
|
|
|
|
ilPath := path.Join(repoDir, ispec.ImageLayoutFile)
|
|
|
|
if _, err := is.store.Stat(context.Background(), ilPath); err != nil {
|
|
|
|
il := ispec.ImageLayout{Version: ispec.ImageLayoutVersion}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
buf, err := json.Marshal(il)
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Msg("unable to marshal JSON")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := writeFile(is.store, ilPath, buf); err != nil {
|
|
|
|
is.log.Error().Err(err).Str("file", ilPath).Msg("unable to write file")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// "index.json" file - create if it doesn't exist
|
|
|
|
indexPath := path.Join(repoDir, "index.json")
|
|
|
|
if _, err := is.store.Stat(context.Background(), indexPath); err != nil {
|
|
|
|
index := ispec.Index{}
|
|
|
|
index.SchemaVersion = 2
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
buf, err := json.Marshal(index)
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Msg("unable to marshal JSON")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := writeFile(is.store, indexPath, buf); err != nil {
|
|
|
|
is.log.Error().Err(err).Str("file", ilPath).Msg("unable to write file")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// InitRepo creates an image repository under this store.
|
|
|
|
func (is *ObjectStorage) InitRepo(name string) error {
|
2021-12-21 08:19:40 -05:00
|
|
|
var lockLatency time.Time
|
|
|
|
|
|
|
|
is.Lock(&lockLatency)
|
|
|
|
defer is.Unlock(&lockLatency)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
|
|
|
return is.initRepo(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidateRepo validates that the repository layout is complaint with the OCI repo layout.
|
|
|
|
func (is *ObjectStorage) ValidateRepo(name string) (bool, error) {
|
2023-01-18 11:24:44 -05:00
|
|
|
if !zreg.FullNameRegexp.MatchString(name) {
|
|
|
|
return false, zerr.ErrInvalidRepositoryName
|
|
|
|
}
|
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
// https://github.com/opencontainers/image-spec/blob/master/image-layout.md#content
|
|
|
|
// at least, expect at least 3 entries - ["blobs", "oci-layout", "index.json"]
|
|
|
|
// and an additional/optional BlobUploadDir in each image store
|
|
|
|
// for objects storage we can not create empty dirs, so we check only against index.json and oci-layout
|
|
|
|
dir := path.Join(is.rootDir, name)
|
|
|
|
if fi, err := is.store.Stat(context.Background(), dir); err != nil || !fi.IsDir() {
|
2021-12-13 14:23:31 -05:00
|
|
|
return false, zerr.ErrRepoNotFound
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
files, err := is.store.List(context.Background(), dir)
|
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Str("dir", dir).Msg("unable to read directory")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
|
|
|
return false, zerr.ErrRepoNotFound
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
2022-10-05 05:21:14 -05:00
|
|
|
//nolint:gomnd
|
2021-07-16 22:53:05 -05:00
|
|
|
if len(files) < 2 {
|
2021-12-13 14:23:31 -05:00
|
|
|
return false, zerr.ErrRepoBadVersion
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
found := map[string]bool{
|
|
|
|
ispec.ImageLayoutFile: false,
|
|
|
|
"index.json": false,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range files {
|
2022-04-12 05:01:04 -05:00
|
|
|
_, err := is.store.Stat(context.Background(), file)
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
filename, err := filepath.Rel(dir, file)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
found[filename] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range found {
|
2022-11-02 17:53:08 -05:00
|
|
|
if !v && k != storageConstants.BlobUploadDir {
|
2021-07-16 22:53:05 -05:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
buf, err := is.store.GetContent(context.Background(), path.Join(dir, ispec.ImageLayoutFile))
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var il ispec.ImageLayout
|
|
|
|
if err := json.Unmarshal(buf, &il); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if il.Version != ispec.ImageLayoutVersion {
|
2021-12-13 14:23:31 -05:00
|
|
|
return false, zerr.ErrRepoBadVersion
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRepositories returns a list of all the repositories under this store.
|
|
|
|
func (is *ObjectStorage) GetRepositories() ([]string, error) {
|
2021-12-21 08:19:40 -05:00
|
|
|
var lockLatency time.Time
|
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
dir := is.rootDir
|
|
|
|
|
2021-12-21 08:19:40 -05:00
|
|
|
is.RLock(&lockLatency)
|
|
|
|
defer is.RUnlock(&lockLatency)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
|
|
|
stores := make([]string, 0)
|
2021-12-13 14:23:31 -05:00
|
|
|
err := is.store.Walk(context.Background(), dir, func(fileInfo driver.FileInfo) error {
|
2021-07-16 22:53:05 -05:00
|
|
|
if !fileInfo.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
rel, err := filepath.Rel(is.rootDir, fileInfo.Path())
|
|
|
|
if err != nil {
|
2021-12-13 14:23:31 -05:00
|
|
|
return nil //nolint:nilerr // ignore paths that are not under root dir
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if ok, err := is.ValidateRepo(rel); !ok || err != nil {
|
2021-12-13 14:23:31 -05:00
|
|
|
return nil //nolint:nilerr // ignore invalid repos
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
stores = append(stores, rel)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
// if the root directory is not yet created then return an empty slice of repositories
|
2021-12-13 14:23:31 -05:00
|
|
|
var perr driver.PathNotFoundError
|
|
|
|
if errors.As(err, &perr) {
|
2021-07-16 22:53:05 -05:00
|
|
|
return stores, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return stores, err
|
|
|
|
}
|
|
|
|
|
2022-09-23 00:27:56 -05:00
|
|
|
// GetNextRepository returns next repository under this store.
|
|
|
|
func (is *ObjectStorage) GetNextRepository(repo string) (string, error) {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
// GetImageTags returns a list of image tags available in the specified repository.
|
|
|
|
func (is *ObjectStorage) GetImageTags(repo string) ([]string, error) {
|
2022-11-22 13:13:08 -05:00
|
|
|
var lockLatency time.Time
|
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
dir := path.Join(is.rootDir, repo)
|
|
|
|
if fi, err := is.store.Stat(context.Background(), dir); err != nil || !fi.IsDir() {
|
2021-12-13 14:23:31 -05:00
|
|
|
return nil, zerr.ErrRepoNotFound
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
2022-11-22 13:13:08 -05:00
|
|
|
is.RLock(&lockLatency)
|
|
|
|
defer is.RUnlock(&lockLatency)
|
|
|
|
|
2022-09-30 12:35:16 -05:00
|
|
|
index, err := storage.GetIndex(is, repo, is.log)
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-09-30 12:35:16 -05:00
|
|
|
return storage.GetTagsByIndex(index), nil
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetImageManifest returns the image manifest of an image in the specific repository.
|
2022-10-22 15:46:13 -05:00
|
|
|
func (is *ObjectStorage) GetImageManifest(repo, reference string) ([]byte, godigest.Digest, string, error) {
|
2022-11-22 13:13:08 -05:00
|
|
|
var lockLatency time.Time
|
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
dir := path.Join(is.rootDir, repo)
|
|
|
|
if fi, err := is.store.Stat(context.Background(), dir); err != nil || !fi.IsDir() {
|
2021-12-13 14:23:31 -05:00
|
|
|
return nil, "", "", zerr.ErrRepoNotFound
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
2022-11-22 13:13:08 -05:00
|
|
|
is.RLock(&lockLatency)
|
|
|
|
defer is.RUnlock(&lockLatency)
|
|
|
|
|
2022-09-30 12:35:16 -05:00
|
|
|
index, err := storage.GetIndex(is, repo, is.log)
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
2022-09-30 12:35:16 -05:00
|
|
|
return nil, "", "", zerr.ErrRepoNotFound
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
2022-09-30 12:35:16 -05:00
|
|
|
manifestDesc, found := storage.GetManifestDescByReference(index, reference)
|
2021-07-16 22:53:05 -05:00
|
|
|
if !found {
|
2021-12-13 14:23:31 -05:00
|
|
|
return nil, "", "", zerr.ErrManifestNotFound
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
buf, err := is.GetBlobContent(repo, manifestDesc.Digest)
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
2022-09-30 12:35:16 -05:00
|
|
|
if errors.Is(err, zerr.ErrBlobNotFound) {
|
|
|
|
return nil, "", "", zerr.ErrManifestNotFound
|
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return nil, "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
var manifest ispec.Manifest
|
|
|
|
if err := json.Unmarshal(buf, &manifest); err != nil {
|
|
|
|
is.log.Error().Err(err).Str("dir", dir).Msg("invalid JSON")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return nil, "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
monitoring.IncDownloadCounter(is.metrics, repo)
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
return buf, manifestDesc.Digest, manifestDesc.MediaType, nil
|
2022-08-20 03:18:48 -05:00
|
|
|
}
|
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
// PutImageManifest adds an image manifest to the repository.
|
2022-08-20 03:18:48 -05:00
|
|
|
func (is *ObjectStorage) PutImageManifest(repo, reference, mediaType string, //nolint: gocyclo
|
2022-10-22 15:46:13 -05:00
|
|
|
body []byte,
|
2023-05-12 11:32:01 -05:00
|
|
|
) (godigest.Digest, godigest.Digest, error) {
|
2021-07-16 22:53:05 -05:00
|
|
|
if err := is.InitRepo(repo); err != nil {
|
|
|
|
is.log.Debug().Err(err).Msg("init repo")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2023-05-12 11:32:01 -05:00
|
|
|
return "", "", err
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
2022-11-22 13:13:08 -05:00
|
|
|
var lockLatency time.Time
|
|
|
|
|
|
|
|
is.Lock(&lockLatency)
|
|
|
|
defer is.Unlock(&lockLatency)
|
|
|
|
|
2022-09-30 12:35:16 -05:00
|
|
|
dig, err := storage.ValidateManifest(is, repo, reference, mediaType, body, is.log)
|
|
|
|
if err != nil {
|
2023-05-12 11:32:01 -05:00
|
|
|
return dig, "", err
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
2022-09-30 12:35:16 -05:00
|
|
|
refIsDigest := true
|
2021-07-16 22:53:05 -05:00
|
|
|
|
2022-09-30 12:35:16 -05:00
|
|
|
mDigest, err := storage.GetAndValidateRequestDigest(body, reference, is.log)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, zerr.ErrBadManifest) {
|
2023-05-12 11:32:01 -05:00
|
|
|
return mDigest, "", err
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
2022-09-30 12:35:16 -05:00
|
|
|
refIsDigest = false
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
2022-09-30 12:35:16 -05:00
|
|
|
index, err := storage.GetIndex(is, repo, is.log)
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
2023-05-12 11:32:01 -05:00
|
|
|
return "", "", err
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// create a new descriptor
|
2021-12-13 14:23:31 -05:00
|
|
|
desc := ispec.Descriptor{
|
|
|
|
MediaType: mediaType, Size: int64(len(body)), Digest: mDigest,
|
|
|
|
}
|
2022-09-30 12:35:16 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
if !refIsDigest {
|
|
|
|
desc.Annotations = map[string]string{ispec.AnnotationRefName: reference}
|
|
|
|
}
|
|
|
|
|
2023-05-12 11:32:01 -05:00
|
|
|
var subjectDigest godigest.Digest
|
|
|
|
|
|
|
|
artifactType := ""
|
|
|
|
|
|
|
|
if mediaType == ispec.MediaTypeImageManifest {
|
|
|
|
var manifest ispec.Manifest
|
|
|
|
|
|
|
|
err := json.Unmarshal(body, &manifest)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if manifest.Subject != nil {
|
|
|
|
subjectDigest = manifest.Subject.Digest
|
|
|
|
}
|
|
|
|
|
|
|
|
artifactType = zcommon.GetManifestArtifactType(manifest)
|
|
|
|
}
|
|
|
|
|
2022-09-30 12:35:16 -05:00
|
|
|
updateIndex, oldDgst, err := storage.CheckIfIndexNeedsUpdate(&index, &desc, is.log)
|
|
|
|
if err != nil {
|
2023-05-12 11:32:01 -05:00
|
|
|
return "", "", err
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if !updateIndex {
|
2023-05-12 11:32:01 -05:00
|
|
|
return desc.Digest, subjectDigest, nil
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// write manifest to "blobs"
|
2022-09-30 12:35:16 -05:00
|
|
|
dir := path.Join(is.rootDir, repo, "blobs", mDigest.Algorithm().String())
|
2021-07-16 22:53:05 -05:00
|
|
|
manifestPath := path.Join(dir, mDigest.Encoded())
|
|
|
|
|
|
|
|
if err = is.store.PutContent(context.Background(), manifestPath, body); err != nil {
|
|
|
|
is.log.Error().Err(err).Str("file", manifestPath).Msg("unable to write")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2023-05-12 11:32:01 -05:00
|
|
|
return "", "", err
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
2022-09-30 12:35:16 -05:00
|
|
|
err = storage.UpdateIndexWithPrunedImageManifests(is, &index, repo, desc, oldDgst, is.log)
|
|
|
|
if err != nil {
|
2023-05-12 11:32:01 -05:00
|
|
|
return "", "", err
|
2022-08-20 03:18:48 -05:00
|
|
|
}
|
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
// now update "index.json"
|
|
|
|
index.Manifests = append(index.Manifests, desc)
|
|
|
|
dir = path.Join(is.rootDir, repo)
|
|
|
|
indexPath := path.Join(dir, "index.json")
|
|
|
|
|
2022-09-30 12:35:16 -05:00
|
|
|
buf, err := json.Marshal(index)
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Str("file", indexPath).Msg("unable to marshal JSON")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2023-05-12 11:32:01 -05:00
|
|
|
return "", "", err
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
2023-05-12 11:32:01 -05:00
|
|
|
// update the descriptors artifact type in order to check for signatures when applying the linter
|
|
|
|
desc.ArtifactType = artifactType
|
2023-05-10 12:15:33 -05:00
|
|
|
|
2022-06-24 08:08:47 -05:00
|
|
|
// apply linter only on images, not signatures
|
2022-09-30 12:35:16 -05:00
|
|
|
pass, err := storage.ApplyLinter(is, is.linter, repo, desc)
|
|
|
|
if !pass {
|
2023-04-27 21:44:22 -05:00
|
|
|
is.log.Error().Err(err).Str("repository", repo).Str("reference", reference).Msg("linter didn't pass")
|
2022-09-30 12:35:16 -05:00
|
|
|
|
2023-05-12 11:32:01 -05:00
|
|
|
return "", "", err
|
2022-06-24 08:08:47 -05:00
|
|
|
}
|
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
if err = is.store.PutContent(context.Background(), indexPath, buf); err != nil {
|
|
|
|
is.log.Error().Err(err).Str("file", manifestPath).Msg("unable to write")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2023-05-12 11:32:01 -05:00
|
|
|
return "", "", err
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
monitoring.SetStorageUsage(is.metrics, is.rootDir, repo)
|
|
|
|
monitoring.IncUploadCounter(is.metrics, repo)
|
|
|
|
|
2023-05-12 11:32:01 -05:00
|
|
|
return desc.Digest, subjectDigest, nil
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteImageManifest deletes the image manifest from the repository.
|
2022-11-18 12:35:28 -05:00
|
|
|
func (is *ObjectStorage) DeleteImageManifest(repo, reference string, detectCollisions bool) error {
|
2021-12-21 08:19:40 -05:00
|
|
|
var lockLatency time.Time
|
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
dir := path.Join(is.rootDir, repo)
|
|
|
|
if fi, err := is.store.Stat(context.Background(), dir); err != nil || !fi.IsDir() {
|
2021-12-13 14:23:31 -05:00
|
|
|
return zerr.ErrRepoNotFound
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
2022-11-22 13:13:08 -05:00
|
|
|
is.Lock(&lockLatency)
|
|
|
|
defer is.Unlock(&lockLatency)
|
|
|
|
|
2022-09-30 12:35:16 -05:00
|
|
|
index, err := storage.GetIndex(is, repo, is.log)
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-24 14:00:22 -05:00
|
|
|
manifestDesc, err := storage.RemoveManifestDescByReference(&index, reference, detectCollisions)
|
2022-11-18 12:35:28 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-30 12:35:16 -05:00
|
|
|
err = storage.UpdateIndexWithPrunedImageManifests(is, &index, repo, manifestDesc, manifestDesc.Digest, is.log)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
// now update "index.json"
|
|
|
|
dir = path.Join(is.rootDir, repo)
|
|
|
|
file := path.Join(dir, "index.json")
|
|
|
|
|
2022-09-30 12:35:16 -05:00
|
|
|
buf, err := json.Marshal(index)
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := writeFile(is.store, file, buf); err != nil {
|
|
|
|
is.log.Debug().Str("deleting reference", reference).Msg("")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete blob only when blob digest not present in manifest entry.
|
|
|
|
// e.g. 1.0.1 & 1.0.2 have same blob digest so if we delete 1.0.1, blob should not be removed.
|
|
|
|
toDelete := true
|
|
|
|
|
2022-09-30 12:35:16 -05:00
|
|
|
for _, manifest := range index.Manifests {
|
|
|
|
if manifestDesc.Digest.String() == manifest.Digest.String() {
|
2021-07-16 22:53:05 -05:00
|
|
|
toDelete = false
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if toDelete {
|
2022-09-30 12:35:16 -05:00
|
|
|
p := path.Join(dir, "blobs", manifestDesc.Digest.Algorithm().String(), manifestDesc.Digest.Encoded())
|
2021-07-16 22:53:05 -05:00
|
|
|
|
|
|
|
err = is.store.Delete(context.Background(), p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
monitoring.SetStorageUsage(is.metrics, is.rootDir, repo)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// BlobUploadPath returns the upload path for a blob in this store.
|
2022-03-21 12:37:23 -05:00
|
|
|
func (is *ObjectStorage) BlobUploadPath(repo, uuid string) string {
|
2021-07-16 22:53:05 -05:00
|
|
|
dir := path.Join(is.rootDir, repo)
|
2022-11-02 17:53:08 -05:00
|
|
|
blobUploadPath := path.Join(dir, storageConstants.BlobUploadDir, uuid)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
|
|
|
return blobUploadPath
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBlobUpload returns the unique ID for an upload in progress.
|
|
|
|
func (is *ObjectStorage) NewBlobUpload(repo string) (string, error) {
|
|
|
|
if err := is.InitRepo(repo); err != nil {
|
|
|
|
is.log.Error().Err(err).Msg("error initializing repo")
|
|
|
|
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
uuid, err := guuid.NewV4()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
uid := uuid.String()
|
2021-07-16 22:53:05 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
blobUploadPath := is.BlobUploadPath(repo, uid)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
2022-10-20 11:36:58 -05:00
|
|
|
// create multipart upload (append false)
|
|
|
|
_, err = is.store.Writer(context.Background(), blobUploadPath, false)
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
2022-10-20 11:36:58 -05:00
|
|
|
return "", err
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
return uid, nil
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlobUpload returns the current size of a blob upload.
|
2022-03-21 12:37:23 -05:00
|
|
|
func (is *ObjectStorage) GetBlobUpload(repo, uuid string) (int64, error) {
|
2021-07-16 22:53:05 -05:00
|
|
|
blobUploadPath := is.BlobUploadPath(repo, uuid)
|
|
|
|
|
2022-10-20 11:36:58 -05:00
|
|
|
writer, err := is.store.Writer(context.Background(), blobUploadPath, true)
|
|
|
|
if err != nil {
|
|
|
|
if errors.As(err, &driver.PathNotFoundError{}) {
|
|
|
|
return -1, zerr.ErrUploadNotFound
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
2022-10-20 11:36:58 -05:00
|
|
|
return -1, err
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
2022-10-20 11:36:58 -05:00
|
|
|
return writer.Size(), nil
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// PutBlobChunkStreamed appends another chunk of data to the specified blob. It returns
|
|
|
|
// the number of actual bytes to the blob.
|
2022-03-21 12:37:23 -05:00
|
|
|
func (is *ObjectStorage) PutBlobChunkStreamed(repo, uuid string, body io.Reader) (int64, error) {
|
2021-07-16 22:53:05 -05:00
|
|
|
if err := is.InitRepo(repo); err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
blobUploadPath := is.BlobUploadPath(repo, uuid)
|
|
|
|
|
2022-10-20 11:36:58 -05:00
|
|
|
file, err := is.store.Writer(context.Background(), blobUploadPath, true)
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
2022-10-20 11:36:58 -05:00
|
|
|
if errors.As(err, &driver.PathNotFoundError{}) {
|
|
|
|
return -1, zerr.ErrUploadNotFound
|
|
|
|
}
|
2021-07-16 22:53:05 -05:00
|
|
|
|
2022-10-20 11:36:58 -05:00
|
|
|
is.log.Error().Err(err).Msg("failed to continue multipart upload")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
|
|
|
|
_, err = buf.ReadFrom(body)
|
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Msg("failed to read blob")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
nbytes, err := file.Write(buf.Bytes())
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Msg("failed to append to file")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
return int64(nbytes), err
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// PutBlobChunk writes another chunk of data to the specified blob. It returns
|
|
|
|
// the number of actual bytes to the blob.
|
2022-03-21 12:37:23 -05:00
|
|
|
func (is *ObjectStorage) PutBlobChunk(repo, uuid string, from, to int64,
|
|
|
|
body io.Reader,
|
|
|
|
) (int64, error) {
|
2021-07-16 22:53:05 -05:00
|
|
|
if err := is.InitRepo(repo); err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
blobUploadPath := is.BlobUploadPath(repo, uuid)
|
|
|
|
|
2022-10-20 11:36:58 -05:00
|
|
|
file, err := is.store.Writer(context.Background(), blobUploadPath, true)
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
2022-10-20 11:36:58 -05:00
|
|
|
if errors.As(err, &driver.PathNotFoundError{}) {
|
|
|
|
return -1, zerr.ErrUploadNotFound
|
|
|
|
}
|
2021-07-16 22:53:05 -05:00
|
|
|
|
2022-10-20 11:36:58 -05:00
|
|
|
is.log.Error().Err(err).Msg("failed to continue multipart upload")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
if from != file.Size() {
|
|
|
|
is.log.Error().Int64("expected", from).Int64("actual", file.Size()).
|
|
|
|
Msg("invalid range start for blob upload")
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
return -1, zerr.ErrBadUploadRange
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
|
|
|
|
_, err = buf.ReadFrom(body)
|
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Msg("failed to read blob")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
nbytes, err := file.Write(buf.Bytes())
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Msg("failed to append to file")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
return int64(nbytes), err
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// BlobUploadInfo returns the current blob size in bytes.
|
2022-03-21 12:37:23 -05:00
|
|
|
func (is *ObjectStorage) BlobUploadInfo(repo, uuid string) (int64, error) {
|
2021-07-16 22:53:05 -05:00
|
|
|
blobUploadPath := is.BlobUploadPath(repo, uuid)
|
|
|
|
|
2022-10-20 11:36:58 -05:00
|
|
|
writer, err := is.store.Writer(context.Background(), blobUploadPath, true)
|
|
|
|
if err != nil {
|
|
|
|
if errors.As(err, &driver.PathNotFoundError{}) {
|
|
|
|
return -1, zerr.ErrUploadNotFound
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
2022-10-20 11:36:58 -05:00
|
|
|
return -1, err
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
2022-10-20 11:36:58 -05:00
|
|
|
return writer.Size(), nil
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// FinishBlobUpload finalizes the blob upload and moves blob the repository.
|
2022-10-22 15:46:13 -05:00
|
|
|
func (is *ObjectStorage) FinishBlobUpload(repo, uuid string, body io.Reader, dstDigest godigest.Digest) error {
|
|
|
|
if err := dstDigest.Validate(); err != nil {
|
|
|
|
return err
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
src := is.BlobUploadPath(repo, uuid)
|
|
|
|
|
|
|
|
// complete multiUploadPart
|
|
|
|
fileWriter, err := is.store.Writer(context.Background(), src, true)
|
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Str("blob", src).Msg("failed to open blob")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
|
|
|
return zerr.ErrBadBlobDigest
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := fileWriter.Commit(); err != nil {
|
|
|
|
is.log.Error().Err(err).Msg("failed to commit file")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := fileWriter.Close(); err != nil {
|
|
|
|
is.log.Error().Err(err).Msg("failed to close file")
|
2022-11-22 13:13:08 -05:00
|
|
|
|
|
|
|
return err
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fileReader, err := is.store.Reader(context.Background(), src, 0)
|
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Str("blob", src).Msg("failed to open file")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
|
|
|
return zerr.ErrUploadNotFound
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
2022-11-22 13:13:08 -05:00
|
|
|
defer fileReader.Close()
|
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
srcDigest, err := godigest.FromReader(fileReader)
|
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Str("blob", src).Msg("failed to open blob")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
|
|
|
return zerr.ErrBadBlobDigest
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if srcDigest != dstDigest {
|
|
|
|
is.log.Error().Str("srcDigest", srcDigest.String()).
|
|
|
|
Str("dstDigest", dstDigest.String()).Msg("actual digest not equal to expected digest")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
|
|
|
return zerr.ErrBadBlobDigest
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
dst := is.BlobPath(repo, dstDigest)
|
|
|
|
|
2022-03-02 13:29:48 -05:00
|
|
|
var lockLatency time.Time
|
|
|
|
|
|
|
|
is.Lock(&lockLatency)
|
|
|
|
defer is.Unlock(&lockLatency)
|
|
|
|
|
2022-11-02 17:53:08 -05:00
|
|
|
if is.dedupe && fmt.Sprintf("%v", is.cache) != fmt.Sprintf("%v", nil) {
|
2022-04-12 05:01:04 -05:00
|
|
|
if err := is.DedupeBlob(src, dstDigest, dst); err != nil {
|
|
|
|
is.log.Error().Err(err).Str("src", src).Str("dstDigest", dstDigest.String()).
|
|
|
|
Str("dst", dst).Msg("unable to dedupe blob")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2022-04-12 05:01:04 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := is.store.Move(context.Background(), src, dst); err != nil {
|
|
|
|
is.log.Error().Err(err).Str("src", src).Str("dstDigest", dstDigest.String()).
|
|
|
|
Str("dst", dst).Msg("unable to finish blob")
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FullBlobUpload handles a full blob upload, and no partial session is created.
|
2022-10-22 15:46:13 -05:00
|
|
|
func (is *ObjectStorage) FullBlobUpload(repo string, body io.Reader, dstDigest godigest.Digest) (string, int64, error) {
|
|
|
|
if err := dstDigest.Validate(); err != nil {
|
2021-07-16 22:53:05 -05:00
|
|
|
return "", -1, err
|
|
|
|
}
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
if err := is.InitRepo(repo); err != nil {
|
|
|
|
return "", -1, err
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
u, err := guuid.NewV4()
|
|
|
|
if err != nil {
|
|
|
|
return "", -1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
uuid := u.String()
|
|
|
|
src := is.BlobUploadPath(repo, uuid)
|
|
|
|
digester := sha256.New()
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
|
|
|
|
_, err = buf.ReadFrom(body)
|
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Msg("failed to read blob")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return "", -1, err
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
nbytes, err := writeFile(is.store, src, buf.Bytes())
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Msg("failed to write blob")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return "", -1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = digester.Write(buf.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Msg("digester failed to write")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return "", -1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
srcDigest := godigest.NewDigestFromEncoded(godigest.SHA256, fmt.Sprintf("%x", digester.Sum(nil)))
|
|
|
|
if srcDigest != dstDigest {
|
|
|
|
is.log.Error().Str("srcDigest", srcDigest.String()).
|
|
|
|
Str("dstDigest", dstDigest.String()).Msg("actual digest not equal to expected digest")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
|
|
|
return "", -1, zerr.ErrBadBlobDigest
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
2021-12-21 08:19:40 -05:00
|
|
|
var lockLatency time.Time
|
|
|
|
|
|
|
|
is.Lock(&lockLatency)
|
|
|
|
defer is.Unlock(&lockLatency)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
|
|
|
dst := is.BlobPath(repo, dstDigest)
|
|
|
|
|
2022-11-02 17:53:08 -05:00
|
|
|
if is.dedupe && fmt.Sprintf("%v", is.cache) != fmt.Sprintf("%v", nil) {
|
2022-04-12 05:01:04 -05:00
|
|
|
if err := is.DedupeBlob(src, dstDigest, dst); err != nil {
|
|
|
|
is.log.Error().Err(err).Str("src", src).Str("dstDigest", dstDigest.String()).
|
|
|
|
Str("dst", dst).Msg("unable to dedupe blob")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2022-04-12 05:01:04 -05:00
|
|
|
return "", -1, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := is.store.Move(context.Background(), src, dst); err != nil {
|
|
|
|
is.log.Error().Err(err).Str("src", src).Str("dstDigest", dstDigest.String()).
|
|
|
|
Str("dst", dst).Msg("unable to finish blob")
|
|
|
|
|
|
|
|
return "", -1, err
|
|
|
|
}
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
return uuid, int64(nbytes), nil
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (is *ObjectStorage) DedupeBlob(src string, dstDigest godigest.Digest, dst string) error {
|
2022-04-12 05:01:04 -05:00
|
|
|
retry:
|
|
|
|
is.log.Debug().Str("src", src).Str("dstDigest", dstDigest.String()).Str("dst", dst).Msg("dedupe: enter")
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
dstRecord, err := is.cache.GetBlob(dstDigest)
|
2022-04-12 05:01:04 -05:00
|
|
|
if err := test.Error(err); err != nil && !errors.Is(err, zerr.ErrCacheMiss) {
|
|
|
|
is.log.Error().Err(err).Str("blobPath", dst).Msg("dedupe: unable to lookup blob record")
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if dstRecord == "" {
|
|
|
|
// cache record doesn't exist, so first disk and cache entry for this digest
|
2022-10-22 15:46:13 -05:00
|
|
|
if err := is.cache.PutBlob(dstDigest, dst); err != nil {
|
2022-04-12 05:01:04 -05:00
|
|
|
is.log.Error().Err(err).Str("blobPath", dst).Msg("dedupe: unable to insert blob record")
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// move the blob from uploads to final dest
|
|
|
|
if err := is.store.Move(context.Background(), src, dst); err != nil {
|
|
|
|
is.log.Error().Err(err).Str("src", src).Str("dst", dst).Msg("dedupe: unable to rename blob")
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
is.log.Debug().Str("src", src).Str("dst", dst).Msg("dedupe: rename")
|
|
|
|
} else {
|
|
|
|
// cache record exists, but due to GC and upgrades from older versions,
|
|
|
|
// disk content and cache records may go out of sync
|
|
|
|
_, err := is.store.Stat(context.Background(), dstRecord)
|
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Str("blobPath", dstRecord).Msg("dedupe: unable to stat")
|
|
|
|
// the actual blob on disk may have been removed by GC, so sync the cache
|
2022-10-22 15:46:13 -05:00
|
|
|
err := is.cache.DeleteBlob(dstDigest, dstRecord)
|
2022-04-12 05:01:04 -05:00
|
|
|
if err = test.Error(err); err != nil {
|
2022-10-05 05:21:14 -05:00
|
|
|
//nolint:lll
|
2022-04-12 05:01:04 -05:00
|
|
|
is.log.Error().Err(err).Str("dstDigest", dstDigest.String()).Str("dst", dst).Msg("dedupe: unable to delete blob record")
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
goto retry
|
|
|
|
}
|
|
|
|
|
|
|
|
fileInfo, err := is.store.Stat(context.Background(), dst)
|
|
|
|
if err != nil && !errors.As(err, &driver.PathNotFoundError{}) {
|
|
|
|
is.log.Error().Err(err).Str("blobPath", dstRecord).Msg("dedupe: unable to stat")
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// prevent overwrite original blob
|
|
|
|
if fileInfo == nil && dstRecord != dst {
|
|
|
|
// put empty file so that we are compliant with oci layout, this will act as a deduped blob
|
|
|
|
err = is.store.PutContent(context.Background(), dst, []byte{})
|
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Str("blobPath", dstRecord).Msg("dedupe: unable to write empty file")
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2022-09-07 17:12:14 -05:00
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
if err := is.cache.PutBlob(dstDigest, dst); err != nil {
|
2022-09-07 17:12:14 -05:00
|
|
|
is.log.Error().Err(err).Str("blobPath", dst).Msg("dedupe: unable to insert blob record")
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2022-04-12 05:01:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// remove temp blobupload
|
|
|
|
if err := is.store.Delete(context.Background(), src); err != nil {
|
|
|
|
is.log.Error().Err(err).Str("src", src).Msg("dedupe: unable to remove blob")
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
is.log.Debug().Str("src", src).Msg("dedupe: remove")
|
|
|
|
}
|
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-23 00:27:56 -05:00
|
|
|
func (is *ObjectStorage) RunGCRepo(repo string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (is *ObjectStorage) RunGCPeriodically(interval time.Duration, sch *scheduler.Scheduler) {
|
2022-03-21 13:40:37 -05:00
|
|
|
}
|
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
// DeleteBlobUpload deletes an existing blob upload that is currently in progress.
|
2022-03-21 12:37:23 -05:00
|
|
|
func (is *ObjectStorage) DeleteBlobUpload(repo, uuid string) error {
|
2021-07-16 22:53:05 -05:00
|
|
|
blobUploadPath := is.BlobUploadPath(repo, uuid)
|
2022-10-20 11:36:58 -05:00
|
|
|
|
|
|
|
writer, err := is.store.Writer(context.Background(), blobUploadPath, true)
|
|
|
|
if err != nil {
|
|
|
|
if errors.As(err, &driver.PathNotFoundError{}) {
|
|
|
|
return zerr.ErrUploadNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer writer.Close()
|
|
|
|
|
|
|
|
if err := writer.Cancel(); err != nil {
|
2021-07-16 22:53:05 -05:00
|
|
|
is.log.Error().Err(err).Str("blobUploadPath", blobUploadPath).Msg("error deleting blob upload")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// BlobPath returns the repository path of a blob.
|
|
|
|
func (is *ObjectStorage) BlobPath(repo string, digest godigest.Digest) string {
|
|
|
|
return path.Join(is.rootDir, repo, "blobs", digest.Algorithm().String(), digest.Encoded())
|
|
|
|
}
|
|
|
|
|
|
|
|
// CheckBlob verifies a blob and returns true if the blob is correct.
|
2022-10-22 15:46:13 -05:00
|
|
|
func (is *ObjectStorage) CheckBlob(repo string, digest godigest.Digest) (bool, int64, error) {
|
2021-12-21 08:19:40 -05:00
|
|
|
var lockLatency time.Time
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
if err := digest.Validate(); err != nil {
|
|
|
|
return false, -1, err
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
blobPath := is.BlobPath(repo, digest)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
2022-11-02 17:53:08 -05:00
|
|
|
if is.dedupe && fmt.Sprintf("%v", is.cache) != fmt.Sprintf("%v", nil) {
|
2022-04-12 05:01:04 -05:00
|
|
|
is.Lock(&lockLatency)
|
|
|
|
defer is.Unlock(&lockLatency)
|
|
|
|
} else {
|
|
|
|
is.RLock(&lockLatency)
|
|
|
|
defer is.RUnlock(&lockLatency)
|
|
|
|
}
|
2021-07-16 22:53:05 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
binfo, err := is.store.Stat(context.Background(), blobPath)
|
2022-04-12 05:01:04 -05:00
|
|
|
if err == nil && binfo.Size() > 0 {
|
|
|
|
is.log.Debug().Str("blob path", blobPath).Msg("blob path found")
|
|
|
|
|
|
|
|
return true, binfo.Size(), nil
|
|
|
|
}
|
|
|
|
// otherwise is a 'deduped' blob (empty file)
|
|
|
|
|
|
|
|
// Check blobs in cache
|
|
|
|
dstRecord, err := is.checkCacheBlob(digest)
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
2022-10-22 15:46:13 -05:00
|
|
|
is.log.Error().Err(err).Str("digest", digest.String()).Msg("cache: not found")
|
2021-07-16 22:53:05 -05:00
|
|
|
|
2022-04-12 05:01:04 -05:00
|
|
|
return false, -1, zerr.ErrBlobNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
blobSize, err := is.copyBlob(repo, blobPath, dstRecord)
|
|
|
|
if err != nil {
|
|
|
|
return false, -1, zerr.ErrBlobNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
// put deduped blob in cache
|
|
|
|
if err := is.cache.PutBlob(digest, blobPath); err != nil {
|
|
|
|
is.log.Error().Err(err).Str("blobPath", blobPath).Msg("dedupe: unable to insert blob record")
|
2021-07-16 22:53:05 -05:00
|
|
|
|
|
|
|
return false, -1, err
|
|
|
|
}
|
|
|
|
|
2022-04-12 05:01:04 -05:00
|
|
|
return true, blobSize, nil
|
|
|
|
}
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
func (is *ObjectStorage) checkCacheBlob(digest godigest.Digest) (string, error) {
|
|
|
|
if err := digest.Validate(); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2022-11-02 17:53:08 -05:00
|
|
|
if fmt.Sprintf("%v", is.cache) == fmt.Sprintf("%v", nil) {
|
2022-04-12 05:01:04 -05:00
|
|
|
return "", zerr.ErrBlobNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
dstRecord, err := is.cache.GetBlob(digest)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2022-10-11 12:05:39 -05:00
|
|
|
if _, err := is.store.Stat(context.Background(), dstRecord); err != nil {
|
|
|
|
is.log.Error().Err(err).Str("blob", dstRecord).Msg("failed to stat blob")
|
|
|
|
|
|
|
|
// the actual blob on disk may have been removed by GC, so sync the cache
|
|
|
|
if err := is.cache.DeleteBlob(digest, dstRecord); err != nil {
|
2022-10-22 15:46:13 -05:00
|
|
|
is.log.Error().Err(err).Str("digest", digest.String()).Str("blobPath", dstRecord).
|
|
|
|
Msg("unable to remove blob path from cache")
|
2022-10-11 12:05:39 -05:00
|
|
|
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", zerr.ErrBlobNotFound
|
|
|
|
}
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
is.log.Debug().Str("digest", digest.String()).Str("dstRecord", dstRecord).Msg("cache: found dedupe record")
|
2022-04-12 05:01:04 -05:00
|
|
|
|
|
|
|
return dstRecord, nil
|
|
|
|
}
|
|
|
|
|
2023-01-09 15:37:44 -05:00
|
|
|
func (is *ObjectStorage) copyBlob(repo string, blobPath, dstRecord string) (int64, error) {
|
2022-04-12 05:01:04 -05:00
|
|
|
if err := is.initRepo(repo); err != nil {
|
2023-04-27 21:44:22 -05:00
|
|
|
is.log.Error().Err(err).Str("repository", repo).Msg("unable to initialize an empty repo")
|
2022-04-12 05:01:04 -05:00
|
|
|
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := is.store.PutContent(context.Background(), blobPath, []byte{}); err != nil {
|
|
|
|
is.log.Error().Err(err).Str("blobPath", blobPath).Str("link", dstRecord).Msg("dedupe: unable to link")
|
|
|
|
|
|
|
|
return -1, zerr.ErrBlobNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
// return original blob with content instead of the deduped one (blobPath)
|
|
|
|
binfo, err := is.store.Stat(context.Background(), dstRecord)
|
|
|
|
if err == nil {
|
|
|
|
return binfo.Size(), nil
|
|
|
|
}
|
2021-07-16 22:53:05 -05:00
|
|
|
|
2022-04-12 05:01:04 -05:00
|
|
|
return -1, zerr.ErrBlobNotFound
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
2022-08-30 17:12:10 -05:00
|
|
|
// blobStream is using to serve blob range requests.
|
|
|
|
type blobStream struct {
|
|
|
|
reader io.Reader
|
|
|
|
closer io.Closer
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewBlobStream(readCloser io.ReadCloser, from, to int64) (io.ReadCloser, error) {
|
|
|
|
return &blobStream{reader: io.LimitReader(readCloser, to-from+1), closer: readCloser}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bs *blobStream) Read(buf []byte) (int, error) {
|
|
|
|
return bs.reader.Read(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bs *blobStream) Close() error {
|
|
|
|
return bs.closer.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlobPartial returns a partial stream to read the blob.
|
|
|
|
// blob selector instead of directly downloading the blob.
|
2022-10-22 15:46:13 -05:00
|
|
|
func (is *ObjectStorage) GetBlobPartial(repo string, digest godigest.Digest, mediaType string, from, to int64,
|
2022-08-30 17:12:10 -05:00
|
|
|
) (io.ReadCloser, int64, int64, error) {
|
|
|
|
var lockLatency time.Time
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
if err := digest.Validate(); err != nil {
|
|
|
|
return nil, -1, -1, err
|
2022-08-30 17:12:10 -05:00
|
|
|
}
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
blobPath := is.BlobPath(repo, digest)
|
2022-08-30 17:12:10 -05:00
|
|
|
|
|
|
|
is.RLock(&lockLatency)
|
|
|
|
defer is.RUnlock(&lockLatency)
|
|
|
|
|
|
|
|
binfo, err := is.store.Stat(context.Background(), blobPath)
|
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Str("blob", blobPath).Msg("failed to stat blob")
|
|
|
|
|
|
|
|
return nil, -1, -1, zerr.ErrBlobNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
end := to
|
|
|
|
|
|
|
|
if to < 0 || to >= binfo.Size() {
|
|
|
|
end = binfo.Size() - 1
|
|
|
|
}
|
|
|
|
|
|
|
|
blobHandle, err := is.store.Reader(context.Background(), blobPath, from)
|
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Str("blob", blobPath).Msg("failed to open blob")
|
|
|
|
|
|
|
|
return nil, -1, -1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
blobReadCloser, err := NewBlobStream(blobHandle, from, end)
|
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Str("blob", blobPath).Msg("failed to open blob stream")
|
|
|
|
|
|
|
|
return nil, -1, -1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// is a 'deduped' blob?
|
|
|
|
if binfo.Size() == 0 {
|
|
|
|
defer blobReadCloser.Close()
|
|
|
|
|
|
|
|
// Check blobs in cache
|
|
|
|
dstRecord, err := is.checkCacheBlob(digest)
|
|
|
|
if err != nil {
|
2022-10-22 15:46:13 -05:00
|
|
|
is.log.Error().Err(err).Str("digest", digest.String()).Msg("cache: not found")
|
2022-08-30 17:12:10 -05:00
|
|
|
|
|
|
|
return nil, -1, -1, zerr.ErrBlobNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
binfo, err := is.store.Stat(context.Background(), dstRecord)
|
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Str("blob", dstRecord).Msg("failed to stat blob")
|
|
|
|
|
|
|
|
return nil, -1, -1, zerr.ErrBlobNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
end := to
|
|
|
|
|
|
|
|
if to < 0 || to >= binfo.Size() {
|
|
|
|
end = binfo.Size() - 1
|
|
|
|
}
|
|
|
|
|
|
|
|
blobHandle, err := is.store.Reader(context.Background(), dstRecord, from)
|
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Str("blob", dstRecord).Msg("failed to open blob")
|
|
|
|
|
|
|
|
return nil, -1, -1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
blobReadCloser, err := NewBlobStream(blobHandle, from, end)
|
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Str("blob", blobPath).Msg("failed to open blob stream")
|
|
|
|
|
|
|
|
return nil, -1, -1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return blobReadCloser, end - from + 1, binfo.Size(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// The caller function is responsible for calling Close()
|
|
|
|
return blobReadCloser, end - from + 1, binfo.Size(), nil
|
|
|
|
}
|
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
// GetBlob returns a stream to read the blob.
|
|
|
|
// blob selector instead of directly downloading the blob.
|
2022-10-22 15:46:13 -05:00
|
|
|
func (is *ObjectStorage) GetBlob(repo string, digest godigest.Digest, mediaType string) (io.ReadCloser, int64, error) {
|
2021-12-21 08:19:40 -05:00
|
|
|
var lockLatency time.Time
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
if err := digest.Validate(); err != nil {
|
|
|
|
return nil, -1, err
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
blobPath := is.BlobPath(repo, digest)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
2021-12-21 08:19:40 -05:00
|
|
|
is.RLock(&lockLatency)
|
|
|
|
defer is.RUnlock(&lockLatency)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
binfo, err := is.store.Stat(context.Background(), blobPath)
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Str("blob", blobPath).Msg("failed to stat blob")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
|
|
|
return nil, -1, zerr.ErrBlobNotFound
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
2022-08-19 05:38:59 -05:00
|
|
|
blobReadCloser, err := is.store.Reader(context.Background(), blobPath, 0)
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Str("blob", blobPath).Msg("failed to open blob")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return nil, -1, err
|
|
|
|
}
|
|
|
|
|
2022-08-30 17:12:10 -05:00
|
|
|
// is a 'deduped' blob?
|
2023-04-07 11:49:24 -05:00
|
|
|
if binfo.Size() == 0 {
|
2022-04-12 05:01:04 -05:00
|
|
|
// Check blobs in cache
|
|
|
|
dstRecord, err := is.checkCacheBlob(digest)
|
2022-09-07 17:12:14 -05:00
|
|
|
if err != nil {
|
2022-10-22 15:46:13 -05:00
|
|
|
is.log.Error().Err(err).Str("digest", digest.String()).Msg("cache: not found")
|
2022-04-12 05:01:04 -05:00
|
|
|
|
2022-09-07 17:12:14 -05:00
|
|
|
return nil, -1, zerr.ErrBlobNotFound
|
|
|
|
}
|
2022-04-12 05:01:04 -05:00
|
|
|
|
2022-09-07 17:12:14 -05:00
|
|
|
binfo, err := is.store.Stat(context.Background(), dstRecord)
|
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Str("blob", dstRecord).Msg("failed to stat blob")
|
2022-04-12 05:01:04 -05:00
|
|
|
|
2022-09-07 17:12:14 -05:00
|
|
|
return nil, -1, zerr.ErrBlobNotFound
|
2022-04-12 05:01:04 -05:00
|
|
|
}
|
2022-09-07 17:12:14 -05:00
|
|
|
|
|
|
|
blobReadCloser, err := is.store.Reader(context.Background(), dstRecord, 0)
|
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Str("blob", dstRecord).Msg("failed to open blob")
|
|
|
|
|
|
|
|
return nil, -1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return blobReadCloser, binfo.Size(), nil
|
2022-04-12 05:01:04 -05:00
|
|
|
}
|
|
|
|
|
2022-08-19 05:38:59 -05:00
|
|
|
// The caller function is responsible for calling Close()
|
|
|
|
return blobReadCloser, binfo.Size(), nil
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
2022-11-22 13:13:08 -05:00
|
|
|
// GetBlobContent returns blob contents, SHOULD lock from outside.
|
2022-10-22 15:46:13 -05:00
|
|
|
func (is *ObjectStorage) GetBlobContent(repo string, digest godigest.Digest) ([]byte, error) {
|
2022-11-22 13:13:08 -05:00
|
|
|
if err := digest.Validate(); err != nil {
|
2021-07-16 22:53:05 -05:00
|
|
|
return []byte{}, err
|
|
|
|
}
|
|
|
|
|
2022-11-22 13:13:08 -05:00
|
|
|
blobPath := is.BlobPath(repo, digest)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
2022-11-22 13:13:08 -05:00
|
|
|
binfo, err := is.store.Stat(context.Background(), blobPath)
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
2022-11-22 13:13:08 -05:00
|
|
|
is.log.Error().Err(err).Str("blob", blobPath).Msg("failed to stat blob")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2022-11-22 13:13:08 -05:00
|
|
|
return []byte{}, zerr.ErrBlobNotFound
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
2022-11-22 13:13:08 -05:00
|
|
|
blobBuf, err := is.store.GetContent(context.Background(), blobPath)
|
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Str("blob", blobPath).Msg("failed to open blob")
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// is a 'deduped' blob?
|
2023-04-07 11:49:24 -05:00
|
|
|
if binfo.Size() == 0 {
|
2022-11-22 13:13:08 -05:00
|
|
|
// Check blobs in cache
|
|
|
|
dstRecord, err := is.checkCacheBlob(digest)
|
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Str("digest", digest.String()).Msg("cache: not found")
|
|
|
|
|
|
|
|
return nil, zerr.ErrBlobNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
blobBuf, err := is.store.GetContent(context.Background(), dstRecord)
|
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Str("blob", dstRecord).Msg("failed to open blob")
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return blobBuf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return blobBuf, nil
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
2023-01-26 13:13:12 -05:00
|
|
|
func (is *ObjectStorage) GetReferrers(repo string, gdigest godigest.Digest, artifactTypes []string,
|
2022-11-08 03:38:16 -05:00
|
|
|
) (ispec.Index, error) {
|
2022-11-22 13:13:08 -05:00
|
|
|
var lockLatency time.Time
|
|
|
|
|
|
|
|
is.RLock(&lockLatency)
|
|
|
|
defer is.RUnlock(&lockLatency)
|
|
|
|
|
2023-01-26 13:13:12 -05:00
|
|
|
return storage.GetReferrers(is, repo, gdigest, artifactTypes, is.log)
|
2022-11-08 03:38:16 -05:00
|
|
|
}
|
|
|
|
|
2022-11-15 01:21:49 -05:00
|
|
|
func (is *ObjectStorage) GetOrasReferrers(repo string, gdigest godigest.Digest, artifactType string,
|
2022-10-22 15:46:13 -05:00
|
|
|
) ([]artifactspec.Descriptor, error) {
|
2022-11-22 13:13:08 -05:00
|
|
|
var lockLatency time.Time
|
|
|
|
|
|
|
|
is.RLock(&lockLatency)
|
|
|
|
defer is.RUnlock(&lockLatency)
|
|
|
|
|
2022-11-15 01:21:49 -05:00
|
|
|
return storage.GetOrasReferrers(is, repo, gdigest, artifactType, is.log)
|
2021-10-29 21:10:55 -05:00
|
|
|
}
|
|
|
|
|
2022-11-22 13:13:08 -05:00
|
|
|
// GetIndexContent returns index.json contents, SHOULD lock from outside.
|
2021-07-16 22:53:05 -05:00
|
|
|
func (is *ObjectStorage) GetIndexContent(repo string) ([]byte, error) {
|
|
|
|
dir := path.Join(is.rootDir, repo)
|
|
|
|
|
|
|
|
buf, err := is.store.GetContent(context.Background(), path.Join(dir, "index.json"))
|
|
|
|
if err != nil {
|
2022-11-22 13:13:08 -05:00
|
|
|
if errors.Is(err, driver.PathNotFoundError{}) {
|
|
|
|
is.log.Error().Err(err).Str("dir", dir).Msg("index.json doesn't exist")
|
|
|
|
|
|
|
|
return []byte{}, zerr.ErrRepoNotFound
|
|
|
|
}
|
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
is.log.Error().Err(err).Str("dir", dir).Msg("failed to read index.json")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2022-11-22 13:13:08 -05:00
|
|
|
return []byte{}, err
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return buf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteBlob removes the blob from the repository.
|
2022-10-22 15:46:13 -05:00
|
|
|
func (is *ObjectStorage) DeleteBlob(repo string, digest godigest.Digest) error {
|
2021-12-21 08:19:40 -05:00
|
|
|
var lockLatency time.Time
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
if err := digest.Validate(); err != nil {
|
|
|
|
return err
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
blobPath := is.BlobPath(repo, digest)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
2021-12-21 08:19:40 -05:00
|
|
|
is.Lock(&lockLatency)
|
|
|
|
defer is.Unlock(&lockLatency)
|
2021-07-16 22:53:05 -05:00
|
|
|
|
2022-10-22 15:46:13 -05:00
|
|
|
_, err := is.store.Stat(context.Background(), blobPath)
|
2021-07-16 22:53:05 -05:00
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Str("blob", blobPath).Msg("failed to stat blob")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
|
|
|
return zerr.ErrBlobNotFound
|
2021-07-16 22:53:05 -05:00
|
|
|
}
|
|
|
|
|
2022-11-02 17:53:08 -05:00
|
|
|
if fmt.Sprintf("%v", is.cache) != fmt.Sprintf("%v", nil) {
|
2022-04-12 05:01:04 -05:00
|
|
|
dstRecord, err := is.cache.GetBlob(digest)
|
|
|
|
if err != nil && !errors.Is(err, zerr.ErrCacheMiss) {
|
|
|
|
is.log.Error().Err(err).Str("blobPath", dstRecord).Msg("dedupe: unable to lookup blob record")
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove cache entry and move blob contents to the next candidate if there is any
|
2023-05-19 11:51:15 -05:00
|
|
|
if ok := is.cache.HasBlob(digest, blobPath); ok {
|
|
|
|
if err := is.cache.DeleteBlob(digest, blobPath); err != nil {
|
|
|
|
is.log.Error().Err(err).Str("digest", digest.String()).Str("blobPath", blobPath).
|
|
|
|
Msg("unable to remove blob path from cache")
|
2022-04-12 05:01:04 -05:00
|
|
|
|
2023-05-19 11:51:15 -05:00
|
|
|
return err
|
|
|
|
}
|
2022-04-12 05:01:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// if the deleted blob is one with content
|
|
|
|
if dstRecord == blobPath {
|
|
|
|
// get next candidate
|
|
|
|
dstRecord, err := is.cache.GetBlob(digest)
|
|
|
|
if err != nil && !errors.Is(err, zerr.ErrCacheMiss) {
|
|
|
|
is.log.Error().Err(err).Str("blobPath", dstRecord).Msg("dedupe: unable to lookup blob record")
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we have a new candidate move the blob content to it
|
|
|
|
if dstRecord != "" {
|
|
|
|
if err := is.store.Move(context.Background(), blobPath, dstRecord); err != nil {
|
|
|
|
is.log.Error().Err(err).Str("blobPath", blobPath).Msg("unable to remove blob path")
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
if err := is.store.Delete(context.Background(), blobPath); err != nil {
|
|
|
|
is.log.Error().Err(err).Str("blobPath", blobPath).Msg("unable to remove blob path")
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2021-07-16 22:53:05 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do not use for multipart upload, buf must not be empty.
|
|
|
|
// If you want to create an empty file use is.store.PutContent().
|
2021-12-13 14:23:31 -05:00
|
|
|
func writeFile(store driver.StorageDriver, filepath string, buf []byte) (int, error) {
|
2021-07-16 22:53:05 -05:00
|
|
|
var n int
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
if stwr, err := store.Writer(context.Background(), filepath, false); err == nil {
|
|
|
|
defer stwr.Close()
|
2021-07-16 22:53:05 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
if n, err = stwr.Write(buf); err != nil {
|
2021-07-16 22:53:05 -05:00
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
if err := stwr.Commit(); err != nil {
|
2021-07-16 22:53:05 -05:00
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return n, nil
|
|
|
|
}
|
2023-04-07 11:49:24 -05:00
|
|
|
|
|
|
|
func (is *ObjectStorage) GetNextDigestWithBlobPaths(lastDigests []godigest.Digest) (godigest.Digest, []string, error) {
|
|
|
|
var lockLatency time.Time
|
|
|
|
|
|
|
|
dir := is.rootDir
|
|
|
|
|
|
|
|
is.RLock(&lockLatency)
|
|
|
|
defer is.RUnlock(&lockLatency)
|
|
|
|
|
|
|
|
var duplicateBlobs []string
|
|
|
|
|
|
|
|
var digest godigest.Digest
|
|
|
|
|
|
|
|
err := is.store.Walk(context.Background(), dir, func(fileInfo driver.FileInfo) error {
|
|
|
|
if fileInfo.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
blobDigest := godigest.NewDigestFromEncoded("sha256", path.Base(fileInfo.Path()))
|
|
|
|
if err := blobDigest.Validate(); err != nil {
|
|
|
|
return nil //nolint:nilerr // ignore files which are not blobs
|
|
|
|
}
|
|
|
|
|
2023-05-10 12:15:33 -05:00
|
|
|
if digest == "" && !zcommon.DContains(lastDigests, blobDigest) {
|
2023-04-07 11:49:24 -05:00
|
|
|
digest = blobDigest
|
|
|
|
}
|
|
|
|
|
|
|
|
if blobDigest == digest {
|
|
|
|
duplicateBlobs = append(duplicateBlobs, fileInfo.Path())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
// if the root directory is not yet created
|
|
|
|
var perr driver.PathNotFoundError
|
|
|
|
|
|
|
|
if errors.As(err, &perr) {
|
|
|
|
return digest, duplicateBlobs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return digest, duplicateBlobs, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (is *ObjectStorage) getOriginalBlobFromDisk(duplicateBlobs []string) (string, error) {
|
|
|
|
for _, blobPath := range duplicateBlobs {
|
|
|
|
binfo, err := is.store.Stat(context.Background(), blobPath)
|
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Str("path", blobPath).Msg("rebuild dedupe: failed to stat blob")
|
|
|
|
|
|
|
|
return "", zerr.ErrBlobNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
if binfo.Size() > 0 {
|
|
|
|
return blobPath, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", zerr.ErrBlobNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
func (is *ObjectStorage) getOriginalBlob(digest godigest.Digest, duplicateBlobs []string) (string, error) {
|
2023-04-27 02:09:46 -05:00
|
|
|
var originalBlob string
|
2023-04-07 11:49:24 -05:00
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
originalBlob, err = is.checkCacheBlob(digest)
|
|
|
|
if err != nil && !errors.Is(err, zerr.ErrBlobNotFound) && !errors.Is(err, zerr.ErrCacheMiss) {
|
|
|
|
is.log.Error().Err(err).Msg("rebuild dedupe: unable to find blob in cache")
|
|
|
|
|
|
|
|
return originalBlob, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we still don't have, search it
|
|
|
|
if originalBlob == "" {
|
|
|
|
is.log.Warn().Msg("rebuild dedupe: failed to find blob in cache, searching it in s3...")
|
|
|
|
// a rebuild dedupe was attempted in the past
|
|
|
|
// get original blob, should be found otherwise exit with error
|
|
|
|
|
|
|
|
originalBlob, err = is.getOriginalBlobFromDisk(duplicateBlobs)
|
|
|
|
if err != nil {
|
|
|
|
return originalBlob, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-27 21:44:22 -05:00
|
|
|
is.log.Info().Str("originalBlob", originalBlob).Msg("rebuild dedupe: found original blob")
|
2023-04-07 11:49:24 -05:00
|
|
|
|
|
|
|
return originalBlob, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (is *ObjectStorage) dedupeBlobs(digest godigest.Digest, duplicateBlobs []string) error {
|
|
|
|
if fmt.Sprintf("%v", is.cache) == fmt.Sprintf("%v", nil) {
|
|
|
|
is.log.Error().Err(zerr.ErrDedupeRebuild).Msg("no cache driver found, can not dedupe blobs")
|
|
|
|
|
|
|
|
return zerr.ErrDedupeRebuild
|
|
|
|
}
|
|
|
|
|
2023-04-27 21:44:22 -05:00
|
|
|
is.log.Info().Str("digest", digest.String()).Msg("rebuild dedupe: deduping blobs for digest")
|
2023-04-07 11:49:24 -05:00
|
|
|
|
|
|
|
var originalBlob string
|
|
|
|
|
|
|
|
// rebuild from dedupe false to true
|
|
|
|
for _, blobPath := range duplicateBlobs {
|
|
|
|
binfo, err := is.store.Stat(context.Background(), blobPath)
|
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Str("path", blobPath).Msg("rebuild dedupe: failed to stat blob")
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if binfo.Size() == 0 {
|
|
|
|
is.log.Warn().Msg("rebuild dedupe: found file without content, trying to find the original blob")
|
|
|
|
// a rebuild dedupe was attempted in the past
|
|
|
|
// get original blob, should be found otherwise exit with error
|
|
|
|
if originalBlob == "" {
|
|
|
|
originalBlob, err = is.getOriginalBlob(digest, duplicateBlobs)
|
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Msg("rebuild dedupe: unable to find original blob")
|
|
|
|
|
|
|
|
return zerr.ErrDedupeRebuild
|
|
|
|
}
|
|
|
|
|
|
|
|
// cache original blob
|
|
|
|
if ok := is.cache.HasBlob(digest, originalBlob); !ok {
|
|
|
|
if err := is.cache.PutBlob(digest, originalBlob); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// cache dedupe blob
|
|
|
|
if ok := is.cache.HasBlob(digest, blobPath); !ok {
|
|
|
|
if err := is.cache.PutBlob(digest, blobPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// cache it
|
|
|
|
if ok := is.cache.HasBlob(digest, blobPath); !ok {
|
|
|
|
if err := is.cache.PutBlob(digest, blobPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we have an original blob cached then we can safely dedupe the rest of them
|
|
|
|
if originalBlob != "" {
|
|
|
|
if err := is.store.PutContent(context.Background(), blobPath, []byte{}); err != nil {
|
|
|
|
is.log.Error().Err(err).Str("path", blobPath).Msg("rebuild dedupe: unable to dedupe blob")
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// mark blob as preserved
|
|
|
|
originalBlob = blobPath
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-27 21:44:22 -05:00
|
|
|
is.log.Info().Str("digest", digest.String()).Msg("rebuild dedupe: deduping blobs for digest finished successfully")
|
2023-04-07 11:49:24 -05:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (is *ObjectStorage) restoreDedupedBlobs(digest godigest.Digest, duplicateBlobs []string) error {
|
2023-04-27 21:44:22 -05:00
|
|
|
is.log.Info().Str("digest", digest.String()).Msg("rebuild dedupe: restoring deduped blobs for digest")
|
2023-04-07 11:49:24 -05:00
|
|
|
|
|
|
|
// first we need to find the original blob, either in cache or by checking each blob size
|
|
|
|
originalBlob, err := is.getOriginalBlob(digest, duplicateBlobs)
|
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Msg("rebuild dedupe: unable to find original blob")
|
|
|
|
|
|
|
|
return zerr.ErrDedupeRebuild
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, blobPath := range duplicateBlobs {
|
|
|
|
binfo, err := is.store.Stat(context.Background(), blobPath)
|
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Str("path", blobPath).Msg("rebuild dedupe: failed to stat blob")
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we find a deduped blob, then copy original blob content to deduped one
|
|
|
|
if binfo.Size() == 0 {
|
|
|
|
// move content from original blob to deduped one
|
|
|
|
buf, err := is.store.GetContent(context.Background(), originalBlob)
|
|
|
|
if err != nil {
|
|
|
|
is.log.Error().Err(err).Str("path", originalBlob).Msg("rebuild dedupe: failed to get original blob content")
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = writeFile(is.store, blobPath, buf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
is.log.Info().Str("digest", digest.String()).
|
2023-04-27 21:44:22 -05:00
|
|
|
Msg("rebuild dedupe: restoring deduped blobs for digest finished successfully")
|
2023-04-07 11:49:24 -05:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (is *ObjectStorage) RunDedupeForDigest(digest godigest.Digest, dedupe bool, duplicateBlobs []string) error {
|
|
|
|
var lockLatency time.Time
|
|
|
|
|
|
|
|
is.Lock(&lockLatency)
|
|
|
|
defer is.Unlock(&lockLatency)
|
|
|
|
|
|
|
|
if dedupe {
|
|
|
|
return is.dedupeBlobs(digest, duplicateBlobs)
|
|
|
|
}
|
|
|
|
|
|
|
|
return is.restoreDedupedBlobs(digest, duplicateBlobs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (is *ObjectStorage) RunDedupeBlobs(interval time.Duration, sch *scheduler.Scheduler) {
|
|
|
|
generator := &storage.DedupeTaskGenerator{
|
|
|
|
ImgStore: is,
|
|
|
|
Dedupe: is.dedupe,
|
|
|
|
Log: is.log,
|
|
|
|
}
|
|
|
|
|
|
|
|
sch.SubmitGenerator(generator, interval, scheduler.HighPriority)
|
|
|
|
}
|