Skip to content

Commit 1009173

Browse files
committed
Add config switch to share pg_socket in /var/run/postgresql via an emptyDir with the sidecar containers
1 parent 8959618 commit 1009173

File tree

8 files changed

+114
-0
lines changed

8 files changed

+114
-0
lines changed

docs/reference/operator_parameters.md

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

285+
* **share_pg_socket_with_sidecars**
286+
global option to create an emptyDir volume named `postgresql-run`. This is
287+
mounted by all containers at `/var/run/postgresql` sharing the unix socket of
288+
PostgreSQL (`pg_socket`) with the sidecars this way.
289+
Default is `false`.
290+
285291
* **secret_name_template**
286292
a template for the name of the database user secrets generated by the
287293
operator. `{namespace}` is replaced with name of the namespace if

manifests/operatorconfiguration.crd.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ spec:
182182
enable_sidecars:
183183
type: boolean
184184
default: true
185+
share_pg_socket_with_sidecars:
186+
type: boolean
187+
default: false
185188
infrastructure_roles_secret_name:
186189
type: string
187190
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
@@ -1099,6 +1099,9 @@ var OperatorConfigCRDResourceValidation = apiextv1.CustomResourceValidation{
10991099
"enable_sidecars": {
11001100
Type: "boolean",
11011101
},
1102+
"share_pg_socket_with_sidecars": {
1103+
Type: "boolean",
1104+
},
11021105
"infrastructure_roles_secret_name": {
11031106
Type: "string",
11041107
},

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ type KubernetesMetaConfiguration struct {
6868
StorageResizeMode string `json:"storage_resize_mode,omitempty"`
6969
EnableInitContainers *bool `json:"enable_init_containers,omitempty"`
7070
EnableSidecars *bool `json:"enable_sidecars,omitempty"`
71+
SharePGSocketWithSidecars *bool `json:"share_pgsocket_with_sidecars,omitempty"`
7172
SecretNameTemplate config.StringTemplate `json:"secret_name_template,omitempty"`
7273
ClusterDomain string `json:"cluster_domain,omitempty"`
7374
OAuthTokenSecretName spec.NamespacedName `json:"oauth_token_secret_name,omitempty"`

pkg/cluster/k8sres.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,7 @@ func (c *Cluster) generatePodTemplate(
584584
spiloContainer *v1.Container,
585585
initContainers []v1.Container,
586586
sidecarContainers []v1.Container,
587+
sharePGSocketWithSidecars *bool,
587588
tolerationsSpec *[]v1.Toleration,
588589
spiloRunAsUser *int64,
589590
spiloRunAsGroup *int64,
@@ -646,6 +647,10 @@ func (c *Cluster) generatePodTemplate(
646647
podSpec.PriorityClassName = priorityClassName
647648
}
648649

650+
if sharePGSocketWithSidecars != nil && *sharePGSocketWithSidecars {
651+
addVarRunVolume(&podSpec)
652+
}
653+
649654
if additionalSecretMount != "" {
650655
addSecretVolume(&podSpec, additionalSecretMount, additionalSecretMountPath)
651656
}
@@ -1256,6 +1261,7 @@ func (c *Cluster) generateStatefulSet(spec *acidv1.PostgresSpec) (*appsv1.Statef
12561261
spiloContainer,
12571262
initContainers,
12581263
sidecarContainers,
1264+
c.OpConfig.SharePGSocketWithSidecars,
12591265
&tolerationSpec,
12601266
effectiveRunAsUser,
12611267
effectiveRunAsGroup,
@@ -1440,6 +1446,28 @@ func addShmVolume(podSpec *v1.PodSpec) {
14401446
podSpec.Volumes = volumes
14411447
}
14421448

1449+
func addVarRunVolume(podSpec *v1.PodSpec) {
1450+
volumes := append(podSpec.Volumes, v1.Volume{
1451+
Name: "postgresql-run",
1452+
VolumeSource: v1.VolumeSource{
1453+
EmptyDir: &v1.EmptyDirVolumeSource{
1454+
Medium: "Memory",
1455+
},
1456+
},
1457+
})
1458+
1459+
for i := range podSpec.Containers {
1460+
mounts := append(podSpec.Containers[i].VolumeMounts,
1461+
v1.VolumeMount{
1462+
Name: "postgresql-run",
1463+
MountPath: "/var/run/postgresql",
1464+
})
1465+
podSpec.Containers[i].VolumeMounts = mounts
1466+
}
1467+
1468+
podSpec.Volumes = volumes
1469+
}
1470+
14431471
func addSecretVolume(podSpec *v1.PodSpec, additionalSecretMount string, additionalSecretMountPath string) {
14441472
volumes := append(podSpec.Volumes, v1.Volume{
14451473
Name: additionalSecretMount,
@@ -2006,6 +2034,7 @@ func (c *Cluster) generateLogicalBackupJob() (*batchv1beta1.CronJob, error) {
20062034
logicalBackupContainer,
20072035
[]v1.Container{},
20082036
[]v1.Container{},
2037+
util.False(),
20092038
&[]v1.Toleration{},
20102039
nil,
20112040
nil,

pkg/cluster/k8sres_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,76 @@ func TestShmVolume(t *testing.T) {
453453
}
454454
}
455455

456+
func TestVarRunVolume(t *testing.T) {
457+
testName := "TestVarRunVolume"
458+
tests := []struct {
459+
subTest string
460+
podSpec *v1.PodSpec
461+
varRunPos int
462+
}{
463+
{
464+
subTest: "empty PodSpec",
465+
podSpec: &v1.PodSpec{
466+
Volumes: []v1.Volume{},
467+
Containers: []v1.Container{
468+
{
469+
VolumeMounts: []v1.VolumeMount{},
470+
},
471+
},
472+
},
473+
varRunPos: 0,
474+
},
475+
{
476+
subTest: "non empty PodSpec",
477+
podSpec: &v1.PodSpec{
478+
Volumes: []v1.Volume{{}},
479+
Containers: []v1.Container{
480+
{
481+
VolumeMounts: []v1.VolumeMount{
482+
{
483+
Name: "data",
484+
ReadOnly: false,
485+
MountPath: "/data",
486+
},
487+
},
488+
},
489+
},
490+
},
491+
varRunPos: 1,
492+
},
493+
}
494+
for _, tt := range tests {
495+
varRunVolumeName := "postgresql-run"
496+
497+
numMounts := len(tt.podSpec.Containers[0].VolumeMounts)
498+
499+
addVarRunVolume(tt.podSpec)
500+
501+
volumeName := tt.podSpec.Volumes[tt.varRunPos].Name
502+
503+
if volumeName != varRunVolumeName {
504+
t.Errorf("%s %s: Expected volume %s was not created, have %s instead",
505+
testName, tt.subTest, varRunVolumeName, volumeName)
506+
}
507+
508+
for i := range tt.podSpec.Containers {
509+
volumeMountName := tt.podSpec.Containers[i].VolumeMounts[tt.varRunPos].Name
510+
511+
if volumeMountName != varRunVolumeName {
512+
t.Errorf("%s %s: Expected mount %s was not created, have %s instead",
513+
testName, tt.subTest, varRunVolumeName, volumeMountName)
514+
}
515+
}
516+
517+
numMountsCheck := len(tt.podSpec.Containers[0].VolumeMounts)
518+
519+
if numMountsCheck != numMounts+1 {
520+
t.Errorf("Unexpected number of VolumeMounts: got %v instead of %v",
521+
numMountsCheck, numMounts+1)
522+
}
523+
}
524+
}
525+
456526
func TestCloneEnv(t *testing.T) {
457527
testName := "TestCloneEnv"
458528
tests := []struct {

pkg/controller/operator_config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ func (c *Controller) importConfigurationFromCRD(fromCRD *acidv1.OperatorConfigur
8181
result.StorageResizeMode = util.Coalesce(fromCRD.Kubernetes.StorageResizeMode, "pvc")
8282
result.EnableInitContainers = util.CoalesceBool(fromCRD.Kubernetes.EnableInitContainers, util.True())
8383
result.EnableSidecars = util.CoalesceBool(fromCRD.Kubernetes.EnableSidecars, util.True())
84+
result.SharePGSocketWithSidecars = util.CoalesceBool(fromCRD.Kubernetes.SharePGSocketWithSidecars, util.False())
8485
result.SecretNameTemplate = fromCRD.Kubernetes.SecretNameTemplate
8586
result.OAuthTokenSecretName = fromCRD.Kubernetes.OAuthTokenSecretName
8687
result.EnableCrossNamespaceSecret = fromCRD.Kubernetes.EnableCrossNamespaceSecret

pkg/util/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ type Config struct {
197197
EnablePodDisruptionBudget *bool `name:"enable_pod_disruption_budget" default:"true"`
198198
EnableInitContainers *bool `name:"enable_init_containers" default:"true"`
199199
EnableSidecars *bool `name:"enable_sidecars" default:"true"`
200+
SharePGSocketWithSidecars *bool `name:"share_pg_socket_with_sidecars" default:"false"`
200201
Workers uint32 `name:"workers" default:"8"`
201202
APIPort int `name:"api_port" default:"8080"`
202203
RingLogLines int `name:"ring_log_lines" default:"100"`

0 commit comments

Comments
 (0)