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/storage/memory.go

103 lines
2.4 KiB

package storage
import (
"errors"
"sync"
"github.com/kubernetes/helm/pkg/proto/hapi/release"
)
// Memory is an in-memory ReleaseStorage implementation.
type Memory struct {
sync.RWMutex
releases map[string]*release.Release
}
// NewMemory creates a new in-memory storage.
func NewMemory() *Memory {
return &Memory{
releases: map[string]*release.Release{},
}
}
// ErrNotFound indicates that a release is not found.
var ErrNotFound = errors.New("release not found")
// Read returns the named Release.
//
// If the release is not found, an ErrNotFound error is returned.
func (m *Memory) Read(k string) (*release.Release, error) {
m.RLock()
defer m.RUnlock()
v, ok := m.releases[k]
if !ok {
return v, ErrNotFound
}
return v, nil
}
// Create sets a release.
func (m *Memory) Create(rel *release.Release) error {
m.Lock()
defer m.Unlock()
m.releases[rel.Name] = rel
return nil
}
// Update sets a release.
func (m *Memory) Update(rel *release.Release) error {
m.Lock()
defer m.Unlock()
if _, ok := m.releases[rel.Name]; !ok {
return ErrNotFound
}
// FIXME: When Release is done, we need to do this right by marking the old
// release as superseded, and creating a new release.
m.releases[rel.Name] = rel
return nil
}
// Delete removes a release.
func (m *Memory) Delete(name string) (*release.Release, error) {
m.Lock()
defer m.Unlock()
rel, ok := m.releases[name]
if !ok {
return nil, ErrNotFound
}
delete(m.releases, name)
return rel, nil
}
// List returns all releases whose status is not Status_DELETED.
func (m *Memory) List() ([]*release.Release, error) {
m.RLock()
defer m.RUnlock()
buf := []*release.Release{}
for _, v := range m.releases {
if v.Info.Status.Code != release.Status_DELETED {
buf = append(buf, v)
}
}
return buf, nil
}
// Query searches all releases for matches.
func (m *Memory) Query(labels map[string]string) ([]*release.Release, error) {
m.RLock()
defer m.RUnlock()
return []*release.Release{}, errors.New("not implemented")
}
// History returns the history of this release, in the form of a series of releases.
func (m *Memory) History(name string) ([]*release.Release, error) {
// TODO: This _should_ return all of the releases with the given name, sorted
// by LastDeployed, regardless of status.
r, err := m.Read(name)
if err != nil {
return []*release.Release{}, err
}
return []*release.Release{r}, nil
}