mirror of
https://github.com/goharbor/harbor.git
synced 2024-12-19 15:17:43 +01:00
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:
parent
4e583ad65a
commit
b73f8a96c3
@ -43,9 +43,9 @@ func init() {
|
|||||||
|
|
||||||
type adapter struct {
|
type adapter struct {
|
||||||
*adp.DefaultImageRegistry
|
*adp.DefaultImageRegistry
|
||||||
registry *model.Registry
|
registry *model.Registry
|
||||||
coreServiceURL string
|
url string
|
||||||
client *common_http.Client
|
client *common_http.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
func newAdapter(registry *model.Registry) (*adapter, error) {
|
func newAdapter(registry *model.Registry) (*adapter, error) {
|
||||||
@ -72,8 +72,8 @@ func newAdapter(registry *model.Registry) (*adapter, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &adapter{
|
return &adapter{
|
||||||
registry: registry,
|
registry: registry,
|
||||||
coreServiceURL: registry.URL,
|
url: registry.URL,
|
||||||
client: common_http.NewClient(
|
client: common_http.NewClient(
|
||||||
&http.Client{
|
&http.Client{
|
||||||
Transport: transport,
|
Transport: transport,
|
||||||
@ -112,7 +112,7 @@ func (a *adapter) Info() (*model.RegistryInfo, error) {
|
|||||||
sys := &struct {
|
sys := &struct {
|
||||||
ChartRegistryEnabled bool `json:"with_chartmuseum"`
|
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
|
return nil, err
|
||||||
}
|
}
|
||||||
if sys.ChartRegistryEnabled {
|
if sys.ChartRegistryEnabled {
|
||||||
@ -158,7 +158,7 @@ func (a *adapter) PrepareForPush(resources []*model.Resource) error {
|
|||||||
Name: project.Name,
|
Name: project.Name,
|
||||||
Metadata: project.Metadata,
|
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 err != nil {
|
||||||
if httpErr, ok := err.(*common_http.Error); ok && httpErr.Code == http.StatusConflict {
|
if httpErr, ok := err.(*common_http.Error); ok && httpErr.Code == http.StatusConflict {
|
||||||
log.Debugf("got 409 when trying to create project %s", project.Name)
|
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) {
|
func (a *adapter) getProjects(name string) ([]*project, error) {
|
||||||
projects := []*project{}
|
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 {
|
if err := a.client.GetAndIteratePagination(url, &projects); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -246,9 +246,19 @@ func (a *adapter) getProject(name string) (*project, error) {
|
|||||||
|
|
||||||
func (a *adapter) getRepositories(projectID int64) ([]*repository, error) {
|
func (a *adapter) getRepositories(projectID int64) ([]*repository, error) {
|
||||||
repositories := []*repository{}
|
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 {
|
if err := a.client.GetAndIteratePagination(url, &repositories); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return repositories, nil
|
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
|
||||||
|
}
|
||||||
|
@ -80,7 +80,7 @@ func (a *adapter) FetchCharts(filters []*model.Filter) ([]*model.Resource, error
|
|||||||
}
|
}
|
||||||
resources := []*model.Resource{}
|
resources := []*model.Resource{}
|
||||||
for _, project := range projects {
|
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{}
|
charts := []*chart{}
|
||||||
if err := a.client.Get(url, &charts); err != nil {
|
if err := a.client.Get(url, &charts); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -93,7 +93,7 @@ func (a *adapter) FetchCharts(filters []*model.Filter) ([]*model.Resource, error
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
for _, chart := range charts {
|
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{}
|
chartVersions := []*chartVersion{}
|
||||||
if err := a.client.Get(url, &chartVersions); err != nil {
|
if err := a.client.Get(url, &chartVersions); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -136,7 +136,7 @@ func (a *adapter) getChartInfo(name, version string) (*chartVersionDetail, error
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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{}
|
info := &chartVersionDetail{}
|
||||||
if err = a.client.Get(url, info); err != nil {
|
if err = a.client.Get(url, info); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -159,7 +159,7 @@ func (a *adapter) DownloadChart(name, version string) (io.ReadCloser, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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)
|
req, err := http.NewRequest(http.MethodGet, url, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -189,7 +189,7 @@ func (a *adapter) UploadChart(name, version string, chart io.Reader) error {
|
|||||||
}
|
}
|
||||||
w.Close()
|
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)
|
req, err := http.NewRequest(http.MethodPost, url, buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -220,7 +220,7 @@ func (a *adapter) DeleteChart(name, version string) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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)
|
return a.client.Delete(url)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -73,7 +73,7 @@ func (a *adapter) FetchImages(filters []*model.Filter) ([]*model.Resource, error
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
for _, repository := range repositories {
|
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{}
|
tags := []*tag{}
|
||||||
if err = a.client.Get(url, &tags); err != nil {
|
if err = a.client.Get(url, &tags); err != nil {
|
||||||
return nil, err
|
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
|
// override the default implementation from the default image registry
|
||||||
// by calling Harbor API directly
|
// by calling Harbor API directly
|
||||||
func (a *adapter) DeleteManifest(repository, reference string) error {
|
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)
|
return a.client.Delete(url)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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, ","))
|
srcRepo, strings.Join(src.tags, ","), dstRepo, strings.Join(dst.tags, ","))
|
||||||
var err error
|
var err error
|
||||||
for i := range src.tags {
|
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())
|
t.logger.Errorf(e.Error())
|
||||||
err = e
|
err = e
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user