@@ -679,6 +679,7 @@ func generateContainer(
679679 volumeMounts []v1.VolumeMount ,
680680 privilegedMode bool ,
681681 privilegeEscalationMode * bool ,
682+ readOnlyRootFilesystem * bool ,
682683 additionalPodCapabilities * v1.Capabilities ,
683684) * v1.Container {
684685 return & v1.Container {
@@ -705,7 +706,7 @@ func generateContainer(
705706 SecurityContext : & v1.SecurityContext {
706707 AllowPrivilegeEscalation : privilegeEscalationMode ,
707708 Privileged : & privilegedMode ,
708- ReadOnlyRootFilesystem : util . False () ,
709+ ReadOnlyRootFilesystem : readOnlyRootFilesystem ,
709710 Capabilities : additionalPodCapabilities ,
710711 },
711712 }
@@ -738,7 +739,7 @@ func (c *Cluster) generateSidecarContainers(sidecars []cpov1.Sidecar,
738739}
739740
740741// adds common fields to sidecars
741- func patchSidecarContainers (in []v1.Container , volumeMounts []v1.VolumeMount , superUserName string , credentialsSecretName string , logger * logrus.Entry ) []v1.Container {
742+ func patchSidecarContainers (in []v1.Container , volumeMounts []v1.VolumeMount , superUserName string , credentialsSecretName string , logger * logrus.Entry , privilegedMode bool , privilegeEscalationMode * bool , additionalPodCapabilities * v1. Capabilities ) []v1.Container {
742743 result := []v1.Container {}
743744
744745 for _ , container := range in {
@@ -779,6 +780,7 @@ func patchSidecarContainers(in []v1.Container, volumeMounts []v1.VolumeMount, su
779780 },
780781 }
781782 container .Env = appendEnvVars (env , container .Env ... )
783+
782784 result = append (result , container )
783785 }
784786
@@ -875,6 +877,15 @@ func (c *Cluster) generatePodTemplate(
875877 podSpec .PriorityClassName = priorityClassName
876878 }
877879
880+ if c .Postgresql .Spec .Monitoring != nil {
881+ addEmptyDirVolume (& podSpec , "exporter-tmp" , "postgres-exporter" , "/tmp" )
882+ }
883+
884+ if c .OpConfig .ReadOnlyRootFilesystem != nil && * c .OpConfig .ReadOnlyRootFilesystem {
885+ addRunVolume (& podSpec , "postgres-run" , "postgres" , "/run" )
886+ addEmptyDirVolume (& podSpec , "postgres-tmp" , "postgres" , "/tmp" )
887+ }
888+
878889 if sharePgSocketWithSidecars != nil && * sharePgSocketWithSidecars {
879890 addVarRunVolume (& podSpec )
880891 }
@@ -990,6 +1001,19 @@ func (c *Cluster) generateSpiloPodEnvVars(
9901001 Name : "HUMAN_ROLE" ,
9911002 Value : c .OpConfig .PamRoleName ,
9921003 },
1004+ // NSS WRAPPER
1005+ {
1006+ Name : "LD_PRELOAD" ,
1007+ Value : "/usr/lib64/libnss_wrapper.so" ,
1008+ },
1009+ {
1010+ Name : "NSS_WRAPPER_PASSWD" ,
1011+ Value : "/tmp/nss_wrapper/passwd" ,
1012+ },
1013+ {
1014+ Name : "NSS_WRAPPER_GROUP" ,
1015+ Value : "/tmp/nss_wrapper/group" ,
1016+ },
9931017 }
9941018
9951019 if c .OpConfig .EnableSpiloWalPathCompat {
@@ -1245,6 +1269,8 @@ func getSidecarContainer(sidecar cpov1.Sidecar, index int, resources *v1.Resourc
12451269 Resources : * resources ,
12461270 Env : sidecar .Env ,
12471271 Ports : sidecar .Ports ,
1272+ SecurityContext : sidecar .SecurityContext ,
1273+ VolumeMounts : sidecar .VolumeMounts ,
12481274 }
12491275}
12501276
@@ -1294,6 +1320,23 @@ func generateSpiloReadinessProbe() *v1.Probe {
12941320 }
12951321}
12961322
1323+ func generatePatroniLivenessProbe () * v1.Probe {
1324+ return & v1.Probe {
1325+ FailureThreshold : 6 ,
1326+ ProbeHandler : v1.ProbeHandler {
1327+ HTTPGet : & v1.HTTPGetAction {
1328+ Path : "/liveness" ,
1329+ Port : intstr.IntOrString {IntVal : patroni .ApiPort },
1330+ Scheme : v1 .URISchemeHTTP ,
1331+ },
1332+ },
1333+ InitialDelaySeconds : 30 ,
1334+ PeriodSeconds : 10 ,
1335+ TimeoutSeconds : 5 ,
1336+ SuccessThreshold : 1 ,
1337+ }
1338+ }
1339+
12971340func (c * Cluster ) generateStatefulSet (spec * cpov1.PostgresSpec ) (* appsv1.StatefulSet , error ) {
12981341
12991342 var (
@@ -1424,6 +1467,7 @@ func (c *Cluster) generateStatefulSet(spec *cpov1.PostgresSpec) (*appsv1.Statefu
14241467 }
14251468 additionalVolumes = append (additionalVolumes , tlsVolumes ... )
14261469 }
1470+
14271471 repo_host_mode := false
14281472 // Add this envVar so that it is not added to the pgbackrest initcontainer
14291473 if specHasPgbackrestPVCRepo (spec ) {
@@ -1448,13 +1492,18 @@ func (c *Cluster) generateStatefulSet(spec *cpov1.PostgresSpec) (*appsv1.Statefu
14481492 volumeMounts ,
14491493 c .OpConfig .Resources .SpiloPrivileged ,
14501494 c .OpConfig .Resources .SpiloAllowPrivilegeEscalation ,
1495+ c .OpConfig .Resources .ReadOnlyRootFilesystem ,
14511496 generateCapabilities (c .OpConfig .AdditionalPodCapabilities ),
14521497 )
14531498
14541499 // Patroni responds 200 to probe only if it either owns the leader lock or postgres is running and DCS is accessible
14551500 if c .OpConfig .EnableReadinessProbe {
14561501 spiloContainer .ReadinessProbe = generateSpiloReadinessProbe ()
14571502 }
1503+ //
1504+ if c .OpConfig .EnableLivenessProbe {
1505+ spiloContainer .LivenessProbe = generatePatroniLivenessProbe ()
1506+ }
14581507
14591508 // generate container specs for sidecars specified in the cluster manifest
14601509 clusterSpecificSidecars := []v1.Container {}
@@ -1510,7 +1559,7 @@ func (c *Cluster) generateStatefulSet(spec *cpov1.PostgresSpec) (*appsv1.Statefu
15101559 containerName , containerName )
15111560 }
15121561
1513- sidecarContainers = patchSidecarContainers (sidecarContainers , volumeMounts , c .OpConfig .SuperUsername , c .credentialSecretName (c .OpConfig .SuperUsername ), c .logger )
1562+ sidecarContainers = patchSidecarContainers (sidecarContainers , volumeMounts , c .OpConfig .SuperUsername , c .credentialSecretName (c .OpConfig .SuperUsername ), c .logger , c . OpConfig . Resources . SpiloPrivileged , c . OpConfig . Resources . SpiloAllowPrivilegeEscalation , generateCapabilities ( c . OpConfig . AdditionalPodCapabilities ) )
15141563
15151564 tolerationSpec := tolerations (& spec .Tolerations , c .OpConfig .PodToleration )
15161565 topologySpreadConstraintsSpec := topologySpreadConstraints (& spec .TopologySpreadConstraints )
@@ -1519,7 +1568,7 @@ func (c *Cluster) generateStatefulSet(spec *cpov1.PostgresSpec) (*appsv1.Statefu
15191568 podAnnotations := c .generatePodAnnotations (spec )
15201569
15211570 if spec .GetBackup ().Pgbackrest != nil {
1522- initContainers = append (initContainers , c .generatePgbackrestRestoreContainer (spec , repo_host_mode , volumeMounts , resourceRequirements ))
1571+ initContainers = append (initContainers , c .generatePgbackrestRestoreContainer (spec , repo_host_mode , volumeMounts , resourceRequirements , c . OpConfig . Resources . SpiloPrivileged , c . OpConfig . Resources . SpiloAllowPrivilegeEscalation , generateCapabilities ( c . OpConfig . AdditionalPodCapabilities ) ))
15231572
15241573 additionalVolumes = append (additionalVolumes , c .generatePgbackrestConfigVolume (spec .Backup .Pgbackrest , false ))
15251574
@@ -1622,7 +1671,7 @@ func (c *Cluster) generateStatefulSet(spec *cpov1.PostgresSpec) (*appsv1.Statefu
16221671 return statefulSet , nil
16231672}
16241673
1625- func (c * Cluster ) generatePgbackrestRestoreContainer (spec * cpov1.PostgresSpec , repo_host_mode bool , volumeMounts []v1.VolumeMount , resourceRequirements * v1.ResourceRequirements ) v1.Container {
1674+ func (c * Cluster ) generatePgbackrestRestoreContainer (spec * cpov1.PostgresSpec , repo_host_mode bool , volumeMounts []v1.VolumeMount , resourceRequirements * v1.ResourceRequirements , privilegedMode bool , privilegeEscalationMode * bool , additionalPodCapabilities * v1. Capabilities ) v1.Container {
16261675 isOptional := true
16271676 pgbackrestRestoreEnvVars := []v1.EnvVar {
16281677 {
@@ -1702,6 +1751,12 @@ func (c *Cluster) generatePgbackrestRestoreContainer(spec *cpov1.PostgresSpec, r
17021751 Env : pgbackrestRestoreEnvVars ,
17031752 VolumeMounts : volumeMounts ,
17041753 Resources : * resourceRequirements ,
1754+ SecurityContext : & v1.SecurityContext {
1755+ AllowPrivilegeEscalation : privilegeEscalationMode ,
1756+ Privileged : & privilegedMode ,
1757+ ReadOnlyRootFilesystem : util .True (),
1758+ Capabilities : additionalPodCapabilities ,
1759+ },
17051760 }
17061761}
17071762
@@ -1760,6 +1815,7 @@ func (c *Cluster) generateRepoHostStatefulSet(spec *cpov1.PostgresSpec) (*appsv1
17601815 volumeMounts ,
17611816 c .OpConfig .Resources .SpiloPrivileged ,
17621817 c .OpConfig .Resources .SpiloAllowPrivilegeEscalation ,
1818+ c .OpConfig .Resources .ReadOnlyRootFilesystem ,
17631819 generateCapabilities (c .OpConfig .AdditionalPodCapabilities ),
17641820 )
17651821
@@ -2163,6 +2219,48 @@ func addShmVolume(podSpec *v1.PodSpec) {
21632219 podSpec .Volumes = volumes
21642220}
21652221
2222+ func addEmptyDirVolume (podSpec * v1.PodSpec , volumeName string , containerName string , path string ) {
2223+ vol := v1.Volume {
2224+ Name : volumeName ,
2225+ VolumeSource : v1.VolumeSource {
2226+ EmptyDir : & v1.EmptyDirVolumeSource {},
2227+ },
2228+ }
2229+ podSpec .Volumes = append (podSpec .Volumes , vol )
2230+
2231+ mount := v1.VolumeMount {
2232+ Name : vol .Name ,
2233+ MountPath : path ,
2234+ }
2235+
2236+ for i := range podSpec .Containers {
2237+ if podSpec .Containers [i ].Name == containerName {
2238+ podSpec .Containers [i ].VolumeMounts = append (podSpec .Containers [i ].VolumeMounts , mount )
2239+ }
2240+ }
2241+ }
2242+
2243+ func addRunVolume (podSpec * v1.PodSpec , volumeName string , containerName string , path string ) {
2244+ vol := v1.Volume {
2245+ Name : volumeName ,
2246+ VolumeSource : v1.VolumeSource {
2247+ EmptyDir : & v1.EmptyDirVolumeSource {},
2248+ },
2249+ }
2250+ podSpec .Volumes = append (podSpec .Volumes , vol )
2251+
2252+ mount := v1.VolumeMount {
2253+ Name : vol .Name ,
2254+ MountPath : path ,
2255+ }
2256+
2257+ for i := range podSpec .Containers {
2258+ if podSpec .Containers [i ].Name == containerName {
2259+ podSpec .Containers [i ].VolumeMounts = append (podSpec .Containers [i ].VolumeMounts , mount )
2260+ }
2261+ }
2262+ }
2263+
21662264func addVarRunVolume (podSpec * v1.PodSpec ) {
21672265 volumes := append (podSpec .Volumes , v1.Volume {
21682266 Name : "postgresql-run" ,
@@ -2730,6 +2828,7 @@ func (c *Cluster) generateLogicalBackupJob() (*batchv1.CronJob, error) {
27302828 []v1.VolumeMount {},
27312829 c .OpConfig .SpiloPrivileged , // use same value as for normal DB pods
27322830 c .OpConfig .SpiloAllowPrivilegeEscalation ,
2831+ util .False (),
27332832 nil ,
27342833 )
27352834
@@ -3256,9 +3355,13 @@ func (c *Cluster) generatePgbackrestJob(backup *cpov1.Pgbackrest, repo *cpov1.Re
32563355 []v1.VolumeMount {},
32573356 c .OpConfig .SpiloPrivileged , // use same value as for normal DB pods
32583357 c .OpConfig .SpiloAllowPrivilegeEscalation ,
3358+ c .OpConfig .Resources .ReadOnlyRootFilesystem ,
32593359 nil ,
32603360 )
32613361
3362+ // Patch securityContext - readOnlyRootFilesystem
3363+ pgbackrestContainer .SecurityContext .ReadOnlyRootFilesystem = util .True ()
3364+
32623365 podAffinityTerm := v1.PodAffinityTerm {
32633366 LabelSelector : c .roleLabelsSelector (Master ),
32643367 TopologyKey : "kubernetes.io/hostname" ,
0 commit comments