@@ -3,6 +3,7 @@ package cluster
33import (
44 "encoding/json"
55 "fmt"
6+ "path"
67 "sort"
78
89 "github.com/sirupsen/logrus"
@@ -30,7 +31,10 @@ const (
3031 patroniPGBinariesParameterName = "bin_dir"
3132 patroniPGParametersParameterName = "parameters"
3233 patroniPGHBAConfParameterName = "pg_hba"
33- localHost = "127.0.0.1/32"
34+
35+ // the gid of the postgres user in the default spilo image
36+ spiloPostgresGID = 103
37+ localHost = "127.0.0.1/32"
3438)
3539
3640type pgUser struct {
@@ -446,6 +450,7 @@ func generatePodTemplate(
446450 podAntiAffinityTopologyKey string ,
447451 additionalSecretMount string ,
448452 additionalSecretMountPath string ,
453+ volumes []v1.Volume ,
449454) (* v1.PodTemplateSpec , error ) {
450455
451456 terminateGracePeriodSeconds := terminateGracePeriod
@@ -464,6 +469,7 @@ func generatePodTemplate(
464469 InitContainers : initContainers ,
465470 Tolerations : * tolerationsSpec ,
466471 SecurityContext : & securityContext ,
472+ Volumes : volumes ,
467473 }
468474
469475 if shmVolume != nil && * shmVolume {
@@ -724,6 +730,7 @@ func (c *Cluster) generateStatefulSet(spec *acidv1.PostgresSpec) (*appsv1.Statef
724730 sidecarContainers []v1.Container
725731 podTemplate * v1.PodTemplateSpec
726732 volumeClaimTemplate * v1.PersistentVolumeClaim
733+ volumes []v1.Volume
727734 )
728735
729736 // Improve me. Please.
@@ -840,21 +847,76 @@ func (c *Cluster) generateStatefulSet(spec *acidv1.PostgresSpec) (*appsv1.Statef
840847 }
841848
842849 // generate environment variables for the spilo container
843- spiloEnvVars := deduplicateEnvVars (
844- c .generateSpiloPodEnvVars (c .Postgresql .GetUID (), spiloConfiguration , & spec .Clone ,
845- spec .StandbyCluster , customPodEnvVarsList ), c .containerName (), c .logger )
850+ spiloEnvVars := c .generateSpiloPodEnvVars (
851+ c .Postgresql .GetUID (),
852+ spiloConfiguration ,
853+ & spec .Clone ,
854+ spec .StandbyCluster ,
855+ customPodEnvVarsList ,
856+ )
846857
847858 // pickup the docker image for the spilo container
848859 effectiveDockerImage := util .Coalesce (spec .DockerImage , c .OpConfig .DockerImage )
849860
861+ // determine the FSGroup for the spilo pod
862+ effectiveFSGroup := c .OpConfig .Resources .SpiloFSGroup
863+ if spec .SpiloFSGroup != nil {
864+ effectiveFSGroup = spec .SpiloFSGroup
865+ }
866+
850867 volumeMounts := generateVolumeMounts (spec .Volume )
851868
869+ // configure TLS with a custom secret volume
870+ if spec .TLS != nil && spec .TLS .SecretName != "" {
871+ if effectiveFSGroup == nil {
872+ c .logger .Warnf ("Setting the default FSGroup to satisfy the TLS configuration" )
873+ fsGroup := int64 (spiloPostgresGID )
874+ effectiveFSGroup = & fsGroup
875+ }
876+ // this is combined with the FSGroup above to give read access to the
877+ // postgres user
878+ defaultMode := int32 (0640 )
879+ volumes = append (volumes , v1.Volume {
880+ Name : "tls-secret" ,
881+ VolumeSource : v1.VolumeSource {
882+ Secret : & v1.SecretVolumeSource {
883+ SecretName : spec .TLS .SecretName ,
884+ DefaultMode : & defaultMode ,
885+ },
886+ },
887+ })
888+
889+ mountPath := "/tls"
890+ volumeMounts = append (volumeMounts , v1.VolumeMount {
891+ MountPath : mountPath ,
892+ Name : "tls-secret" ,
893+ ReadOnly : true ,
894+ })
895+
896+ // use the same filenames as Secret resources by default
897+ certFile := ensurePath (spec .TLS .CertificateFile , mountPath , "tls.crt" )
898+ privateKeyFile := ensurePath (spec .TLS .PrivateKeyFile , mountPath , "tls.key" )
899+ spiloEnvVars = append (
900+ spiloEnvVars ,
901+ v1.EnvVar {Name : "SSL_CERTIFICATE_FILE" , Value : certFile },
902+ v1.EnvVar {Name : "SSL_PRIVATE_KEY_FILE" , Value : privateKeyFile },
903+ )
904+
905+ if spec .TLS .CAFile != "" {
906+ caFile := ensurePath (spec .TLS .CAFile , mountPath , "" )
907+ spiloEnvVars = append (
908+ spiloEnvVars ,
909+ v1.EnvVar {Name : "SSL_CA_FILE" , Value : caFile },
910+ )
911+ }
912+ }
913+
852914 // generate the spilo container
853915 c .logger .Debugf ("Generating Spilo container, environment variables: %v" , spiloEnvVars )
854916 spiloContainer := generateContainer (c .containerName (),
855917 & effectiveDockerImage ,
856918 resourceRequirements ,
857- spiloEnvVars ,
919+ deduplicateEnvVars ( spiloEnvVars , c . containerName (), c . logger ) ,
858920 volumeMounts ,
859921 c .OpConfig .Resources .SpiloPrivileged ,
860922 )
@@ -893,16 +955,10 @@ func (c *Cluster) generateStatefulSet(spec *acidv1.PostgresSpec) (*appsv1.Statef
893955 tolerationSpec := tolerations (& spec .Tolerations , c .OpConfig .PodToleration )
894956 effectivePodPriorityClassName := util .Coalesce (spec .PodPriorityClassName , c .OpConfig .PodPriorityClassName )
895957
896- // determine the FSGroup for the spilo pod
897- effectiveFSGroup := c .OpConfig .Resources .SpiloFSGroup
898- if spec .SpiloFSGroup != nil {
899- effectiveFSGroup = spec .SpiloFSGroup
900- }
901-
902958 annotations := c .generatePodAnnotations (spec )
903959
904960 // generate pod template for the statefulset, based on the spilo container and sidecars
905- if podTemplate , err = generatePodTemplate (
961+ podTemplate , err = generatePodTemplate (
906962 c .Namespace ,
907963 c .labelsSet (true ),
908964 annotations ,
@@ -920,10 +976,9 @@ func (c *Cluster) generateStatefulSet(spec *acidv1.PostgresSpec) (*appsv1.Statef
920976 c .OpConfig .EnablePodAntiAffinity ,
921977 c .OpConfig .PodAntiAffinityTopologyKey ,
922978 c .OpConfig .AdditionalSecretMount ,
923- c .OpConfig .AdditionalSecretMountPath ); err != nil {
924- return nil , fmt .Errorf ("could not generate pod template: %v" , err )
925- }
926-
979+ c .OpConfig .AdditionalSecretMountPath ,
980+ volumes ,
981+ )
927982 if err != nil {
928983 return nil , fmt .Errorf ("could not generate pod template: %v" , err )
929984 }
@@ -1539,7 +1594,8 @@ func (c *Cluster) generateLogicalBackupJob() (*batchv1beta1.CronJob, error) {
15391594 false ,
15401595 "" ,
15411596 c .OpConfig .AdditionalSecretMount ,
1542- c .OpConfig .AdditionalSecretMountPath ); err != nil {
1597+ c .OpConfig .AdditionalSecretMountPath ,
1598+ nil ); err != nil {
15431599 return nil , fmt .Errorf ("could not generate pod template for logical backup pod: %v" , err )
15441600 }
15451601
@@ -1671,3 +1727,13 @@ func (c *Cluster) generateLogicalBackupPodEnvVars() []v1.EnvVar {
16711727func (c * Cluster ) getLogicalBackupJobName () (jobName string ) {
16721728 return "logical-backup-" + c .clusterName ().Name
16731729}
1730+
1731+ func ensurePath (file string , defaultDir string , defaultFile string ) string {
1732+ if file == "" {
1733+ return path .Join (defaultDir , defaultFile )
1734+ }
1735+ if ! path .IsAbs (file ) {
1736+ return path .Join (defaultDir , file )
1737+ }
1738+ return file
1739+ }
0 commit comments