@@ -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,71 @@ 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 cert-manager by default
897+ certFile := ensurePath (spec .TLS .CertificateFile , mountPath , "tls.crt" )
898+ privateKeyFile := ensurePath (spec .TLS .PrivateKeyFile , mountPath , "tls.key" )
899+ caFile := ensurePath (spec .TLS .CAFile , mountPath , "ca.crt" )
900+
901+ spiloEnvVars = append (
902+ spiloEnvVars ,
903+ v1.EnvVar {Name : "SSL_CERTIFICATE_FILE" , Value : certFile },
904+ v1.EnvVar {Name : "SSL_PRIVATE_KEY_FILE" , Value : privateKeyFile },
905+ v1.EnvVar {Name : "SSL_CA_FILE" , Value : caFile },
906+ )
907+ }
908+
852909 // generate the spilo container
853910 c .logger .Debugf ("Generating Spilo container, environment variables: %v" , spiloEnvVars )
854911 spiloContainer := generateContainer (c .containerName (),
855912 & effectiveDockerImage ,
856913 resourceRequirements ,
857- spiloEnvVars ,
914+ deduplicateEnvVars ( spiloEnvVars , c . containerName (), c . logger ) ,
858915 volumeMounts ,
859916 c .OpConfig .Resources .SpiloPrivileged ,
860917 )
@@ -893,16 +950,10 @@ func (c *Cluster) generateStatefulSet(spec *acidv1.PostgresSpec) (*appsv1.Statef
893950 tolerationSpec := tolerations (& spec .Tolerations , c .OpConfig .PodToleration )
894951 effectivePodPriorityClassName := util .Coalesce (spec .PodPriorityClassName , c .OpConfig .PodPriorityClassName )
895952
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-
902953 annotations := c .generatePodAnnotations (spec )
903954
904955 // generate pod template for the statefulset, based on the spilo container and sidecars
905- if podTemplate , err = generatePodTemplate (
956+ podTemplate , err = generatePodTemplate (
906957 c .Namespace ,
907958 c .labelsSet (true ),
908959 annotations ,
@@ -920,10 +971,9 @@ func (c *Cluster) generateStatefulSet(spec *acidv1.PostgresSpec) (*appsv1.Statef
920971 c .OpConfig .EnablePodAntiAffinity ,
921972 c .OpConfig .PodAntiAffinityTopologyKey ,
922973 c .OpConfig .AdditionalSecretMount ,
923- c .OpConfig .AdditionalSecretMountPath ); err != nil {
924- return nil , fmt .Errorf ("could not generate pod template: %v" , err )
925- }
926-
974+ c .OpConfig .AdditionalSecretMountPath ,
975+ volumes ,
976+ )
927977 if err != nil {
928978 return nil , fmt .Errorf ("could not generate pod template: %v" , err )
929979 }
@@ -1523,7 +1573,8 @@ func (c *Cluster) generateLogicalBackupJob() (*batchv1beta1.CronJob, error) {
15231573 false ,
15241574 "" ,
15251575 c .OpConfig .AdditionalSecretMount ,
1526- c .OpConfig .AdditionalSecretMountPath ); err != nil {
1576+ c .OpConfig .AdditionalSecretMountPath ,
1577+ nil ); err != nil {
15271578 return nil , fmt .Errorf ("could not generate pod template for logical backup pod: %v" , err )
15281579 }
15291580
@@ -1651,3 +1702,13 @@ func (c *Cluster) generateLogicalBackupPodEnvVars() []v1.EnvVar {
16511702func (c * Cluster ) getLogicalBackupJobName () (jobName string ) {
16521703 return "logical-backup-" + c .clusterName ().Name
16531704}
1705+
1706+ func ensurePath (file string , defaultDir string , defaultFile string ) string {
1707+ if file == "" {
1708+ return path .Join (defaultDir , defaultFile )
1709+ }
1710+ if ! path .IsAbs (file ) {
1711+ return path .Join (defaultDir , file )
1712+ }
1713+ return file
1714+ }
0 commit comments