Skip to content

Commit b430d68

Browse files
authored
Add proper support for the shared_buffer configuration parameter (#220)
1 parent 38bceed commit b430d68

File tree

2 files changed

+106
-2
lines changed

2 files changed

+106
-2
lines changed

api/v1/postgres_types.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package v1
99
import (
1010
"fmt"
1111
"reflect"
12+
"strconv"
1213

1314
"regexp"
1415

@@ -17,6 +18,7 @@ import (
1718
"inet.af/netaddr"
1819
corev1 "k8s.io/api/core/v1"
1920
networkingv1 "k8s.io/api/networking/v1"
21+
"k8s.io/apimachinery/pkg/api/resource"
2022
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2123
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2224
"k8s.io/apimachinery/pkg/runtime"
@@ -50,6 +52,8 @@ const (
5052
BackupConfigLabelName string = "postgres.database.fits.cloud/is-backup"
5153
// BackupConfigKey defines the key under which the BackupConfig is stored in the data map.
5254
BackupConfigKey = "config"
55+
// SharedBufferParameterKey defines the key under which the shared buffer size is stored in the parameters map. Defined by the postgres-operator/patroni
56+
SharedBufferParameterKey = "shared_buffer"
5357
)
5458

5559
var (
@@ -401,8 +405,8 @@ func (p *Postgres) ToUnstructuredZalandoPostgresql(z *zalando.Postgresql, c *cor
401405

402406
z.Spec.NumberOfInstances = p.Spec.NumberOfInstances
403407
z.Spec.PostgresqlParam.PgVersion = p.Spec.Version
404-
// TODO set shared_buffer from p.Spec.Size.SharedBuffer
405-
// z.Spec.PostgresqlParam.Parameters = map[string]string{}
408+
z.Spec.PostgresqlParam.Parameters = map[string]string{}
409+
setSharedBufferSize(z.Spec.PostgresqlParam.Parameters, p.Spec.Size.SharedBuffer)
406410
z.Spec.Resources.ResourceRequests.CPU = p.Spec.Size.CPU
407411
z.Spec.Resources.ResourceRequests.Memory = p.Spec.Size.Memory
408412
z.Spec.Resources.ResourceLimits.CPU = p.Spec.Size.CPU
@@ -542,3 +546,18 @@ func (p *Postgres) buildSidecars(c *corev1.ConfigMap) []zalando.Sidecar {
542546

543547
return sidecars
544548
}
549+
550+
// setSharedBufferSize converts and, if valid, sets the shared_buffer parameter in the given map.
551+
func setSharedBufferSize(parameters map[string]string, shmSize string) {
552+
// First step is to convert the string back to a quantity
553+
size, err := resource.ParseQuantity(shmSize)
554+
if err == nil {
555+
// if successful, get the given shared buffer size in bytes.
556+
sizeInBytes, ok := size.AsInt64()
557+
if ok && sizeInBytes >= (32*1024*1024) {
558+
// if more than 32Mi (our minimum value), convert the value to MB as required by postgres (although the docs are not very specific about that)
559+
sizeInMB := sizeInBytes / (1024 * 1024)
560+
parameters[SharedBufferParameterKey] = strconv.FormatInt(sizeInMB, 10) + "MB"
561+
}
562+
}
563+
}

api/v1/postgres_types_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
/ SPDX-FileCopyrightText: 2021 Finanz Informatik Technologie Services GmbHs
3+
/
4+
/ SPDX-License-Identifier: AGPL-1.0-only
5+
*/
6+
7+
package v1
8+
9+
import (
10+
"testing"
11+
)
12+
13+
func Test_setSharedBufferSize(t *testing.T) {
14+
15+
tests := []struct {
16+
name string
17+
input string
18+
expected string
19+
}{
20+
{
21+
name: "default",
22+
input: "64Mi",
23+
expected: "64MB",
24+
},
25+
{
26+
name: "min",
27+
input: "32Mi",
28+
expected: "32MB",
29+
},
30+
{
31+
name: "belowMin",
32+
input: "31Mi",
33+
expected: "",
34+
},
35+
{
36+
name: "Gibibytes",
37+
input: "1Gi",
38+
expected: "1024MB",
39+
},
40+
{
41+
name: "Kibibytes",
42+
input: "102400Ki",
43+
expected: "100MB",
44+
},
45+
{
46+
name: "Bytes",
47+
input: "67108864",
48+
expected: "64MB",
49+
},
50+
{
51+
name: "empty",
52+
input: "",
53+
expected: "",
54+
},
55+
{
56+
name: "empty2",
57+
input: " ",
58+
expected: "",
59+
},
60+
{
61+
name: "invalid",
62+
input: "64MB",
63+
expected: "",
64+
},
65+
}
66+
for _, tt := range tests {
67+
tt := tt // pin!
68+
t.Run(tt.name, func(t *testing.T) {
69+
parameters := map[string]string{}
70+
71+
setSharedBufferSize(parameters, tt.input)
72+
73+
result, ok := parameters[SharedBufferParameterKey]
74+
if ok {
75+
if tt.expected != result {
76+
t.Errorf("Adapter.ScopedGet() assertion failed: expected %q, got %q\n", tt.expected, result)
77+
}
78+
} else {
79+
if tt.expected != "" {
80+
t.Errorf("Conversion failed")
81+
}
82+
}
83+
})
84+
}
85+
}

0 commit comments

Comments
 (0)