Skip to content

Commit

Permalink
csi: look for subvolumepath instead of subvolumename
Browse files Browse the repository at this point in the history
In case of ROX pvc, the subvolume name is different and is not listed
in the backend as it is just a shallow volume and it links to the source
path.
incase the source volume is delete the subvolme will be shown stale, but
actually that is being used by the ROX pvc.
Soinstead getting the subvolumename to look for stale subvolumes,
it is better to retrive the name from the subvolumepath.

Signed-off-by: yati1998 <ypadia@redhat.com>
  • Loading branch information
yati1998 committed Jul 16, 2024
1 parent 00d1470 commit 7258831
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
21 changes: 20 additions & 1 deletion pkg/filesystem/subvolume.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,31 @@ func getK8sRefSubvolume(ctx context.Context, clientsets *k8sutil.Clientsets) map
subvolumeNames := make(map[string]subVolumeInfo)
for _, pv := range pvList.Items {
if pv.Spec.CSI != nil {
subvolumeNames[pv.Spec.CSI.VolumeAttributes["subvolumeName"]] = subVolumeInfo{}
subvolumePath := pv.Spec.CSI.VolumeAttributes["subvolumePath"]
name, err := getSubvolumeNameFromPath(subvolumePath)
if err != nil {
logging.Error(err, "failed to get subvolume name")
continue
}
subvolumeNames[name] = subVolumeInfo{}
}
}
return subvolumeNames
}

// getSubvolumeNameFromPath get the subvolumename from the path.
// subvolumepath: /volumes/csi/csi-vol-6a99b552-fdcc-441d-b1e6-a522a85a503d/5f4e4caa-f835-41ba-83c1-5bbd57f6aedf
// subvolumename: csi-vol-6a99b552-fdcc-441d-b1e6-a522a85a503d
func getSubvolumeNameFromPath(path string) (string, error) {
splitSubvol := strings.Split(path, "/")
if len(splitSubvol) < 4 {
return "", fmt.Errorf("failed to get name from subvolumepath: %s", path)
}
name := splitSubvol[3]

return name, nil
}

// runCommand checks for the presence of externalcluster and runs the command accordingly.
func runCommand(ctx context.Context, clientsets *k8sutil.Clientsets, operatorNamespace, clusterNamespace, cmd string, args []string) (string, error) {
if checkForExternalStorage(ctx, clientsets, clusterNamespace) {
Expand Down
44 changes: 43 additions & 1 deletion pkg/filesystem/subvolume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ limitations under the License.

package subvolume

import "testing"
import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetOmapVal(t *testing.T) {

Expand Down Expand Up @@ -59,3 +64,40 @@ func TestGetOmapVal(t *testing.T) {
})
}
}

func TestGetSubvolumeNameFromPath(t *testing.T) {

tests := []struct {
path string
name string
err error
}{
{
path: "/volumes/csi/csi-vol-6a99b552-fdcc-441d-b1e6-a522a85a503d/5f4e4caa-f835-41ba-83c1-5bbd57f6aedf",
name: "csi-vol-6a99b552-fdcc-441d-b1e6-a522a85a503d",
},
{
path: "",
err: fmt.Errorf("failed to get name from subvolumepath: "),
},
{
path: "/volumes/csi-vol-6a99b552-fdcc-441d-b1e6-a522a85a503d/5f4e4caa-f835-41ba-83c1-5bbd57f6aedf",
name: "5f4e4caa-f835-41ba-83c1-5bbd57f6aedf",
},
{
path: "csi-vol-6a99b552-fdcc-441d-b1e6-a522a85a503d/5f4e4caa-f835-41ba-83c1-5bbd57f6aedf",
err: fmt.Errorf(`failed to get name from subvolumepath: csi-vol-6a99b552-fdcc-441d-b1e6-a522a85a503d/5f4e4caa-f835-41ba-83c1-5bbd57f6aedf`),
},
}
for _, tt := range tests {
t.Run(tt.path, func(t *testing.T) {
name, err := getSubvolumeNameFromPath(tt.path)
if err != nil {
assert.Error(t, err)
assert.Equal(t, tt.err.Error(), err.Error())
return
}
assert.Equal(t, name, tt.name)
})
}
}

0 comments on commit 7258831

Please sign in to comment.