Skip to content
Open
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
9 changes: 9 additions & 0 deletions internal/controller/everest/providers/pg/applier.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,15 @@ func (p *applier) Proxy() error {
pg.Spec.Proxy.PGBouncer.Resources.Limits[corev1.ResourceMemory] = database.Spec.Proxy.Resources.Memory
}
pg.Spec.Proxy.PGBouncer.ExposeSuperusers = true

if database.Spec.Proxy.Config != "" {
cfg, err := ParsePGBouncerConfig(database.Spec.Proxy.Config)
if err != nil {
return fmt.Errorf("proxy config: %w", err)
}
pg.Spec.Proxy.PGBouncer.Config = cfg
}

pg.Spec.Users = []crunchyv1beta1.PostgresUserSpec{
{
Name: "postgres",
Expand Down
68 changes: 68 additions & 0 deletions internal/controller/everest/providers/pg/pgbouncer_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// everest-operator
// Copyright (C) 2022 Percona LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package pg

import (
"fmt"
"strings"

crunchyv1beta1 "github.com/percona/percona-postgresql-operator/v2/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
"gopkg.in/yaml.v3"
)

// rawPGBouncerConfig is the YAML structure for DatabaseCluster.spec.proxy.config.
// Supports top-level keys: global, databases, users.
// See https://www.pgbouncer.org/config.html
type rawPGBouncerConfig struct {
Global map[string]interface{} `yaml:"global"`
Databases map[string]interface{} `yaml:"databases"`
Users map[string]interface{} `yaml:"users"`
}

// ParsePGBouncerConfig parses the proxy config YAML string from DatabaseCluster.spec.proxy.config
// into a PGBouncerConfiguration. Empty or whitespace-only config returns an empty config.
func ParsePGBouncerConfig(configYAML string) (crunchyv1beta1.PGBouncerConfiguration, error) {
out := crunchyv1beta1.PGBouncerConfiguration{}
s := strings.TrimSpace(configYAML)
if s == "" {
return out, nil
}

var raw rawPGBouncerConfig
if err := yaml.Unmarshal([]byte(s), &raw); err != nil {
return out, fmt.Errorf("parse pgBouncer config YAML: %w", err)
}

out.Global = toStringMap(raw.Global)
out.Databases = toStringMap(raw.Databases)
out.Users = toStringMap(raw.Users)
return out, nil
}

func toStringMap(m map[string]interface{}) map[string]string {
if len(m) == 0 {
return nil
}
out := make(map[string]string, len(m))
for k, v := range m {
if v == nil {
out[k] = ""
continue
}
out[k] = fmt.Sprint(v)
}
return out
}
102 changes: 102 additions & 0 deletions internal/controller/everest/providers/pg/pgbouncer_config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package pg

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestParsePGBouncerConfig(t *testing.T) {

Check failure on line 10 in internal/controller/everest/providers/pg/pgbouncer_config_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

Function TestParsePGBouncerConfig missing the call to method parallel (paralleltest)
tests := []struct {
name string
yaml string
check func(*testing.T, *struct{ Global, Databases, Users map[string]string })
wantErr bool
}{
{
name: "empty",
yaml: "",
check: func(t *testing.T, c *struct {

Check failure on line 20 in internal/controller/everest/providers/pg/pgbouncer_config_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

test helper function should start from t.Helper() (thelper)
Global map[string]string
Databases map[string]string
Users map[string]string
}) {
assert.Nil(t, c.Global)
assert.Nil(t, c.Databases)
assert.Nil(t, c.Users)
},
},
{
name: "whitespace only",
yaml: " \n\t ",
check: func(t *testing.T, c *struct {

Check failure on line 33 in internal/controller/everest/providers/pg/pgbouncer_config_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

test helper function should start from t.Helper() (thelper)
Global map[string]string
Databases map[string]string
Users map[string]string
}) {
assert.Nil(t, c.Global)
},
},
{
name: "global only",
yaml: `
global:
client_tls_sslmode: allow
default_pool_size: "150"
max_client_conn: "300"
`,
check: func(t *testing.T, c *struct {

Check failure on line 49 in internal/controller/everest/providers/pg/pgbouncer_config_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

test helper function should start from t.Helper() (thelper)
Global map[string]string
Databases map[string]string
Users map[string]string
}) {
require.NotNil(t, c.Global)
assert.Equal(t, "allow", c.Global["client_tls_sslmode"])
assert.Equal(t, "150", c.Global["default_pool_size"])
assert.Equal(t, "300", c.Global["max_client_conn"])
assert.Nil(t, c.Databases)
assert.Nil(t, c.Users)
},
},
{
name: "global with numeric values",
yaml: `
global:
default_pool_size: 150
max_client_conn: 300
`,
check: func(t *testing.T, c *struct {
Global map[string]string
Databases map[string]string
Users map[string]string
}) {
require.NotNil(t, c.Global)
assert.Equal(t, "150", c.Global["default_pool_size"])
assert.Equal(t, "300", c.Global["max_client_conn"])
},
},
{
name: "invalid YAML",
yaml: "global:\n [",
wantErr: true,
},
}
for _, tt := range tests {

Check failure on line 85 in internal/controller/everest/providers/pg/pgbouncer_config_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

Range statement for test TestParsePGBouncerConfig missing the call to method parallel in test Run (paralleltest)
t.Run(tt.name, func(t *testing.T) {
got, err := ParsePGBouncerConfig(tt.yaml)
if tt.wantErr {
require.Error(t, err)
return
}
require.NoError(t, err)
if tt.check != nil {
tt.check(t, &struct {
Global map[string]string
Databases map[string]string
Users map[string]string
}{got.Global, got.Databases, got.Users})
}
})
}
}
Loading