From efb9e57656d06a03d822a4b2c72e3ebf8aa41e9b Mon Sep 17 00:00:00 2001 From: Andrei Matveyeu Date: Mon, 30 Sep 2024 09:50:16 +0200 Subject: [PATCH] Make etcd tree prefix configurable in etcd.New() --- cmd/executionspace/main.go | 3 ++- internal/database/etcd/etcd.go | 26 +++++++++++++------------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/cmd/executionspace/main.go b/cmd/executionspace/main.go index 2c72854..f52d5b4 100644 --- a/cmd/executionspace/main.go +++ b/cmd/executionspace/main.go @@ -69,7 +69,8 @@ func main() { }) log.Info("Loading v1alpha routes") - provider := provider.Kubernetes{}.New(etcd.New(cfg, logger), cfg) + executionSpaceEtcdTreePrefix := "/execution-space" + provider := provider.Kubernetes{}.New(etcd.New(cfg, logger, executionSpaceEtcdTreePrefix), cfg) providerServiceApp := providerservice.New(cfg, log, provider, ctx) defer providerServiceApp.Close() handler := application.New(providerServiceApp) diff --git a/internal/database/etcd/etcd.go b/internal/database/etcd/etcd.go index c97b585..363e37c 100644 --- a/internal/database/etcd/etcd.go +++ b/internal/database/etcd/etcd.go @@ -29,21 +29,20 @@ import ( clientv3 "go.etcd.io/etcd/client/v3" ) -const etcdTreePrefix string = "/execution-space" - // TODO: refactor the client so that it does not store data it fetched. // However, without it implementing the database.Opener interface would be more complex (methods readByte, read). type Etcd struct { - cfg config.Config - client *clientv3.Client - ID uuid.UUID - ctx context.Context - data []byte - hasRead bool + cfg config.Config + client *clientv3.Client + ID uuid.UUID + ctx context.Context + treePrefix string + data []byte + hasRead bool } // New returns a new Etcd Object/Struct. -func New(cfg config.Config, logger *logrus.Logger) database.Opener { +func New(cfg config.Config, logger *logrus.Logger, treePrefix string) database.Opener { client, err := clientv3.New(clientv3.Config{ Endpoints: []string{cfg.DatabaseURI()}, DialTimeout: 5 * time.Second, @@ -53,8 +52,9 @@ func New(cfg config.Config, logger *logrus.Logger) database.Opener { } return Etcd{ - client: client, - cfg: cfg, + client: client, + cfg: cfg, + treePrefix: treePrefix, } } @@ -73,7 +73,7 @@ func (etcd Etcd) Write(p []byte) (int, error) { if etcd.ID == uuid.Nil { return 0, errors.New("please create a new etcd client using Open") } - key := fmt.Sprintf("%s/%s", etcdTreePrefix, etcd.ID.String()) + key := fmt.Sprintf("%s/%s", etcd.treePrefix, etcd.ID.String()) _, err := etcd.client.Put(etcd.ctx, key, string(p)) if err != nil { return 0, err @@ -95,7 +95,7 @@ func (etcd *Etcd) Read(p []byte) (n int, err error) { return n, err } - key := fmt.Sprintf("%s/%s", etcdTreePrefix, etcd.ID.String()) + key := fmt.Sprintf("%s/%s", etcd.treePrefix, etcd.ID.String()) if !etcd.hasRead { resp, err := etcd.client.Get(etcd.ctx, key)