Assorted changes

- Update deleteConnectionPooler to include role
- Rename EnableMasterConnectionPooler back to original name for backward
  compatiility
- other minor chnages and code improvements
This commit is contained in:
Rafia Sabih
2020-09-03 16:01:25 +02:00
parent 503082cf1a
commit 1814342dc3
17 changed files with 54 additions and 53 deletions
+1 -1
View File
@@ -259,7 +259,7 @@ var PostgresCRDResourceValidation = apiextv1beta1.CustomResourceValidation{
"dockerImage": {
Type: "string",
},
"enableMasterConnectionPooler": {
"enableConnectionPooler": {
Type: "boolean",
},
"enableReplicaConnectionPooler": {
+1 -1
View File
@@ -29,7 +29,7 @@ type PostgresSpec struct {
Patroni `json:"patroni,omitempty"`
Resources `json:"resources,omitempty"`
EnableMasterConnectionPooler *bool `json:"enableMasterConnectionPooler,omitempty"`
EnableConnectionPooler *bool `json:"enableConnectionPooler,omitempty"`
EnableReplicaConnectionPooler *bool `json:"enableReplicaConnectionPooler,omitempty"`
ConnectionPooler *ConnectionPooler `json:"connectionPooler,omitempty"`
+5 -2
View File
@@ -831,9 +831,12 @@ func (c *Cluster) Delete() {
// Delete connection pooler objects anyway, even if it's not mentioned in the
// manifest, just to not keep orphaned components in case if something went
// wrong
if err := c.deleteConnectionPooler(); err != nil {
c.logger.Warningf("could not remove connection pooler: %v", err)
for _, role := range [2]PostgresRole{Master, Replica} {
if err := c.deleteConnectionPooler(role); err != nil {
c.logger.Warningf("could not remove connection pooler: %v", err)
}
}
}
//NeedsRepair returns true if the cluster should be included in the repair scan (based on its in-memory status).
+1 -1
View File
@@ -719,7 +719,7 @@ func TestInitSystemUsers(t *testing.T) {
}
// cluster with connection pooler
cl.Spec.EnableMasterConnectionPooler = boolToPointer(true)
cl.Spec.EnableConnectionPooler = boolToPointer(true)
cl.initSystemUsers()
if _, exist := cl.systemUsers[constants.ConnectionPoolerUserKeyName]; !exist {
t.Errorf("%s, connection pooler user is not present", testName)
+5 -10
View File
@@ -2273,12 +2273,9 @@ func (c *Cluster) generateConnectionPoolerDeployment(spec *acidv1.PostgresSpec,
return nil, err
}
var name string
name = c.connectionPoolerName(role)
deployment := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Name: c.connectionPoolerName(role),
Namespace: c.Namespace,
Labels: c.connectionPoolerLabelsSelector(role).MatchLabels,
Annotations: map[string]string{},
@@ -2311,24 +2308,22 @@ func (c *Cluster) generateConnectionPoolerService(spec *acidv1.PostgresSpec, rol
if spec.ConnectionPooler == nil {
spec.ConnectionPooler = &acidv1.ConnectionPooler{}
}
name := c.connectionPoolerName(role)
serviceSpec := v1.ServiceSpec{
Ports: []v1.ServicePort{
{
Name: name,
Name: c.connectionPoolerName(role),
Port: pgPort,
TargetPort: intstr.IntOrString{StrVal: c.servicePort(role)},
},
},
Type: v1.ServiceTypeClusterIP,
Type: v1.ServiceTypeClusterIP,
Selector: map[string]string{"connection-pooler": c.connectionPoolerName(role)},
}
serviceSpec.Selector = map[string]string{"connection-pooler": name}
service := &v1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Name: c.connectionPoolerName(role),
Namespace: c.Namespace,
Labels: c.connectionPoolerLabelsSelector(role).MatchLabels,
Annotations: map[string]string{},
+4 -4
View File
@@ -126,7 +126,7 @@ func (c *Cluster) createConnectionPooler(lookup InstallFunction) (*ConnectionPoo
msg = "could not prepare database for connection pooler: %v"
return nil, fmt.Errorf(msg, err)
}
if c.Spec.EnableMasterConnectionPooler != nil || c.ConnectionPooler != nil {
if c.Spec.EnableConnectionPooler != nil || c.ConnectionPooler != nil {
deploymentSpec, err := c.generateConnectionPoolerDeployment(&c.Spec, Master)
if err != nil {
msg = "could not generate deployment for connection pooler: %v"
@@ -197,7 +197,7 @@ func (c *Cluster) createConnectionPooler(lookup InstallFunction) (*ConnectionPoo
return c.ConnectionPooler, nil
}
func (c *Cluster) deleteConnectionPooler() (err error) {
func (c *Cluster) deleteConnectionPooler(role PostgresRole) (err error) {
c.setProcessName("deleting connection pooler")
c.logger.Debugln("deleting connection pooler")
@@ -210,7 +210,7 @@ func (c *Cluster) deleteConnectionPooler() (err error) {
// Clean up the deployment object. If deployment resource we've remembered
// is somehow empty, try to delete based on what would we generate
deploymentName := c.connectionPoolerName(Master)
deploymentName := c.connectionPoolerName(role)
deployment := c.ConnectionPooler.Deployment
if deployment != nil {
@@ -235,7 +235,7 @@ func (c *Cluster) deleteConnectionPooler() (err error) {
// Repeat the same for the service object
service := c.ConnectionPooler.Service
serviceName := c.connectionPoolerName(Master)
serviceName := c.connectionPoolerName(role)
if service != nil {
serviceName = service.Name
+6 -6
View File
@@ -62,7 +62,7 @@ func TestConnectionPoolerCreationAndDeletion(t *testing.T) {
t.Errorf("%s: Connection pooler service is empty", testName)
}
err = cluster.deleteConnectionPooler()
err = cluster.deleteConnectionPooler(Master)
if err != nil {
t.Errorf("%s: Cannot delete connection pooler, %s", testName, err)
}
@@ -97,7 +97,7 @@ func TestNeedConnectionPooler(t *testing.T) {
}
cluster.Spec = acidv1.PostgresSpec{
EnableMasterConnectionPooler: boolToPointer(true),
EnableConnectionPooler: boolToPointer(true),
}
if !cluster.needConnectionPooler() {
@@ -106,8 +106,8 @@ func TestNeedConnectionPooler(t *testing.T) {
}
cluster.Spec = acidv1.PostgresSpec{
EnableMasterConnectionPooler: boolToPointer(false),
ConnectionPooler: &acidv1.ConnectionPooler{},
EnableConnectionPooler: boolToPointer(false),
ConnectionPooler: &acidv1.ConnectionPooler{},
}
if cluster.needConnectionPooler() {
@@ -116,8 +116,8 @@ func TestNeedConnectionPooler(t *testing.T) {
}
cluster.Spec = acidv1.PostgresSpec{
EnableMasterConnectionPooler: boolToPointer(true),
ConnectionPooler: &acidv1.ConnectionPooler{},
EnableConnectionPooler: boolToPointer(true),
ConnectionPooler: &acidv1.ConnectionPooler{},
}
if !cluster.needConnectionPooler() {
+8 -5
View File
@@ -895,8 +895,10 @@ func (c *Cluster) syncConnectionPooler(oldSpec,
if oldNeedConnectionPooler && !newNeedConnectionPooler {
// delete and cleanup resources
if err = c.deleteConnectionPooler(); err != nil {
c.logger.Warningf("could not remove connection pooler: %v", err)
for _, role := range [2]PostgresRole{Master, Replica} {
if err = c.deleteConnectionPooler(role); err != nil {
c.logger.Warningf("could not remove connection pooler: %v", err)
}
}
}
@@ -905,9 +907,10 @@ func (c *Cluster) syncConnectionPooler(oldSpec,
if c.ConnectionPooler != nil &&
(c.ConnectionPooler.Deployment != nil ||
c.ConnectionPooler.Service != nil) {
if err = c.deleteConnectionPooler(); err != nil {
c.logger.Warningf("could not remove connection pooler: %v", err)
for _, role := range [2]PostgresRole{Master, Replica} {
if err = c.deleteConnectionPooler(role); err != nil {
c.logger.Warningf("could not remove connection pooler: %v", err)
}
}
}
}
+5 -5
View File
@@ -139,7 +139,7 @@ func TestConnectionPoolerSynchronization(t *testing.T) {
},
newSpec: &acidv1.Postgresql{
Spec: acidv1.PostgresSpec{
EnableMasterConnectionPooler: boolToPointer(true),
EnableConnectionPooler: boolToPointer(true),
},
},
cluster: clusterMissingObjects,
@@ -232,14 +232,14 @@ func TestConnectionPoolerSynchronization(t *testing.T) {
subTest: "there is no sync from nil to an empty spec",
oldSpec: &acidv1.Postgresql{
Spec: acidv1.PostgresSpec{
EnableMasterConnectionPooler: boolToPointer(true),
ConnectionPooler: nil,
EnableConnectionPooler: boolToPointer(true),
ConnectionPooler: nil,
},
},
newSpec: &acidv1.Postgresql{
Spec: acidv1.PostgresSpec{
EnableMasterConnectionPooler: boolToPointer(true),
ConnectionPooler: &acidv1.ConnectionPooler{},
EnableConnectionPooler: boolToPointer(true),
ConnectionPooler: &acidv1.ConnectionPooler{},
},
},
cluster: clusterMock,
+2 -2
View File
@@ -520,8 +520,8 @@ func (c *Cluster) patroniKubernetesUseConfigMaps() bool {
}
func (c *Cluster) needConnectionPoolerWorker(spec *acidv1.PostgresSpec) bool {
if spec.EnableMasterConnectionPooler != nil {
return *spec.EnableMasterConnectionPooler
if spec.EnableConnectionPooler != nil {
return *spec.EnableConnectionPooler
} else if spec.EnableReplicaConnectionPooler != nil {
return *spec.EnableReplicaConnectionPooler
} else if spec.ConnectionPooler == nil {