diff --git a/pkg/cluster/cluster.go b/pkg/cluster/cluster.go index 8c902afb5..94ca8e864 100644 --- a/pkg/cluster/cluster.go +++ b/pkg/cluster/cluster.go @@ -1262,7 +1262,7 @@ func syncResources(a, b *v1.ResourceRequirements) bool { return false } -// Check if we need to synchronize connection pool deploymend due to new +// Check if we need to synchronize connection pool deployment due to new // defaults, that are different from what we see in the DeploymentSpec func (c *Cluster) needSyncConnPoolDefaults( spec *acidv1.ConnectionPool, @@ -1283,7 +1283,7 @@ func (c *Cluster) needSyncConnPoolDefaults( *deployment.Spec.Replicas != *config.NumberOfInstances { sync = true - msg := fmt.Sprintf("NumberOfInstances is different (%d vs %d)", + msg := fmt.Sprintf("NumberOfInstances is different (having %d, required %d)", *deployment.Spec.Replicas, *config.NumberOfInstances) reasons = append(reasons, msg) } @@ -1292,7 +1292,7 @@ func (c *Cluster) needSyncConnPoolDefaults( poolContainer.Image != config.Image { sync = true - msg := fmt.Sprintf("DockerImage is different (%s vs %s)", + msg := fmt.Sprintf("DockerImage is different (having %s, required %s)", poolContainer.Image, config.Image) reasons = append(reasons, msg) } @@ -1306,7 +1306,7 @@ func (c *Cluster) needSyncConnPoolDefaults( // updates for new resource values). if err == nil && syncResources(&poolContainer.Resources, expectedResources) { sync = true - msg := fmt.Sprintf("Resources are different (%+v vs %+v)", + msg := fmt.Sprintf("Resources are different (having %+v, required %+v)", poolContainer.Resources, expectedResources) reasons = append(reasons, msg) } @@ -1321,7 +1321,7 @@ func (c *Cluster) needSyncConnPoolDefaults( if ref.Name != c.credentialSecretName(config.User) { sync = true - msg := fmt.Sprintf("Pool user is different (%s vs %s)", + msg := fmt.Sprintf("Pool user is different (having %s, required %s)", ref.Name, config.User) reasons = append(reasons, msg) } @@ -1329,7 +1329,7 @@ func (c *Cluster) needSyncConnPoolDefaults( if env.Name == "PGSCHEMA" && env.Value != config.Schema { sync = true - msg := fmt.Sprintf("Pool schema is different (%s vs %s)", + msg := fmt.Sprintf("Pool schema is different (having %s, required %s)", env.Value, config.Schema) reasons = append(reasons, msg) } diff --git a/pkg/cluster/database.go b/pkg/cluster/database.go index 064094680..4ad6374a8 100644 --- a/pkg/cluster/database.go +++ b/pkg/cluster/database.go @@ -51,11 +51,16 @@ const ( ` ) -func (c *Cluster) pgConnectionString() string { +func (c *Cluster) pgConnectionString(dbname string) string { password := c.systemUsers[constants.SuperuserKeyName].Password - return fmt.Sprintf("host='%s' dbname=postgres sslmode=require user='%s' password='%s' connect_timeout='%d'", + if dbname == "" { + dbname = "postgres" + } + + return fmt.Sprintf("host='%s' dbname='%s' sslmode=require user='%s' password='%s' connect_timeout='%d'", fmt.Sprintf("%s.%s.svc.%s", c.Name, c.Namespace, c.OpConfig.ClusterDomain), + dbname, c.systemUsers[constants.SuperuserKeyName].Name, strings.Replace(password, "$", "\\$", -1), constants.PostgresConnectTimeout/time.Second) @@ -70,13 +75,17 @@ func (c *Cluster) databaseAccessDisabled() bool { } func (c *Cluster) initDbConn() error { + return c.initDbConnWithName("") +} + +func (c *Cluster) initDbConnWithName(dbname string) error { c.setProcessName("initializing db connection") if c.pgDb != nil { return nil } var conn *sql.DB - connstring := c.pgConnectionString() + connstring := c.pgConnectionString(dbname) finalerr := retryutil.Retry(constants.PostgresConnectTimeout, constants.PostgresConnectRetryTimeout, func() (bool, error) { @@ -275,6 +284,8 @@ func (c *Cluster) installLookupFunction(poolSchema, poolUser string) error { return fmt.Errorf("could not init database connection") } defer func() { + // in case if everything went fine this can generate a warning about + // trying to close an empty connection. if err := c.closeDbConn(); err != nil { c.logger.Errorf("could not close database connection: %v", err) } @@ -289,6 +300,10 @@ func (c *Cluster) installLookupFunction(poolSchema, poolUser string) error { templater := template.Must(template.New("sql").Parse(connectionPoolLookup)) for dbname, _ := range currentDatabases { + if err := c.initDbConnWithName(dbname); err != nil { + return fmt.Errorf("could not init database connection") + } + c.logger.Infof("Install pool lookup function into %s", dbname) params := TemplateParams{ @@ -325,6 +340,9 @@ func (c *Cluster) installLookupFunction(poolSchema, poolUser string) error { } c.logger.Infof("Pool lookup function installed into %s", dbname) + if err := c.closeDbConn(); err != nil { + c.logger.Errorf("could not close database connection: %v", err) + } } c.ConnectionPool.LookupFunction = true