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

Add CloudSQL support for Postgres #89

Merged
merged 2 commits into from
May 11, 2020
Merged
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
2 changes: 1 addition & 1 deletion cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func main() {
}

if err := c.Initialize(); err != nil {
klog.Exitf("persist init with %s: %v", c, err)
klog.Exitf("persist initialize for %s: %v", c, err)
}

cfg := triage.Config{
Expand Down
6 changes: 4 additions & 2 deletions docs/deploy.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,7 @@ Examples flag settings:
* **MySQL**: `--persist-backend=mysql --persist-path="user:password@tcp(127.0.0.1:3306)/tp"`
* **PostgreSQL**: `--persist-backend=postgres --persist-path="dbname=tp"`
* **CockroachDB**: `--persist-backend=postgres postgresql://[email protected]:26257?sslmode=disable`
* **CloudSQL (MySQL)**: `--persist-backend=cloudsql --persist-path="user:password@tcp(project/us-central1/triage-party)/db"`
* May require configuring [GOOGLE_APPLICATION_CREDENTIALS](https://cloud.google.com/docs/authentication/getting-started)
* **CloudSQL - MySQL**: `--persist-backend=cloudsql --persist-path="user:password@tcp(project/us-central1/triage-party)/db"`
* **CloudSQL - Postgres**: `--persist-backend=cloudsql --persist-path="host=projectname:us-central1:dbname user=postgres password=pw"`

NOTE: Local development with CloudSQL backends may require setting up [GOOGLE_APPLICATION_CREDENTIALS](https://cloud.google.com/docs/authentication/getting-started)
45 changes: 36 additions & 9 deletions pkg/persist/cloudsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,63 @@
package persist

import (
"fmt"
"strings"

cloudsql "github.com/GoogleCloudPlatform/cloudsql-proxy/proxy/dialers/mysql"
cmysql "github.com/GoogleCloudPlatform/cloudsql-proxy/proxy/dialers/mysql"
_ "github.com/GoogleCloudPlatform/cloudsql-proxy/proxy/dialers/postgres"
"github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
"k8s.io/klog/v2"
)

// NewCloudSQL returns a new Google Cloud SQL store (MySQL)
func NewCloudSQL(cfg Config) (*MySQL, error) {
// DSN that works:
// $USER:$PASS@tcp($PROJECT/$REGION/$INSTANCE)/$DB"
func NewCloudSQL(cfg Config) (Cacher, error) {

// This heuristic may be totally wrong. My apologies.
if strings.Contains(cfg.Path, "(") {
return newCloudMySQL(cfg)
}

return newCloudPostgres(cfg)
}

func newCloudMySQL(cfg Config) (*MySQL, error) {
// Example DSN: $USER:$PASS@tcp($PROJECT/$REGION/$INSTANCE)/$DB"
dsn, err := mysql.ParseDSN(cfg.Path)
if err != nil {
return nil, err
return nil, fmt.Errorf("mysql parse dsn: %w", err)
}

mcfg := cloudsql.Cfg(dsn.Addr, dsn.User, dsn.Passwd)
mcfg := cmysql.Cfg(dsn.Addr, dsn.User, dsn.Passwd)
// Strip port
mcfg.Addr = strings.Split(dsn.Addr, ":")[0]
mcfg.Addr = strings.Replace(mcfg.Addr, "/", ":", -1)
mcfg.DBName = dsn.DBName
mcfg.ParseTime = true
klog.Infof("mcfg: %#v", mcfg)

db, err := cloudsql.DialCfg(mcfg)
db, err := cmysql.DialCfg(mcfg)
if err != nil {
return nil, err
return nil, fmt.Errorf("cloudmysql dialcfg: %w", err)
}

dbx := sqlx.NewDb(db, "mysql")
return &MySQL{db: dbx}, err
return &MySQL{db: dbx}, nil
}

func newCloudPostgres(cfg Config) (*Postgres, error) {
// required for CloudSQL, as the encryption is between the proxy and upstream instead
if !strings.Contains(cfg.Path, "sslmode=disable") {
cfg.Path = cfg.Path + " sslmode=disable"
}

// See https://github.com/GoogleCloudPlatform/cloudsql-proxy/blob/7e668d9ad0ba579372f5142f149a18c38d14a9d0/proxy/dialers/postgres/hook_test.go#L30
dbx, err := sqlx.Open("cloudsqlpostgres", cfg.Path)
if err != nil {
return nil, fmt.Errorf("cloudsqlpostgres open: %w", err)
}

klog.Infof("opened cloudsqlpostgres db at %s", cfg.Path)
return &Postgres{db: dbx}, nil
}
2 changes: 1 addition & 1 deletion pkg/persist/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type Postgres struct {
func NewPostgres(cfg Config) (*Postgres, error) {
dbx, err := sqlx.Connect("postgres", cfg.Path)
if err != nil {
return nil, err
return nil, fmt.Errorf("connect: %w", err)
}

m := &Postgres{
Expand Down