You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
helm/pkg/repo/inmem_repo_service.go

157 lines
3.4 KiB

/*
Copyright 2015 The Kubernetes Authors 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 repo
import (
"errors"
"fmt"
"strings"
"sync"
)
type inmemRepoService struct {
sync.RWMutex
repositories map[string]IRepo
}
// NewInmemRepoService returns a new memory based repository service.
func NewInmemRepoService() IRepoService {
rs := &inmemRepoService{
repositories: make(map[string]IRepo),
}
r, err := NewPublicGCSRepo(nil)
if err == nil {
9 years ago
rs.CreateRepo(r)
}
return rs
}
9 years ago
// ListRepos returns the list of all known chart repositories
func (rs *inmemRepoService) ListRepos() (map[string]string, error) {
rs.RLock()
defer rs.RUnlock()
ret := make(map[string]string)
for _, r := range rs.repositories {
ret[r.GetName()] = r.GetURL()
}
return ret, nil
}
9 years ago
// CreateRepo adds a known repository to the list
func (rs *inmemRepoService) CreateRepo(repository IRepo) error {
rs.Lock()
defer rs.Unlock()
9 years ago
URL := repository.GetURL()
name := repository.GetName()
valid := GCSRepoURLMatcher.MatchString(URL)
if !valid {
return errors.New(URL + " is an invalid Repo URL")
}
for u, r := range rs.repositories {
if u == URL {
return fmt.Errorf("Repository with URL %s already exists", URL)
} else if r.GetName() == name {
return fmt.Errorf("Repository with Name %s already exists", name)
}
}
9 years ago
rs.repositories[URL] = repository
return nil
}
9 years ago
// GetRepoByURL returns the repository with the given URL
func (rs *inmemRepoService) GetRepoByURL(URL string) (IRepo, error) {
rs.RLock()
defer rs.RUnlock()
9 years ago
r, ok := rs.repositories[URL]
if !ok {
9 years ago
return nil, fmt.Errorf("No repository with URL %s", URL)
}
return r, nil
}
9 years ago
// GetRepoByChartURL returns the repository that backs the given chart URL
func (rs *inmemRepoService) GetRepoByChartURL(URL string) (IRepo, error) {
rs.RLock()
defer rs.RUnlock()
cSplit := strings.Split(URL, "/")
var found IRepo
for _, r := range rs.repositories {
rURL := r.GetURL()
rSplit := strings.Split(rURL, "/")
if hasPrefix(cSplit, rSplit) {
if found == nil || len(found.GetURL()) < len(rURL) {
found = r
}
}
}
if found == nil {
return nil, fmt.Errorf("No repository found for chart url: %s", URL)
}
return found, nil
}
func hasPrefix(cSplit, rSplit []string) bool {
if len(rSplit) > len(cSplit) {
return false
}
for i := range rSplit {
if rSplit[i] != cSplit[i] {
return false
}
}
return true
}
9 years ago
// DeleteRepo removes a known repository from the list
func (rs *inmemRepoService) DeleteRepo(URL string) error {
rs.Lock()
defer rs.Unlock()
9 years ago
_, ok := rs.repositories[URL]
if !ok {
9 years ago
return fmt.Errorf("No repository with URL %s", URL)
}
9 years ago
delete(rs.repositories, URL)
return nil
}
func (rs *inmemRepoService) GetRepoURLByName(name string) (string, error) {
for url, r := range rs.repositories {
if r.GetName() == name {
return url, nil
}
}
err := fmt.Errorf("No repository url found with name %s", name)
return "", err
}