forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.go
79 lines (67 loc) · 2.08 KB
/
admin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package server
import (
"fmt"
"net/http"
"github.com/docker/distribution"
"github.com/docker/distribution/context"
ctxu "github.com/docker/distribution/context"
"github.com/docker/distribution/digest"
"github.com/docker/distribution/registry/api/errcode"
"github.com/docker/distribution/registry/api/v2"
"github.com/docker/distribution/registry/handlers"
"github.com/docker/distribution/registry/storage"
storagedriver "github.com/docker/distribution/registry/storage/driver"
gorillahandlers "github.com/gorilla/handlers"
)
// BlobDispatcher takes the request context and builds the appropriate handler
// for handling blob requests.
func BlobDispatcher(ctx *handlers.Context, r *http.Request) http.Handler {
reference := ctxu.GetStringValue(ctx, "vars.digest")
dgst, _ := digest.ParseDigest(reference)
blobHandler := &blobHandler{
Context: ctx,
Digest: dgst,
}
return gorillahandlers.MethodHandler{
"DELETE": http.HandlerFunc(blobHandler.Delete),
}
}
// blobHandler handles http operations on blobs.
type blobHandler struct {
*handlers.Context
Digest digest.Digest
}
// Delete deletes the blob from the storage backend.
func (bh *blobHandler) Delete(w http.ResponseWriter, req *http.Request) {
defer req.Body.Close()
if len(bh.Digest) == 0 {
bh.Errors = append(bh.Errors, v2.ErrorCodeBlobUnknown)
return
}
bd, err := storage.RegistryBlobDeleter(bh.Namespace())
if err != nil {
bh.Errors = append(bh.Errors, errcode.ErrorCodeUnknown.WithDetail(err))
return
}
err = bd.Delete(bh, bh.Digest)
if err != nil {
// ignore not found error
switch t := err.(type) {
case storagedriver.PathNotFoundError:
case errcode.Error:
if t.Code != v2.ErrorCodeBlobUnknown {
bh.Errors = append(bh.Errors, err)
return
}
default:
if err != distribution.ErrBlobUnknown {
detail := fmt.Sprintf("error deleting blob %q: %v", bh.Digest, err)
err = errcode.ErrorCodeUnknown.WithDetail(detail)
bh.Errors = append(bh.Errors, err)
return
}
}
context.GetLogger(bh).Infof("blobHandler: ignoring %T error: %v", err, err)
}
w.WriteHeader(http.StatusNoContent)
}