From 54481d76afa5e1592ece9724c4560616c05e2f8d Mon Sep 17 00:00:00 2001 From: Felix Kunde Date: Wed, 25 Aug 2021 17:22:14 +0200 Subject: [PATCH] remove role from installLookupFunction and run it on database sync, too --- pkg/cluster/connection_pooler.go | 52 +++++++++++++++------------ pkg/cluster/connection_pooler_test.go | 2 +- pkg/cluster/database.go | 6 ++-- pkg/cluster/sync.go | 7 ++++ pkg/cluster/types.go | 2 +- 5 files changed, 42 insertions(+), 27 deletions(-) diff --git a/pkg/cluster/connection_pooler.go b/pkg/cluster/connection_pooler.go index 00a634267..d8780d61f 100644 --- a/pkg/cluster/connection_pooler.go +++ b/pkg/cluster/connection_pooler.go @@ -684,7 +684,7 @@ func logPoolerEssentials(log *logrus.Entry, oldSpec, newSpec *acidv1.Postgresql) } } - log.Debugf("syncing connection pooler from (%v, %v) to (%v, %v)", v[0], v[1], v[2], v[3]) + log.Debugf("syncing connection pooler (master, replica) from (%v, %v) to (%v, %v)", v[0], v[1], v[2], v[3]) } func (c *Cluster) syncConnectionPooler(oldSpec, newSpec *acidv1.Postgresql, LookupFunction InstallFunction) (SyncReason, error) { @@ -760,27 +760,9 @@ func (c *Cluster) syncConnectionPooler(oldSpec, newSpec *acidv1.Postgresql, Look // in between // in this case also do not forget to install lookup function - // new databases could have been created that need the pooler schema - if !c.ConnectionPooler[role].LookupFunction { - newConnectionPooler := newSpec.Spec.ConnectionPooler - - specSchema := "" - specUser := "" - - if newConnectionPooler != nil { - specSchema = newConnectionPooler.Schema - specUser = newConnectionPooler.User - } - - schema := util.Coalesce( - specSchema, - c.OpConfig.ConnectionPooler.Schema) - - user := util.Coalesce( - specUser, - c.OpConfig.ConnectionPooler.User) - - if err = LookupFunction(schema, user, role); err != nil { + if c.ConnectionPooler[role].LookupFunction { + if err = c.syncConnectionPoolerSchema(LookupFunction); err != nil { + c.ConnectionPooler[role].LookupFunction = true return NoSync, err } } @@ -935,3 +917,29 @@ func (c *Cluster) syncConnectionPoolerWorker(oldSpec, newSpec *acidv1.Postgresql return NoSync, nil } + +func (c *Cluster) syncConnectionPoolerSchema(LookupFunction InstallFunction) error { + + connectionPooler := c.Spec.ConnectionPooler + specSchema := "" + specUser := "" + + if connectionPooler != nil { + specSchema = connectionPooler.Schema + specUser = connectionPooler.User + } + + schema := util.Coalesce( + specSchema, + c.OpConfig.ConnectionPooler.Schema) + + user := util.Coalesce( + specUser, + c.OpConfig.ConnectionPooler.User) + + if err := LookupFunction(schema, user); err != nil { + return err + } + + return nil +} diff --git a/pkg/cluster/connection_pooler_test.go b/pkg/cluster/connection_pooler_test.go index 280adb101..9b983c7b0 100644 --- a/pkg/cluster/connection_pooler_test.go +++ b/pkg/cluster/connection_pooler_test.go @@ -19,7 +19,7 @@ import ( "k8s.io/client-go/kubernetes/fake" ) -func mockInstallLookupFunction(schema string, user string, role PostgresRole) error { +func mockInstallLookupFunction(schema string, user string) error { return nil } diff --git a/pkg/cluster/database.go b/pkg/cluster/database.go index ba4cf223a..aa3a5e3be 100644 --- a/pkg/cluster/database.go +++ b/pkg/cluster/database.go @@ -508,7 +508,7 @@ func (c *Cluster) execCreateOrAlterExtension(extName, schemaName, statement, doi // Creates a connection pool credentials lookup function in every database to // perform remote authentication. -func (c *Cluster) installLookupFunction(poolerSchema, poolerUser string, role PostgresRole) error { +func (c *Cluster) installLookupFunction(poolerSchema, poolerUser string) error { var stmtBytes bytes.Buffer c.logger.Info("Installing lookup function") @@ -604,8 +604,8 @@ func (c *Cluster) installLookupFunction(poolerSchema, poolerUser string, role Po c.logger.Infof("pooler lookup function installed into %s", dbname) } - if len(failedDatabases) == 0 { - c.ConnectionPooler[role].LookupFunction = true + if len(failedDatabases) > 0 { + return fmt.Errorf("could not install pooler lookup function in every specified databases") } return nil diff --git a/pkg/cluster/sync.go b/pkg/cluster/sync.go index 4937a2034..e1136db5c 100644 --- a/pkg/cluster/sync.go +++ b/pkg/cluster/sync.go @@ -758,6 +758,13 @@ func (c *Cluster) syncDatabases() error { } } + if len(createDatabases) > 0 { + // create the pooler objects in new database if needed + if needConnectionPooler(&c.Spec) { + c.syncConnectionPoolerSchema(c.installLookupFunction) + } + } + // set default privileges for prepared database for _, preparedDatabase := range preparedDatabases { if err := c.initDbConnWithName(preparedDatabase); err != nil { diff --git a/pkg/cluster/types.go b/pkg/cluster/types.go index 8aa519817..199914ccc 100644 --- a/pkg/cluster/types.go +++ b/pkg/cluster/types.go @@ -72,7 +72,7 @@ type ClusterStatus struct { type TemplateParams map[string]interface{} -type InstallFunction func(schema string, user string, role PostgresRole) error +type InstallFunction func(schema string, user string) error type SyncReason []string