-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Oleg Bulatov <[email protected]>
- Loading branch information
Oleg Bulatov
committed
Jun 14, 2017
1 parent
322171b
commit c930106
Showing
3 changed files
with
230 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
package server | ||
|
||
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" | ||
"github.com/docker/distribution/registry/storage/driver" | ||
|
||
kerrors "k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
imageapi "github.com/openshift/origin/pkg/image/api" | ||
) | ||
|
||
func imageStreamHasManifestDigest(is *imageapi.ImageStream, dgst digest.Digest) bool { | ||
for _, tagEventList := range is.Status.Tags { | ||
for _, tagEvent := range tagEventList.Items { | ||
if tagEvent.Image == string(dgst) { | ||
return true | ||
} | ||
} | ||
} | ||
return false | ||
} | ||
|
||
// Prune removes blobs which are not used by Images in OpenShift. | ||
// | ||
// TODO(dmage): remove layer links to a blob if the blob is removed or it doesn't belong to the ImageStream. | ||
// TODO(dmage): keep young blobs (docker/distribution#2297). | ||
func Prune(ctx context.Context, storageDriver driver.StorageDriver, registry distribution.Namespace, registryClient RegistryClient) { | ||
logger := context.GetLogger(ctx) | ||
|
||
repositoryEnumerator, ok := registry.(distribution.RepositoryEnumerator) | ||
if !ok { | ||
logger.Fatal("unable to convert Namespace to RepositoryEnumerator") | ||
} | ||
|
||
oc, _, err := registryClient.Clients() | ||
if err != nil { | ||
logger.Fatalf("error getting clients: %s", err) | ||
} | ||
|
||
imageList, err := oc.Images().List(metav1.ListOptions{}) | ||
if err != nil { | ||
logger.Fatalf("error listing images: %s", err) | ||
} | ||
|
||
inuse := make(map[string]string) | ||
for _, image := range imageList.Items { | ||
// Keep the manifest. | ||
inuse[image.Name] = image.DockerImageReference | ||
|
||
// Keep the config for a schema 2 manifest. | ||
if image.DockerImageManifestMediaType == schema2.MediaTypeManifest { | ||
inuse[image.DockerImageMetadata.ID] = image.DockerImageReference | ||
} | ||
|
||
// Keep image layers. | ||
for _, layer := range image.DockerImageLayers { | ||
inuse[layer.Name] = image.DockerImageReference | ||
} | ||
} | ||
|
||
var reposToDelete []string | ||
err = repositoryEnumerator.Enumerate(ctx, func(repoName string) error { | ||
logger.Debugln("Processing repository", repoName) | ||
|
||
named, err := reference.WithName(repoName) | ||
if err != nil { | ||
return fmt.Errorf("failed to parse the repo name %s: %v", repoName, err) | ||
} | ||
|
||
ref, err := imageapi.ParseDockerImageReference(repoName) | ||
if err != nil { | ||
return fmt.Errorf("failed to parse the image reference %s: %v", repoName, err) | ||
} | ||
|
||
is, err := oc.ImageStreams(ref.Namespace).Get(ref.Name, metav1.GetOptions{}) | ||
if kerrors.IsNotFound(err) { | ||
logger.Printf("The image stream %s/%s is not found, will remove the whole repository", ref.Namespace, ref.Name) | ||
|
||
// We cannot delete the repository at this point, because it would break Enumerate. | ||
reposToDelete = append(reposToDelete, repoName) | ||
|
||
return nil | ||
} else if err != nil { | ||
return fmt.Errorf("failed to get the image stream %s: %v", repoName, err) | ||
} | ||
|
||
repository, err := registry.Repository(ctx, named) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
manifestService, err := repository.Manifests(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
manifestEnumerator, ok := manifestService.(distribution.ManifestEnumerator) | ||
if !ok { | ||
return fmt.Errorf("unable to convert ManifestService into ManifestEnumerator") | ||
} | ||
|
||
err = manifestEnumerator.Enumerate(ctx, func(dgst digest.Digest) error { | ||
if imageReference, ok := inuse[string(dgst)]; ok && imageStreamHasManifestDigest(is, dgst) { | ||
logger.Debugf("Keeping the manifest %s@%s (it belongs to the image %s)", repoName, dgst, imageReference) | ||
return nil | ||
} | ||
|
||
logger.Printf("Deleting the manifest: %s@%s", repoName, dgst) | ||
err = manifestService.Delete(ctx, dgst) | ||
if err != nil { | ||
return fmt.Errorf("failed to delete the manifest %s: %s", dgst, err) | ||
} | ||
|
||
return nil | ||
}) | ||
if e, ok := err.(driver.PathNotFoundError); ok { | ||
logger.Printf("Skipped manifests pruning for the repository %s: %s", repoName, e) | ||
} else if err != nil { | ||
return fmt.Errorf("failed to prune manifests in the repository %s: %s", repoName, err) | ||
} | ||
|
||
return nil | ||
}) | ||
if e, ok := err.(driver.PathNotFoundError); ok { | ||
logger.Warnf("No repositories are found: %s", e) | ||
return | ||
} else if err != nil { | ||
logger.Fatal(err) | ||
} | ||
|
||
vacuum := storage.NewVacuum(ctx, storageDriver) | ||
|
||
logger.Debugln("Removing repositories") | ||
for _, repoName := range reposToDelete { | ||
err = vacuum.RemoveRepository(repoName) | ||
if err != nil { | ||
logger.Fatal("Failed to remove the repository %s: %v", repoName, err) | ||
} | ||
} | ||
|
||
logger.Debugln("Processing blobs") | ||
err = registry.Blobs().Enumerate(ctx, func(dgst digest.Digest) error { | ||
if imageReference, ok := inuse[string(dgst)]; ok { | ||
logger.Debugf("Keeping the blob %s (it belongs to the image %s)", dgst, imageReference) | ||
return nil | ||
} | ||
|
||
err := vacuum.RemoveBlob(string(dgst)) | ||
if err != nil { | ||
return fmt.Errorf("failed to delete the blob %s: %s", dgst, err) | ||
} | ||
|
||
return nil | ||
}) | ||
if err != nil { | ||
logger.Fatal(err) | ||
} | ||
} |