@@ -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.
6266var clusterLog = log .WithName ("cluster-resource" ).WithValues ("version" , "v1" )
6367
@@ -944,25 +948,40 @@ func (r *Cluster) validateImagePullPolicy() field.ErrorList {
944948func (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"
0 commit comments