From 3a906aba930ee26a2b3ad9d8325668a20926917b Mon Sep 17 00:00:00 2001 From: Rafia Sabih Date: Fri, 28 Aug 2020 13:25:46 +0200 Subject: [PATCH] Add pooler for replica --- manifests/complete-postgres-manifest.yaml | 5 +- pkg/apis/acid.zalan.do/v1/crds.go | 3 ++ pkg/apis/acid.zalan.do/v1/postgresql_type.go | 5 +- pkg/cluster/k8sres.go | 50 +++++++++++++++----- 4 files changed, 47 insertions(+), 16 deletions(-) diff --git a/manifests/complete-postgres-manifest.yaml b/manifests/complete-postgres-manifest.yaml index 69f7a2d9f..be2250ff9 100644 --- a/manifests/complete-postgres-manifest.yaml +++ b/manifests/complete-postgres-manifest.yaml @@ -1,7 +1,7 @@ apiVersion: "acid.zalan.do/v1" kind: postgresql metadata: - name: acid-test-cluster + name: acid-test-cluster2 # labels: # environment: demo # annotations: @@ -18,7 +18,8 @@ spec: - createdb enableMasterLoadBalancer: false enableReplicaLoadBalancer: false -# enableConnectionPooler: true # not needed when connectionPooler section is present (see below) + #enableConnectionPooler: true # not needed when connectionPooler section is present (see below) + enableReplicaConnectionPooler: true # set to enable connectionPooler for replica endpoints allowedSourceRanges: # load balancers' source ranges for both master and replica services - 127.0.0.1/32 databases: diff --git a/pkg/apis/acid.zalan.do/v1/crds.go b/pkg/apis/acid.zalan.do/v1/crds.go index 43c313c16..87da0bda3 100644 --- a/pkg/apis/acid.zalan.do/v1/crds.go +++ b/pkg/apis/acid.zalan.do/v1/crds.go @@ -262,6 +262,9 @@ var PostgresCRDResourceValidation = apiextv1beta1.CustomResourceValidation{ "enableConnectionPooler": { Type: "boolean", }, + "enableReplicaConnectionPooler": { + Type: "boolean", + }, "enableLogicalBackup": { Type: "boolean", }, diff --git a/pkg/apis/acid.zalan.do/v1/postgresql_type.go b/pkg/apis/acid.zalan.do/v1/postgresql_type.go index 24ef24d63..867157d26 100644 --- a/pkg/apis/acid.zalan.do/v1/postgresql_type.go +++ b/pkg/apis/acid.zalan.do/v1/postgresql_type.go @@ -29,8 +29,9 @@ type PostgresSpec struct { Patroni `json:"patroni,omitempty"` Resources `json:"resources,omitempty"` - EnableConnectionPooler *bool `json:"enableConnectionPooler,omitempty"` - ConnectionPooler *ConnectionPooler `json:"connectionPooler,omitempty"` + EnableConnectionPooler *bool `json:"enableConnectionPooler,omitempty"` + EnableReplicaConnectionPooler *bool `json:"enableReplicaConnectionPooler,omitempty"` + ConnectionPooler *ConnectionPooler `json:"connectionPooler,omitempty"` TeamID string `json:"teamId"` DockerImage string `json:"dockerImage,omitempty"` diff --git a/pkg/cluster/k8sres.go b/pkg/cluster/k8sres.go index d7878942c..c6743d553 100644 --- a/pkg/cluster/k8sres.go +++ b/pkg/cluster/k8sres.go @@ -2085,9 +2085,13 @@ func (c *Cluster) getConnectionPoolerEnvVars(spec *acidv1.PostgresSpec) []v1.Env return []v1.EnvVar{ { - Name: "CONNECTION_POOLER_PORT", + Name: "CONNECTION_POOLER_MASTER_PORT", Value: fmt.Sprint(pgPort), }, + { + Name: "CONNECTION_POOLER_REPLICA_PORT", + Value: fmt.Sprint(5433), + }, { Name: "CONNECTION_POOLER_MODE", Value: effectiveMode, @@ -2304,19 +2308,41 @@ func (c *Cluster) generateConnectionPoolerService(spec *acidv1.PostgresSpec) *v1 if spec.ConnectionPooler == nil { spec.ConnectionPooler = &acidv1.ConnectionPooler{} } + var serviceSpec = v1.ServiceSpec{} - serviceSpec := v1.ServiceSpec{ - Ports: []v1.ServicePort{ - { - Name: c.connectionPoolerName(), - Port: pgPort, - TargetPort: intstr.IntOrString{StrVal: c.servicePort(Master)}, + if *spec.EnableReplicaConnectionPooler == false { + serviceSpec = v1.ServiceSpec{ + Ports: []v1.ServicePort{ + { + Name: c.connectionPoolerName(), + Port: pgPort, + TargetPort: intstr.IntOrString{StrVal: c.servicePort(Master)}, + }, }, - }, - Type: v1.ServiceTypeClusterIP, - Selector: map[string]string{ - "connection-pooler": c.connectionPoolerName(), - }, + Type: v1.ServiceTypeClusterIP, + Selector: map[string]string{ + "connection-pooler": c.connectionPoolerName(), + }, + } + } else { + serviceSpec = v1.ServiceSpec{ + Ports: []v1.ServicePort{ + { + Name: c.connectionPoolerName(), + Port: pgPort, + TargetPort: intstr.IntOrString{StrVal: c.servicePort(Master)}, + }, + { + Name: c.connectionPoolerName() + "-repl", + Port: 5433, + TargetPort: intstr.IntOrString{StrVal: c.servicePort(Replica)}, + }, + }, + Type: v1.ServiceTypeClusterIP, + Selector: map[string]string{ + "connection-pooler": c.connectionPoolerName(), + }, + } } service := &v1.Service{