Skip to content

Commit 8824136

Browse files
committed
hold
1 parent b86f239 commit 8824136

File tree

7 files changed

+52
-14
lines changed

7 files changed

+52
-14
lines changed

config/crd/bases/postgres-operator.crunchydata.com_postgresclusters.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14375,7 +14375,13 @@ spec:
1437514375
description: |-
1437614376
Settings that apply to the entire PgBouncer process.
1437714377
More info: https://www.pgbouncer.org/config.html
14378+
14379+
# Logging
1437814380
type: object
14381+
x-kubernetes-map-type: granular
14382+
x-kubernetes-validations:
14383+
- message: logfile config must end with '.log'
14384+
rule: '!has(self.logfile) || self.logfile.endsWith(''.log'')'
1437914385
users:
1438014386
additionalProperties:
1438114387
type: string
@@ -33300,7 +33306,13 @@ spec:
3330033306
description: |-
3330133307
Settings that apply to the entire PgBouncer process.
3330233308
More info: https://www.pgbouncer.org/config.html
33309+
33310+
# Logging
3330333311
type: object
33312+
x-kubernetes-map-type: granular
33313+
x-kubernetes-validations:
33314+
- message: logfile config must end with '.log'
33315+
rule: '!has(self.logfile) || self.logfile.endsWith(''.log'')'
3330433316
users:
3330533317
additionalProperties:
3330633318
type: string

internal/collector/pgbouncer.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
_ "embed"
1010
"encoding/json"
1111
"fmt"
12+
"path/filepath"
1213
"slices"
1314
"strconv"
1415

@@ -29,7 +30,7 @@ const PGBouncerPostRotateScript = "pkill -HUP --exact pgbouncer"
2930
// NewConfigForPgBouncerPod creates a config for the OTel collector container
3031
// that runs as a sidecar in the pgBouncer Pod
3132
func NewConfigForPgBouncerPod(
32-
ctx context.Context, cluster *v1beta1.PostgresCluster, sqlQueryUsername string,
33+
ctx context.Context, cluster *v1beta1.PostgresCluster, sqlQueryUsername, logfile string,
3334
) *Config {
3435
if cluster.Spec.Proxy == nil || cluster.Spec.Proxy.PGBouncer == nil {
3536
// pgBouncer is disabled; return nil
@@ -38,7 +39,7 @@ func NewConfigForPgBouncerPod(
3839

3940
config := NewConfig(cluster.Spec.Instrumentation)
4041

41-
EnablePgBouncerLogging(ctx, cluster, config)
42+
EnablePgBouncerLogging(ctx, cluster, config, logfile)
4243
EnablePgBouncerMetrics(ctx, cluster, config, sqlQueryUsername)
4344

4445
return config
@@ -49,14 +50,15 @@ func NewConfigForPgBouncerPod(
4950
func EnablePgBouncerLogging(ctx context.Context,
5051
inCluster *v1beta1.PostgresCluster,
5152
outConfig *Config,
53+
logfile string,
5254
) {
5355
var spec *v1beta1.InstrumentationLogsSpec
5456
if inCluster != nil && inCluster.Spec.Instrumentation != nil {
5557
spec = inCluster.Spec.Instrumentation.Logs
5658
}
5759

5860
if OpenTelemetryLogsEnabled(ctx, inCluster) {
59-
directory := naming.PGBouncerLogPath
61+
directory := filepath.Dir(logfile)
6062

6163
// Keep track of what log records and files have been processed.
6264
// Use a subdirectory of the logs directory to stay within the same failure domain.

internal/collector/pgbouncer_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"gotest.tools/v3/assert"
1212

1313
"github.com/crunchydata/postgres-operator/internal/feature"
14+
"github.com/crunchydata/postgres-operator/internal/naming"
1415
"github.com/crunchydata/postgres-operator/internal/testing/require"
1516
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
1617
)
@@ -28,7 +29,7 @@ func TestEnablePgBouncerLogging(t *testing.T) {
2829
require.UnmarshalInto(t, &cluster.Spec, `{
2930
instrumentation: {}
3031
}`)
31-
EnablePgBouncerLogging(ctx, cluster, config)
32+
EnablePgBouncerLogging(ctx, cluster, config, naming.PGBouncerFullLogPath)
3233

3334
result, err := config.ToYAML()
3435
assert.NilError(t, err)
@@ -127,7 +128,7 @@ service:
127128
cluster := new(v1beta1.PostgresCluster)
128129
cluster.Spec.Instrumentation = testInstrumentationSpec()
129130

130-
EnablePgBouncerLogging(ctx, cluster, config)
131+
EnablePgBouncerLogging(ctx, cluster, config, naming.PGBouncerFullLogPath)
131132

132133
result, err := config.ToYAML()
133134
assert.NilError(t, err)

internal/controller/postgrescluster/pgbouncer.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ func (r *Reconciler) reconcilePGBouncer(
4444
secret, err = r.reconcilePGBouncerSecret(ctx, cluster, root, service)
4545
}
4646
if err == nil {
47-
config := collector.NewConfigForPgBouncerPod(ctx, cluster, pgbouncer.PostgresqlUser)
48-
configmap, err = r.reconcilePGBouncerConfigMap(ctx, cluster, config)
47+
logfile := setPGBouncerLogfile(ctx, cluster)
48+
config := collector.NewConfigForPgBouncerPod(ctx, cluster, pgbouncer.PostgresqlUser, logfile)
49+
configmap, err = r.reconcilePGBouncerConfigMap(ctx, cluster, config, logfile)
4950
}
5051
if err == nil {
5152
err = r.reconcilePGBouncerDeployment(ctx, cluster, primaryCertificate, configmap, secret)
@@ -59,13 +60,28 @@ func (r *Reconciler) reconcilePGBouncer(
5960
return err
6061
}
6162

63+
// setPGBouncerLogfile retrieves the logfile config if present in the user config.
64+
// If not present, set to the OTEL default.
65+
// If OTEL is not enabled, we do not use this value.
66+
// TODO: Check INI config files specified on the cluster
67+
func setPGBouncerLogfile(ctx context.Context, cluster *v1beta1.PostgresCluster) string {
68+
logfile := naming.PGBouncerFullLogPath
69+
70+
if dest, ok := cluster.Spec.Proxy.PGBouncer.Config.Global["logfile"]; ok {
71+
logfile = dest
72+
}
73+
74+
return logfile
75+
}
76+
6277
// +kubebuilder:rbac:groups="",resources="configmaps",verbs={get}
6378
// +kubebuilder:rbac:groups="",resources="configmaps",verbs={create,delete,patch}
6479

6580
// reconcilePGBouncerConfigMap writes the ConfigMap for a PgBouncer Pod.
6681
func (r *Reconciler) reconcilePGBouncerConfigMap(
6782
ctx context.Context, cluster *v1beta1.PostgresCluster,
6883
otelConfig *collector.Config,
84+
logfile string,
6985
) (*corev1.ConfigMap, error) {
7086
configmap := &corev1.ConfigMap{ObjectMeta: naming.ClusterPGBouncer(cluster)}
7187
configmap.SetGroupVersionKind(corev1.SchemeGroupVersion.WithKind("ConfigMap"))
@@ -104,7 +120,7 @@ func (r *Reconciler) reconcilePGBouncerConfigMap(
104120
// If OTel logging is enabled, add logrotate config
105121
if err == nil && collector.OpenTelemetryLogsEnabled(ctx, cluster) {
106122
logrotateConfig := collector.LogrotateConfig{
107-
LogFiles: []string{naming.PGBouncerFullLogPath},
123+
LogFiles: []string{logfile},
108124
PostrotateScript: collector.PGBouncerPostRotateScript,
109125
}
110126
collector.AddLogrotateConfigs(ctx, cluster.Spec.Instrumentation, configmap,

internal/pgbouncer/config.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,13 @@ func clusterINI(ctx context.Context, cluster *v1beta1.PostgresCluster) string {
127127
"unix_socket_dir": "",
128128
}
129129

130+
// Override the above with any specified settings.
131+
maps.Copy(global, cluster.Spec.Proxy.PGBouncer.Config.Global)
132+
130133
// If OpenTelemetryLogs feature is enabled, enable logging to file
131-
if collector.OpenTelemetryLogsEnabled(ctx, cluster) {
132-
global["logfile"] = naming.PGBouncerLogPath + "/pgbouncer.log"
134+
// if not otherwise set
135+
if _, ok := global["logfile"]; !ok && collector.OpenTelemetryLogsEnabled(ctx, cluster) {
136+
global["logfile"] = naming.PGBouncerFullLogPath
133137
}
134138

135139
// When OTel metrics are enabled, allow pgBouncer's postgres user
@@ -138,9 +142,6 @@ func clusterINI(ctx context.Context, cluster *v1beta1.PostgresCluster) string {
138142
global["stats_users"] = PostgresqlUser
139143
}
140144

141-
// Override the above with any specified settings.
142-
maps.Copy(global, cluster.Spec.Proxy.PGBouncer.Config.Global)
143-
144145
// Prevent the user from bypassing the main configuration file.
145146
global["conffile"] = iniFileAbsolutePath
146147

internal/pgbouncer/reconcile.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ func ConfigMap(
3737
initialize.Map(&outConfigMap.Data)
3838

3939
outConfigMap.Data[emptyConfigMapKey] = ""
40-
outConfigMap.Data[iniFileConfigMapKey] = clusterINI(ctx, inCluster)
40+
clusterINI := clusterINI(ctx, inCluster)
41+
outConfigMap.Data[iniFileConfigMapKey] = clusterINI
4142
}
4243

4344
// Secret populates the PgBouncer Secret.

pkg/apis/postgres-operator.crunchydata.com/v1beta1/pgbouncer_types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@ type PGBouncerConfiguration struct {
2626

2727
// Settings that apply to the entire PgBouncer process.
2828
// More info: https://www.pgbouncer.org/config.html
29+
//
30+
// # Logging
31+
// +kubebuilder:validation:XValidation:rule=`!has(self.logfile) || self.logfile.endsWith('.log')`,message=`logfile config must end with '.log'`
32+
//
2933
// +optional
34+
// +mapType=granular
3035
Global map[string]string `json:"global,omitempty"`
3136

3237
// PgBouncer database definitions. The key is the database requested by a

0 commit comments

Comments
 (0)