-
Notifications
You must be signed in to change notification settings - Fork 53
/
storage.go
48 lines (45 loc) · 1.18 KB
/
storage.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
package machinery
import (
"context"
"errors"
"fmt"
"github.com/rancher/opni/pkg/config/v1beta1"
"github.com/rancher/opni/pkg/storage"
)
func ConfigureStorageBackend(ctx context.Context, cfg *v1beta1.StorageSpec) (storage.Backend, error) {
storageBackend := storage.CompositeBackend{}
builder := storage.GetStoreBuilder(cfg.Type)
if builder == nil {
return nil, fmt.Errorf("unknown storage backend %s", cfg.Type)
}
var store any
var err error
switch cfg.Type {
case v1beta1.StorageTypeEtcd:
options := cfg.Etcd
if options == nil {
return nil, errors.New("etcd storage options are not set")
}
store, err = builder(ctx, cfg.Etcd, "gateway")
case v1beta1.StorageTypeCRDs:
options := cfg.CustomResources
crdOpts := []any{}
if options != nil {
crdOpts = append(crdOpts, options.Namespace)
}
store, err = builder(crdOpts...)
case v1beta1.StorageTypeJetStream:
options := cfg.JetStream
if options == nil {
return nil, errors.New("jetstream storage options are not set")
}
store, err = builder(ctx, options)
default:
return nil, errors.New("unknown storage type")
}
if err != nil {
return nil, err
}
storageBackend.Use(store)
return storageBackend, nil
}