From 793f4edf0bcfec39e9829630ba558f9d13a614c1 Mon Sep 17 00:00:00 2001 From: bullshit Date: Mon, 18 Mar 2024 20:19:45 +0100 Subject: [PATCH] feat: generate postgres user secret with r2dbc uris --- internal/controller/postgrescluster/postgres.go | 14 ++++++++++++++ .../controller/postgrescluster/postgres_test.go | 12 +++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/internal/controller/postgrescluster/postgres.go b/internal/controller/postgrescluster/postgres.go index 2caba509d1..a501488146 100644 --- a/internal/controller/postgrescluster/postgres.go +++ b/internal/controller/postgrescluster/postgres.go @@ -128,6 +128,14 @@ func (r *Reconciler) generatePostgresUserSecret( Path: database, RawQuery: query.Encode(), }).String()) + // The R2DBC driver requires a different URI scheme than the JDBC driver + // - https://r2dbc.io/spec/1.0.0.RELEASE/spec/html/#overview.connection.url + intent.Data["r2dbc-uri"] = []byte((&url.URL{ + Scheme: "r2dbc:postgresql", + Host: net.JoinHostPort(hostname, port), + Path: database, + RawQuery: query.Encode(), + }).String()) } // When PgBouncer is enabled, include values for connecting through it. @@ -164,6 +172,12 @@ func (r *Reconciler) generatePostgresUserSecret( Path: database, RawQuery: query.Encode(), }).String()) + intent.Data["pgbouncer-r2dbc-uri"] = []byte((&url.URL{ + Scheme: "r2dbc:postgresql", + Host: net.JoinHostPort(hostname, port), + Path: database, + RawQuery: query.Encode(), + }).String()) } } diff --git a/internal/controller/postgrescluster/postgres_test.go b/internal/controller/postgrescluster/postgres_test.go index 296c64be25..04bcfe850c 100644 --- a/internal/controller/postgrescluster/postgres_test.go +++ b/internal/controller/postgrescluster/postgres_test.go @@ -183,6 +183,10 @@ func TestGeneratePostgresUserSecret(t *testing.T) { `^jdbc:postgresql://hippo2-primary.ns1.svc:9999/db1`+ `[?]password=[^&]+&user=some-user-name$`, string(secret.Data["jdbc-uri"]))) + assert.Assert(t, cmp.Regexp( + `^r2dbc:postgresql://hippo2-primary.ns1.svc:9999/db1`+ + `[?]password=[^&]+&user=some-user-name$`, + string(secret.Data["r2dbc-uri"]))) } // Only the first in the list. @@ -199,7 +203,9 @@ func TestGeneratePostgresUserSecret(t *testing.T) { assert.Assert(t, cmp.Regexp( `^jdbc:postgresql://hippo2-primary.ns1.svc:9999/first[?].+$`, string(secret.Data["jdbc-uri"]))) - + assert.Assert(t, cmp.Regexp( + `^r2dbc:postgresql://hippo2-primary.ns1.svc:9999/first[?].+$`, + string(secret.Data["r2dbc-uri"]))) } }) @@ -233,6 +239,10 @@ func TestGeneratePostgresUserSecret(t *testing.T) { `^jdbc:postgresql://hippo2-pgbouncer.ns1.svc:10220/yes`+ `[?]password=[^&]+&prepareThreshold=0&user=some-user-name$`, string(secret.Data["pgbouncer-jdbc-uri"]))) + assert.Assert(t, cmp.Regexp( + `^r2dbc:postgresql://hippo2-pgbouncer.ns1.svc:10220/yes`+ + `[?]password=[^&]+&prepareThreshold=0&user=some-user-name$`, + string(secret.Data["pgbouncer-r2dbc-uri"]))) } }) }