diff --git a/api/search.go b/api/search.go index e12cd2383..aac78b3b2 100644 --- a/api/search.go +++ b/api/search.go @@ -22,6 +22,7 @@ import ( "github.com/vmware/harbor/dao" "github.com/vmware/harbor/models" + "github.com/vmware/harbor/service/cache" "github.com/vmware/harbor/utils" "github.com/vmware/harbor/utils/log" ) @@ -84,17 +85,12 @@ func (s *SearchAPI) Get() { } } - repos, err := dao.GetAllRepositories() + repositories, err := cache.GetRepoFromCache() if err != nil { log.Errorf("failed to list repositories: %v", err) s.CustomAbort(http.StatusInternalServerError, "") } - repositories := []string{} - for _, repo := range repos { - repositories = append(repositories, repo.Name) - } - sort.Strings(repositories) repositoryResult := filterRepositories(repositories, projects, keyword) result := &searchResult{Project: projectResult, Repository: repositoryResult} diff --git a/service/cache/cache.go b/service/cache/cache.go index 762ea9cda..67c3c224f 100644 --- a/service/cache/cache.go +++ b/service/cache/cache.go @@ -16,9 +16,9 @@ package cache import ( - "os" "time" + "github.com/vmware/harbor/dao" "github.com/vmware/harbor/utils/log" "github.com/vmware/harbor/utils/registry" "github.com/vmware/harbor/utils/registry/auth" @@ -28,9 +28,7 @@ import ( var ( // Cache is the global cache in system. - Cache cache.Cache - endpoint string - username string + Cache cache.Cache ) const catalogKey string = "catalog" @@ -41,52 +39,17 @@ func init() { if err != nil { log.Errorf("Failed to initialize cache, error:%v", err) } - - endpoint = os.Getenv("REGISTRY_URL") - username = "admin" } // RefreshCatalogCache calls registry's API to get repository list and write it to cache. func RefreshCatalogCache() error { log.Debug("refreshing catalog cache...") - registryClient, err := NewRegistryClient(endpoint, true, username, - "registry", "catalog", "*") + repos, err := getAllRepositories() if err != nil { return err } - - rs, err := registryClient.Catalog() - if err != nil { - return err - } - /* - repos := []string{} - - for _, repo := range rs { - rc, ok := repositoryClients[repo] - if !ok { - rc, err = registry.NewRepositoryWithUsername(repo, endpoint, username) - if err != nil { - log.Errorf("error occurred while initializing repository client used by cache: %s %v", repo, err) - continue - } - repositoryClients[repo] = rc - } - tags, err := rc.ListTag() - if err != nil { - log.Errorf("error occurred while list tag for %s: %v", repo, err) - continue - } - - if len(tags) != 0 { - repos = append(repos, repo) - log.Debugf("add %s to catalog cache", repo) - } - } - */ - - Cache.Put(catalogKey, rs, 600*time.Second) + Cache.Put(catalogKey, repos, 600*time.Second) return nil } @@ -108,6 +71,18 @@ func GetRepoFromCache() ([]string, error) { return result.([]string), nil } +func getAllRepositories() ([]string, error) { + var repos []string + rs, err := dao.GetAllRepositories() + if err != nil { + return repos, err + } + for _, e := range rs { + repos = append(repos, e.Name) + } + return repos, nil +} + // NewRegistryClient ... func NewRegistryClient(endpoint string, insecure bool, username, scopeType, scopeName string, scopeActions ...string) (*registry.Registry, error) {