From 682b6f7e9da321d54cb049b4ebed25606719bf3d Mon Sep 17 00:00:00 2001 From: Jason Hall Date: Mon, 15 Aug 2022 10:18:10 -0400 Subject: [PATCH] registry: implement blob deletion --- pkg/registry/blobs.go | 38 +++++++++++++++++++++++++++++++++++ pkg/registry/registry_test.go | 7 +++++++ 2 files changed, 45 insertions(+) diff --git a/pkg/registry/blobs.go b/pkg/registry/blobs.go index 9677d69d9..1c715c9de 100644 --- a/pkg/registry/blobs.go +++ b/pkg/registry/blobs.go @@ -76,6 +76,13 @@ type blobPutHandler interface { Put(ctx context.Context, repo string, h v1.Hash, rc io.ReadCloser) error } +// blobDeleteHandler is an extension interface representing a blob storage +// backend that can delete blob contents. +type blobDeleteHandler interface { + // Delete the blob contents. + Delete(ctx context.Context, repo string, h v1.Hash) error +} + // redirectError represents a signal that the blob handler doesn't have the blob // contents, but that those contents are at another location which registry // clients should redirect to. @@ -129,6 +136,17 @@ func (m *memHandler) Put(_ context.Context, _ string, h v1.Hash, rc io.ReadClose m.m[h.String()] = all return nil } +func (m *memHandler) Delete(_ context.Context, _ string, h v1.Hash) error { + m.lock.Lock() + defer m.lock.Unlock() + + if _, found := m.m[h.String()]; !found { + return errNotFound + } + + delete(m.m, h.String()) + return nil +} // blobs type blobs struct { @@ -435,6 +453,26 @@ func (b *blobs) handle(resp http.ResponseWriter, req *http.Request) *regError { resp.WriteHeader(http.StatusCreated) return nil + case http.MethodDelete: + bdh, ok := b.blobHandler.(blobDeleteHandler) + if !ok { + return regErrUnsupported + } + + h, err := v1.NewHash(target) + if err != nil { + return ®Error{ + Status: http.StatusBadRequest, + Code: "NAME_INVALID", + Message: "invalid digest", + } + } + if err := bdh.Delete(req.Context(), repo, h); err != nil { + return regErrInternal(err) + } + resp.WriteHeader(http.StatusAccepted) + return nil + default: return ®Error{ Status: http.StatusBadRequest, diff --git a/pkg/registry/registry_test.go b/pkg/registry/registry_test.go index 6186aafc1..25501d583 100644 --- a/pkg/registry/registry_test.go +++ b/pkg/registry/registry_test.go @@ -149,6 +149,13 @@ func TestCalls(t *testing.T) { "Docker-Content-Digest": "sha256:2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae", }, }, + { + Description: "DELETE blob", + Digests: map[string]string{"sha256:2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae": "foo"}, + Method: "DELETE", + URL: "/v2/foo/blobs/sha256:2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae", + Code: http.StatusAccepted, + }, { Description: "blob url with no container", Method: "GET",