-
Notifications
You must be signed in to change notification settings - Fork 0
/
namespace_store.go
157 lines (130 loc) · 3.85 KB
/
namespace_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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package etcd
import (
"context"
"errors"
"fmt"
"path"
v3 "github.com/coreos/etcd/clientv3"
"github.com/coreos/etcd/mvcc/mvccpb"
"github.com/gogo/protobuf/proto"
"github.com/sensu/sensu-go/backend/store"
"github.com/sensu/sensu-go/types"
)
const (
namespacesPathPrefix = "namespaces"
)
func getNamespacePath(name string) string {
return path.Join(EtcdRoot, namespacesPathPrefix, name)
}
// GetNamespacesPath gets the path of the namespace store.
func GetNamespacesPath(ctx context.Context, name string) string {
return path.Join(EtcdRoot, namespacesPathPrefix, name)
}
// CreateNamespace creates a namespace with the provided namespace
func (s *Store) CreateNamespace(ctx context.Context, namespace *types.Namespace) error {
if err := namespace.Validate(); err != nil {
return err
}
namespaceBytes, err := proto.Marshal(namespace)
if err != nil {
return err
}
namespaceKey := getNamespacePath(namespace.Name)
res, err := s.client.Txn(ctx).
If(
// Ensure the namespace does not already exist
v3.Compare(v3.Version(namespaceKey), "=", 0)).
Then(
// Create it
v3.OpPut(namespaceKey, string(namespaceBytes)),
).Commit()
if err != nil {
return err
}
if !res.Succeeded {
return fmt.Errorf(
"could not create the namespace %s",
namespace.Name,
)
}
return err
}
// DeleteNamespace deletes the namespace with the given name
func (s *Store) DeleteNamespace(ctx context.Context, name string) error {
if name == "" {
return errors.New("must specify name")
}
// Validate whether there are any resources referencing the namespace
getresp, err := s.client.Txn(ctx).Then(
v3.OpGet(checkKeyBuilder.WithNamespace(name).Build(), v3.WithPrefix(), v3.WithCountOnly()),
v3.OpGet(entityKeyBuilder.WithNamespace(name).Build(), v3.WithPrefix(), v3.WithCountOnly()),
v3.OpGet(assetKeyBuilder.WithNamespace(name).Build(), v3.WithPrefix(), v3.WithCountOnly()),
v3.OpGet(handlerKeyBuilder.WithNamespace(name).Build(), v3.WithPrefix(), v3.WithCountOnly()),
v3.OpGet(mutatorKeyBuilder.WithNamespace(name).Build(), v3.WithPrefix(), v3.WithCountOnly()),
).Commit()
if err != nil {
return err
}
for _, r := range getresp.Responses {
if r.GetResponseRange().Count > 0 {
return errors.New("namespace is not empty")
}
}
// Delete the resource
resp, err := s.client.Delete(ctx, getNamespacePath(name), v3.WithPrefix())
if err != nil {
return err
}
if resp.Deleted != 1 {
return fmt.Errorf("namespace %s does not exist", name)
}
return nil
}
// GetNamespace returns a single namespace with the given name
func (s *Store) GetNamespace(ctx context.Context, name string) (*types.Namespace, error) {
resp, err := s.client.Get(
ctx,
getNamespacePath(name),
v3.WithLimit(1),
)
if err != nil {
return nil, err
}
if len(resp.Kvs) == 0 {
return nil, nil
}
namespaces, err := unmarshalNamespaces(resp.Kvs)
if err != nil {
return &types.Namespace{}, err
}
return namespaces[0], nil
}
// ListNamespaces returns all namespaces
func (s *Store) ListNamespaces(ctx context.Context, pred *store.SelectionPredicate) ([]*types.Namespace, error) {
namespaces := []*types.Namespace{}
err := List(ctx, s.client, GetNamespacesPath, &namespaces, pred)
return namespaces, err
}
// UpdateNamespace updates a namespace with the given object
func (s *Store) UpdateNamespace(ctx context.Context, namespace *types.Namespace) error {
if err := namespace.Validate(); err != nil {
return err
}
bytes, err := proto.Marshal(namespace)
if err != nil {
return err
}
_, err = s.client.Put(ctx, getNamespacePath(namespace.Name), string(bytes))
return err
}
func unmarshalNamespaces(kvs []*mvccpb.KeyValue) ([]*types.Namespace, error) {
s := make([]*types.Namespace, len(kvs))
for i, kv := range kvs {
namespace := &types.Namespace{}
s[i] = namespace
if err := unmarshal(kv.Value, namespace); err != nil {
return nil, err
}
}
return s, nil
}