Skip to content

Commit 7bcb73a

Browse files
authored
feat: Add SubPathExpr option for additionalVolumes (#2463)
1 parent 1839baa commit 7bcb73a

File tree

8 files changed

+78
-7
lines changed

8 files changed

+78
-7
lines changed

charts/postgres-operator/crds/postgresqls.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ spec:
101101
x-kubernetes-preserve-unknown-fields: true
102102
subPath:
103103
type: string
104+
isSubPathExpr:
105+
type: boolean
104106
allowedSourceRanges:
105107
type: array
106108
nullable: true

docs/reference/cluster_manifest.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ These parameters are grouped directly under the `spec` key in the manifest.
242242
It allows you to mount existing PersistentVolumeClaims, ConfigMaps and Secrets inside the StatefulSet.
243243
Also an `emptyDir` volume can be shared between initContainer and statefulSet.
244244
Additionaly, you can provide a `SubPath` for volume mount (a file in a configMap source volume, for example).
245+
Set `isSubPathExpr` to true if you want to include [API environment variables](https://kubernetes.io/docs/concepts/storage/volumes/#using-subpath-expanded-environment).
245246
You can also specify in which container the additional Volumes will be mounted with the `targetContainers` array option.
246247
If `targetContainers` is empty, additional volumes will be mounted only in the `postgres` container.
247248
If you set the `all` special item, it will be mounted in all containers (postgres + sidecars).

manifests/complete-postgres-manifest.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,16 @@ spec:
8383
# PersistentVolumeClaim:
8484
# claimName: pvc-postgresql-data-partitions
8585
# readyOnly: false
86+
# - name: data
87+
# mountPath: /home/postgres/pgdata/partitions
88+
# subPath: $(NODE_NAME)/$(POD_NAME)
89+
# isSubPathExpr: true
90+
# targetContainers:
91+
# - postgres
92+
# volumeSource:
93+
# PersistentVolumeClaim:
94+
# claimName: pvc-postgresql-data-partitions
95+
# readyOnly: false
8696
# - name: conf
8797
# mountPath: /etc/telegraf
8898
# subPath: telegraf.conf

manifests/postgresql.crd.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ spec:
9999
x-kubernetes-preserve-unknown-fields: true
100100
subPath:
101101
type: string
102+
isSubPathExpr:
103+
type: boolean
102104
allowedSourceRanges:
103105
type: array
104106
nullable: true

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@ var PostgresCRDResourceValidation = apiextv1.CustomResourceValidation{
168168
"subPath": {
169169
Type: "string",
170170
},
171+
"isSubPathExpr": {
172+
Type: "boolean",
173+
},
171174
},
172175
},
173176
},

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ type AdditionalVolume struct {
143143
Name string `json:"name"`
144144
MountPath string `json:"mountPath"`
145145
SubPath string `json:"subPath,omitempty"`
146+
IsSubPathExpr bool `json:"isSubPathExpr,omitemtpy"`
146147
TargetContainers []string `json:"targetContainers"`
147148
VolumeSource v1.VolumeSource `json:"volumeSource"`
148149
}

pkg/cluster/k8sres.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1820,11 +1820,18 @@ func (c *Cluster) addAdditionalVolumes(podSpec *v1.PodSpec,
18201820
for _, additionalVolume := range additionalVolumes {
18211821
for _, target := range additionalVolume.TargetContainers {
18221822
if podSpec.Containers[i].Name == target || target == "all" {
1823-
mounts = append(mounts, v1.VolumeMount{
1823+
v := v1.VolumeMount{
18241824
Name: additionalVolume.Name,
18251825
MountPath: additionalVolume.MountPath,
1826-
SubPath: additionalVolume.SubPath,
1827-
})
1826+
}
1827+
1828+
if additionalVolume.IsSubPathExpr {
1829+
v.SubPathExpr = additionalVolume.SubPath
1830+
} else {
1831+
v.SubPath = additionalVolume.SubPath
1832+
}
1833+
1834+
mounts = append(mounts, v)
18281835
}
18291836
}
18301837
}

pkg/cluster/k8sres_test.go

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1889,6 +1889,25 @@ func TestAdditionalVolume(t *testing.T) {
18891889
EmptyDir: &v1.EmptyDirVolumeSource{},
18901890
},
18911891
},
1892+
{
1893+
Name: "test5",
1894+
MountPath: "/test5",
1895+
SubPath: "subpath",
1896+
TargetContainers: nil, // should mount only to postgres
1897+
VolumeSource: v1.VolumeSource{
1898+
EmptyDir: &v1.EmptyDirVolumeSource{},
1899+
},
1900+
},
1901+
{
1902+
Name: "test6",
1903+
MountPath: "/test6",
1904+
SubPath: "$(POD_NAME)",
1905+
IsSubPathExpr: true,
1906+
TargetContainers: nil, // should mount only to postgres
1907+
VolumeSource: v1.VolumeSource{
1908+
EmptyDir: &v1.EmptyDirVolumeSource{},
1909+
},
1910+
},
18921911
}
18931912

18941913
pg := acidv1.Postgresql{
@@ -1935,9 +1954,10 @@ func TestAdditionalVolume(t *testing.T) {
19351954
assert.NoError(t, err)
19361955

19371956
tests := []struct {
1938-
subTest string
1939-
container string
1940-
expectedMounts []string
1957+
subTest string
1958+
container string
1959+
expectedMounts []string
1960+
expectedSubPath []string
19411961
}{
19421962
{
19431963
subTest: "checking volume mounts of postgres container",
@@ -1949,6 +1969,17 @@ func TestAdditionalVolume(t *testing.T) {
19491969
container: "sidecar",
19501970
expectedMounts: []string{"pgdata", "test1", "test2"},
19511971
},
1972+
{
1973+
subTest: "checking volume mounts with subPath",
1974+
container: constants.PostgresContainerName,
1975+
expectedMounts: []string{"test5"},
1976+
expectedSubPath: []string{"subpath"},
1977+
},
1978+
{
1979+
subTest: "checking volume mounts with subPathExpr",
1980+
container: constants.PostgresContainerName,
1981+
expectedMounts: []string{"test6"},
1982+
},
19521983
}
19531984

19541985
for _, tt := range tests {
@@ -1957,12 +1988,26 @@ func TestAdditionalVolume(t *testing.T) {
19571988
continue
19581989
}
19591990
mounts := []string{}
1991+
subPaths := []string{}
1992+
subPathExprs := []string{}
19601993
for _, volumeMounts := range container.VolumeMounts {
19611994
mounts = append(mounts, volumeMounts.Name)
1995+
subPaths = append(subPaths, volumeMounts.SubPath)
1996+
subPathExprs = append(subPathExprs, volumeMounts.SubPathExpr)
19621997
}
19631998

19641999
if !util.IsEqualIgnoreOrder(mounts, tt.expectedMounts) {
1965-
t.Errorf("%s %s: different volume mounts: got %v, epxected %v",
2000+
t.Errorf("%s %s: different volume mounts: got %v, expected %v",
2001+
t.Name(), tt.subTest, mounts, tt.expectedMounts)
2002+
}
2003+
2004+
if !util.IsEqualIgnoreOrder(subPaths, tt.expectedSubPath) {
2005+
t.Errorf("%s %s: different volume subPaths: got %v, expected %v",
2006+
t.Name(), tt.subTest, mounts, tt.expectedSubPath)
2007+
}
2008+
2009+
if !util.IsEqualIgnoreOrder(subPathExprs, []string{container.Name}) {
2010+
t.Errorf("%s %s: different volume subPathExprs: got %v, expected %v",
19662011
t.Name(), tt.subTest, mounts, tt.expectedMounts)
19672012
}
19682013
}

0 commit comments

Comments
 (0)