Fix issue: replication cannot work when Harbor is deployed on Kubernetes whose hairpin mode is disabled (#7718)

Fix issue https://github.com/goharbor/harbor-helm/issues/222: replication cannot work when Harbor is deployed on Kubernetes whose hairpin mode is disabled

Signed-off-by: Wenkai Yin <yinw@vmware.com>
This commit is contained in:
Wenkai Yin 2019-05-09 17:10:32 +08:00 committed by Wang Yan
parent 4e583ad65a
commit b73f8a96c3
4 changed files with 28 additions and 18 deletions

View File

@ -43,9 +43,9 @@ func init() {
type adapter struct {
*adp.DefaultImageRegistry
registry *model.Registry
coreServiceURL string
client *common_http.Client
registry *model.Registry
url string
client *common_http.Client
}
func newAdapter(registry *model.Registry) (*adapter, error) {
@ -72,8 +72,8 @@ func newAdapter(registry *model.Registry) (*adapter, error) {
return nil, err
}
return &adapter{
registry: registry,
coreServiceURL: registry.URL,
registry: registry,
url: registry.URL,
client: common_http.NewClient(
&http.Client{
Transport: transport,
@ -112,7 +112,7 @@ func (a *adapter) Info() (*model.RegistryInfo, error) {
sys := &struct {
ChartRegistryEnabled bool `json:"with_chartmuseum"`
}{}
if err := a.client.Get(a.coreServiceURL+"/api/systeminfo", sys); err != nil {
if err := a.client.Get(a.getURL()+"/api/systeminfo", sys); err != nil {
return nil, err
}
if sys.ChartRegistryEnabled {
@ -158,7 +158,7 @@ func (a *adapter) PrepareForPush(resources []*model.Resource) error {
Name: project.Name,
Metadata: project.Metadata,
}
err := a.client.Post(a.coreServiceURL+"/api/projects", pro)
err := a.client.Post(a.getURL()+"/api/projects", pro)
if err != nil {
if httpErr, ok := err.(*common_http.Error); ok && httpErr.Code == http.StatusConflict {
log.Debugf("got 409 when trying to create project %s", project.Name)
@ -211,7 +211,7 @@ type project struct {
func (a *adapter) getProjects(name string) ([]*project, error) {
projects := []*project{}
url := fmt.Sprintf("%s/api/projects?name=%s&page=1&page_size=500", a.coreServiceURL, name)
url := fmt.Sprintf("%s/api/projects?name=%s&page=1&page_size=500", a.getURL(), name)
if err := a.client.GetAndIteratePagination(url, &projects); err != nil {
return nil, err
}
@ -246,9 +246,19 @@ func (a *adapter) getProject(name string) (*project, error) {
func (a *adapter) getRepositories(projectID int64) ([]*repository, error) {
repositories := []*repository{}
url := fmt.Sprintf("%s/api/repositories?project_id=%d&page=1&page_size=500", a.coreServiceURL, projectID)
url := fmt.Sprintf("%s/api/repositories?project_id=%d&page=1&page_size=500", a.getURL(), projectID)
if err := a.client.GetAndIteratePagination(url, &repositories); err != nil {
return nil, err
}
return repositories, nil
}
// when the adapter is created for local Harbor, returns the "http://127.0.0.1:8080"
// as URL to avoid issue https://github.com/goharbor/harbor-helm/issues/222
// when harbor is deployed on Kubernetes
func (a *adapter) getURL() string {
if a.registry.Type == model.RegistryTypeHarbor && a.registry.Name == "Local" {
return "http://127.0.0.1:8080"
}
return a.url
}

View File

@ -80,7 +80,7 @@ func (a *adapter) FetchCharts(filters []*model.Filter) ([]*model.Resource, error
}
resources := []*model.Resource{}
for _, project := range projects {
url := fmt.Sprintf("%s/api/chartrepo/%s/charts", a.coreServiceURL, project.Name)
url := fmt.Sprintf("%s/api/chartrepo/%s/charts", a.getURL(), project.Name)
charts := []*chart{}
if err := a.client.Get(url, &charts); err != nil {
return nil, err
@ -93,7 +93,7 @@ func (a *adapter) FetchCharts(filters []*model.Filter) ([]*model.Resource, error
return nil, err
}
for _, chart := range charts {
url := fmt.Sprintf("%s/api/chartrepo/%s/charts/%s", a.coreServiceURL, project.Name, chart.Name)
url := fmt.Sprintf("%s/api/chartrepo/%s/charts/%s", a.getURL(), project.Name, chart.Name)
chartVersions := []*chartVersion{}
if err := a.client.Get(url, &chartVersions); err != nil {
return nil, err
@ -136,7 +136,7 @@ func (a *adapter) getChartInfo(name, version string) (*chartVersionDetail, error
if err != nil {
return nil, err
}
url := fmt.Sprintf("%s/api/chartrepo/%s/charts/%s/%s", a.coreServiceURL, project, name, version)
url := fmt.Sprintf("%s/api/chartrepo/%s/charts/%s/%s", a.getURL(), project, name, version)
info := &chartVersionDetail{}
if err = a.client.Get(url, info); err != nil {
return nil, err
@ -159,7 +159,7 @@ func (a *adapter) DownloadChart(name, version string) (io.ReadCloser, error) {
if err != nil {
return nil, err
}
url = fmt.Sprintf("%s/chartrepo/%s/%s", a.coreServiceURL, project, url)
url = fmt.Sprintf("%s/chartrepo/%s/%s", a.getURL(), project, url)
}
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
@ -189,7 +189,7 @@ func (a *adapter) UploadChart(name, version string, chart io.Reader) error {
}
w.Close()
url := fmt.Sprintf("%s/api/chartrepo/%s/charts", a.coreServiceURL, project)
url := fmt.Sprintf("%s/api/chartrepo/%s/charts", a.getURL(), project)
req, err := http.NewRequest(http.MethodPost, url, buf)
if err != nil {
@ -220,7 +220,7 @@ func (a *adapter) DeleteChart(name, version string) error {
if err != nil {
return err
}
url := fmt.Sprintf("%s/api/chartrepo/%s/charts/%s/%s", a.coreServiceURL, project, name, version)
url := fmt.Sprintf("%s/api/chartrepo/%s/charts/%s/%s", a.getURL(), project, name, version)
return a.client.Delete(url)
}

View File

@ -73,7 +73,7 @@ func (a *adapter) FetchImages(filters []*model.Filter) ([]*model.Resource, error
return nil, err
}
for _, repository := range repositories {
url := fmt.Sprintf("%s/api/repositories/%s/tags", a.coreServiceURL, repository.Name)
url := fmt.Sprintf("%s/api/repositories/%s/tags", a.getURL(), repository.Name)
tags := []*tag{}
if err = a.client.Get(url, &tags); err != nil {
return nil, err
@ -146,7 +146,7 @@ func (a *adapter) listCandidateProjects(filters []*model.Filter) ([]*project, er
// override the default implementation from the default image registry
// by calling Harbor API directly
func (a *adapter) DeleteManifest(repository, reference string) error {
url := fmt.Sprintf("%s/api/repositories/%s/tags/%s", a.coreServiceURL, repository, reference)
url := fmt.Sprintf("%s/api/repositories/%s/tags/%s", a.getURL(), repository, reference)
return a.client.Delete(url)
}

View File

@ -139,7 +139,7 @@ func (t *transfer) copy(src *repository, dst *repository, override bool) error {
srcRepo, strings.Join(src.tags, ","), dstRepo, strings.Join(dst.tags, ","))
var err error
for i := range src.tags {
if e := t.copyImage(srcRepo, src.tags[i], dstRepo, dst.tags[i], override); err != nil {
if e := t.copyImage(srcRepo, src.tags[i], dstRepo, dst.tags[i], override); e != nil {
t.logger.Errorf(e.Error())
err = e
}