harbor/service/cache/cache.go

145 lines
3.6 KiB
Go
Raw Normal View History

2016-02-01 11:59:10 +00:00
/*
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.
*/
2016-02-26 10:54:14 +00:00
package cache
2016-02-01 11:59:10 +00:00
import (
2016-04-15 05:17:32 +00:00
"os"
2016-02-01 11:59:10 +00:00
"time"
2016-03-25 01:31:50 +00:00
"github.com/vmware/harbor/utils/log"
2016-04-15 05:17:32 +00:00
"github.com/vmware/harbor/utils/registry"
"github.com/vmware/harbor/utils/registry/auth"
2016-02-01 11:59:10 +00:00
"github.com/astaxie/beego/cache"
)
2016-04-27 09:59:43 +00:00
var (
// Cache is the global cache in system.
Cache cache.Cache
endpoint string
username string
2016-04-27 09:59:43 +00:00
)
2016-04-15 05:17:32 +00:00
2016-02-26 10:35:55 +00:00
const catalogKey string = "catalog"
2016-02-01 11:59:10 +00:00
func init() {
var err error
Cache, err = cache.NewCache("memory", `{"interval":720}`)
if err != nil {
2016-03-26 16:18:11 +00:00
log.Errorf("Failed to initialize cache, error:%v", err)
2016-02-01 11:59:10 +00:00
}
2016-04-15 05:17:32 +00:00
2016-04-27 09:59:43 +00:00
endpoint = os.Getenv("REGISTRY_URL")
username = "admin"
2016-02-01 11:59:10 +00:00
}
2016-02-26 10:35:55 +00:00
// RefreshCatalogCache calls registry's API to get repository list and write it to cache.
2016-02-01 11:59:10 +00:00
func RefreshCatalogCache() error {
2016-04-15 05:17:32 +00:00
log.Debug("refreshing catalog cache...")
2016-04-27 09:59:43 +00:00
registryClient, err := NewRegistryClient(endpoint, true, username,
"registry", "catalog", "*")
if err != nil {
return err
2016-04-27 09:59:43 +00:00
}
2016-04-17 14:39:10 +00:00
rs, err := registryClient.Catalog()
2016-02-01 11:59:10 +00:00
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()
2016-04-27 09:59:43 +00:00
if err != nil {
log.Errorf("error occurred while list tag for %s: %v", repo, err)
2016-04-29 08:59:54 +00:00
continue
2016-04-27 09:59:43 +00:00
}
2016-04-15 05:17:32 +00:00
if len(tags) != 0 {
repos = append(repos, repo)
log.Debugf("add %s to catalog cache", repo)
}
2016-04-27 09:59:43 +00:00
}
*/
Cache.Put(catalogKey, rs, 600*time.Second)
2016-02-01 11:59:10 +00:00
return nil
}
2016-02-26 10:35:55 +00:00
// GetRepoFromCache get repository list from cache, it refreshes the cache if it's empty.
2016-02-01 11:59:10 +00:00
func GetRepoFromCache() ([]string, error) {
2016-02-26 10:35:55 +00:00
result := Cache.Get(catalogKey)
2016-02-01 11:59:10 +00:00
if result == nil {
err := RefreshCatalogCache()
if err != nil {
return nil, err
}
2016-02-26 10:35:55 +00:00
cached := Cache.Get(catalogKey)
2016-02-01 11:59:10 +00:00
if cached != nil {
return cached.([]string), nil
}
return nil, nil
}
return result.([]string), nil
}
// NewRegistryClient ...
func NewRegistryClient(endpoint string, insecure bool, username, scopeType, scopeName string,
scopeActions ...string) (*registry.Registry, error) {
authorizer := auth.NewUsernameTokenAuthorizer(username, scopeType, scopeName, scopeActions...)
2016-06-22 04:03:50 +00:00
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...)
2016-06-22 04:03:50 +00:00
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
}