mirror of
https://github.com/goharbor/harbor.git
synced 2024-10-04 00:07:47 +02:00
bug fix: fix confliction when more than one job are creating the same project
This commit is contained in:
parent
dbc84dc99b
commit
7a6308b4f9
@ -18,6 +18,7 @@ package replication
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -43,6 +44,10 @@ const (
|
|||||||
StatePushManifest = "push_manifest"
|
StatePushManifest = "push_manifest"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrConflict = errors.New("conflict")
|
||||||
|
)
|
||||||
|
|
||||||
// BaseHandler holds informations shared by other state handlers
|
// BaseHandler holds informations shared by other state handlers
|
||||||
type BaseHandler struct {
|
type BaseHandler struct {
|
||||||
project string // project_name
|
project string // project_name
|
||||||
@ -136,15 +141,24 @@ type Checker struct {
|
|||||||
// Enter check existence of project, if it does not exist, create it,
|
// Enter check existence of project, if it does not exist, create it,
|
||||||
// if it exists, check whether the user has write privilege to it.
|
// if it exists, check whether the user has write privilege to it.
|
||||||
func (c *Checker) Enter() (string, error) {
|
func (c *Checker) Enter() (string, error) {
|
||||||
|
enter:
|
||||||
exist, canWrite, err := c.projectExist()
|
exist, canWrite, err := c.projectExist()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.logger.Errorf("an error occurred while checking existence of project %s on %s with user %s : %v", c.project, c.dstURL, c.dstUsr, err)
|
c.logger.Errorf("an error occurred while checking existence of project %s on %s with user %s : %v", c.project, c.dstURL, c.dstUsr, err)
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
if !exist {
|
if !exist {
|
||||||
if err := c.createProject(); err != nil {
|
err := c.createProject()
|
||||||
c.logger.Errorf("an error occurred while creating project %s on %s with user %s : %v", c.project, c.dstURL, c.dstUsr, err)
|
if err != nil {
|
||||||
return "", err
|
// 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
|
||||||
|
// the existence of project
|
||||||
|
if err == ErrConflict {
|
||||||
|
goto enter
|
||||||
|
} else {
|
||||||
|
c.logger.Errorf("an error occurred while creating project %s on %s with user %s : %v", c.project, c.dstURL, c.dstUsr, err)
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
c.logger.Infof("project %s is created on %s with user %s", c.project, c.dstURL, c.dstUsr)
|
c.logger.Infof("project %s is created on %s with user %s", c.project, c.dstURL, c.dstUsr)
|
||||||
return StatePullManifest, nil
|
return StatePullManifest, nil
|
||||||
@ -246,6 +260,10 @@ func (c *Checker) createProject() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusCreated {
|
if resp.StatusCode != http.StatusCreated {
|
||||||
|
if resp.StatusCode == http.StatusConflict {
|
||||||
|
return ErrConflict
|
||||||
|
}
|
||||||
|
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
message, err := ioutil.ReadAll(resp.Body)
|
message, err := ioutil.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user