mirror of
https://github.com/goharbor/harbor.git
synced 2024-11-23 10:45:45 +01:00
Support image delete in DockerHub adapter (#7422)
Signed-off-by: cd1989 <chende@caicloud.io>
This commit is contained in:
parent
c3375e4ac2
commit
4ce2ba8688
@ -320,6 +320,32 @@ func (a *adapter) FetchImages(filters []*model.Filter) ([]*model.Resource, error
|
||||
return resources, nil
|
||||
}
|
||||
|
||||
// DeleteManifest ...
|
||||
// Note: DockerHub only supports delete by tag
|
||||
func (a *adapter) DeleteManifest(repository, reference string) error {
|
||||
parts := strings.Split(repository, "/")
|
||||
if len(parts) != 2 {
|
||||
return fmt.Errorf("dockerhub only support repo in format <namespace>/<name>, but got: %s", repository)
|
||||
}
|
||||
|
||||
resp, err := a.client.Do(http.MethodDelete, deleteTagPath(parts[0], parts[1], reference), nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if resp.StatusCode/100 != 2 {
|
||||
log.Errorf("Delete tag error: %d -- %s", resp.StatusCode, string(body))
|
||||
return fmt.Errorf("%d -- %s", resp.StatusCode, string(body))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// getRepos gets a page of repos from DockerHub
|
||||
func (a *adapter) getRepos(namespace, name string, page, pageSize int) (*ReposResp, error) {
|
||||
resp, err := a.client.Do(http.MethodGet, listReposPath(namespace, name, page, pageSize), nil)
|
||||
@ -335,7 +361,7 @@ func (a *adapter) getRepos(namespace, name string, page, pageSize int) (*ReposRe
|
||||
|
||||
if resp.StatusCode/100 != 2 {
|
||||
log.Errorf("list repos error: %d -- %s", resp.StatusCode, string(body))
|
||||
return nil, fmt.Errorf("%d -- %s", resp.StatusCode, body)
|
||||
return nil, fmt.Errorf("%d -- %s", resp.StatusCode, string(body))
|
||||
}
|
||||
|
||||
repos := &ReposResp{}
|
||||
|
@ -28,3 +28,7 @@ func listReposPath(namespace, name string, page, pageSize int) string {
|
||||
func listTagsPath(namespace, repo string, page, pageSize int) string {
|
||||
return fmt.Sprintf("/v2/repositories/%s/%s/tags/?page=%d&page_size=%d", namespace, repo, page, pageSize)
|
||||
}
|
||||
|
||||
func deleteTagPath(namespace, repo, tag string) string {
|
||||
return fmt.Sprintf("/v2/repositories/%s/%s/tags/%s/", namespace, repo, tag)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user