From 0e8151db1be24420fd8f818b50d8a7b3cede1936 Mon Sep 17 00:00:00 2001 From: Rahmat Hidayat Date: Mon, 9 Oct 2023 13:55:19 +0700 Subject: [PATCH] chore(dlq): access app config for mapping --- cli/server/configs.go | 2 +- cli/server/server.go | 7 +++++++ internal/server/server.go | 3 ++- internal/server/v1/dlq/handler.go | 8 +------- internal/server/v1/dlq/mapper.go | 5 +++-- internal/server/v1/dlq/routes.go | 8 ++++++-- internal/server/v1/dlq/service.go | 20 +++++++++++++++++++- 7 files changed, 39 insertions(+), 14 deletions(-) diff --git a/cli/server/configs.go b/cli/server/configs.go index 50a78c8..d8bdc3a 100644 --- a/cli/server/configs.go +++ b/cli/server/configs.go @@ -51,7 +51,7 @@ type optimusConfig struct { } type dlqConfig struct { - Addr string `mapstructure:"addr"` + DlqJobImage string `mapstructure:"dlq_job_image"` } type serveConfig struct { diff --git a/cli/server/server.go b/cli/server/server.go index 4cae414..8b77516 100644 --- a/cli/server/server.go +++ b/cli/server/server.go @@ -9,6 +9,7 @@ import ( shieldv1beta1 "buf.build/gen/go/gotocompany/proton/grpc/go/gotocompany/shield/v1beta1/shieldv1beta1grpc" sirenv1beta1 "buf.build/gen/go/gotocompany/proton/grpc/go/gotocompany/siren/v1beta1/sirenv1beta1grpc" "github.com/MakeNowJust/heredoc" + dlqv1 "github.com/goto/dex/internal/server/v1/dlq" "github.com/newrelic/go-agent/v3/newrelic" "github.com/spf13/cobra" "go.uber.org/zap" @@ -104,6 +105,11 @@ func runServer(baseCtx context.Context, nrApp *newrelic.Application, zapLog *zap if err != nil { return err } + + dlqConfig := &dlqv1.DlqJobConfig{ + // TODO: map cfg.Dlq + } + return server.Serve(ctx, cfg.Service.Addr(), nrApp, zapLog, shieldv1beta1.NewShieldServiceClient(shieldConn), entropyv1beta1.NewResourceServiceClient(entropyConn), @@ -113,5 +119,6 @@ func runServer(baseCtx context.Context, nrApp *newrelic.Application, zapLog *zap &gcs.Client{StorageClient: gcsClient}, cfg.Odin.Addr, cfg.StencilAddr, + dlqConfig, ) } diff --git a/internal/server/server.go b/internal/server/server.go index e6bab0b..c0e98b6 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -38,6 +38,7 @@ func Serve(ctx context.Context, addr string, gcsClient gcs.BlobStorageClient, odinAddr string, stencilAddr string, + dlqConfig *dlqv1.DlqJobConfig, ) error { alertSvc := &alertsv1.Service{Siren: sirenClient} @@ -62,7 +63,7 @@ func Serve(ctx context.Context, addr string, r.Route("/subscriptions", alertsv1.SubscriptionRoutes(sirenClient, shieldClient)) r.Route("/optimus", optimusv1.Routes(optimusClient)) r.Route("/projects", projectsv1.Routes(shieldClient)) - r.Route("/dlq", dlqv1.Routes(entropyClient, gcsClient)) + r.Route("/dlq", dlqv1.Routes(entropyClient, gcsClient, dlqConfig)) r.Route("/firehoses", firehosev1.Routes(entropyClient, shieldClient, alertSvc, compassClient, odinAddr, stencilAddr)) r.Route("/kubernetes", kubernetesv1.Routes(entropyClient)) }) diff --git a/internal/server/v1/dlq/handler.go b/internal/server/v1/dlq/handler.go index 4ba2122..bee8ace 100644 --- a/internal/server/v1/dlq/handler.go +++ b/internal/server/v1/dlq/handler.go @@ -16,9 +16,6 @@ import ( type Handler struct { service *Service } -type DlqConfig struct { - ContainerImage string `json:"container_image,omitempty"` -} func NewHandler(service *Service) *Handler { return &Handler{service: service} @@ -66,10 +63,7 @@ func (*Handler) listDlqJobs(w http.ResponseWriter, _ *http.Request) { func (*Handler) createDlqJob(w http.ResponseWriter, _ *http.Request) { // transform request body into DlqJob (validation?) - // fetch firehose details - // enrich DlqJob with firehose details - // map DlqJob to entropy resource -> return entropy.Resource (kind = job) - // entropy create resource + // call service.CreateDLQJob utils.WriteJSON(w, http.StatusOK, map[string]interface{}{ "dlq_job": nil, diff --git a/internal/server/v1/dlq/mapper.go b/internal/server/v1/dlq/mapper.go index 44cfd50..8733396 100644 --- a/internal/server/v1/dlq/mapper.go +++ b/internal/server/v1/dlq/mapper.go @@ -86,7 +86,7 @@ type DlqJob struct { PrometheusHost string `json:"prometheus_host,omitempty"` } -func enrichDlqJob(job *DlqJob, f models.Firehose, ac DlqConfig) error { +func enrichDlqJob(job *DlqJob, f models.Firehose, cfg *DlqJobConfig) error { var env_vars []string for key := range f.Configs.EnvVars { env_vars = append(env_vars, key) @@ -96,7 +96,8 @@ func enrichDlqJob(job *DlqJob, f models.Firehose, ac DlqConfig) error { FirehoseDeployment: f.Configs.DeploymentID, KubeCluster: *f.Configs.KubeCluster, EnvVars: env_vars, - // ContainerImage: ac.ContainerImage, + ContainerImage: cfg.DlqJobImage, + PrometheusHost: cfg.PrometheusHost, } return nil } diff --git a/internal/server/v1/dlq/routes.go b/internal/server/v1/dlq/routes.go index a75f919..758a1df 100644 --- a/internal/server/v1/dlq/routes.go +++ b/internal/server/v1/dlq/routes.go @@ -7,8 +7,12 @@ import ( "github.com/goto/dex/internal/server/gcs" ) -func Routes(entropyClient entropyv1beta1rpc.ResourceServiceClient, gcsClient gcs.BlobStorageClient) func(r chi.Router) { - service := NewService(entropyClient, gcsClient) +func Routes( + entropyClient entropyv1beta1rpc.ResourceServiceClient, + gcsClient gcs.BlobStorageClient, + cfg *DlqJobConfig, +) func(r chi.Router) { + service := NewService(entropyClient, gcsClient, cfg) handler := NewHandler(service) return func(r chi.Router) { diff --git a/internal/server/v1/dlq/service.go b/internal/server/v1/dlq/service.go index 583abc8..44d7ff3 100644 --- a/internal/server/v1/dlq/service.go +++ b/internal/server/v1/dlq/service.go @@ -6,14 +6,32 @@ import ( "github.com/goto/dex/internal/server/gcs" ) +type DlqJobConfig struct { + DlqJobImage string `mapstructure:"dlq_job_image"` + PrometheusHost string `mapstructure:"prometheus_host"` +} + type Service struct { client entropyv1beta1rpc.ResourceServiceClient gcsClient gcs.BlobStorageClient + cfg *DlqJobConfig } -func NewService(client entropyv1beta1rpc.ResourceServiceClient, gcsClient gcs.BlobStorageClient) *Service { +func NewService(client entropyv1beta1rpc.ResourceServiceClient, gcsClient gcs.BlobStorageClient, cfg *DlqJobConfig) *Service { return &Service{ client: client, gcsClient: gcsClient, + cfg: cfg, } } + +// TODO: replace *DlqJob with a generated models.DlqJob +func (s *Service) CreateDLQJob(dlqJob *DlqJob) error { + // validate dlqJob for creation + + // fetch firehose details + // enrich DlqJob with firehose details + // map DlqJob to entropy resource -> return entropy.Resource (kind = job) + // entropy create resource + return nil +}