-
Notifications
You must be signed in to change notification settings - Fork 53
/
object.go
51 lines (45 loc) · 1.11 KB
/
object.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
package store
import (
"errors"
"fmt"
"github.com/nats-io/nats.go"
corev1 "github.com/rancher/opni/pkg/apis/core/v1"
)
const (
ReprHeaderKey = "repr"
TopologyObjectStoreId = "downstream-topology"
)
func NewTopologyObjectStore(nc *nats.Conn) (nats.ObjectStore, error) {
mgr, err := nc.JetStream()
if err != nil {
return nil, err
}
if obj, err := mgr.ObjectStore(TopologyObjectStoreId); errors.Is(err, nats.ErrStreamNotFound) {
objSt, err := mgr.CreateObjectStore(&nats.ObjectStoreConfig{
Bucket: TopologyObjectStoreId,
Description: "Store downstream kubernetes topology, indexed by cluster id",
})
if err != nil {
return nil, err
}
return objSt, nil
} else if err != nil {
return nil, err
} else {
return obj, nil
}
}
func GetTopologyObjectStore(nc *nats.Conn) (nats.ObjectStore, error) {
mgr, err := nc.JetStream()
if err != nil {
return nil, err
}
obj, err := mgr.ObjectStore(TopologyObjectStoreId)
if err != nil {
return nil, err
}
return obj, nil
}
func NewClusterKey(clusterId *corev1.Reference) string {
return fmt.Sprintf("topology-%s", clusterId.GetId())
}