Move keyvalue initialisation to options

Co-authored-by: Florian Schade <f.schade@opencloud.eu>

Signed-off-by: Christian Richter <c.richter@opencloud.eu>
This commit is contained in:
Christian Richter
2025-09-30 13:57:25 +02:00
committed by Ralf Haferkamp
parent 07a8fef80e
commit 5225b66f65
5 changed files with 44 additions and 34 deletions

View File

@@ -5,6 +5,7 @@ import (
"fmt"
"os/signal"
"github.com/nats-io/nats.go"
"github.com/opencloud-eu/opencloud/pkg/config/configlog"
"github.com/opencloud-eu/opencloud/pkg/runner"
"github.com/opencloud-eu/opencloud/pkg/tracing"
@@ -44,6 +45,28 @@ func Server(cfg *config.Config) *cli.Command {
mtrcs := metrics.New()
mtrcs.BuildInfo.WithLabelValues(version.GetString()).Set(1)
//Connect to NATS servers
natsOptions := nats.Options{
Servers: cfg.Store.Nodes,
}
conn, err := natsOptions.Connect()
if err != nil {
return err
}
js, err := conn.JetStream()
if err != nil {
return err
}
kv, err := js.KeyValue(cfg.Store.Database)
if err != nil {
return err
}
if err != nil {
return err
}
gr := runner.NewGroup()
{
server, err := http.Server(
@@ -52,6 +75,7 @@ func Server(cfg *config.Config) *cli.Command {
http.Config(cfg),
http.Metrics(mtrcs),
http.TraceProvider(traceProvider),
http.NatsKeyValue(kv),
)
if err != nil {
logger.Error().Err(err).Str("transport", "http").Msg("Failed to initialize server")

View File

@@ -3,6 +3,7 @@ package http
import (
"context"
"github.com/nats-io/nats.go"
"github.com/opencloud-eu/opencloud/pkg/log"
"github.com/opencloud-eu/opencloud/services/graph/pkg/config"
"github.com/opencloud-eu/opencloud/services/graph/pkg/metrics"
@@ -22,6 +23,7 @@ type Options struct {
Flags []cli.Flag
Namespace string
TraceProvider trace.TracerProvider
NatsKeyValue nats.KeyValue
}
// newOptions initializes the available default options.
@@ -83,3 +85,10 @@ func TraceProvider(val trace.TracerProvider) Option {
o.TraceProvider = val
}
}
// NatsKeyValue provides a function to set the NatsKeyValue option.
func NatsKeyValue(val nats.KeyValue) Option {
return func(o *Options) {
o.NatsKeyValue = val
}
}

View File

@@ -178,6 +178,7 @@ func Server(opts ...Option) (http.Service, error) {
svc.KeycloakClient(keyCloakClient),
svc.EventHistoryClient(hClient),
svc.TraceProvider(options.TraceProvider),
svc.WithNatsKeyValue(options.NatsKeyValue),
)
if err != nil {

View File

@@ -5,6 +5,7 @@ import (
"net/http"
gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1"
"github.com/nats-io/nats.go"
"github.com/opencloud-eu/reva/v2/pkg/events"
"github.com/opencloud-eu/reva/v2/pkg/rgrpc/todo/pool"
"go.opentelemetry.io/otel/trace"
@@ -43,6 +44,7 @@ type Options struct {
KeycloakClient keycloak.Client
EventHistoryClient ehsvc.EventHistoryService
TraceProvider trace.TracerProvider
NatsKeyValue nats.KeyValue
}
// newOptions initializes the available default options.
@@ -112,6 +114,13 @@ func WithIdentityEducationBackend(val identity.EducationBackend) Option {
}
}
// WithNatsKeyValue provides a function to set the NatsKeyValue option.
func WithNatsKeyValue(val nats.KeyValue) Option {
return func(o *Options) {
o.NatsKeyValue = val
}
}
// WithRoleService provides a function to set the RoleService option.
func WithRoleService(val RoleService) Option {
return func(o *Options) {

View File

@@ -15,7 +15,6 @@ import (
"github.com/go-chi/chi/v5/middleware"
ldapv3 "github.com/go-ldap/ldap/v3"
"github.com/jellydator/ttlcache/v3"
"github.com/nats-io/nats.go"
"github.com/riandyrn/otelchi"
microstore "go-micro.dev/v4/store"
@@ -154,38 +153,6 @@ func NewService(opts ...Option) (Graph, error) { //nolint:maintidx
identity.IdentityCacheWithGroupsTTL(time.Duration(options.Config.Spaces.GroupsCacheTTL)),
)
// Connect to NATS servers
natsOptions := nats.Options{
Servers: options.Config.Store.Nodes,
}
conn, err := natsOptions.Connect()
if err != nil {
return Graph{}, err
}
js, err := conn.JetStream()
if err != nil {
return Graph{}, err
}
kv, err := js.KeyValue(options.Config.Store.Database)
if err != nil {
if !errors.Is(err, nats.ErrBucketNotFound) {
return Graph{}, fmt.Errorf("Failed to get bucket (%s): %w", options.Config.Store.Database, err)
}
kv, err = js.CreateKeyValue(&nats.KeyValueConfig{
Bucket: options.Config.Store.Database,
})
if err != nil {
return Graph{}, fmt.Errorf("Failed to create bucket (%s): %w", options.Config.Store.Database, err)
}
}
if err != nil {
return Graph{}, err
}
baseGraphService := BaseGraphService{
logger: &options.Logger,
identityCache: identityCache,
@@ -231,7 +198,7 @@ func NewService(opts ...Option) (Graph, error) { //nolint:maintidx
historyClient: options.EventHistoryClient,
traceProvider: options.TraceProvider,
valueService: options.ValueService,
natskv: kv,
natskv: options.NatsKeyValue,
}
if err := setIdentityBackends(options, &svc); err != nil {