From daf50394fc184652edaeb55d1886e666aeed29c4 Mon Sep 17 00:00:00 2001 From: Andrei Matveyeu Date: Mon, 30 Sep 2024 09:45:48 +0200 Subject: [PATCH] Use shared etcd client in IUT Provider --- cmd/iut/main.go | 22 ++++++++++++---------- pkg/iut/v1alpha1/v1alpha1.go | 12 +++++++----- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/cmd/iut/main.go b/cmd/iut/main.go index 19cb0bd..1189ad7 100644 --- a/cmd/iut/main.go +++ b/cmd/iut/main.go @@ -25,6 +25,7 @@ import ( "time" config "github.com/eiffel-community/etos-api/internal/configs/iut" + "github.com/eiffel-community/etos-api/internal/database/etcd" "github.com/eiffel-community/etos-api/internal/logging" server "github.com/eiffel-community/etos-api/internal/server" "github.com/eiffel-community/etos-api/pkg/application" @@ -32,7 +33,6 @@ import ( "github.com/sirupsen/logrus" "github.com/snowzach/rotatefilehook" "go.elastic.co/ecslogrus" - clientv3 "go.etcd.io/etcd/client/v3" ) // main sets up logging and starts up the webserver. @@ -63,16 +63,18 @@ func main() { }) // Database connection test - cli, err := clientv3.New(clientv3.Config{ - Endpoints: []string{cfg.DatabaseURI()}, - DialTimeout: 5 * time.Second, - }) - if err != nil { - log.WithError(err).Fatal("failed to create etcd connection") - } - + // cli, err := clientv3.New(clientv3.Config{ + // Endpoints: []string{cfg.DatabaseURI()}, + // DialTimeout: 5 * time.Second, + // }) + // if err != nil { + // log.WithError(err).Fatal("failed to create etcd connection") + // } + + iutEtcdTreePrefix := "/iut" + db := etcd.New(cfg, logger, iutEtcdTreePrefix) log.Info("Loading v1alpha1 routes") - v1alpha1App := v1alpha1.New(cfg, log, ctx, cli) + v1alpha1App := v1alpha1.New(cfg, log, ctx, db) defer v1alpha1App.Close() router := application.New(v1alpha1App) diff --git a/pkg/iut/v1alpha1/v1alpha1.go b/pkg/iut/v1alpha1/v1alpha1.go index 53f356c..cabcbc8 100644 --- a/pkg/iut/v1alpha1/v1alpha1.go +++ b/pkg/iut/v1alpha1/v1alpha1.go @@ -26,9 +26,9 @@ import ( eiffelevents "github.com/eiffel-community/eiffelevents-sdk-go" config "github.com/eiffel-community/etos-api/internal/configs/iut" + "github.com/eiffel-community/etos-api/internal/database" "github.com/eiffel-community/etos-api/pkg/application" packageurl "github.com/package-url/packageurl-go" - clientv3 "go.etcd.io/etcd/client/v3" "github.com/google/uuid" "github.com/julienschmidt/httprouter" @@ -38,14 +38,14 @@ import ( type V1Alpha1Application struct { logger *logrus.Entry cfg config.Config - database *clientv3.Client + database database.Opener wg *sync.WaitGroup } type V1Alpha1Handler struct { logger *logrus.Entry cfg config.Config - database *clientv3.Client + database database.Opener wg *sync.WaitGroup } @@ -72,11 +72,11 @@ func (a *V1Alpha1Application) Close() { } // New returns a new V1Alpha1Application object/struct -func New(cfg config.Config, log *logrus.Entry, ctx context.Context, cli *clientv3.Client) application.Application { +func New(cfg config.Config, log *logrus.Entry, ctx context.Context, db database.Opener) application.Application { return &V1Alpha1Application{ logger: log, cfg: cfg, - database: cli, + database: db, wg: &sync.WaitGroup{}, } } @@ -153,6 +153,8 @@ func (h V1Alpha1Handler) Start(w http.ResponseWriter, r *http.Request, ps httpro RespondWithError(w, http.StatusInternalServerError, err.Error()) return } + client := h.database.Open(r.Context(), checkOutID) + client.Write() _, err = h.database.Put(r.Context(), fmt.Sprintf("/iut/%s", checkOutID.String()), string(iuts)) if err != nil { RespondWithError(w, http.StatusInternalServerError, err.Error())