2016-04-27 20:49:01 +02:00
|
|
|
package storage
|
2016-01-19 23:26:15 +01:00
|
|
|
|
|
|
|
import (
|
2017-08-12 00:31:16 +02:00
|
|
|
"context"
|
2016-01-19 23:26:15 +01:00
|
|
|
"fmt"
|
|
|
|
|
2020-08-24 13:18:39 +02:00
|
|
|
"github.com/distribution/distribution/v3"
|
|
|
|
"github.com/distribution/distribution/v3/reference"
|
|
|
|
"github.com/distribution/distribution/v3/registry/storage/driver"
|
2016-12-17 01:28:34 +01:00
|
|
|
"github.com/opencontainers/go-digest"
|
2016-01-19 23:26:15 +01:00
|
|
|
)
|
|
|
|
|
2016-03-25 00:03:25 +01:00
|
|
|
func emit(format string, a ...interface{}) {
|
2016-04-27 20:49:01 +02:00
|
|
|
fmt.Printf(format+"\n", a...)
|
2016-03-24 00:42:50 +01:00
|
|
|
}
|
|
|
|
|
2017-06-06 10:02:47 +02:00
|
|
|
// GCOpts contains options for garbage collector
|
|
|
|
type GCOpts struct {
|
|
|
|
DryRun bool
|
|
|
|
RemoveUntagged bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// ManifestDel contains manifest structure which will be deleted
|
|
|
|
type ManifestDel struct {
|
|
|
|
Name string
|
|
|
|
Digest digest.Digest
|
|
|
|
Tags []string
|
|
|
|
}
|
|
|
|
|
2016-04-27 20:49:01 +02:00
|
|
|
// MarkAndSweep performs a mark and sweep of registry data
|
2017-06-06 10:02:47 +02:00
|
|
|
func MarkAndSweep(ctx context.Context, storageDriver driver.StorageDriver, registry distribution.Namespace, opts GCOpts) error {
|
2016-01-19 23:26:15 +01:00
|
|
|
repositoryEnumerator, ok := registry.(distribution.RepositoryEnumerator)
|
|
|
|
if !ok {
|
2016-03-30 20:35:24 +02:00
|
|
|
return fmt.Errorf("unable to convert Namespace to RepositoryEnumerator")
|
2016-01-19 23:26:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// mark
|
|
|
|
markSet := make(map[digest.Digest]struct{})
|
2017-06-06 10:02:47 +02:00
|
|
|
manifestArr := make([]ManifestDel, 0)
|
2016-03-25 00:03:25 +01:00
|
|
|
err := repositoryEnumerator.Enumerate(ctx, func(repoName string) error {
|
2016-10-13 17:04:19 +02:00
|
|
|
emit(repoName)
|
2016-03-24 00:42:50 +01:00
|
|
|
|
2016-01-19 23:26:15 +01:00
|
|
|
var err error
|
2017-01-14 02:06:03 +01:00
|
|
|
named, err := reference.WithName(repoName)
|
2016-01-19 23:26:15 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse repo name %s: %v", repoName, err)
|
|
|
|
}
|
|
|
|
repository, err := registry.Repository(ctx, named)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to construct repository: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
manifestService, err := repository.Manifests(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to construct manifest service: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
manifestEnumerator, ok := manifestService.(distribution.ManifestEnumerator)
|
|
|
|
if !ok {
|
2016-03-30 20:35:24 +02:00
|
|
|
return fmt.Errorf("unable to convert ManifestService into ManifestEnumerator")
|
2016-01-19 23:26:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
err = manifestEnumerator.Enumerate(ctx, func(dgst digest.Digest) error {
|
2017-06-06 10:02:47 +02:00
|
|
|
if opts.RemoveUntagged {
|
|
|
|
// fetch all tags where this manifest is the latest one
|
|
|
|
tags, err := repository.Tags(ctx).Lookup(ctx, distribution.Descriptor{Digest: dgst})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to retrieve tags for digest %v: %v", dgst, err)
|
|
|
|
}
|
|
|
|
if len(tags) == 0 {
|
|
|
|
emit("manifest eligible for deletion: %s", dgst)
|
|
|
|
// fetch all tags from repository
|
|
|
|
// all of these tags could contain manifest in history
|
|
|
|
// which means that we need check (and delete) those references when deleting manifest
|
|
|
|
allTags, err := repository.Tags(ctx).All(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to retrieve tags %v", err)
|
|
|
|
}
|
|
|
|
manifestArr = append(manifestArr, ManifestDel{Name: repoName, Digest: dgst, Tags: allTags})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2016-03-25 00:03:25 +01:00
|
|
|
// Mark the manifest's blob
|
2016-10-13 17:04:19 +02:00
|
|
|
emit("%s: marking manifest %s ", repoName, dgst)
|
2016-01-19 23:26:15 +01:00
|
|
|
markSet[dgst] = struct{}{}
|
|
|
|
|
|
|
|
manifest, err := manifestService.Get(ctx, dgst)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to retrieve manifest for digest %v: %v", dgst, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
descriptors := manifest.References()
|
|
|
|
for _, descriptor := range descriptors {
|
|
|
|
markSet[descriptor.Digest] = struct{}{}
|
2016-10-13 17:04:19 +02:00
|
|
|
emit("%s: marking blob %s", repoName, descriptor.Digest)
|
2016-01-19 23:26:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2019-02-05 01:42:44 +01:00
|
|
|
// In certain situations such as unfinished uploads, deleting all
|
|
|
|
// tags in S3 or removing the _manifests folder manually, this
|
|
|
|
// error may be of type PathNotFound.
|
|
|
|
//
|
|
|
|
// In these cases we can continue marking other manifests safely.
|
|
|
|
if _, ok := err.(driver.PathNotFoundError); ok {
|
|
|
|
return nil
|
2016-04-26 06:14:00 +02:00
|
|
|
}
|
|
|
|
|
2016-01-19 23:26:15 +01:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
2016-11-14 23:03:01 +01:00
|
|
|
return fmt.Errorf("failed to mark: %v", err)
|
2016-01-19 23:26:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// sweep
|
2017-06-06 10:02:47 +02:00
|
|
|
vacuum := NewVacuum(ctx, storageDriver)
|
|
|
|
if !opts.DryRun {
|
|
|
|
for _, obj := range manifestArr {
|
|
|
|
err = vacuum.RemoveManifest(obj.Name, obj.Digest, obj.Tags)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to delete manifest %s: %v", obj.Digest, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-19 23:26:15 +01:00
|
|
|
blobService := registry.Blobs()
|
|
|
|
deleteSet := make(map[digest.Digest]struct{})
|
|
|
|
err = blobService.Enumerate(ctx, func(dgst digest.Digest) error {
|
|
|
|
// check if digest is in markSet. If not, delete it!
|
|
|
|
if _, ok := markSet[dgst]; !ok {
|
|
|
|
deleteSet[dgst] = struct{}{}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2016-03-30 20:35:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error enumerating blobs: %v", err)
|
|
|
|
}
|
2017-06-06 10:02:47 +02:00
|
|
|
emit("\n%d blobs marked, %d blobs and %d manifests eligible for deletion", len(markSet), len(deleteSet), len(manifestArr))
|
2016-01-19 23:26:15 +01:00
|
|
|
for dgst := range deleteSet {
|
2016-10-13 17:04:19 +02:00
|
|
|
emit("blob eligible for deletion: %s", dgst)
|
2017-06-06 10:02:47 +02:00
|
|
|
if opts.DryRun {
|
2016-03-24 00:42:50 +01:00
|
|
|
continue
|
|
|
|
}
|
2016-01-19 23:26:15 +01:00
|
|
|
err = vacuum.RemoveBlob(string(dgst))
|
|
|
|
if err != nil {
|
2016-11-14 23:03:01 +01:00
|
|
|
return fmt.Errorf("failed to delete blob %s: %v", dgst, err)
|
2016-01-19 23:26:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|