mirror of
https://github.com/goharbor/harbor
synced 2025-04-24 13:16:22 +00:00
commit
5011e2de20
@ -190,14 +190,14 @@ func (t *TargetAPI) Post() {
|
|||||||
t.CustomAbort(http.StatusConflict, "name is already used")
|
t.CustomAbort(http.StatusConflict, "name is already used")
|
||||||
}
|
}
|
||||||
|
|
||||||
ta, err = dao.GetRepTargetByConnInfo(target.URL, target.Username)
|
ta, err = dao.GetRepTargetByEndpoint(target.URL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("failed to get target [ %s %s ]: %v", target.URL, target.Username, err)
|
log.Errorf("failed to get target [ %s ]: %v", target.URL, err)
|
||||||
t.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
|
t.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
|
||||||
}
|
}
|
||||||
|
|
||||||
if ta != nil {
|
if ta != nil {
|
||||||
t.CustomAbort(http.StatusConflict, "the connection information[ endpoint, username ] is conflict with other target")
|
t.CustomAbort(http.StatusConflict, fmt.Sprintf("the target whose endpoint is %s already exists", target.URL))
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(target.Password) != 0 {
|
if len(target.Password) != 0 {
|
||||||
@ -260,15 +260,15 @@ func (t *TargetAPI) Put() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if target.URL != originalTarget.URL || target.Username != originalTarget.Username {
|
if target.URL != originalTarget.URL {
|
||||||
ta, err := dao.GetRepTargetByConnInfo(target.URL, target.Username)
|
ta, err := dao.GetRepTargetByEndpoint(target.URL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("failed to get target [ %s %s ]: %v", target.URL, target.Username, err)
|
log.Errorf("failed to get target [ %s ]: %v", target.URL, err)
|
||||||
t.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
|
t.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
|
||||||
}
|
}
|
||||||
|
|
||||||
if ta != nil {
|
if ta != nil {
|
||||||
t.CustomAbort(http.StatusConflict, "the connection information[ endpoint, username ] is conflict with other target")
|
t.CustomAbort(http.StatusConflict, fmt.Sprintf("the target whose endpoint is %s already exists", target.URL))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -855,6 +855,22 @@ func TestGetRepTargetByName(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetRepTargetByEndpoint(t *testing.T) {
|
||||||
|
target, err := GetRepTarget(targetID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to get target %d: %v", targetID, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
target2, err := GetRepTargetByEndpoint(target.URL)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to get target %s: %v", target.URL, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if target.URL != target2.URL {
|
||||||
|
t.Errorf("unexpected target URL: %s, expected: %s", target2.URL, target.URL)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestUpdateRepTarget(t *testing.T) {
|
func TestUpdateRepTarget(t *testing.T) {
|
||||||
target := &models.RepTarget{
|
target := &models.RepTarget{
|
||||||
Name: "name",
|
Name: "name",
|
||||||
|
@ -53,14 +53,13 @@ func GetRepTargetByName(name string) (*models.RepTarget, error) {
|
|||||||
return &t, err
|
return &t, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetRepTargetByConnInfo ...
|
// GetRepTargetByEndpoint ...
|
||||||
func GetRepTargetByConnInfo(endpoint, username string) (*models.RepTarget, error) {
|
func GetRepTargetByEndpoint(endpoint string) (*models.RepTarget, error) {
|
||||||
o := GetOrmer()
|
o := GetOrmer()
|
||||||
t := models.RepTarget{
|
t := models.RepTarget{
|
||||||
URL: endpoint,
|
URL: endpoint,
|
||||||
Username: username,
|
|
||||||
}
|
}
|
||||||
err := o.Read(&t, "URL", "Username")
|
err := o.Read(&t, "URL")
|
||||||
if err == orm.ErrNoRows {
|
if err == orm.ErrNoRows {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
@ -28,6 +28,7 @@ import (
|
|||||||
"github.com/docker/distribution"
|
"github.com/docker/distribution"
|
||||||
"github.com/docker/distribution/manifest/schema1"
|
"github.com/docker/distribution/manifest/schema1"
|
||||||
"github.com/docker/distribution/manifest/schema2"
|
"github.com/docker/distribution/manifest/schema2"
|
||||||
|
"github.com/vmware/harbor/dao"
|
||||||
"github.com/vmware/harbor/models"
|
"github.com/vmware/harbor/models"
|
||||||
"github.com/vmware/harbor/utils/log"
|
"github.com/vmware/harbor/utils/log"
|
||||||
"github.com/vmware/harbor/utils/registry"
|
"github.com/vmware/harbor/utils/registry"
|
||||||
@ -192,7 +193,13 @@ enter:
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
if !exist {
|
if !exist {
|
||||||
err := c.createProject()
|
project, err := dao.GetProjectByName(c.project)
|
||||||
|
if err != nil {
|
||||||
|
c.logger.Errorf("an error occurred while getting project %s on %s: %v", c.project, c.srcURL, err)
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = c.createProject(project.Public == 1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// other job may be also doing the same thing when the current job
|
// other job may be also doing the same thing when the current job
|
||||||
// is creating project, so when the response code is 409, re-check
|
// is creating project, so when the response code is 409, re-check
|
||||||
@ -286,13 +293,13 @@ func (c *Checker) projectExist() (exist, canWrite bool, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Checker) createProject() error {
|
func (c *Checker) createProject(isPublic bool) error {
|
||||||
// TODO handle publicity of project
|
|
||||||
project := struct {
|
project := struct {
|
||||||
ProjectName string `json:"project_name"`
|
ProjectName string `json:"project_name"`
|
||||||
Public bool `json:"public"`
|
Public bool `json:"public"`
|
||||||
}{
|
}{
|
||||||
ProjectName: c.project,
|
ProjectName: c.project,
|
||||||
|
Public: isPublic,
|
||||||
}
|
}
|
||||||
|
|
||||||
data, err := json.Marshal(project)
|
data, err := json.Marshal(project)
|
||||||
@ -495,8 +502,8 @@ func (m *ManifestPusher) enter() (string, error) {
|
|||||||
} else {
|
} else {
|
||||||
m.logger.Infof("manifest of %s:%s exists on source registry %s, continue manifest pushing", name, tag, m.srcURL)
|
m.logger.Infof("manifest of %s:%s exists on source registry %s, continue manifest pushing", name, tag, m.srcURL)
|
||||||
|
|
||||||
_, manifestExist, err := m.dstClient.ManifestExist(m.digest)
|
digest, manifestExist, err := m.dstClient.ManifestExist(tag)
|
||||||
if manifestExist {
|
if manifestExist && digest == m.digest {
|
||||||
m.logger.Infof("manifest of %s:%s exists on destination registry %s, skip manifest pushing", name, tag, m.dstURL)
|
m.logger.Infof("manifest of %s:%s exists on destination registry %s, skip manifest pushing", name, tag, m.dstURL)
|
||||||
|
|
||||||
m.tags = m.tags[1:]
|
m.tags = m.tags[1:]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user