From e394504b74f208750887813681886ae1f1644926 Mon Sep 17 00:00:00 2001 From: Shivanjan Chakravorty Date: Wed, 26 Jul 2023 01:37:15 -0600 Subject: [PATCH] [PWX-30455] feat: volume-mount override via custom mount Signed-off-by: Shivanjan Chakravorty --- drivers/storage/portworx/deployment.go | 30 ++++++ drivers/storage/portworx/deployment_test.go | 102 ++++++++++++++++++++ 2 files changed, 132 insertions(+) diff --git a/drivers/storage/portworx/deployment.go b/drivers/storage/portworx/deployment.go index 801b06cb06..43ad190db5 100644 --- a/drivers/storage/portworx/deployment.go +++ b/drivers/storage/portworx/deployment.go @@ -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) diff --git a/drivers/storage/portworx/deployment_test.go b/drivers/storage/portworx/deployment_test.go index b7379f23cf..171d7542b0 100644 --- a/drivers/storage/portworx/deployment_test.go +++ b/drivers/storage/portworx/deployment_test.go @@ -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) + } + }) + } +}