Skip to content

Commit

Permalink
registry: add support for garbage collection types (digitalocean#417)
Browse files Browse the repository at this point in the history
  • Loading branch information
waynr authored Nov 24, 2020
1 parent 8f57e99 commit 4cac4dd
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 10 deletions.
51 changes: 41 additions & 10 deletions registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type RegistryService interface {
ListRepositoryTags(context.Context, string, string, *ListOptions) ([]*RepositoryTag, *Response, error)
DeleteTag(context.Context, string, string, string) (*Response, error)
DeleteManifest(context.Context, string, string, string) (*Response, error)
StartGarbageCollection(context.Context, string) (*GarbageCollection, *Response, error)
StartGarbageCollection(context.Context, string, ...*StartGarbageCollectionRequest) (*GarbageCollection, *Response, error)
GetGarbageCollection(context.Context, string) (*GarbageCollection, *Response, error)
ListGarbageCollections(context.Context, string, *ListOptions) ([]*GarbageCollection, *Response, error)
UpdateGarbageCollection(context.Context, string, string, *UpdateGarbageCollectionRequest) (*GarbageCollection, *Response, error)
Expand Down Expand Up @@ -100,13 +100,14 @@ type repositoryTagsRoot struct {

// GarbageCollection represents a garbage collection.
type GarbageCollection struct {
UUID string `json:"uuid"`
RegistryName string `json:"registry_name"`
Status string `json:"status"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
BlobsDeleted uint64 `json:"blobs_deleted"`
FreedBytes uint64 `json:"freed_bytes"`
UUID string `json:"uuid"`
RegistryName string `json:"registry_name"`
Status string `json:"status"`
Type GarbageCollectionType `json:"type"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
BlobsDeleted uint64 `json:"blobs_deleted"`
FreedBytes uint64 `json:"freed_bytes"`
}

type garbageCollectionRoot struct {
Expand All @@ -119,6 +120,26 @@ type garbageCollectionsRoot struct {
Meta *Meta `json:"meta"`
}

type GarbageCollectionType string

const (
// GCTypeUntaggedManifestsOnly indicates that a garbage collection should
// only delete untagged manifests.
GCTypeUntaggedManifestsOnly = GarbageCollectionType("untagged manifests only")
// GCTypeUnreferencedBlobsOnly indicates that a garbage collection should
// only delete unreferenced blobs.
GCTypeUnreferencedBlobsOnly = GarbageCollectionType("unreferenced blobs only")
// GCTypeUntaggedManifestsAndUnreferencedBlobs indicates that a garbage
// collection should delete both untagged manifests and unreferenced blobs.
GCTypeUntaggedManifestsAndUnreferencedBlobs = GarbageCollectionType("untagged manifests and unreferenced blobs")
)

// StartGarbageCollectionRequest represents options to a garbage collection
// start request.
type StartGarbageCollectionRequest struct {
Type GarbageCollectionType `json:"type"`
}

// UpdateGarbageCollectionRequest represents a request to update a garbage
// collection.
type UpdateGarbageCollectionRequest struct {
Expand Down Expand Up @@ -330,9 +351,19 @@ func (svc *RegistryServiceOp) DeleteManifest(ctx context.Context, registry, repo

// StartGarbageCollection requests a garbage collection for the specified
// registry.
func (svc *RegistryServiceOp) StartGarbageCollection(ctx context.Context, registry string) (*GarbageCollection, *Response, error) {
func (svc *RegistryServiceOp) StartGarbageCollection(ctx context.Context, registry string, request ...*StartGarbageCollectionRequest) (*GarbageCollection, *Response, error) {
path := fmt.Sprintf("%s/%s/garbage-collection", registryPath, registry)
req, err := svc.client.NewRequest(ctx, http.MethodPost, path, nil)
var requestParams interface{}
if len(request) < 1 {
// default to only garbage collecting unreferenced blobs for backwards
// compatibility
requestParams = &StartGarbageCollectionRequest{
Type: GCTypeUnreferencedBlobsOnly,
}
} else {
requestParams = request[0]
}
req, err := svc.client.NewRequest(ctx, http.MethodPost, path, requestParams)
if err != nil {
return nil, nil, err
}
Expand Down
16 changes: 16 additions & 0 deletions registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const (
testGCFreedBytes = 666
testGCStatus = "requested"
testGCUUID = "mew-mew-id"
testGCType = GCTypeUnreferencedBlobsOnly
)

var (
Expand All @@ -38,6 +39,7 @@ var (
UpdatedAt: testTime,
BlobsDeleted: testGCBlobsDeleted,
FreedBytes: testGCFreedBytes,
Type: testGCType,
}
)

Expand Down Expand Up @@ -362,6 +364,7 @@ func TestGarbageCollection_Start(t *testing.T) {
"uuid": "{{.UUID}}",
"registry_name": "{{.RegistryName}}",
"status": "{{.Status}}",
"type": "{{.Type}}",
"created_at": "{{.CreatedAt.Format "2006-01-02T15:04:05Z07:00"}}",
"updated_at": "{{.UpdatedAt.Format "2006-01-02T15:04:05Z07:00"}}",
"blobs_deleted": {{.BlobsDeleted}},
Expand All @@ -370,9 +373,19 @@ func TestGarbageCollection_Start(t *testing.T) {
}`
requestResponseJSON := reifyTemplateStr(t, requestResponseJSONTmpl, want)

createRequest := &StartGarbageCollectionRequest{
Type: GCTypeUnreferencedBlobsOnly,
}
mux.HandleFunc("/v2/registry/"+testRegistry+"/garbage-collection",
func(w http.ResponseWriter, r *http.Request) {
v := new(StartGarbageCollectionRequest)
err := json.NewDecoder(r.Body).Decode(v)
if err != nil {
t.Fatal(err)
}

testMethod(t, r, http.MethodPost)
require.Equal(t, v, createRequest)
fmt.Fprint(w, requestResponseJSON)
})

Expand All @@ -392,6 +405,7 @@ func TestGarbageCollection_Get(t *testing.T) {
"uuid": "{{.UUID}}",
"registry_name": "{{.RegistryName}}",
"status": "{{.Status}}",
"type": "{{.Type}}",
"created_at": "{{.CreatedAt.Format "2006-01-02T15:04:05Z07:00"}}",
"updated_at": "{{.UpdatedAt.Format "2006-01-02T15:04:05Z07:00"}}",
"blobs_deleted": {{.BlobsDeleted}},
Expand Down Expand Up @@ -423,6 +437,7 @@ func TestGarbageCollection_List(t *testing.T) {
"uuid": "{{.UUID}}",
"registry_name": "{{.RegistryName}}",
"status": "{{.Status}}",
"type": "{{.Type}}",
"created_at": "{{.CreatedAt.Format "2006-01-02T15:04:05Z07:00"}}",
"updated_at": "{{.UpdatedAt.Format "2006-01-02T15:04:05Z07:00"}}",
"blobs_deleted": {{.BlobsDeleted}},
Expand Down Expand Up @@ -483,6 +498,7 @@ func TestGarbageCollection_Update(t *testing.T) {
"uuid": "{{.UUID}}",
"registry_name": "{{.RegistryName}}",
"status": "{{.Status}}",
"type": "{{.Type}}",
"created_at": "{{.CreatedAt.Format "2006-01-02T15:04:05Z07:00"}}",
"updated_at": "{{.UpdatedAt.Format "2006-01-02T15:04:05Z07:00"}}",
"blobs_deleted": {{.BlobsDeleted}},
Expand Down

0 comments on commit 4cac4dd

Please sign in to comment.