Skip to content

Commit

Permalink
chore: read esl table in export-service (#1619)
Browse files Browse the repository at this point in the history
  • Loading branch information
sven-urbanski-freiheit-com authored May 22, 2024
1 parent 1082384 commit e39c3b3
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 8 deletions.
13 changes: 12 additions & 1 deletion charts/kuberpult/templates/manifest-repo-export-service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}"
Expand Down
12 changes: 10 additions & 2 deletions charts/kuberpult/tests/charts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}

Expand Down Expand Up @@ -381,7 +389,7 @@ cd:
},
},
{
Name: "Database cloudsql enabled",
Name: "Database cloudsql enabled 1",
Values: `
git:
url: "testURL"
Expand Down Expand Up @@ -420,7 +428,7 @@ cd:
ExpectedMissing: []core.EnvVar{},
},
{
Name: "Database cloudsql enabled",
Name: "Database cloudsql enabled 2",
Values: `
git:
url: "testURL"
Expand Down
10 changes: 10 additions & 0 deletions docker-compose-earthly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
6 changes: 3 additions & 3 deletions infrastructure/earthly/go/Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion services/manifest-repo-export-service/Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
82 changes: 81 additions & 1 deletion services/manifest-repo-export-service/pkg/cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit e39c3b3

Please sign in to comment.