forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
66 lines (55 loc) · 1.47 KB
/
store.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package clusterprovisioner
import (
"encoding/json"
"github.com/rancher/kontainer-engine/cluster"
"github.com/rancher/rancher/pkg/encryptedstore"
"github.com/rancher/types/apis/core/v1"
"github.com/sirupsen/logrus"
typedv1 "k8s.io/client-go/kubernetes/typed/core/v1"
)
const (
dataKey = "cluster"
)
func NewPersistentStore(namespaces v1.NamespaceInterface, secretsGetter typedv1.SecretsGetter) cluster.PersistentStore {
store, err := encryptedstore.NewGenericEncrypedStore("c-", "", namespaces, secretsGetter)
if err != nil {
logrus.Fatal(err)
}
return &engineStore{
store: store,
}
}
type engineStore struct {
store *encryptedstore.GenericEncryptedStore
}
func (s *engineStore) GetStatus(name string) (string, error) {
cls, err := s.Get(name)
if err != nil {
return "", err
}
return cls.Status, nil
}
func (s *engineStore) Get(name string) (cluster.Cluster, error) {
cluster := cluster.Cluster{}
data, err := s.store.Get(name)
if err != nil {
return cluster, err
}
return cluster, json.Unmarshal([]byte(data[dataKey]), &cluster)
}
func (s *engineStore) Remove(name string) error {
return s.store.Remove(name)
}
func (s *engineStore) Store(cluster cluster.Cluster) error {
content, err := json.Marshal(cluster)
if err != nil {
return err
}
return s.store.Set(cluster.Name, map[string]string{
dataKey: string(content),
})
}
func (s *engineStore) PersistStatus(cluster cluster.Cluster, status string) error {
cluster.Status = status
return s.Store(cluster)
}