Support image delete in DockerHub adapter (#7422)

Signed-off-by: cd1989 <chende@caicloud.io>
This commit is contained in:
De Chen 2019-04-18 18:24:42 +08:00 committed by Wang Yan
parent c3375e4ac2
commit 4ce2ba8688
2 changed files with 31 additions and 1 deletions

View File

@ -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{}

View File

@ -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)
}