diff --git a/src/replication/adapter/dockerhub/adapter.go b/src/replication/adapter/dockerhub/adapter.go index c24c66132..ed6f5f8e2 100644 --- a/src/replication/adapter/dockerhub/adapter.go +++ b/src/replication/adapter/dockerhub/adapter.go @@ -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 /, 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{} diff --git a/src/replication/adapter/dockerhub/consts.go b/src/replication/adapter/dockerhub/consts.go index 13b570124..952ae87e0 100644 --- a/src/replication/adapter/dockerhub/consts.go +++ b/src/replication/adapter/dockerhub/consts.go @@ -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) +}