Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions internal/controller/postgrescluster/pgbouncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (r *Reconciler) reconcilePGBouncerConfigMap(
configmap := &corev1.ConfigMap{ObjectMeta: naming.ClusterPGBouncer(cluster)}
configmap.SetGroupVersionKind(corev1.SchemeGroupVersion.WithKind("ConfigMap"))

if cluster.Spec.Proxy == nil || cluster.Spec.Proxy.PGBouncer == nil {
if !cluster.Spec.Proxy.PGBouncerEnabled() {
// PgBouncer is disabled; delete the ConfigMap if it exists. Check the
// client cache first using Get.
key := client.ObjectKeyFromObject(configmap)
Expand Down Expand Up @@ -134,7 +134,7 @@ func (r *Reconciler) reconcilePGBouncerInPostgreSQL(

// K8SPG-345
var exposeSuperusers bool
if cluster.Spec.Proxy != nil && cluster.Spec.Proxy.PGBouncer != nil {
if cluster.Spec.Proxy.PGBouncerEnabled() {
exposeSuperusers = cluster.Spec.Proxy.PGBouncer.ExposeSuperusers
if exposeSuperusers {
log.Info("Superusers are exposed through PGBouncer")
Expand All @@ -144,7 +144,7 @@ func (r *Reconciler) reconcilePGBouncerInPostgreSQL(
action := func(ctx context.Context, exec postgres.Executor) error {
return errors.WithStack(pgbouncer.EnableInPostgreSQL(ctx, exec, clusterSecret, exposeSuperusers))
}
if cluster.Spec.Proxy == nil || cluster.Spec.Proxy.PGBouncer == nil {
if !cluster.Spec.Proxy.PGBouncerEnabled() {
// PgBouncer is disabled.
action = func(ctx context.Context, exec postgres.Executor) error {
return errors.WithStack(pgbouncer.DisableInPostgreSQL(ctx, exec))
Expand Down Expand Up @@ -254,7 +254,7 @@ func (r *Reconciler) reconcilePGBouncerSecret(
return nil, err
}

if cluster.Spec.Proxy == nil || cluster.Spec.Proxy.PGBouncer == nil {
if !cluster.Spec.Proxy.PGBouncerEnabled() {
// PgBouncer is disabled; delete the Secret if it exists.
if err == nil {
err = errors.WithStack(r.deleteControlled(ctx, cluster, existing))
Expand Down Expand Up @@ -397,7 +397,7 @@ func (r *Reconciler) generatePGBouncerService(
service := &corev1.Service{ObjectMeta: naming.ClusterPGBouncer(cluster)}
service.SetGroupVersionKind(corev1.SchemeGroupVersion.WithKind("Service"))

if cluster.Spec.Proxy == nil || cluster.Spec.Proxy.PGBouncer == nil {
if !cluster.Spec.Proxy.PGBouncerEnabled() {
return service, false, nil
}

Expand Down Expand Up @@ -504,7 +504,7 @@ func (r *Reconciler) generatePGBouncerDeployment(
deploy := &appsv1.Deployment{ObjectMeta: naming.ClusterPGBouncer(cluster)}
deploy.SetGroupVersionKind(appsv1.SchemeGroupVersion.WithKind("Deployment"))

if cluster.Spec.Proxy == nil || cluster.Spec.Proxy.PGBouncer == nil {
if !cluster.Spec.Proxy.PGBouncerEnabled() {
return deploy, false, nil
}

Expand Down Expand Up @@ -693,7 +693,7 @@ func (r *Reconciler) reconcilePGBouncerPodDisruptionBudget(
return client.IgnoreNotFound(err)
}

if cluster.Spec.Proxy == nil || cluster.Spec.Proxy.PGBouncer == nil {
if !cluster.Spec.Proxy.PGBouncerEnabled() {
return deleteExistingPDB(cluster)
}

Expand Down
18 changes: 18 additions & 0 deletions internal/controller/postgrescluster/pgbouncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,24 @@ namespace: ns5
}
})

t.Run("ZeroReplicas", func(t *testing.T) {
cluster := cluster.DeepCopy()
cluster.Spec.Proxy = &v1beta1.PostgresProxySpec{
PGBouncer: &v1beta1.PGBouncerPodSpec{
Replicas: new(int32),
},
}

service, specified, err := reconciler.generatePGBouncerService(cluster)
assert.NilError(t, err)
assert.Assert(t, !specified)

assert.Assert(t, cmp.MarshalMatches(service.ObjectMeta, `
name: pg7-pgbouncer
namespace: ns5
`))
})

cluster.Spec.Proxy = &v1beta1.PostgresProxySpec{
PGBouncer: &v1beta1.PGBouncerPodSpec{
Port: new(int32(9651)),
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/postgrescluster/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (r *Reconciler) generatePostgresUserSecret(
}

// When PgBouncer is enabled, include values for connecting through it.
if cluster.Spec.Proxy != nil && cluster.Spec.Proxy.PGBouncer != nil {
if cluster.Spec.Proxy.PGBouncerEnabled() {
pgBouncer := naming.ClusterPGBouncer(cluster)
hostname := pgBouncer.Name + "." + pgBouncer.Namespace + ".svc"
port := fmt.Sprint(*cluster.Spec.Proxy.PGBouncer.Port)
Expand Down
8 changes: 4 additions & 4 deletions internal/pgbouncer/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func ConfigMap(
inCluster *v1beta1.PostgresCluster,
outConfigMap *corev1.ConfigMap,
) {
if inCluster.Spec.Proxy == nil || inCluster.Spec.Proxy.PGBouncer == nil {
if !inCluster.Spec.Proxy.PGBouncerEnabled() {
// PgBouncer is disabled; there is nothing to do.
return
}
Expand Down Expand Up @@ -56,7 +56,7 @@ func Secret(ctx context.Context,
frontendCertManagerSecret *corev1.Secret,
additionalCAs [][]byte,
) error {
if inCluster.Spec.Proxy == nil || inCluster.Spec.Proxy.PGBouncer == nil {
if !inCluster.Spec.Proxy.PGBouncerEnabled() {
// PgBouncer is disabled; there is nothing to do.
return nil
}
Expand Down Expand Up @@ -171,7 +171,7 @@ func Pod(
inSecret *corev1.Secret,
outPod *corev1.PodSpec,
) {
if inCluster.Spec.Proxy == nil || inCluster.Spec.Proxy.PGBouncer == nil {
if !inCluster.Spec.Proxy.PGBouncerEnabled() {
// PgBouncer is disabled; there is nothing to do.
return
}
Expand Down Expand Up @@ -302,7 +302,7 @@ func PostgreSQL(
inCluster *v1beta1.PostgresCluster,
outHBAs *postgres.HBAs,
) {
if inCluster.Spec.Proxy == nil || inCluster.Spec.Proxy.PGBouncer == nil {
if !inCluster.Spec.Proxy.PGBouncerEnabled() {
// PgBouncer is disabled; there is nothing to do.
return
}
Expand Down
7 changes: 7 additions & 0 deletions percona/controller/pgcluster/gethost_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ func TestGetHost(t *testing.T) {
proxy: &v2.PGProxySpec{PGBouncer: nil},
expectedHost: clusterName + "-primary." + ns + ".svc",
},
{
name: "PGBouncer configured with zero replicas",
proxy: &v2.PGProxySpec{PGBouncer: &v2.PGBouncerSpec{
Replicas: new(int32),
}},
expectedHost: clusterName + "-primary." + ns + ".svc",
},
{
name: "PGBouncer configured, no ServiceExpose",
proxy: &v2.PGProxySpec{PGBouncer: &v2.PGBouncerSpec{}},
Expand Down
5 changes: 1 addition & 4 deletions percona/controller/pgcluster/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (r *PGClusterReconciler) getHost(ctx context.Context, cr *v2.PerconaPGClust
}

// If proxy is not configured, use the primary service as host.
if cr.Spec.Proxy == nil || cr.Spec.Proxy.PGBouncer == nil {
if !cr.Spec.Proxy.PGBouncerEnabled() {
return svcFQDN(naming.ClusterPrimaryService(postgresCluster).Name, postgresCluster.Namespace), nil
}

Expand Down Expand Up @@ -190,7 +190,6 @@ func updateConditions(cr *v2.PerconaPGCluster, status *v1beta1.PostgresClusterSt
}

setClusterNotReadyCondition(metav1.ConditionTrue, "AllConditionsAreTrue")

}

func syncConditionsFromPostgresToPercona(cr *v2.PerconaPGCluster, postgresStatus *v1beta1.PostgresClusterStatus) {
Expand All @@ -207,7 +206,6 @@ func syncConditionsFromPostgresToPercona(cr *v2.PerconaPGCluster, postgresStatus
}

func syncPatroniFromPostgresToPercona(cr *v2.PerconaPGCluster, postgresStatus *v1beta1.PostgresClusterStatus) {

if cr.Status.Patroni.Status == nil {
cr.Status.Patroni.Status = &v1beta1.PatroniStatus{}
}
Expand All @@ -227,5 +225,4 @@ func syncPgbackrestFromPostgresToPercona(cr *v2.PerconaPGCluster, postgresStatus
if postgresStatus.PGBackRest != nil {
cr.Status.PGBackRest = postgresStatus.PGBackRest.DeepCopy()
}

}
4 changes: 4 additions & 0 deletions pkg/apis/pgv2.percona.com/v2/perconapgcluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -1182,6 +1182,10 @@ func (p *PGProxySpec) IsSet() bool {
return p != nil && p.PGBouncer != nil
}

func (p *PGProxySpec) PGBouncerEnabled() bool {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a unit test for this function, especiallty because it is exported

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return p.IsSet() && (p.PGBouncer.Replicas == nil || *p.PGBouncer.Replicas != 0)
}
Comment thread
pooknull marked this conversation as resolved.

func (p *PGProxySpec) ToCrunchy(version string) *crunchyv1beta1.PostgresProxySpec {
if p == nil {
return nil
Expand Down
40 changes: 40 additions & 0 deletions pkg/apis/pgv2.percona.com/v2/perconapgcluster_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,46 @@ func TestPerconaPGCluster_Proxy(t *testing.T) {
})
}

func TestPGProxySpec_PGBouncerEnabled(t *testing.T) {
tests := map[string]struct {
spec *PGProxySpec
expected bool
}{
"nil proxy": {
spec: nil,
expected: false,
},
"nil PgBouncer": {
spec: &PGProxySpec{},
expected: false,
},
"replicas unspecified": {
spec: &PGProxySpec{
PGBouncer: &PGBouncerSpec{},
},
expected: true,
},
"zero replicas": {
spec: &PGProxySpec{
PGBouncer: &PGBouncerSpec{Replicas: new(int32(0))},
},
expected: false,
},
"non-zero replicas": {
spec: &PGProxySpec{
PGBouncer: &PGBouncerSpec{Replicas: new(int32(1))},
},
expected: true,
},
}

for name, tt := range tests {
t.Run(name, func(t *testing.T) {
assert.Equal(t, tt.expected, tt.spec.PGBouncerEnabled())
})
}
}

func TestPerconaPGCluster_PostgresImage(t *testing.T) {
cluster := new(PerconaPGCluster)
cluster.Default()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,11 @@ func (s *PostgresProxySpec) Default() {
}
}

// K8SPG-1062
Comment thread
pooknull marked this conversation as resolved.
func (s *PostgresProxySpec) PGBouncerEnabled() bool {
return s != nil && s.PGBouncer != nil && (s.PGBouncer.Replicas == nil || *s.PGBouncer.Replicas != 0)
}
Comment thread
pooknull marked this conversation as resolved.

type RegistrationRequirementStatus struct {
PGOVersion string `json:"pgoVersion,omitempty"`
}
Expand Down
Loading