Skip to content

Commit 024aab1

Browse files
authored
Add config switch to share pg_socket in /var/run/postgresql via an emptyDir with the sidecar containers (zalando#962)
1 parent a6a49fa commit 024aab1

File tree

9 files changed

+58
-1
lines changed

9 files changed

+58
-1
lines changed

docs/reference/operator_parameters.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,12 @@ configuration they are grouped under the `kubernetes` key.
344344
to run alongside Spilo on the same pod. Globally defined sidecars are always
345345
enabled. Default is true.
346346

347+
* **share_pg_socket_with_sidecars**
348+
global option to create an emptyDir volume named `postgresql-run`. This is
349+
mounted by all containers at `/var/run/postgresql` sharing the unix socket of
350+
PostgreSQL (`pg_socket`) with the sidecars this way.
351+
Default is `false`.
352+
347353
* **secret_name_template**
348354
a template for the name of the database user secrets generated by the
349355
operator. `{namespace}` is replaced with name of the namespace if

docs/user.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1006,6 +1006,14 @@ option must be set to `true`.
10061006

10071007
If you want to add a sidecar to every cluster managed by the operator, you can specify it in the [operator configuration](administrator.md#sidecars-for-postgres-clusters) instead.
10081008

1009+
### Accessing the PostgreSQL socket from sidecars
1010+
1011+
If enabled by the `share_pg_socket_with_sidecars` option in the operator
1012+
configuration the PostgreSQL socket is placed in a volume of type
1013+
`emptyDir` named `postgresql-run`.
1014+
To allow access to the socket from any sidecar container simply add a
1015+
VolumeMount to this volume to your sidecar spec.
1016+
10091017
## InitContainers Support
10101018

10111019
Each cluster can specify arbitrary init containers to run. These containers can
@@ -1049,7 +1057,7 @@ When using AWS with gp3 volumes you should set the mode to `mixed` because it
10491057
will also adjust the IOPS and throughput that can be defined in the manifest.
10501058
Check the [AWS docs](https://aws.amazon.com/ebs/general-purpose/) to learn
10511059
about default and maximum values. Keep in mind that AWS rate-limits updating
1052-
volume specs to no more than once every 6 hours.
1060+
volume specs to no more than once every 6 hours.
10531061

10541062
```yaml
10551063
spec:

manifests/operatorconfiguration.crd.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,9 @@ spec:
222222
type: array
223223
items:
224224
type: string
225+
share_pg_socket_with_sidecars:
226+
type: boolean
227+
default: false
225228
infrastructure_roles_secret_name:
226229
type: string
227230
infrastructure_roles_secrets:

pkg/apis/acid.zalan.do/v1/crds.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,6 +1289,9 @@ var OperatorConfigCRDResourceValidation = apiextv1.CustomResourceValidation{
12891289
},
12901290
},
12911291
},
1292+
"share_pg_socket_with_sidecars": {
1293+
Type: "boolean",
1294+
},
12921295
"infrastructure_roles_secret_name": {
12931296
Type: "string",
12941297
},

pkg/apis/acid.zalan.do/v1/operator_configuration_type.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ type KubernetesMetaConfiguration struct {
7272
StorageResizeMode string `json:"storage_resize_mode,omitempty"`
7373
EnableInitContainers *bool `json:"enable_init_containers,omitempty"`
7474
EnableSidecars *bool `json:"enable_sidecars,omitempty"`
75+
SharePGSocketWithSidecars *bool `json:"share_pgsocket_with_sidecars,omitempty"`
7576
SecretNameTemplate config.StringTemplate `json:"secret_name_template,omitempty"`
7677
ClusterDomain string `json:"cluster_domain,omitempty"`
7778
OAuthTokenSecretName spec.NamespacedName `json:"oauth_token_secret_name,omitempty"`

pkg/apis/acid.zalan.do/v1/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/cluster/k8sres.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,7 @@ func (c *Cluster) generatePodTemplate(
713713
spiloContainer *v1.Container,
714714
initContainers []v1.Container,
715715
sidecarContainers []v1.Container,
716+
sharePGSocketWithSidecars *bool,
716717
tolerationsSpec *[]v1.Toleration,
717718
spiloRunAsUser *int64,
718719
spiloRunAsGroup *int64,
@@ -775,6 +776,10 @@ func (c *Cluster) generatePodTemplate(
775776
podSpec.PriorityClassName = priorityClassName
776777
}
777778

779+
if sharePGSocketWithSidecars != nil && *sharePGSocketWithSidecars {
780+
addVarRunVolume(&podSpec)
781+
}
782+
778783
if additionalSecretMount != "" {
779784
addSecretVolume(&podSpec, additionalSecretMount, additionalSecretMountPath)
780785
}
@@ -1357,6 +1362,7 @@ func (c *Cluster) generateStatefulSet(spec *acidv1.PostgresSpec) (*appsv1.Statef
13571362
spiloContainer,
13581363
initContainers,
13591364
sidecarContainers,
1365+
c.OpConfig.SharePGSocketWithSidecars,
13601366
&tolerationSpec,
13611367
effectiveRunAsUser,
13621368
effectiveRunAsGroup,
@@ -1550,6 +1556,28 @@ func addShmVolume(podSpec *v1.PodSpec) {
15501556
podSpec.Volumes = volumes
15511557
}
15521558

1559+
func addVarRunVolume(podSpec *v1.PodSpec) {
1560+
volumes := append(podSpec.Volumes, v1.Volume{
1561+
Name: "postgresql-run",
1562+
VolumeSource: v1.VolumeSource{
1563+
EmptyDir: &v1.EmptyDirVolumeSource{
1564+
Medium: "Memory",
1565+
},
1566+
},
1567+
})
1568+
1569+
for i := range podSpec.Containers {
1570+
mounts := append(podSpec.Containers[i].VolumeMounts,
1571+
v1.VolumeMount{
1572+
Name: "postgresql-run",
1573+
MountPath: "/var/run/postgresql",
1574+
})
1575+
podSpec.Containers[i].VolumeMounts = mounts
1576+
}
1577+
1578+
podSpec.Volumes = volumes
1579+
}
1580+
15531581
func addSecretVolume(podSpec *v1.PodSpec, additionalSecretMount string, additionalSecretMountPath string) {
15541582
volumes := append(podSpec.Volumes, v1.Volume{
15551583
Name: additionalSecretMount,
@@ -2080,6 +2108,7 @@ func (c *Cluster) generateLogicalBackupJob() (*batchv1.CronJob, error) {
20802108
logicalBackupContainer,
20812109
[]v1.Container{},
20822110
[]v1.Container{},
2111+
util.False(),
20832112
&[]v1.Toleration{},
20842113
nil,
20852114
nil,

pkg/controller/operator_config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ func (c *Controller) importConfigurationFromCRD(fromCRD *acidv1.OperatorConfigur
8686
result.StorageResizeMode = util.Coalesce(fromCRD.Kubernetes.StorageResizeMode, "pvc")
8787
result.EnableInitContainers = util.CoalesceBool(fromCRD.Kubernetes.EnableInitContainers, util.True())
8888
result.EnableSidecars = util.CoalesceBool(fromCRD.Kubernetes.EnableSidecars, util.True())
89+
result.SharePGSocketWithSidecars = util.CoalesceBool(fromCRD.Kubernetes.SharePGSocketWithSidecars, util.False())
8990
result.SecretNameTemplate = fromCRD.Kubernetes.SecretNameTemplate
9091
result.OAuthTokenSecretName = fromCRD.Kubernetes.OAuthTokenSecretName
9192
result.EnableCrossNamespaceSecret = fromCRD.Kubernetes.EnableCrossNamespaceSecret

pkg/util/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ type Config struct {
212212
EnablePodDisruptionBudget *bool `name:"enable_pod_disruption_budget" default:"true"`
213213
EnableInitContainers *bool `name:"enable_init_containers" default:"true"`
214214
EnableSidecars *bool `name:"enable_sidecars" default:"true"`
215+
SharePGSocketWithSidecars *bool `name:"share_pg_socket_with_sidecars" default:"false"`
215216
Workers uint32 `name:"workers" default:"8"`
216217
APIPort int `name:"api_port" default:"8080"`
217218
RingLogLines int `name:"ring_log_lines" default:"100"`

0 commit comments

Comments
 (0)