From e39c3b3d7933d6d44d9ed13b5e743c18386bb616 Mon Sep 17 00:00:00 2001 From: Sven Urbanski Date: Wed, 22 May 2024 17:27:50 +0200 Subject: [PATCH] chore: read esl table in export-service (#1619) --- .../manifest-repo-export-service.yaml | 13 ++- charts/kuberpult/tests/charts_test.go | 12 ++- docker-compose-earthly.yml | 10 +++ infrastructure/earthly/go/Earthfile | 6 +- .../manifest-repo-export-service/Earthfile | 2 +- .../pkg/cmd/server.go | 82 ++++++++++++++++++- 6 files changed, 117 insertions(+), 8 deletions(-) diff --git a/charts/kuberpult/templates/manifest-repo-export-service.yaml b/charts/kuberpult/templates/manifest-repo-export-service.yaml index 17bb6a324..5f92f4360 100644 --- a/charts/kuberpult/templates/manifest-repo-export-service.yaml +++ b/charts/kuberpult/templates/manifest-repo-export-service.yaml @@ -163,7 +163,18 @@ spec: - name: DD_API_KEY_LOCATION value: "/etc/datadog/api-key" {{- end }} - + - name: KUBERPULT_DB_LOCATION + value: "{{ .Values.cd.db.location }}" + - name: KUBERPULT_DB_NAME + value: "{{ .Values.cd.db.dbName }}" + - name: KUBERPULT_DB_USER_NAME + value: "{{ .Values.cd.db.dbUser }}" + - name: KUBERPULT_DB_USER_PASSWORD + value: "{{ .Values.cd.db.dbPassword }}" + - name: KUBERPULT_DB_OPTION # { NO_DB, cloudsql, sqlite } + value: {{ .Values.cd.db.dbOption }} + - name: KUBERPULT_DB_AUTH_PROXY_PORT + value: "{{ .Values.cd.db.authProxyPort }}" {{- if .Values.datadogTracing.enabled }} - name: DD_TRACE_DEBUG value: "{{ .Values.datadogTracing.debugging }}" diff --git a/charts/kuberpult/tests/charts_test.go b/charts/kuberpult/tests/charts_test.go index 6ba16bf6e..3f2fe3a41 100644 --- a/charts/kuberpult/tests/charts_test.go +++ b/charts/kuberpult/tests/charts_test.go @@ -34,6 +34,7 @@ func runHelm(t *testing.T, valuesData []byte, dirName string) string { testId := strconv.Itoa(rand.Intn(9999)) tempValuesFile := "vals" + "_" + testId + ".yaml" tempValuesFile = dirName + "/" + tempValuesFile + t.Logf("input file: \n%s\n", valuesData) err := os.WriteFile(tempValuesFile, valuesData, 0644) if err != nil { @@ -48,6 +49,13 @@ func runHelm(t *testing.T, valuesData []byte, dirName string) string { t.Fatalf("Error executing helm: Helm output: '%s'\nError: %v\n", string(execOutput), err) } + fileContent, err := os.ReadFile(outputFile) + if err != nil { + t.Fatalf("Error reading file '%s' content: \n%s\n", outputFile, string(fileContent)) + return "" + } + t.Logf("output file: \n%s\n", fileContent) + return outputFile } @@ -381,7 +389,7 @@ cd: }, }, { - Name: "Database cloudsql enabled", + Name: "Database cloudsql enabled 1", Values: ` git: url: "testURL" @@ -420,7 +428,7 @@ cd: ExpectedMissing: []core.EnvVar{}, }, { - Name: "Database cloudsql enabled", + Name: "Database cloudsql enabled 2", Values: ` git: url: "testURL" diff --git a/docker-compose-earthly.yml b/docker-compose-earthly.yml index 926086473..be9060d6e 100644 --- a/docker-compose-earthly.yml +++ b/docker-compose-earthly.yml @@ -30,10 +30,20 @@ services: image: europe-west3-docker.pkg.dev/fdc-public-docker-registry/kuberpult/kuberpult-manifest-repo-export-service:local environment: - LOG_LEVEL=INFO + - KUBERPULT_DB_LOCATION=/kp/database + - KUBERPULT_DB_MIGRATIONS_LOCATION=/kp/database/migrations/sqlite/ + - KUBERPULT_DB_OPTION=sqlite + - KUBERPULT_DB_NAME=mydb + - KUBERPULT_DB_USER_NAME=myname + - KUBERPULT_DB_USER_PASSWORD=mypassword + - KUBERPULT_DB_AUTH_PROXY_PORT=5432 volumes: - ./services/cd-service:/kp/kuberpult - ./database:/kp/database stop_grace_period: 0.5s + depends_on: + cd-service: + condition: service_started frontend-service: image: europe-west3-docker.pkg.dev/fdc-public-docker-registry/kuberpult/kuberpult-frontend-service:local # Note: this `container_name` needs to be the same as in `package.json` diff --git a/infrastructure/earthly/go/Earthfile b/infrastructure/earthly/go/Earthfile index 7b565d9b3..4fa6ea552 100644 --- a/infrastructure/earthly/go/Earthfile +++ b/infrastructure/earthly/go/Earthfile @@ -22,12 +22,12 @@ COMPILE: GOARCH=$USERARCH \ GOOS=linux \ go build -o bin/main . - + IF [ "$cgo_enabled" = "1" ] RUN bash -c "ldd $main_path/bin/main | tr -s [:blank:] '\n' | grep ^/ | xargs -I % install -D % $main_path/%" SAVE ARTIFACT $main_path/lib/ IF [ "$USERARCH" != "arm64" ] - SAVE ARTIFACT $main_path/usr/ + SAVE ARTIFACT --if-exists $main_path/usr/ END END @@ -72,7 +72,7 @@ DOCKER: IF [ "$cgo_enabled" = "1" ] COPY ../../../services/$service+compile/lib/* /lib IF [ "$USERARCH" != "arm64" ] - COPY ../../../services/$service+compile/usr/* /lib + COPY --if-exists ../../../services/$service+compile/usr/* /lib END END diff --git a/services/manifest-repo-export-service/Earthfile b/services/manifest-repo-export-service/Earthfile index c752969f6..7c7e8fdc3 100644 --- a/services/manifest-repo-export-service/Earthfile +++ b/services/manifest-repo-export-service/Earthfile @@ -4,7 +4,7 @@ IMPORT ../../infrastructure/earthly/go AS go-build LOCALLY ARG --global service=$(basename $PWD) ARG --global src_files=$(find pkg -type f ! -name "*_test.go") -ARG --global cgo_enabled=0 +ARG --global cgo_enabled=1 deps: FROM ../../+deps diff --git a/services/manifest-repo-export-service/pkg/cmd/server.go b/services/manifest-repo-export-service/pkg/cmd/server.go index ce27c6285..9d2d81b61 100755 --- a/services/manifest-repo-export-service/pkg/cmd/server.go +++ b/services/manifest-repo-export-service/pkg/cmd/server.go @@ -18,16 +18,96 @@ package cmd import ( "context" + "database/sql" "fmt" + "github.com/freiheit-com/kuberpult/pkg/db" "github.com/freiheit-com/kuberpult/pkg/logger" + "os" ) func RunServer() { err := logger.Wrap(context.Background(), func(ctx context.Context) error { logger.FromContext(ctx).Sugar().Warnf("hello world from the manifest-repo-export-service!") + + dbLocation, err := readEnvVar("KUBERPULT_DB_LOCATION") + if err != nil { + return err + } + dbName, err := readEnvVar("KUBERPULT_DB_NAME") + if err != nil { + return err + } + dbOption, err := readEnvVar("KUBERPULT_DB_OPTION") + if err != nil { + return err + } + dbUserName, err := readEnvVar("KUBERPULT_DB_USER_NAME") + if err != nil { + return err + } + dbPassword, err := readEnvVar("KUBERPULT_DB_USER_PASSWORD") + if err != nil { + return err + } + dbAuthProxyPort, err := readEnvVar("KUBERPULT_DB_AUTH_PROXY_PORT") + if err != nil { + return err + } + + var dbCfg db.DBConfig + if dbOption == "cloudsql" { + dbCfg = db.DBConfig{ + DbHost: dbLocation, + DbPort: dbAuthProxyPort, + DriverName: "postgres", + DbName: dbName, + DbPassword: dbPassword, + DbUser: dbUserName, + MigrationsPath: "", + WriteEslOnly: false, + } + } else if dbOption == "sqlite" { + dbCfg = db.DBConfig{ + DbHost: dbLocation, + DbPort: dbAuthProxyPort, + DriverName: "sqlite3", + DbName: dbName, + DbPassword: dbPassword, + DbUser: dbUserName, + MigrationsPath: "", + WriteEslOnly: false, + } + } else { + logger.FromContext(ctx).Fatal("Database was enabled but no valid DB option was provided.") + } + dbHandler, err := db.Connect(dbCfg) + if err != nil { + return err + } + + err = dbHandler.WithTransaction(ctx, func(ctx context.Context, transaction *sql.Tx) error { + esl, err := dbHandler.DBReadEslEventInternal(ctx, transaction) + if err != nil { + return err + } + logger.FromContext(ctx).Sugar().Warnf("esl event: %v", esl) + + return nil + }) + if err != nil { + return err + } return nil }) if err != nil { - fmt.Printf("error in logger.wrap: %v %#v", err, err) + fmt.Printf("error in startup: %v %#v", err, err) + } +} + +func readEnvVar(envName string) (string, error) { + envValue, ok := os.LookupEnv(envName) + if !ok { + return "", fmt.Errorf("could not read environment variable '%s'", envName) } + return envValue, nil }