Skip to content

Commit

Permalink
[PWX-30455] feat: volume-mount override via custom mount
Browse files Browse the repository at this point in the history
Signed-off-by: Shivanjan Chakravorty <schakravorty@purestorage.com>
  • Loading branch information
Glitchfix committed Jul 27, 2023
1 parent f2a7fbd commit e394504
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 0 deletions.
30 changes: 30 additions & 0 deletions drivers/storage/portworx/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -1295,9 +1295,39 @@ func (t *template) getVolumeMounts() []v1.VolumeMount {
for _, fn := range extensions {
volumeInfoList = append(volumeInfoList, fn()...)
}
volumeInfoList = t.overrideVolumeMount(volumeInfoList)
return t.mountsFromVolInfo(volumeInfoList)
}

func (t *template) overrideVolumeMount(volInfos []volumeInfo) []volumeInfo {
volPaths := map[string]int{}

for i := range volInfos {
volPaths[volInfos[i].mountPath] = i
}

for i := range t.cluster.Spec.Volumes {
v := t.cluster.Spec.Volumes[i]
existingVolIdx, ok := volPaths[v.MountPath]
if !ok {
volInfos = append(volInfos, volumeInfo{
name: v.Name,
readOnly: v.ReadOnly,
hostPath: v.HostPath.String(),
mountPath: v.MountPath,
mountPropagation: v.MountPropagation,
hostPathType: v.HostPath.Type,
configMapType: v.ConfigMap,
})
} else {
volInfos[existingVolIdx].hostPath = v.HostPath.String()
volInfos[existingVolIdx].mountPropagation = v.MountPropagation
}
}

return volInfos
}

func (t *template) mountsFromVolInfo(vols []volumeInfo) []v1.VolumeMount {
volumeMounts := make([]v1.VolumeMount, 0, len(vols))
mountPathSet := make(map[string]bool)
Expand Down
102 changes: 102 additions & 0 deletions drivers/storage/portworx/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4436,3 +4436,105 @@ func assertContainerEqual(t *testing.T, expected, actual v1.Container) {
assert.ElementsMatch(t, expected.Env, actual.Env)
assert.ElementsMatch(t, expected.VolumeMounts, actual.VolumeMounts)
}

func Test_template_overrideVolumeMount(t *testing.T) {
testVols := getCommonVolumeList(pxutil.MinimumPxVersionAutoTLS)
hp0 := &v1.HostPathVolumeSource{
Path: "/test0",
}
overRideVols := append(testVols, []volumeInfo{}...)
for i := range overRideVols {
if overRideVols[i].name == "diagsdump" {
overRideVols[i].hostPath = hp0.String()
overRideVols[i].mountPropagation = mountPropagationModePtr(v1.MountPropagationBidirectional)
break
}
}

hp1 := &v1.HostPathVolumeSource{
Path: "/test1",
}
hp2 := &v1.HostPathVolumeSource{
Path: "/test1",
}
additionalMounts := append(testVols, volumeInfo{
name: "test",
hostPath: hp2.String(),
mountPath: "/test2",
mountPropagation: mountPropagationModePtr(v1.MountPropagationBidirectional),
})
for i := range additionalMounts {
if additionalMounts[i].name == "diagsdump" {
additionalMounts[i].hostPath = hp1.String()
additionalMounts[i].mountPropagation = mountPropagationModePtr(v1.MountPropagationBidirectional)
break
}
}

tests := []struct {
name string
vols []corev1.VolumeSpec
volInfos []volumeInfo
want []volumeInfo
}{
{
"no override",
[]corev1.VolumeSpec{},
testVols,
testVols,
},
{
"override existing volume mounts",
[]corev1.VolumeSpec{
{
Name: "diagsdump",
MountPath: "/var/cores",
VolumeSource: v1.VolumeSource{
HostPath: hp0,
},
MountPropagation: mountPropagationModePtr(v1.MountPropagationBidirectional),
},
},
testVols,
overRideVols,
},
{
"override existing volume mounts and append new mounts",
[]corev1.VolumeSpec{
{
Name: "diagsdump",
MountPath: "/var/cores",
VolumeSource: v1.VolumeSource{
HostPath: hp1,
},
MountPropagation: mountPropagationModePtr(v1.MountPropagationBidirectional),
},
{
Name: "test",
MountPath: "/test2",
VolumeSource: v1.VolumeSource{
HostPath: hp2,
},
MountPropagation: mountPropagationModePtr(v1.MountPropagationBidirectional),
},
},
testVols,
additionalMounts,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tr := &template{
cluster: &corev1.StorageCluster{
Spec: corev1.StorageClusterSpec{
Volumes: tt.vols,
},
},
}
// if got := tr.overrideVolumeMount(tt.volInfos); !reflect.DeepEqual(got, tt.want) {
if got := tr.overrideVolumeMount(tt.volInfos); !assert.Equal(t, got, tt.want) {
t.Errorf("template.overrideVolumeMount() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit e394504

Please sign in to comment.