Skip to content

Commit

Permalink
feat: Add SubPathExpr option for additionalVolumes
Browse files Browse the repository at this point in the history
  • Loading branch information
smutel authored and Samuel Mutel committed Jan 29, 2024
1 parent 409e4c7 commit dd270fb
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 6 deletions.
2 changes: 2 additions & 0 deletions charts/postgres-operator/crds/postgresqls.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ spec:
x-kubernetes-preserve-unknown-fields: true
subPath:
type: string
isSubPathExpr:
type: boolean
allowedSourceRanges:
type: array
nullable: true
Expand Down
1 change: 1 addition & 0 deletions docs/reference/cluster_manifest.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ These parameters are grouped directly under the `spec` key in the manifest.
It allows you to mount existing PersistentVolumeClaims, ConfigMaps and Secrets inside the StatefulSet.
Also an `emptyDir` volume can be shared between initContainer and statefulSet.
Additionaly, you can provide a `SubPath` for volume mount (a file in a configMap source volume, for example).
Set the `isSubPathExpr` to true if you want to include [API environment variables](https://kubernetes.io/docs/concepts/storage/volumes/#using-subpath-expanded-environment).
You can also specify in which container the additional Volumes will be mounted with the `targetContainers` array option.
If `targetContainers` is empty, additional volumes will be mounted only in the `postgres` container.
If you set the `all` special item, it will be mounted in all containers (postgres + sidecars).
Expand Down
10 changes: 10 additions & 0 deletions manifests/complete-postgres-manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ spec:
# PersistentVolumeClaim:
# claimName: pvc-postgresql-data-partitions
# readyOnly: false
# - name: data
# mountPath: /home/postgres/pgdata/partitions
# subPath: $(NODE_NAME)/$(POD_NAME)
# isSubPathExpr: true
# targetContainers:
# - postgres
# volumeSource:
# PersistentVolumeClaim:
# claimName: pvc-postgresql-data-partitions
# readyOnly: false
# - name: conf
# mountPath: /etc/telegraf
# subPath: telegraf.conf
Expand Down
4 changes: 4 additions & 0 deletions manifests/postgresql.crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ spec:
x-kubernetes-preserve-unknown-fields: true
subPath:
type: string
subPathStr:
type: string
subPathExpr:
type: boolean
allowedSourceRanges:
type: array
nullable: true
Expand Down
3 changes: 3 additions & 0 deletions pkg/apis/acid.zalan.do/v1/crds.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ var PostgresCRDResourceValidation = apiextv1.CustomResourceValidation{
"subPath": {
Type: "string",
},
"isSubPathExpr": {
Type: "boolean",
},
},
},
},
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/acid.zalan.do/v1/postgresql_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ type AdditionalVolume struct {
Name string `json:"name"`
MountPath string `json:"mountPath"`
SubPath string `json:"subPath,omitempty"`
IsSubPathExpr bool `json:"isSubPathExpr,omitemtpy"`
TargetContainers []string `json:"targetContainers"`
VolumeSource v1.VolumeSource `json:"volumeSource"`
}
Expand Down
21 changes: 16 additions & 5 deletions pkg/cluster/k8sres.go
Original file line number Diff line number Diff line change
Expand Up @@ -1751,11 +1751,22 @@ func (c *Cluster) addAdditionalVolumes(podSpec *v1.PodSpec,
for _, additionalVolume := range additionalVolumes {
for _, target := range additionalVolume.TargetContainers {
if podSpec.Containers[i].Name == target || target == "all" {
mounts = append(mounts, v1.VolumeMount{
Name: additionalVolume.Name,
MountPath: additionalVolume.MountPath,
SubPath: additionalVolume.SubPath,
})
var v v1.VolumeMount
if additionalVolume.IsSubPathExpr {
v = v1.VolumeMount{
Name: additionalVolume.Name,
MountPath: additionalVolume.MountPath,
SubPathExpr: additionalVolume.SubPath,
}
} else {
v = v1.VolumeMount{
Name: additionalVolume.Name,
MountPath: additionalVolume.MountPath,
SubPath: additionalVolume.SubPath,
}
}

mounts = append(mounts, v)
}
}
}
Expand Down
21 changes: 20 additions & 1 deletion pkg/cluster/k8sres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1812,6 +1812,25 @@ func TestAdditionalVolume(t *testing.T) {
EmptyDir: &v1.EmptyDirVolumeSource{},
},
},
{
Name: "test5",
MountPath: "/test5",
SubPath: "/test5",
TargetContainers: nil, // should mount only to postgres
VolumeSource: v1.VolumeSource{
EmptyDir: &v1.EmptyDirVolumeSource{},
},
},
{
Name: "test6",
MountPath: "/test6",
SubPath: "$(POD_NAME)",
IsSubPathExpr: true,
TargetContainers: nil, // should mount only to postgres
VolumeSource: v1.VolumeSource{
EmptyDir: &v1.EmptyDirVolumeSource{},
},
},
}

pg := acidv1.Postgresql{
Expand Down Expand Up @@ -1865,7 +1884,7 @@ func TestAdditionalVolume(t *testing.T) {
{
subTest: "checking volume mounts of postgres container",
container: constants.PostgresContainerName,
expectedMounts: []string{"pgdata", "test1", "test3", "test4"},
expectedMounts: []string{"pgdata", "test1", "test3", "test4", "test5", "test6"},
},
{
subTest: "checking volume mounts of sidecar container",
Expand Down

0 comments on commit dd270fb

Please sign in to comment.