Skip to content

Commit c0896c6

Browse files
committed
Update test, file creation
1 parent cb3711b commit c0896c6

File tree

5 files changed

+260
-16
lines changed

5 files changed

+260
-16
lines changed

internal/collector/pgbouncer.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,14 @@ func EnablePgBouncerLogging(ctx context.Context,
5959

6060
if OpenTelemetryLogsEnabled(ctx, inCluster) {
6161
directory := filepath.Dir(logfile)
62-
create_directory := false
63-
if directory != "/tmp" {
64-
create_directory = true
65-
}
6662

6763
// Keep track of what log records and files have been processed.
6864
// Use a subdirectory of the logs directory to stay within the same failure domain.
6965
//
7066
// https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/-/extension/storage/filestorage#readme
7167
outConfig.Extensions["file_storage/pgbouncer_logs"] = map[string]any{
7268
"directory": directory + "/receiver",
73-
"create_directory": create_directory,
69+
"create_directory": false,
7470
"fsync": true,
7571
}
7672

internal/controller/postgrescluster/pgbouncer.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ func (r *Reconciler) reconcilePGBouncer(
4444
if err == nil {
4545
secret, err = r.reconcilePGBouncerSecret(ctx, cluster, root, service)
4646
}
47+
logfile := setPGBouncerLogfile(cluster)
4748
if err == nil {
48-
logfile := setPGBouncerLogfile(cluster)
4949
config := collector.NewConfigForPgBouncerPod(ctx, cluster, pgbouncer.PostgresqlUser, logfile)
5050
configmap, err = r.reconcilePGBouncerConfigMap(ctx, cluster, config, logfile)
5151
}
5252
if err == nil {
53-
err = r.reconcilePGBouncerDeployment(ctx, cluster, primaryCertificate, configmap, secret)
53+
err = r.reconcilePGBouncerDeployment(ctx, cluster, primaryCertificate, configmap, secret, logfile)
5454
}
5555
if err == nil {
5656
err = r.reconcilePGBouncerPodDisruptionBudget(ctx, cluster)
@@ -392,6 +392,7 @@ func (r *Reconciler) generatePGBouncerDeployment(
392392
ctx context.Context, cluster *v1beta1.PostgresCluster,
393393
primaryCertificate *corev1.SecretProjection,
394394
configmap *corev1.ConfigMap, secret *corev1.Secret,
395+
logfile string,
395396
) (*appsv1.Deployment, bool, error) {
396397
deploy := &appsv1.Deployment{ObjectMeta: naming.ClusterPGBouncer(cluster)}
397398
deploy.SetGroupVersionKind(appsv1.SchemeGroupVersion.WithKind("Deployment"))
@@ -494,7 +495,7 @@ func (r *Reconciler) generatePGBouncerDeployment(
494495
err := errors.WithStack(r.setControllerReference(cluster, deploy))
495496

496497
if err == nil {
497-
pgbouncer.Pod(ctx, cluster, configmap, primaryCertificate, secret, &deploy.Spec.Template)
498+
pgbouncer.Pod(ctx, cluster, configmap, primaryCertificate, secret, &deploy.Spec.Template, logfile)
498499
}
499500

500501
// Add tmp directory and volume for log files
@@ -521,9 +522,10 @@ func (r *Reconciler) reconcilePGBouncerDeployment(
521522
ctx context.Context, cluster *v1beta1.PostgresCluster,
522523
primaryCertificate *corev1.SecretProjection,
523524
configmap *corev1.ConfigMap, secret *corev1.Secret,
525+
logfile string,
524526
) error {
525527
deploy, specified, err := r.generatePGBouncerDeployment(
526-
ctx, cluster, primaryCertificate, configmap, secret)
528+
ctx, cluster, primaryCertificate, configmap, secret, logfile)
527529

528530
// Set observations whether the deployment exists or not.
529531
defer func() {

internal/controller/postgrescluster/pgbouncer_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ func TestGeneratePGBouncerDeployment(t *testing.T) {
382382
cluster := cluster.DeepCopy()
383383
cluster.Spec.Proxy = spec
384384

385-
deploy, specified, err := reconciler.generatePGBouncerDeployment(ctx, cluster, nil, nil, nil)
385+
deploy, specified, err := reconciler.generatePGBouncerDeployment(ctx, cluster, nil, nil, nil, "")
386386
assert.NilError(t, err)
387387
assert.Assert(t, !specified)
388388

@@ -415,7 +415,7 @@ namespace: ns3
415415
}
416416

417417
deploy, specified, err := reconciler.generatePGBouncerDeployment(
418-
ctx, cluster, primary, configmap, secret)
418+
ctx, cluster, primary, configmap, secret, "")
419419
assert.NilError(t, err)
420420
assert.Assert(t, specified)
421421

@@ -456,7 +456,7 @@ namespace: ns3
456456

457457
t.Run("PodSpec", func(t *testing.T) {
458458
deploy, specified, err := reconciler.generatePGBouncerDeployment(
459-
ctx, cluster, primary, configmap, secret)
459+
ctx, cluster, primary, configmap, secret, "")
460460
assert.NilError(t, err)
461461
assert.Assert(t, specified)
462462

@@ -503,7 +503,7 @@ topologySpreadConstraints:
503503
cluster.Spec.DisableDefaultPodScheduling = initialize.Bool(true)
504504

505505
deploy, specified, err := reconciler.generatePGBouncerDeployment(
506-
ctx, cluster, primary, configmap, secret)
506+
ctx, cluster, primary, configmap, secret, "")
507507
assert.NilError(t, err)
508508
assert.Assert(t, specified)
509509

@@ -521,7 +521,7 @@ topologySpreadConstraints:
521521
}
522522

523523
deploy, specified, err := reconciler.generatePGBouncerDeployment(
524-
ctx, cluster, primary, configmap, secret)
524+
ctx, cluster, primary, configmap, secret, "")
525525

526526
assert.NilError(t, err)
527527
assert.Assert(t, specified)

internal/pgbouncer/reconcile.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package pgbouncer
66

77
import (
88
"context"
9+
"path/filepath"
910

1011
"github.com/pkg/errors"
1112
corev1 "k8s.io/api/core/v1"
@@ -19,6 +20,7 @@ import (
1920
"github.com/crunchydata/postgres-operator/internal/pki"
2021
"github.com/crunchydata/postgres-operator/internal/postgres"
2122
passwd "github.com/crunchydata/postgres-operator/internal/postgres/password"
23+
"github.com/crunchydata/postgres-operator/internal/shell"
2224
"github.com/crunchydata/postgres-operator/internal/util"
2325
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
2426
)
@@ -128,6 +130,7 @@ func Pod(
128130
inPostgreSQLCertificate *corev1.SecretProjection,
129131
inSecret *corev1.Secret,
130132
template *corev1.PodTemplateSpec,
133+
logfile string,
131134
) {
132135
if inCluster.Spec.Proxy == nil || inCluster.Spec.Proxy.PGBouncer == nil {
133136
// PgBouncer is disabled; there is nothing to do.
@@ -146,10 +149,15 @@ func Pod(
146149
),
147150
}
148151

152+
mkdirCommand := ""
153+
if logfile != "" && filepath.Dir(logfile) != "/tmp" {
154+
mkdirCommand = shell.MakeDirectories("/tmp", filepath.Dir(logfile)) + "; "
155+
}
156+
149157
container := corev1.Container{
150158
Name: naming.ContainerPGBouncer,
151159

152-
Command: []string{"pgbouncer", iniFileAbsolutePath},
160+
Command: []string{"sh", "-c", "--", mkdirCommand + `exec "$@"`, "--", "pgbouncer", iniFileAbsolutePath},
153161
Image: config.PGBouncerContainerImage(inCluster),
154162
ImagePullPolicy: inCluster.Spec.ImagePullPolicy,
155163
Resources: inCluster.Spec.Proxy.PGBouncer.Resources,

0 commit comments

Comments
 (0)