Merge pull request #6375 from steven-zou/fix_global_search_502_issue

Fix global search 502 issue happened when chart repo is not enabled
This commit is contained in:
Steven Zou 2018-11-29 16:29:08 +08:00 committed by GitHub
commit 68b1b98f0a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 59 additions and 27 deletions

View File

@ -24,6 +24,7 @@ import (
"github.com/goharbor/harbor/src/common/models"
"github.com/goharbor/harbor/src/common/utils"
"github.com/goharbor/harbor/src/common/utils/log"
"github.com/goharbor/harbor/src/core/config"
coreutils "github.com/goharbor/harbor/src/core/utils"
"k8s.io/helm/cmd/helm/search"
)
@ -124,6 +125,13 @@ func (s *SearchAPI) Get() {
s.CustomAbort(http.StatusInternalServerError, "")
}
result := &searchResult{
Project: projectResult,
Repository: repositoryResult,
}
// If enable chart repository
if config.WithChartMuseum() {
if searchHandler == nil {
searchHandler = chartController.SearchChart
}
@ -134,11 +142,9 @@ func (s *SearchAPI) Get() {
s.CustomAbort(http.StatusInternalServerError, err.Error())
}
result := &searchResult{
Project: projectResult,
Repository: repositoryResult,
Chart: chartResults,
result.Chart = chartResults
}
s.Data["json"] = result
s.ServeJSON()
}

View File

@ -16,10 +16,14 @@ package api
import (
"fmt"
"net/http"
"os"
"testing"
"github.com/goharbor/harbor/src/core/config"
"github.com/goharbor/harbor/src/common"
"github.com/goharbor/harbor/src/common/models"
"github.com/goharbor/harbor/src/common/utils/test"
"k8s.io/helm/cmd/helm/search"
"github.com/goharbor/harbor/src/common/dao"
@ -178,6 +182,27 @@ func TestSearch(t *testing.T) {
_, exist = repositories["search-2/hello-world"]
assert.True(t, exist)
currentAdminServerURL, ok := os.LookupEnv("ADMINSERVER_URL")
if ok {
chartSettings := map[string]interface{}{
common.WithChartMuseum: true,
}
adminServer, err := test.NewAdminserver(chartSettings)
if err != nil {
t.Fatal(nil)
}
defer adminServer.Close()
if err := config.InitByURL(adminServer.URL); err != nil {
t.Fatal(err)
}
defer func() {
// reset config
if err := config.InitByURL(currentAdminServerURL); err != nil {
t.Error(err)
}
}()
// Search chart
err = handleAndParse(&testingRequest{
method: http.MethodGet,
@ -195,4 +220,5 @@ func TestSearch(t *testing.T) {
// Restore chart search handler
searchHandler = nil
}
}