drop gc api of registryctl (#15325)

The API is desiged for read-only gc job, as the read only gc has already deprecated, remove this api accordingly.

Signed-off-by: Wang Yan <wangyan@vmware.com>
This commit is contained in:
Wang Yan 2021-07-23 14:15:02 +08:00 committed by GitHub
parent 766e953325
commit e5a614967d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 0 additions and 116 deletions

View File

@ -1,62 +0,0 @@
package gc
import (
"bytes"
"github.com/goharbor/harbor/src/lib/log"
"github.com/goharbor/harbor/src/registryctl/api"
"net/http"
"os/exec"
"time"
)
// NewHandler returns the handler to handler blob request
func NewHandler(registryConf string) http.Handler {
return &handler{
registryConf: registryConf,
}
}
type handler struct {
registryConf string
}
// ServeHTTP ...
func (h *handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
switch req.Method {
case http.MethodPost:
h.start(w, req)
default:
api.HandleNotMethodAllowed(w)
}
}
// Result ...
type Result struct {
Status bool `json:"status"`
Msg string `json:"msg"`
StartTime time.Time `json:"starttime"`
EndTime time.Time `json:"endtime"`
}
// start ...
func (h *handler) start(w http.ResponseWriter, r *http.Request) {
cmd := exec.Command("/bin/bash", "-c", "registry_DO_NOT_USE_GC garbage-collect --delete-untagged=false "+h.registryConf)
var outBuf, errBuf bytes.Buffer
cmd.Stdout = &outBuf
cmd.Stderr = &errBuf
start := time.Now()
log.Debugf("Start to execute garbage collection...")
if err := cmd.Run(); err != nil {
log.Errorf("Fail to execute GC: %v, command err: %s", err, errBuf.String())
api.HandleInternalServerError(w, err)
return
}
gcr := Result{true, outBuf.String(), start, time.Now()}
if err := api.WriteJSON(w, gcr); err != nil {
log.Errorf("failed to write response: %v", err)
return
}
log.Debugf("Successful to execute garbage collection...")
}

View File

@ -15,7 +15,6 @@
package client package client
import ( import (
"encoding/json"
"fmt" "fmt"
"github.com/goharbor/harbor/src/lib/errors" "github.com/goharbor/harbor/src/lib/errors"
"io/ioutil" "io/ioutil"
@ -25,8 +24,6 @@ import (
common_http "github.com/goharbor/harbor/src/common/http" common_http "github.com/goharbor/harbor/src/common/http"
"github.com/goharbor/harbor/src/common/http/modifier/auth" "github.com/goharbor/harbor/src/common/http/modifier/auth"
"github.com/goharbor/harbor/src/common/utils" "github.com/goharbor/harbor/src/common/utils"
"github.com/goharbor/harbor/src/lib/log"
"github.com/goharbor/harbor/src/registryctl/api/registry/gc"
) )
// const definition // const definition
@ -38,8 +35,6 @@ const (
type Client interface { type Client interface {
// Health tests the connection with registry server // Health tests the connection with registry server
Health() error Health() error
// StartGC enable the gc of registry server
StartGC() (*gc.Result, error)
// DeleteBlob deletes the specified blob. The "reference" should be "digest" // DeleteBlob deletes the specified blob. The "reference" should be "digest"
DeleteBlob(reference string) (err error) DeleteBlob(reference string) (err error)
// DeleteManifest deletes the specified manifest. The "reference" can be "tag" or "digest" // DeleteManifest deletes the specified manifest. The "reference" can be "tag" or "digest"
@ -83,36 +78,6 @@ func (c *client) Health() error {
return utils.TestTCPConn(addr, 60, 2) return utils.TestTCPConn(addr, 60, 2)
} }
// StartGC ...
func (c *client) StartGC() (*gc.Result, error) {
url := c.baseURL + "/api/registry/gc"
gcr := &gc.Result{}
req, err := http.NewRequest(http.MethodPost, url, nil)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
resp, err := c.do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
log.Errorf("Failed to start gc: %d", resp.StatusCode)
return nil, fmt.Errorf("failed to start GC: %d", resp.StatusCode)
}
if err := json.Unmarshal(data, gcr); err != nil {
return nil, err
}
return gcr, nil
}
// DeleteBlob ... // DeleteBlob ...
func (c *client) DeleteBlob(reference string) (err error) { func (c *client) DeleteBlob(reference string) (err error) {
req, err := http.NewRequest(http.MethodDelete, buildBlobURL(c.baseURL, reference), nil) req, err := http.NewRequest(http.MethodDelete, buildBlobURL(c.baseURL, reference), nil)

View File

@ -43,13 +43,6 @@ func (c *clientTestSuite) TesHealth() {
c.Require().Nil(err) c.Require().Nil(err)
} }
func (c *clientTestSuite) TesStartGC() {
gcr, err := c.client.StartGC()
c.Require().Nil(err)
c.Equal(gcr.Msg, "hello-world")
c.Equal(gcr.Status, true)
}
func (c *clientTestSuite) TestDeleteManifest() { func (c *clientTestSuite) TestDeleteManifest() {
server := test.NewServer( server := test.NewServer(
&test.RequestHandlerMapping{ &test.RequestHandlerMapping{

View File

@ -20,7 +20,6 @@ import (
"github.com/goharbor/harbor/src/registryctl/api" "github.com/goharbor/harbor/src/registryctl/api"
"github.com/goharbor/harbor/src/registryctl/api/registry/blob" "github.com/goharbor/harbor/src/registryctl/api/registry/blob"
"github.com/goharbor/harbor/src/registryctl/api/registry/gc"
"github.com/goharbor/harbor/src/registryctl/config" "github.com/goharbor/harbor/src/registryctl/config"
"github.com/gorilla/mux" "github.com/gorilla/mux"
) )
@ -31,7 +30,6 @@ func newRouter(conf config.Configuration) http.Handler {
rootRouter.StrictSlash(true) rootRouter.StrictSlash(true)
rootRouter.HandleFunc("/api/health", api.Health).Methods("GET") rootRouter.HandleFunc("/api/health", api.Health).Methods("GET")
rootRouter.Path("/api/registry/gc").Methods(http.MethodPost).Handler(gc.NewHandler(conf.RegistryConfig))
rootRouter.Path("/api/registry/blob/{reference}").Methods(http.MethodDelete).Handler(blob.NewHandler(conf.StorageDriver)) rootRouter.Path("/api/registry/blob/{reference}").Methods(http.MethodDelete).Handler(blob.NewHandler(conf.StorageDriver))
rootRouter.Path("/api/registry/{name:.*}/manifests/{reference}").Methods(http.MethodDelete).Handler(manifest.NewHandler(conf.StorageDriver)) rootRouter.Path("/api/registry/{name:.*}/manifests/{reference}").Methods(http.MethodDelete).Handler(manifest.NewHandler(conf.StorageDriver))
return rootRouter return rootRouter

View File

@ -1,7 +1,6 @@
package registryctl package registryctl
import ( import (
"github.com/goharbor/harbor/src/registryctl/api/registry/gc"
"github.com/stretchr/testify/mock" "github.com/stretchr/testify/mock"
) )
@ -14,15 +13,6 @@ func (c *Mockclient) Health() error {
return nil return nil
} }
// StartGC ...
func (c *Mockclient) StartGC() (*gc.Result, error) {
result := &gc.Result{
Status: true,
Msg: "this is a mock client",
}
return result, nil
}
// DeleteBlob ... // DeleteBlob ...
func (c *Mockclient) DeleteBlob(reference string) (err error) { func (c *Mockclient) DeleteBlob(reference string) (err error) {
return nil return nil