mirror of
https://github.com/goharbor/harbor.git
synced 2024-11-30 14:13:43 +01:00
Merge pull request #553 from ywk253100/rep_del
Replicate repository deletion by calling API of Harbor to log the operation
This commit is contained in:
commit
22304c1012
@ -16,10 +16,17 @@
|
||||
package replication
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/vmware/harbor/models"
|
||||
"github.com/vmware/harbor/utils/log"
|
||||
"github.com/vmware/harbor/utils/registry"
|
||||
"github.com/vmware/harbor/utils/registry/auth"
|
||||
//"github.com/vmware/harbor/utils/registry"
|
||||
//"github.com/vmware/harbor/utils/registry/auth"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -27,6 +34,10 @@ const (
|
||||
StateDelete = "delete"
|
||||
)
|
||||
|
||||
var (
|
||||
errNotFound = errors.New("Not Found")
|
||||
)
|
||||
|
||||
// Deleter deletes repository or tags
|
||||
type Deleter struct {
|
||||
repository string // prject_name/repo_name
|
||||
@ -38,7 +49,7 @@ type Deleter struct {
|
||||
|
||||
insecure bool
|
||||
|
||||
dstClient *registry.Repository
|
||||
//dstClient *registry.Repository
|
||||
|
||||
logger *log.Logger
|
||||
}
|
||||
@ -76,6 +87,45 @@ func (d *Deleter) Enter() (string, error) {
|
||||
}
|
||||
|
||||
func (d *Deleter) enter() (string, error) {
|
||||
url := strings.TrimRight(d.dstURL, "/") + "/api/repositories/"
|
||||
|
||||
// delete repository
|
||||
if len(d.tags) == 0 {
|
||||
u := url + "?repo_name=" + d.repository
|
||||
if err := del(u, d.dstUsr, d.dstPwd, d.insecure); err != nil {
|
||||
if err == errNotFound {
|
||||
d.logger.Warningf("repository %s does not exist on %s", d.repository, d.dstURL)
|
||||
return models.JobFinished, nil
|
||||
}
|
||||
d.logger.Errorf("an error occurred while deleting repository %s on %s with user %s: %v", d.repository, d.dstURL, d.dstUsr, err)
|
||||
return "", err
|
||||
|
||||
}
|
||||
|
||||
d.logger.Infof("repository %s on %s has been deleted", d.repository, d.dstURL)
|
||||
|
||||
return models.JobFinished, nil
|
||||
|
||||
}
|
||||
|
||||
// delele tags
|
||||
for _, tag := range d.tags {
|
||||
u := url + "?repo_name=" + d.repository + "&tag=" + tag
|
||||
if err := del(u, d.dstUsr, d.dstPwd, d.insecure); err != nil {
|
||||
if err == errNotFound {
|
||||
d.logger.Warningf("repository %s does not exist on %s", d.repository, d.dstURL)
|
||||
continue
|
||||
}
|
||||
|
||||
d.logger.Errorf("an error occurred while deleting repository %s:%s on %s with user %s: %v", d.repository, tag, d.dstURL, d.dstUsr, err)
|
||||
return "", err
|
||||
}
|
||||
d.logger.Infof("repository %s:%s on %s has been deleted", d.repository, tag, d.dstURL)
|
||||
}
|
||||
return models.JobFinished, nil
|
||||
|
||||
/*
|
||||
// the follow codes can be used for non-harbor repository deletion
|
||||
dstCred := auth.NewBasicAuthCredential(d.dstUsr, d.dstPwd)
|
||||
dstClient, err := newRepositoryClient(d.dstURL, d.insecure, dstCred,
|
||||
d.repository, "repository", d.repository, "pull", "push", "*")
|
||||
@ -109,4 +159,43 @@ func (d *Deleter) enter() (string, error) {
|
||||
}
|
||||
|
||||
return models.JobFinished, nil
|
||||
*/
|
||||
}
|
||||
|
||||
func del(url, username, password string, insecure bool) error {
|
||||
req, err := http.NewRequest("DELETE", url, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
req.SetBasicAuth(username, password)
|
||||
|
||||
client := &http.Client{
|
||||
Transport: &http.Transport{
|
||||
TLSClientConfig: &tls.Config{
|
||||
InsecureSkipVerify: insecure,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode == http.StatusOK {
|
||||
return nil
|
||||
}
|
||||
|
||||
if resp.StatusCode == http.StatusNotFound {
|
||||
return errNotFound
|
||||
}
|
||||
|
||||
b, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return fmt.Errorf("%d %s", resp.StatusCode, string(b))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user