Address feedback

Small typo-like fixes and proper installing of a lookup function in all
the databases.
This commit is contained in:
Dmitrii Dolgov 2020-03-17 11:12:29 +01:00
parent 4d61adf6b7
commit cf6541b8cf
2 changed files with 27 additions and 9 deletions

View File

@ -1262,7 +1262,7 @@ func syncResources(a, b *v1.ResourceRequirements) bool {
return false 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 // defaults, that are different from what we see in the DeploymentSpec
func (c *Cluster) needSyncConnPoolDefaults( func (c *Cluster) needSyncConnPoolDefaults(
spec *acidv1.ConnectionPool, spec *acidv1.ConnectionPool,
@ -1283,7 +1283,7 @@ func (c *Cluster) needSyncConnPoolDefaults(
*deployment.Spec.Replicas != *config.NumberOfInstances { *deployment.Spec.Replicas != *config.NumberOfInstances {
sync = true 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) *deployment.Spec.Replicas, *config.NumberOfInstances)
reasons = append(reasons, msg) reasons = append(reasons, msg)
} }
@ -1292,7 +1292,7 @@ func (c *Cluster) needSyncConnPoolDefaults(
poolContainer.Image != config.Image { poolContainer.Image != config.Image {
sync = true 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) poolContainer.Image, config.Image)
reasons = append(reasons, msg) reasons = append(reasons, msg)
} }
@ -1306,7 +1306,7 @@ func (c *Cluster) needSyncConnPoolDefaults(
// updates for new resource values). // updates for new resource values).
if err == nil && syncResources(&poolContainer.Resources, expectedResources) { if err == nil && syncResources(&poolContainer.Resources, expectedResources) {
sync = true sync = true
msg := fmt.Sprintf("Resources are different (%+v vs %+v)", msg := fmt.Sprintf("Resources are different (having %+v, required %+v)",
poolContainer.Resources, expectedResources) poolContainer.Resources, expectedResources)
reasons = append(reasons, msg) reasons = append(reasons, msg)
} }
@ -1321,7 +1321,7 @@ func (c *Cluster) needSyncConnPoolDefaults(
if ref.Name != c.credentialSecretName(config.User) { if ref.Name != c.credentialSecretName(config.User) {
sync = true 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) ref.Name, config.User)
reasons = append(reasons, msg) reasons = append(reasons, msg)
} }
@ -1329,7 +1329,7 @@ func (c *Cluster) needSyncConnPoolDefaults(
if env.Name == "PGSCHEMA" && env.Value != config.Schema { if env.Name == "PGSCHEMA" && env.Value != config.Schema {
sync = true 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) env.Value, config.Schema)
reasons = append(reasons, msg) reasons = append(reasons, msg)
} }

View File

@ -51,11 +51,16 @@ const (
` `
) )
func (c *Cluster) pgConnectionString() string { func (c *Cluster) pgConnectionString(dbname string) string {
password := c.systemUsers[constants.SuperuserKeyName].Password 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), fmt.Sprintf("%s.%s.svc.%s", c.Name, c.Namespace, c.OpConfig.ClusterDomain),
dbname,
c.systemUsers[constants.SuperuserKeyName].Name, c.systemUsers[constants.SuperuserKeyName].Name,
strings.Replace(password, "$", "\\$", -1), strings.Replace(password, "$", "\\$", -1),
constants.PostgresConnectTimeout/time.Second) constants.PostgresConnectTimeout/time.Second)
@ -70,13 +75,17 @@ func (c *Cluster) databaseAccessDisabled() bool {
} }
func (c *Cluster) initDbConn() error { func (c *Cluster) initDbConn() error {
return c.initDbConnWithName("")
}
func (c *Cluster) initDbConnWithName(dbname string) error {
c.setProcessName("initializing db connection") c.setProcessName("initializing db connection")
if c.pgDb != nil { if c.pgDb != nil {
return nil return nil
} }
var conn *sql.DB var conn *sql.DB
connstring := c.pgConnectionString() connstring := c.pgConnectionString(dbname)
finalerr := retryutil.Retry(constants.PostgresConnectTimeout, constants.PostgresConnectRetryTimeout, finalerr := retryutil.Retry(constants.PostgresConnectTimeout, constants.PostgresConnectRetryTimeout,
func() (bool, error) { func() (bool, error) {
@ -275,6 +284,8 @@ func (c *Cluster) installLookupFunction(poolSchema, poolUser string) error {
return fmt.Errorf("could not init database connection") return fmt.Errorf("could not init database connection")
} }
defer func() { 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 { if err := c.closeDbConn(); err != nil {
c.logger.Errorf("could not close database connection: %v", err) 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)) templater := template.Must(template.New("sql").Parse(connectionPoolLookup))
for dbname, _ := range currentDatabases { 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) c.logger.Infof("Install pool lookup function into %s", dbname)
params := TemplateParams{ params := TemplateParams{
@ -325,6 +340,9 @@ func (c *Cluster) installLookupFunction(poolSchema, poolUser string) error {
} }
c.logger.Infof("Pool lookup function installed into %s", dbname) 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 c.ConnectionPool.LookupFunction = true