mirror of
https://github.com/goharbor/harbor.git
synced 2024-09-29 22:07:32 +02:00
Fix global search 502 issue happened when chart repo is not enabled
Signed-off-by: Steven Zou <szou@vmware.com>
This commit is contained in:
parent
c4bf65162c
commit
e7ffaecca5
@ -24,6 +24,7 @@ import (
|
|||||||
"github.com/goharbor/harbor/src/common/models"
|
"github.com/goharbor/harbor/src/common/models"
|
||||||
"github.com/goharbor/harbor/src/common/utils"
|
"github.com/goharbor/harbor/src/common/utils"
|
||||||
"github.com/goharbor/harbor/src/common/utils/log"
|
"github.com/goharbor/harbor/src/common/utils/log"
|
||||||
|
"github.com/goharbor/harbor/src/core/config"
|
||||||
coreutils "github.com/goharbor/harbor/src/core/utils"
|
coreutils "github.com/goharbor/harbor/src/core/utils"
|
||||||
"k8s.io/helm/cmd/helm/search"
|
"k8s.io/helm/cmd/helm/search"
|
||||||
)
|
)
|
||||||
@ -124,21 +125,26 @@ func (s *SearchAPI) Get() {
|
|||||||
s.CustomAbort(http.StatusInternalServerError, "")
|
s.CustomAbort(http.StatusInternalServerError, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
if searchHandler == nil {
|
|
||||||
searchHandler = chartController.SearchChart
|
|
||||||
}
|
|
||||||
|
|
||||||
chartResults, err := searchHandler(keyword, proNames)
|
|
||||||
if err != nil {
|
|
||||||
log.Errorf("failed to filter charts: %v", err)
|
|
||||||
s.CustomAbort(http.StatusInternalServerError, err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
result := &searchResult{
|
result := &searchResult{
|
||||||
Project: projectResult,
|
Project: projectResult,
|
||||||
Repository: repositoryResult,
|
Repository: repositoryResult,
|
||||||
Chart: chartResults,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If enable chart repository
|
||||||
|
if config.WithChartMuseum() {
|
||||||
|
if searchHandler == nil {
|
||||||
|
searchHandler = chartController.SearchChart
|
||||||
|
}
|
||||||
|
|
||||||
|
chartResults, err := searchHandler(keyword, proNames)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("failed to filter charts: %v", err)
|
||||||
|
s.CustomAbort(http.StatusInternalServerError, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
result.Chart = chartResults
|
||||||
|
}
|
||||||
|
|
||||||
s.Data["json"] = result
|
s.Data["json"] = result
|
||||||
s.ServeJSON()
|
s.ServeJSON()
|
||||||
}
|
}
|
||||||
|
@ -16,10 +16,14 @@ package api
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/goharbor/harbor/src/core/config"
|
||||||
|
|
||||||
"github.com/goharbor/harbor/src/common"
|
"github.com/goharbor/harbor/src/common"
|
||||||
"github.com/goharbor/harbor/src/common/models"
|
"github.com/goharbor/harbor/src/common/models"
|
||||||
|
"github.com/goharbor/harbor/src/common/utils/test"
|
||||||
"k8s.io/helm/cmd/helm/search"
|
"k8s.io/helm/cmd/helm/search"
|
||||||
|
|
||||||
"github.com/goharbor/harbor/src/common/dao"
|
"github.com/goharbor/harbor/src/common/dao"
|
||||||
@ -178,21 +182,43 @@ func TestSearch(t *testing.T) {
|
|||||||
_, exist = repositories["search-2/hello-world"]
|
_, exist = repositories["search-2/hello-world"]
|
||||||
assert.True(t, exist)
|
assert.True(t, exist)
|
||||||
|
|
||||||
// Search chart
|
currentAdminServerURL, ok := os.LookupEnv("ADMINSERVER_URL")
|
||||||
err = handleAndParse(&testingRequest{
|
if ok {
|
||||||
method: http.MethodGet,
|
chartSettings := map[string]interface{}{
|
||||||
url: "/api/search",
|
common.WithChartMuseum: true,
|
||||||
queryStruct: struct {
|
}
|
||||||
Keyword string `url:"q"`
|
adminServer, err := test.NewAdminserver(chartSettings)
|
||||||
}{
|
if err != nil {
|
||||||
Keyword: "harbor",
|
t.Fatal(nil)
|
||||||
},
|
}
|
||||||
credential: sysAdmin,
|
defer adminServer.Close()
|
||||||
}, result)
|
|
||||||
require.Nil(t, err)
|
|
||||||
require.Equal(t, 1, len(result.Chart))
|
|
||||||
require.Equal(t, "library/harbor", result.Chart[0].Name)
|
|
||||||
|
|
||||||
// Restore chart search handler
|
if err := config.InitByURL(adminServer.URL); err != nil {
|
||||||
searchHandler = 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,
|
||||||
|
url: "/api/search",
|
||||||
|
queryStruct: struct {
|
||||||
|
Keyword string `url:"q"`
|
||||||
|
}{
|
||||||
|
Keyword: "harbor",
|
||||||
|
},
|
||||||
|
credential: sysAdmin,
|
||||||
|
}, result)
|
||||||
|
require.Nil(t, err)
|
||||||
|
require.Equal(t, 1, len(result.Chart))
|
||||||
|
require.Equal(t, "library/harbor", result.Chart[0].Name)
|
||||||
|
|
||||||
|
// Restore chart search handler
|
||||||
|
searchHandler = nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user