Skip to content

Commit 9977db2

Browse files
committed
If the OpenTelemetryLogs feature gate is set, tell patroni to log to file regardless of whether the user has set any logging settings in the spec.
1 parent 19a28f7 commit 9977db2

File tree

3 files changed

+73
-22
lines changed

3 files changed

+73
-22
lines changed

internal/controller/postgrescluster/cluster.go

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1616
"k8s.io/apimachinery/pkg/util/intstr"
1717

18+
"github.com/crunchydata/postgres-operator/internal/feature"
1819
"github.com/crunchydata/postgres-operator/internal/initialize"
1920
"github.com/crunchydata/postgres-operator/internal/naming"
2021
"github.com/crunchydata/postgres-operator/internal/patroni"
@@ -44,7 +45,7 @@ func (r *Reconciler) reconcileClusterConfigMap(
4445

4546
if err == nil {
4647
err = patroni.ClusterConfigMap(ctx, cluster, pgHBAs, pgParameters,
47-
clusterConfigMap, r.patroniLogSize(cluster))
48+
clusterConfigMap, r.patroniLogSize(ctx, cluster))
4849
}
4950
if err == nil {
5051
err = errors.WithStack(r.apply(ctx, clusterConfigMap))
@@ -57,25 +58,25 @@ func (r *Reconciler) reconcileClusterConfigMap(
5758
// If a value is set, this enables volume based log storage and triggers the
5859
// relevant Patroni configuration. If the value given is less than 25M, the log
5960
// file size storage limit defaults to 25M and an event is triggered.
60-
func (r *Reconciler) patroniLogSize(cluster *v1beta1.PostgresCluster) int64 {
61+
// If a value is not set, but the OpenTelemetryLogs feature gate is enabled, the
62+
// log file size storage limit will be set to 25M.
63+
func (r *Reconciler) patroniLogSize(ctx context.Context, cluster *v1beta1.PostgresCluster) int64 {
64+
if cluster.Spec.Patroni != nil && cluster.Spec.Patroni.Logging != nil &&
65+
cluster.Spec.Patroni.Logging.StorageLimit != nil {
6166

62-
if cluster.Spec.Patroni != nil {
63-
if cluster.Spec.Patroni.Logging != nil {
64-
if cluster.Spec.Patroni.Logging.StorageLimit != nil {
67+
sizeInBytes := cluster.Spec.Patroni.Logging.StorageLimit.Value()
6568

66-
sizeInBytes := cluster.Spec.Patroni.Logging.StorageLimit.Value()
69+
if sizeInBytes < 25000000 {
70+
// TODO(validation): Eventually we should be able to remove this in favor of CEL validation.
71+
// - https://kubernetes.io/docs/reference/using-api/cel/
72+
r.Recorder.Eventf(cluster, corev1.EventTypeWarning, "PatroniLogStorageLimitTooSmall",
73+
"Configured Patroni log storage limit is too small. File size will default to 25M.")
6774

68-
if sizeInBytes < 25000000 {
69-
// TODO(validation): Eventually we should be able to remove this in favor of CEL validation.
70-
// - https://kubernetes.io/docs/reference/using-api/cel/
71-
r.Recorder.Eventf(cluster, corev1.EventTypeWarning, "PatroniLogStorageLimitTooSmall",
72-
"Configured Patroni log storage limit is too small. File size will default to 25M.")
73-
74-
sizeInBytes = 25000000
75-
}
76-
return sizeInBytes
77-
}
75+
sizeInBytes = 25000000
7876
}
77+
return sizeInBytes
78+
} else if feature.Enabled(ctx, feature.OpenTelemetryLogs) {
79+
return 25000000
7980
}
8081
return 0
8182
}

internal/controller/postgrescluster/cluster_test.go

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"sigs.k8s.io/controller-runtime/pkg/reconcile"
2222

2323
"github.com/crunchydata/postgres-operator/internal/controller/runtime"
24+
"github.com/crunchydata/postgres-operator/internal/feature"
2425
"github.com/crunchydata/postgres-operator/internal/initialize"
2526
"github.com/crunchydata/postgres-operator/internal/naming"
2627
"github.com/crunchydata/postgres-operator/internal/testing/cmp"
@@ -787,6 +788,7 @@ postgres-operator.crunchydata.com/role: replica
787788
}
788789

789790
func TestPatroniLogSize(t *testing.T) {
791+
ctx := context.Background()
790792

791793
oneHundredMeg, err := resource.ParseQuantity("100M")
792794
assert.NilError(t, err)
@@ -805,7 +807,7 @@ func TestPatroniLogSize(t *testing.T) {
805807
recorder := events.NewRecorder(t, runtime.Scheme)
806808
reconciler := &Reconciler{Recorder: recorder}
807809

808-
size := reconciler.patroniLogSize(&cluster)
810+
size := reconciler.patroniLogSize(ctx, &cluster)
809811

810812
assert.Equal(t, size, int64(0))
811813
assert.Equal(t, len(recorder.Events), 0)
@@ -818,7 +820,7 @@ func TestPatroniLogSize(t *testing.T) {
818820
cluster.Spec.Patroni = &v1beta1.PatroniSpec{
819821
Logging: &v1beta1.PatroniLogConfig{}}
820822

821-
size := reconciler.patroniLogSize(&cluster)
823+
size := reconciler.patroniLogSize(ctx, &cluster)
822824

823825
assert.Equal(t, size, int64(0))
824826
assert.Equal(t, len(recorder.Events), 0)
@@ -833,7 +835,7 @@ func TestPatroniLogSize(t *testing.T) {
833835
StorageLimit: &oneHundredMeg,
834836
}}
835837

836-
size := reconciler.patroniLogSize(&cluster)
838+
size := reconciler.patroniLogSize(ctx, &cluster)
837839

838840
assert.Equal(t, size, int64(100000000))
839841
assert.Equal(t, len(recorder.Events), 0)
@@ -848,12 +850,51 @@ func TestPatroniLogSize(t *testing.T) {
848850
StorageLimit: &tooSmall,
849851
}}
850852

851-
size := reconciler.patroniLogSize(&cluster)
853+
size := reconciler.patroniLogSize(ctx, &cluster)
852854

853855
assert.Equal(t, size, int64(25000000))
854856
assert.Equal(t, len(recorder.Events), 1)
855857
assert.Equal(t, recorder.Events[0].Regarding.Name, cluster.Name)
856858
assert.Equal(t, recorder.Events[0].Reason, "PatroniLogStorageLimitTooSmall")
857859
assert.Equal(t, recorder.Events[0].Note, "Configured Patroni log storage limit is too small. File size will default to 25M.")
858860
})
861+
862+
t.Run("SizeUnsetOtelLogsEnabled", func(t *testing.T) {
863+
gate := feature.NewGate()
864+
assert.NilError(t, gate.SetFromMap(map[string]bool{
865+
feature.OpenTelemetryLogs: true,
866+
}))
867+
ctx := feature.NewContext(ctx, gate)
868+
869+
recorder := events.NewRecorder(t, runtime.Scheme)
870+
reconciler := &Reconciler{Recorder: recorder}
871+
872+
cluster.Spec.Patroni = nil
873+
874+
size := reconciler.patroniLogSize(ctx, &cluster)
875+
876+
assert.Equal(t, size, int64(25000000))
877+
assert.Equal(t, len(recorder.Events), 0)
878+
})
879+
880+
t.Run("SizeSetOtelLogsEnabled", func(t *testing.T) {
881+
gate := feature.NewGate()
882+
assert.NilError(t, gate.SetFromMap(map[string]bool{
883+
feature.OpenTelemetryLogs: true,
884+
}))
885+
ctx := feature.NewContext(ctx, gate)
886+
887+
recorder := events.NewRecorder(t, runtime.Scheme)
888+
reconciler := &Reconciler{Recorder: recorder}
889+
890+
cluster.Spec.Patroni = &v1beta1.PatroniSpec{
891+
Logging: &v1beta1.PatroniLogConfig{
892+
StorageLimit: &oneHundredMeg,
893+
}}
894+
895+
size := reconciler.patroniLogSize(ctx, &cluster)
896+
897+
assert.Equal(t, size, int64(100000000))
898+
assert.Equal(t, len(recorder.Events), 0)
899+
})
859900
}

internal/patroni/config.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"sigs.k8s.io/yaml"
1515

1616
"github.com/crunchydata/postgres-operator/internal/config"
17+
"github.com/crunchydata/postgres-operator/internal/initialize"
1718
"github.com/crunchydata/postgres-operator/internal/naming"
1819
"github.com/crunchydata/postgres-operator/internal/postgres"
1920
"github.com/crunchydata/postgres-operator/internal/shell"
@@ -151,9 +152,17 @@ func clusterYAML(
151152
},
152153
}
153154

154-
// if a Patroni log file size is configured, configure volume file storage
155+
// If a Patroni log file size is configured (the user set it in the
156+
// spec or the OpenTelemetryLogs feature gate is enabled), we need to
157+
// configure volume file storage
155158
if patroniLogStorageLimit != 0 {
156159

160+
logLevel := initialize.Pointer("INFO")
161+
if cluster.Spec.Patroni != nil && cluster.Spec.Patroni.Logging != nil &&
162+
cluster.Spec.Patroni.Logging.Level != nil {
163+
logLevel = cluster.Spec.Patroni.Logging.Level
164+
}
165+
157166
// Configure the Patroni log settings
158167
// - https://patroni.readthedocs.io/en/latest/yaml_configuration.html#log
159168
root["log"] = map[string]any{
@@ -162,7 +171,7 @@ func clusterYAML(
162171
"type": "json",
163172

164173
// defaults to "INFO"
165-
"level": cluster.Spec.Patroni.Logging.Level,
174+
"level": logLevel,
166175

167176
// Setting group read permissions so that the OTel filelog receiver can
168177
// read the log files.

0 commit comments

Comments
 (0)