Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds PostgreSQL Database Table Schema to Configuration #677

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
| DB_PASSWORD | Postgres Database Password | hunter2 |
| DB_HOST | Postgres Database host | /cloudsql/my-project:us-east1:tekton-results |
| DB_NAME | Postgres Database name | tekton_results |
| DB_SCHEMA | Postgres Table schema | public |
| DB_SSLMODE | Database SSL mode | verify-full |
| DB_SSLROOTCERT | Path to CA cert used to validate Database cert | /etc/tls/db/ca.crt |
| DB_ENABLE_AUTO_MIGRATION | Auto-migrate the database on startup (create/update schemas). For further details, refer to <https://gorm.io/docs/migration.html> | true (default) |
Expand Down
29 changes: 16 additions & 13 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,6 @@ import (
"strings"
"time"

"github.com/tektoncd/results/pkg/api/server/v1alpha2/auth/impersonation"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/status"

"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"

"github.com/golang-jwt/jwt/v4"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
grpc_auth "github.com/grpc-ecosystem/go-grpc-middleware/auth"
Expand All @@ -52,17 +39,28 @@ import (
"github.com/tektoncd/results/pkg/api/server/logger"
v1alpha2 "github.com/tektoncd/results/pkg/api/server/v1alpha2"
"github.com/tektoncd/results/pkg/api/server/v1alpha2/auth"
"github.com/tektoncd/results/pkg/api/server/v1alpha2/auth/impersonation"
v1alpha2pb "github.com/tektoncd/results/proto/v1alpha2/results_go_proto"
_ "go.uber.org/automaxprocs"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/health"
healthpb "google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/reflection"
"google.golang.org/grpc/status"
"gorm.io/driver/postgres"
"gorm.io/gorm"
gormlogger "gorm.io/gorm/logger"
"gorm.io/gorm/schema"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)

func main() {
Expand Down Expand Up @@ -100,7 +98,12 @@ func main() {
var err error

dbURI := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=%s sslrootcert=%s", serverConfig.DB_HOST, serverConfig.DB_USER, serverConfig.DB_PASSWORD, serverConfig.DB_NAME, serverConfig.DB_PORT, serverConfig.DB_SSLMODE, serverConfig.DB_SSLROOTCERT)

gormConfig := &gorm.Config{}
if serverConfig.DB_SCHEMA != "" {
gormConfig.NamingStrategy = schema.NamingStrategy{TablePrefix: serverConfig.DB_SCHEMA}
}

if log.Level() != zap.DebugLevel {
gormConfig.Logger = gormlogger.Default.LogMode(gormlogger.Silent)
}
Expand Down
1 change: 1 addition & 0 deletions config/base/env/config
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ DB_PASSWORD=
DB_HOST=
DB_PORT=5432
DB_NAME=
DB_SCHEMA=public
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This becomes the default "table prefix" value in our distribution. Will this break the database initialization/auto-migration phase of the deployment on upgrade? Is there risk of data being "orphaned" because the apiserver will create a whole new schema in Postgres?

DB_SSLMODE=disable
DB_SSLROOTCERT=
DB_ENABLE_AUTO_MIGRATION=true
Expand Down
5 changes: 4 additions & 1 deletion docs/external-database.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ patches:
- path: delete-database-configmap.yaml
```

## Modifying DB_HOST and DB_NAME
## Modifying DB_HOST, DB_NAME, DB_SCHEMA

You may add patches to modify them. Here, we will utilize env/config. If you
want to securely store these variables, consider adding patches to fetch these
Expand All @@ -68,6 +68,7 @@ Copy the [config](../config/base/env/config) and change these values.
```cfg
DB_HOST=
DB_NAME=
DB_SCHEMA=
```

Here is the required patch if you want to use Kubernetes Secret for passing
Expand All @@ -89,6 +90,8 @@ spec:
value: <put-your-external-db-host-here>
- name: DB_NAME
value: <your-db-name default:tekton-results>
- name: DB_SCHEMA
value: <your-db-schema default:public>
```

## Create Secret for storing database username and password
Expand Down
1 change: 1 addition & 0 deletions pkg/api/server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Config struct {
DB_HOST string `mapstructure:"DB_HOST"`
DB_PORT string `mapstructure:"DB_PORT"`
DB_NAME string `mapstructure:"DB_NAME"`
DB_SCHEMA string `mapstructure:"DB_SCHEMA"`
DB_SSLMODE string `mapstructure:"DB_SSLMODE"`
DB_SSLROOTCERT string `mapstructure:"DB_SSLROOTCERT"`
DB_ENABLE_AUTO_MIGRATION bool `mapstructure:"DB_ENABLE_AUTO_MIGRATION"`
Expand Down
1 change: 1 addition & 0 deletions test/e2e/gcs-emulator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ data:
DB_HOST=
DB_PORT=5432
DB_NAME=
DB_SCHEMA=public
DB_SSLMODE=disable
DB_ENABLE_AUTO_MIGRATION=true
SERVER_PORT=8080
Expand Down