2016-04-27 20:49:01 +02:00
|
|
|
package storage
|
2016-01-19 23:26:15 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/docker/distribution"
|
|
|
|
"github.com/docker/distribution/context"
|
|
|
|
"github.com/docker/distribution/digest"
|
|
|
|
"github.com/docker/distribution/manifest/schema2"
|
|
|
|
"github.com/docker/distribution/reference"
|
|
|
|
"github.com/docker/distribution/registry/storage/driver"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-04-27 20:49:01 +02:00
|
|
|
// MarkAndSweep performs a mark and sweep of registry data
|
|
|
|
func MarkAndSweep(ctx context.Context, storageDriver driver.StorageDriver, registry distribution.Namespace, dryRun bool) 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{})
|
2016-03-25 00:03:25 +01:00
|
|
|
err := repositoryEnumerator.Enumerate(ctx, func(repoName string) error {
|
2016-04-27 20:49:01 +02:00
|
|
|
if dryRun {
|
|
|
|
emit(repoName)
|
|
|
|
}
|
2016-03-24 00:42:50 +01:00
|
|
|
|
2016-01-19 23:26:15 +01:00
|
|
|
var err error
|
|
|
|
named, err := reference.ParseNamed(repoName)
|
|
|
|
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 {
|
2016-03-25 00:03:25 +01:00
|
|
|
// Mark the manifest's blob
|
2016-04-27 20:49:01 +02:00
|
|
|
if dryRun {
|
|
|
|
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-04-27 20:49:01 +02:00
|
|
|
if dryRun {
|
|
|
|
emit("%s: marking blob %s", repoName, descriptor.Digest)
|
|
|
|
}
|
2016-01-19 23:26:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
switch manifest.(type) {
|
|
|
|
case *schema2.DeserializedManifest:
|
|
|
|
config := manifest.(*schema2.DeserializedManifest).Config
|
2016-04-27 20:49:01 +02:00
|
|
|
if dryRun {
|
|
|
|
emit("%s: marking configuration %s", repoName, config.Digest)
|
|
|
|
}
|
2016-01-19 23:26:15 +01:00
|
|
|
markSet[config.Digest] = struct{}{}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2016-04-26 06:14:00 +02:00
|
|
|
if err != nil {
|
|
|
|
// 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-01-19 23:26:15 +01:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to mark: %v\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// sweep
|
|
|
|
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)
|
|
|
|
}
|
2016-04-27 20:49:01 +02:00
|
|
|
if dryRun {
|
|
|
|
emit("\n%d blobs marked, %d blobs eligible for deletion", len(markSet), len(deleteSet))
|
|
|
|
}
|
2016-01-19 23:26:15 +01:00
|
|
|
// Construct vacuum
|
2016-04-27 20:49:01 +02:00
|
|
|
vacuum := NewVacuum(ctx, storageDriver)
|
2016-01-19 23:26:15 +01:00
|
|
|
for dgst := range deleteSet {
|
2016-03-24 00:42:50 +01:00
|
|
|
if dryRun {
|
2016-04-27 20:49:01 +02:00
|
|
|
emit("blob eligible for deletion: %s", dgst)
|
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 {
|
|
|
|
return fmt.Errorf("failed to delete blob %s: %v\n", dgst, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|