Skip to content

Commit 791d48a

Browse files
armrugbartolinilitaocdl
authored
chore: add webhook validation for shared_buffers (#2840)
add webhook validation for `shared_buffers` Signed-off-by: Armando Ruocco <[email protected]> Signed-off-by: Gabriele Bartolini <[email protected]> Signed-off-by: Tao Li <[email protected]> Co-authored-by: Gabriele Bartolini <[email protected]> Co-authored-by: Tao Li <[email protected]>
1 parent 9f7f18c commit 791d48a

3 files changed

Lines changed: 97 additions & 16 deletions

File tree

‎api/v1/cluster_webhook.go‎

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ const (
5858
DefaultApplicationUserName = DefaultApplicationDatabaseName
5959
)
6060

61+
const (
62+
sharedBuffersParameter = "shared_buffers"
63+
)
64+
6165
// clusterLog is for logging in this package.
6266
var clusterLog = log.WithName("cluster-resource").WithValues("version", "v1")
6367

@@ -944,25 +948,40 @@ func (r *Cluster) validateImagePullPolicy() field.ErrorList {
944948
func (r *Cluster) validateResources() field.ErrorList {
945949
var result field.ErrorList
946950

947-
cpuPopulated := !r.Spec.Resources.Requests.Cpu().IsZero() && !r.Spec.Resources.Limits.Cpu().IsZero()
948-
if cpuPopulated {
949-
cpuRequestGtThanLimit := r.Spec.Resources.Requests.Cpu().Cmp(*r.Spec.Resources.Limits.Cpu()) > 0
951+
cpuRequest := r.Spec.Resources.Requests.Cpu()
952+
cpuLimits := r.Spec.Resources.Limits.Cpu()
953+
if !cpuRequest.IsZero() && !cpuLimits.IsZero() {
954+
cpuRequestGtThanLimit := cpuRequest.Cmp(*cpuLimits) > 0
950955
if cpuRequestGtThanLimit {
951956
result = append(result, field.Invalid(
952957
field.NewPath("spec", "resources", "requests", "cpu"),
953-
r.Spec.Resources.Requests.Cpu().String(),
958+
cpuRequest.String(),
954959
"CPU request is greater than the limit",
955960
))
956961
}
957962
}
958963

959-
memoryPopulated := !r.Spec.Resources.Requests.Memory().IsZero() && !r.Spec.Resources.Limits.Memory().IsZero()
960-
if memoryPopulated {
961-
memoryRequestGtThanLimit := r.Spec.Resources.Requests.Memory().Cmp(*r.Spec.Resources.Limits.Memory()) > 0
964+
memoryRequest := r.Spec.Resources.Requests.Memory()
965+
rawSharedBuffer := r.Spec.PostgresConfiguration.Parameters[sharedBuffersParameter]
966+
if !memoryRequest.IsZero() && rawSharedBuffer != "" {
967+
if sharedBuffers, err := parsePostgresQuantityValue(rawSharedBuffer); err == nil {
968+
if memoryRequest.Cmp(sharedBuffers) < 0 {
969+
result = append(result, field.Invalid(
970+
field.NewPath("spec", "resources", "requests", "memory"),
971+
memoryRequest.String(),
972+
"Memory request is lower than PostgreSQL `shared_buffers` value",
973+
))
974+
}
975+
}
976+
}
977+
978+
memoryLimits := r.Spec.Resources.Limits.Memory()
979+
if !memoryRequest.IsZero() && !memoryLimits.IsZero() {
980+
memoryRequestGtThanLimit := memoryRequest.Cmp(*memoryLimits) > 0
962981
if memoryRequestGtThanLimit {
963982
result = append(result, field.Invalid(
964983
field.NewPath("spec", "resources", "requests", "memory"),
965-
r.Spec.Resources.Requests.Memory().String(),
984+
memoryRequest.String(),
966985
"Memory request is greater than the limit",
967986
))
968987
}
@@ -1009,6 +1028,21 @@ func (r *Cluster) validateConfiguration() field.ErrorList {
10091028
}
10101029
}
10111030

1031+
if value := r.Spec.PostgresConfiguration.Parameters[sharedBuffersParameter]; value != "" {
1032+
if _, err := parsePostgresQuantityValue(value); err != nil {
1033+
result = append(
1034+
result,
1035+
field.Invalid(
1036+
field.NewPath("spec", "postgresql", "parameters", sharedBuffersParameter),
1037+
sharedBuffersParameter,
1038+
fmt.Sprintf(
1039+
"Invalid value for configuration parameter %s. More info on accepted values format: "+
1040+
"https://www.postgresql.org/docs/current/config-setting.html#CONFIG-SETTING-NAMES-VALUES",
1041+
sharedBuffersParameter,
1042+
)))
1043+
}
1044+
}
1045+
10121046
// verify the postgres setting min_wal_size < max_wal_size < volume size
10131047
result = append(result, validateWalSizeConfiguration(
10141048
r.Spec.PostgresConfiguration, r.Spec.WalStorage.GetSizeOrNil())...)
@@ -1040,7 +1074,7 @@ func validateWalSizeConfiguration(
10401074
minWalSize = minWalSizeDefault
10411075
hasMinWalSize = false
10421076
}
1043-
minWalSizeValue, err := parseWalSettingValue(minWalSize)
1077+
minWalSizeValue, err := parsePostgresQuantityValue(minWalSize)
10441078
if err != nil {
10451079
result = append(
10461080
result,
@@ -1055,7 +1089,7 @@ func validateWalSizeConfiguration(
10551089
maxWalSize = maxWalSizeDefault
10561090
hasMaxWalSize = false
10571091
}
1058-
maxWalSizeValue, err := parseWalSettingValue(maxWalSize)
1092+
maxWalSizeValue, err := parsePostgresQuantityValue(maxWalSize)
10591093
if err != nil {
10601094
result = append(
10611095
result,
@@ -1107,10 +1141,10 @@ func validateWalSizeConfiguration(
11071141
return result
11081142
}
11091143

1110-
// parseWalSettingValue converts the WAL sizes in the PostgreSQL configuration
1144+
// parsePostgresQuantityValue converts the sizes in the PostgreSQL configuration
11111145
// into kubernetes resource.Quantity values
11121146
// Ref: Numeric with Unit @ https://www.postgresql.org/docs/current/config-setting.html#CONFIG-SETTING-NAMES-VALUES
1113-
func parseWalSettingValue(value string) (resource.Quantity, error) {
1147+
func parsePostgresQuantityValue(value string) (resource.Quantity, error) {
11141148
// If no suffix, default is MB
11151149
if _, err := strconv.Atoi(value); err == nil {
11161150
value += "MB"

‎api/v1/cluster_webhook_test.go‎

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -709,9 +709,9 @@ var _ = Describe("Image name validation", func() {
709709
})
710710
})
711711

712-
var _ = DescribeTable("parseWalSettingValue",
712+
var _ = DescribeTable("parsePostgresQuantityValue",
713713
func(value string, parsedValue resource.Quantity, expectError bool) {
714-
quantity, err := parseWalSettingValue(value)
714+
quantity, err := parsePostgresQuantityValue(value)
715715
if !expectError {
716716
Expect(quantity, err).Should(BeComparableTo(parsedValue))
717717
} else {
@@ -1052,6 +1052,20 @@ var _ = Describe("configuration change validation", func() {
10521052
}
10531053
Expect(clusterNew.validateConfiguration()).To(HaveLen(1))
10541054
})
1055+
1056+
It("should detect an invalid `shared_buffers` value", func() {
1057+
cluster := Cluster{
1058+
Spec: ClusterSpec{
1059+
PostgresConfiguration: PostgresConfiguration{
1060+
Parameters: map[string]string{
1061+
"shared_buffers": "invalid",
1062+
},
1063+
},
1064+
},
1065+
}
1066+
1067+
Expect(cluster.validateConfiguration()).To(HaveLen(1))
1068+
})
10551069
})
10561070

10571071
var _ = Describe("validate image name change", func() {
@@ -3157,6 +3171,9 @@ var _ = Describe("validateResources", func() {
31573171
BeforeEach(func() {
31583172
cluster = &Cluster{
31593173
Spec: ClusterSpec{
3174+
PostgresConfiguration: PostgresConfiguration{
3175+
Parameters: map[string]string{},
3176+
},
31603177
Resources: corev1.ResourceRequirements{
31613178
Requests: map[corev1.ResourceName]resource.Quantity{},
31623179
Limits: map[corev1.ResourceName]resource.Quantity{},
@@ -3228,4 +3245,34 @@ var _ = Describe("validateResources", func() {
32283245
errors := cluster.validateResources()
32293246
Expect(errors).To(BeEmpty())
32303247
})
3248+
3249+
It("returns an error when memoryRequest is less than shared_buffers in kB", func() {
3250+
cluster.Spec.Resources.Requests["memory"] = resource.MustParse("1Gi")
3251+
cluster.Spec.PostgresConfiguration.Parameters["shared_buffers"] = "2000000kB"
3252+
errors := cluster.validateResources()
3253+
Expect(errors).To(HaveLen(1))
3254+
Expect(errors[0].Detail).To(Equal("Memory request is lower than the shared_buffer value"))
3255+
})
3256+
3257+
It("returns an error when memoryRequest is less than shared_buffers in MB", func() {
3258+
cluster.Spec.Resources.Requests["memory"] = resource.MustParse("1000Mi")
3259+
cluster.Spec.PostgresConfiguration.Parameters["shared_buffers"] = "2000MB"
3260+
errors := cluster.validateResources()
3261+
Expect(errors).To(HaveLen(1))
3262+
Expect(errors[0].Detail).To(Equal("Memory request is lower than the shared_buffer value"))
3263+
})
3264+
3265+
It("returns no errors when memoryRequest is greater than or equal to shared_buffers in GB", func() {
3266+
cluster.Spec.Resources.Requests["memory"] = resource.MustParse("2Gi")
3267+
cluster.Spec.PostgresConfiguration.Parameters["shared_buffers"] = "1GB"
3268+
errors := cluster.validateResources()
3269+
Expect(errors).To(BeEmpty())
3270+
})
3271+
3272+
It("returns no errors when shared_buffers is in a format that can't be parsed", func() {
3273+
cluster.Spec.Resources.Requests["memory"] = resource.MustParse("1Gi")
3274+
cluster.Spec.PostgresConfiguration.Parameters["shared_buffers"] = "invalid_value"
3275+
errors := cluster.validateResources()
3276+
Expect(errors).To(BeEmpty())
3277+
})
32313278
})

‎tests/e2e/asserts_test.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2793,8 +2793,8 @@ func AssertClusterRollingRestart(namespace, clusterName string) {
27932793
// waiting for cluster phase to end up in "Upgrading cluster" state after restarting the cluster.
27942794
Eventually(func() (bool, error) {
27952795
cluster, err := env.GetCluster(namespace, clusterName)
2796-
return ( cluster.Status.Phase == apiv1.PhaseUpgrade ) ||
2797-
( cluster.Status.Phase == apiv1.PhaseWaitingForInstancesToBeActive ), err
2796+
return (cluster.Status.Phase == apiv1.PhaseUpgrade) ||
2797+
(cluster.Status.Phase == apiv1.PhaseWaitingForInstancesToBeActive), err
27982798
}, 120, 3).Should(BeTrue())
27992799
})
28002800
AssertClusterIsReady(namespace, clusterName, testTimeouts[testsUtils.ClusterIsReadyQuick], env)

0 commit comments

Comments
 (0)