mirror of
https://github.com/goharbor/harbor.git
synced 2024-11-29 21:54:13 +01:00
remove cache
This commit is contained in:
parent
d6a6f67596
commit
1d83d8b642
@ -28,7 +28,6 @@ import (
|
|||||||
"github.com/vmware/harbor/src/common/models"
|
"github.com/vmware/harbor/src/common/models"
|
||||||
"github.com/vmware/harbor/src/common/utils/log"
|
"github.com/vmware/harbor/src/common/utils/log"
|
||||||
"github.com/vmware/harbor/src/common/utils/registry"
|
"github.com/vmware/harbor/src/common/utils/registry"
|
||||||
"github.com/vmware/harbor/src/ui/service/cache"
|
|
||||||
svc_utils "github.com/vmware/harbor/src/ui/service/utils"
|
svc_utils "github.com/vmware/harbor/src/ui/service/utils"
|
||||||
|
|
||||||
registry_error "github.com/vmware/harbor/src/common/utils/registry/error"
|
registry_error "github.com/vmware/harbor/src/common/utils/registry/error"
|
||||||
@ -195,13 +194,6 @@ func (ra *RepositoryAPI) Delete() {
|
|||||||
ra.CustomAbort(http.StatusInternalServerError, "")
|
ra.CustomAbort(http.StatusInternalServerError, "")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
|
||||||
log.Debug("refreshing catalog cache")
|
|
||||||
if err := cache.RefreshCatalogCache(); err != nil {
|
|
||||||
log.Errorf("error occurred while refresh catalog cache: %v", err)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type tag struct {
|
type tag struct {
|
||||||
@ -392,7 +384,7 @@ func (ra *RepositoryAPI) initRepositoryClient(repoName string) (r *registry.Repo
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return cache.NewRepositoryClient(endpoint, !verify, username, repoName,
|
return NewRepositoryClient(endpoint, !verify, username, repoName,
|
||||||
"repository", repoName, "pull", "push", "*")
|
"repository", repoName, "pull", "push", "*")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,7 +26,6 @@ import (
|
|||||||
"github.com/vmware/harbor/src/common/utils"
|
"github.com/vmware/harbor/src/common/utils"
|
||||||
"github.com/vmware/harbor/src/common/utils/log"
|
"github.com/vmware/harbor/src/common/utils/log"
|
||||||
"github.com/vmware/harbor/src/ui/config"
|
"github.com/vmware/harbor/src/ui/config"
|
||||||
"github.com/vmware/harbor/src/ui/service/cache"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// SearchAPI handles requesst to /api/search
|
// SearchAPI handles requesst to /api/search
|
||||||
@ -160,7 +159,7 @@ func getTags(repository string) ([]string, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
client, err := cache.NewRepositoryClient(url, true,
|
client, err := NewRepositoryClient(url, true,
|
||||||
"admin", repository, "repository", repository, "pull")
|
"admin", repository, "repository", repository, "pull")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -29,9 +29,9 @@ import (
|
|||||||
"github.com/vmware/harbor/src/common/utils"
|
"github.com/vmware/harbor/src/common/utils"
|
||||||
"github.com/vmware/harbor/src/common/utils/log"
|
"github.com/vmware/harbor/src/common/utils/log"
|
||||||
"github.com/vmware/harbor/src/common/utils/registry"
|
"github.com/vmware/harbor/src/common/utils/registry"
|
||||||
|
"github.com/vmware/harbor/src/common/utils/registry/auth"
|
||||||
registry_error "github.com/vmware/harbor/src/common/utils/registry/error"
|
registry_error "github.com/vmware/harbor/src/common/utils/registry/error"
|
||||||
"github.com/vmware/harbor/src/ui/config"
|
"github.com/vmware/harbor/src/ui/config"
|
||||||
"github.com/vmware/harbor/src/ui/service/cache"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func checkProjectPermission(userID int, projectID int64) bool {
|
func checkProjectPermission(userID int, projectID int64) bool {
|
||||||
@ -352,7 +352,7 @@ func diffRepos(reposInRegistry []string, reposInDB []string) ([]string, []string
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return needsAdd, needsDel, err
|
return needsAdd, needsDel, err
|
||||||
}
|
}
|
||||||
client, err := cache.NewRepositoryClient(endpoint, true,
|
client, err := NewRepositoryClient(endpoint, true,
|
||||||
"admin", repoInR, "repository", repoInR)
|
"admin", repoInR, "repository", repoInR)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return needsAdd, needsDel, err
|
return needsAdd, needsDel, err
|
||||||
@ -377,7 +377,7 @@ func diffRepos(reposInRegistry []string, reposInDB []string) ([]string, []string
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return needsAdd, needsDel, err
|
return needsAdd, needsDel, err
|
||||||
}
|
}
|
||||||
client, err := cache.NewRepositoryClient(endpoint, true,
|
client, err := NewRepositoryClient(endpoint, true,
|
||||||
"admin", repoInR, "repository", repoInR)
|
"admin", repoInR, "repository", repoInR)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return needsAdd, needsDel, err
|
return needsAdd, needsDel, err
|
||||||
@ -440,7 +440,7 @@ func initRegistryClient() (r *registry.Registry, err error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
registryClient, err := cache.NewRegistryClient(endpoint, true, "admin",
|
registryClient, err := NewRegistryClient(endpoint, true, "admin",
|
||||||
"registry", "catalog", "*")
|
"registry", "catalog", "*")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -485,10 +485,6 @@ func getReposByProject(name string, keyword ...string) ([]string, error) {
|
|||||||
return repositories, nil
|
return repositories, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getAllRepos() ([]string, error) {
|
|
||||||
return cache.GetRepoFromCache()
|
|
||||||
}
|
|
||||||
|
|
||||||
func repositoryExist(name string, client *registry.Repository) (bool, error) {
|
func repositoryExist(name string, client *registry.Repository) (bool, error) {
|
||||||
tags, err := client.ListTag()
|
tags, err := client.ListTag()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -499,3 +495,38 @@ func repositoryExist(name string, client *registry.Repository) (bool, error) {
|
|||||||
}
|
}
|
||||||
return len(tags) != 0, nil
|
return len(tags) != 0, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewRegistryClient ...
|
||||||
|
func NewRegistryClient(endpoint string, insecure bool, username, scopeType, scopeName string,
|
||||||
|
scopeActions ...string) (*registry.Registry, error) {
|
||||||
|
authorizer := auth.NewUsernameTokenAuthorizer(username, scopeType, scopeName, scopeActions...)
|
||||||
|
|
||||||
|
store, err := auth.NewAuthorizerStore(endpoint, insecure, authorizer)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := registry.NewRegistryWithModifiers(endpoint, insecure, store)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return client, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewRepositoryClient ...
|
||||||
|
func NewRepositoryClient(endpoint string, insecure bool, username, repository, scopeType, scopeName string,
|
||||||
|
scopeActions ...string) (*registry.Repository, error) {
|
||||||
|
|
||||||
|
authorizer := auth.NewUsernameTokenAuthorizer(username, scopeType, scopeName, scopeActions...)
|
||||||
|
|
||||||
|
store, err := auth.NewAuthorizerStore(endpoint, insecure, authorizer)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := registry.NewRepositoryWithModifiers(repository, endpoint, insecure, store)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return client, nil
|
||||||
|
}
|
||||||
|
119
src/ui/service/cache/cache.go
vendored
119
src/ui/service/cache/cache.go
vendored
@ -1,119 +0,0 @@
|
|||||||
/*
|
|
||||||
Copyright (c) 2016 VMware, Inc. All Rights Reserved.
|
|
||||||
Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
you may not use this file except in compliance with the License.
|
|
||||||
You may obtain a copy of the License at
|
|
||||||
|
|
||||||
http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
|
|
||||||
Unless required by applicable law or agreed to in writing, software
|
|
||||||
distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
See the License for the specific language governing permissions and
|
|
||||||
limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package cache
|
|
||||||
|
|
||||||
import (
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/vmware/harbor/src/common/dao"
|
|
||||||
"github.com/vmware/harbor/src/common/utils/log"
|
|
||||||
"github.com/vmware/harbor/src/common/utils/registry"
|
|
||||||
"github.com/vmware/harbor/src/common/utils/registry/auth"
|
|
||||||
|
|
||||||
"github.com/astaxie/beego/cache"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
// Cache is the global cache in system.
|
|
||||||
Cache cache.Cache
|
|
||||||
)
|
|
||||||
|
|
||||||
const catalogKey string = "catalog"
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
var err error
|
|
||||||
Cache, err = cache.NewCache("memory", `{"interval":720}`)
|
|
||||||
if err != nil {
|
|
||||||
log.Errorf("Failed to initialize cache, error:%v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// RefreshCatalogCache calls registry's API to get repository list and write it to cache.
|
|
||||||
func RefreshCatalogCache() error {
|
|
||||||
log.Debug("refreshing catalog cache...")
|
|
||||||
|
|
||||||
repos, err := getAllRepositories()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
Cache.Put(catalogKey, repos, 600*time.Second)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetRepoFromCache get repository list from cache, it refreshes the cache if it's empty.
|
|
||||||
func GetRepoFromCache() ([]string, error) {
|
|
||||||
|
|
||||||
result := Cache.Get(catalogKey)
|
|
||||||
if result == nil {
|
|
||||||
err := RefreshCatalogCache()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
cached := Cache.Get(catalogKey)
|
|
||||||
if cached != nil {
|
|
||||||
return cached.([]string), nil
|
|
||||||
}
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
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) {
|
|
||||||
authorizer := auth.NewUsernameTokenAuthorizer(username, scopeType, scopeName, scopeActions...)
|
|
||||||
|
|
||||||
store, err := auth.NewAuthorizerStore(endpoint, insecure, authorizer)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
client, err := registry.NewRegistryWithModifiers(endpoint, insecure, store)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return client, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewRepositoryClient ...
|
|
||||||
func NewRepositoryClient(endpoint string, insecure bool, username, repository, scopeType, scopeName string,
|
|
||||||
scopeActions ...string) (*registry.Repository, error) {
|
|
||||||
|
|
||||||
authorizer := auth.NewUsernameTokenAuthorizer(username, scopeType, scopeName, scopeActions...)
|
|
||||||
|
|
||||||
store, err := auth.NewAuthorizerStore(endpoint, insecure, authorizer)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
client, err := registry.NewRepositoryWithModifiers(repository, endpoint, insecure, store)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return client, nil
|
|
||||||
}
|
|
9
src/ui/service/cache/cache_test.go
vendored
9
src/ui/service/cache/cache_test.go
vendored
@ -1,9 +0,0 @@
|
|||||||
package cache
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestMain(t *testing.T) {
|
|
||||||
}
|
|
||||||
|
|
@ -25,7 +25,6 @@ import (
|
|||||||
"github.com/vmware/harbor/src/common/utils"
|
"github.com/vmware/harbor/src/common/utils"
|
||||||
"github.com/vmware/harbor/src/common/utils/log"
|
"github.com/vmware/harbor/src/common/utils/log"
|
||||||
"github.com/vmware/harbor/src/ui/api"
|
"github.com/vmware/harbor/src/ui/api"
|
||||||
"github.com/vmware/harbor/src/ui/service/cache"
|
|
||||||
|
|
||||||
"github.com/astaxie/beego"
|
"github.com/astaxie/beego"
|
||||||
)
|
)
|
||||||
@ -82,9 +81,6 @@ func (n *NotificationHandler) Post() {
|
|||||||
if err := dao.AddRepository(repoRecord); err != nil {
|
if err := dao.AddRepository(repoRecord); err != nil {
|
||||||
log.Errorf("Error happens when adding repository: %v", err)
|
log.Errorf("Error happens when adding repository: %v", err)
|
||||||
}
|
}
|
||||||
if err := cache.RefreshCatalogCache(); err != nil {
|
|
||||||
log.Errorf("failed to refresh cache: %v", err)
|
|
||||||
}
|
|
||||||
}()
|
}()
|
||||||
go api.TriggerReplicationByRepository(repository, []string{tag}, models.RepOpTransfer)
|
go api.TriggerReplicationByRepository(repository, []string{tag}, models.RepOpTransfer)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user