feat: Add SubPathExpr option for additionalVolumes (#2463)

This commit is contained in:
Samuel Mutel
2024-05-24 11:55:22 +02:00
committed by GitHub
parent 1839baaad3
commit 7bcb73a402
8 changed files with 78 additions and 7 deletions
+3
View File
@@ -168,6 +168,9 @@ var PostgresCRDResourceValidation = apiextv1.CustomResourceValidation{
"subPath": {
Type: "string",
},
"isSubPathExpr": {
Type: "boolean",
},
},
},
},
@@ -143,6 +143,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"`
}
+10 -3
View File
@@ -1820,11 +1820,18 @@ 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{
v := v1.VolumeMount{
Name: additionalVolume.Name,
MountPath: additionalVolume.MountPath,
SubPath: additionalVolume.SubPath,
})
}
if additionalVolume.IsSubPathExpr {
v.SubPathExpr = additionalVolume.SubPath
} else {
v.SubPath = additionalVolume.SubPath
}
mounts = append(mounts, v)
}
}
}
+49 -4
View File
@@ -1889,6 +1889,25 @@ func TestAdditionalVolume(t *testing.T) {
EmptyDir: &v1.EmptyDirVolumeSource{},
},
},
{
Name: "test5",
MountPath: "/test5",
SubPath: "subpath",
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{
@@ -1935,9 +1954,10 @@ func TestAdditionalVolume(t *testing.T) {
assert.NoError(t, err)
tests := []struct {
subTest string
container string
expectedMounts []string
subTest string
container string
expectedMounts []string
expectedSubPath []string
}{
{
subTest: "checking volume mounts of postgres container",
@@ -1949,6 +1969,17 @@ func TestAdditionalVolume(t *testing.T) {
container: "sidecar",
expectedMounts: []string{"pgdata", "test1", "test2"},
},
{
subTest: "checking volume mounts with subPath",
container: constants.PostgresContainerName,
expectedMounts: []string{"test5"},
expectedSubPath: []string{"subpath"},
},
{
subTest: "checking volume mounts with subPathExpr",
container: constants.PostgresContainerName,
expectedMounts: []string{"test6"},
},
}
for _, tt := range tests {
@@ -1957,12 +1988,26 @@ func TestAdditionalVolume(t *testing.T) {
continue
}
mounts := []string{}
subPaths := []string{}
subPathExprs := []string{}
for _, volumeMounts := range container.VolumeMounts {
mounts = append(mounts, volumeMounts.Name)
subPaths = append(subPaths, volumeMounts.SubPath)
subPathExprs = append(subPathExprs, volumeMounts.SubPathExpr)
}
if !util.IsEqualIgnoreOrder(mounts, tt.expectedMounts) {
t.Errorf("%s %s: different volume mounts: got %v, epxected %v",
t.Errorf("%s %s: different volume mounts: got %v, expected %v",
t.Name(), tt.subTest, mounts, tt.expectedMounts)
}
if !util.IsEqualIgnoreOrder(subPaths, tt.expectedSubPath) {
t.Errorf("%s %s: different volume subPaths: got %v, expected %v",
t.Name(), tt.subTest, mounts, tt.expectedSubPath)
}
if !util.IsEqualIgnoreOrder(subPathExprs, []string{container.Name}) {
t.Errorf("%s %s: different volume subPathExprs: got %v, expected %v",
t.Name(), tt.subTest, mounts, tt.expectedMounts)
}
}