From 086e03d8ce02c76de9b80ea808ab68579da202b5 Mon Sep 17 00:00:00 2001 From: yati1998 Date: Wed, 19 Jun 2024 16:20:20 +0530 Subject: [PATCH] csi: delete nfs config for stale subvolume this commit deletes the nfs config entry for the stale subvolume Signed-off-by: yati1998 --- pkg/filesystem/subvolume.go | 66 +++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/pkg/filesystem/subvolume.go b/pkg/filesystem/subvolume.go index cd5925cc..10c25d5c 100644 --- a/pkg/filesystem/subvolume.go +++ b/pkg/filesystem/subvolume.go @@ -362,6 +362,20 @@ func deleteOmapForSubvolume(ctx context.Context, clientsets *k8sutil.Clientsets, logging.Info("omap key:%q deleted", omapkey) } + nfsClusterName := getNfsClusterName(ctx, clientsets, OperatorNamespace, CephClusterNamespace, subVol, fs) + if nfsClusterName != "" { + exportPath := getNfsExportPath(ctx, clientsets, OperatorNamespace, CephClusterNamespace, nfsClusterName) + if exportPath == "" { + logging.Info("export path not found for subvol %q: %q", subVol, nfsClusterName) + } else { + cmd := "ceph" + args := []string{"nfs", "export", "delete", nfsClusterName, exportPath} + _, err := runCommand(ctx, clientsets, OperatorNamespace, CephClusterNamespace, cmd, args) + if err != nil { + logging.Fatal(err, "failed to delete export for subvol %q: %q %q", subVol, nfsClusterName, exportPath) + } + } + } } // getOmapKey gets the omap key and value details for a given subvolume. @@ -392,6 +406,58 @@ func getOmapKey(ctx context.Context, clientsets *k8sutil.Clientsets, OperatorNam return omapkey } +// getNfsClusterName returns the cluster name from the omap. +// csi.nfs.cluster +// value (26 bytes) : +// 00000000 6f 63 73 2d 73 74 6f 72 61 67 65 63 6c 75 73 74 |my-cluster-cephn| +// 00000010 65 72 2d 63 65 70 68 6e 66 73 |fs| +// 0000001a +func getNfsClusterName(ctx context.Context, clientsets *k8sutil.Clientsets, OperatorNamespace, CephClusterNamespace, subVol, fs string) string { + + poolName, err := getMetadataPoolName(ctx, clientsets, OperatorNamespace, CephClusterNamespace, fs) + if err != nil || poolName == "" { + logging.Fatal(fmt.Errorf("pool name not found %q: %q", poolName, err)) + } + omapval := getOmapVal(subVol) + + args := []string{"getomapval", omapval, "csi.nfs.cluster", "-p", poolName, "--namespace", "csi", "/dev/stdout"} + cmd := "rados" + nfscluster, err := runCommand(ctx, clientsets, OperatorNamespace, CephClusterNamespace, cmd, args) + if err != nil || nfscluster == "" { + logging.Info("nfs cluster not found for subvolume %s: %s %s", subVol, poolName, err) + return "" + } + + return nfscluster +} + +func getNfsExportPath(ctx context.Context, clientsets *k8sutil.Clientsets, OperatorNamespace, CephClusterNamespace, clusterName string) string { + + args := []string{"nfs", "export", "ls", clusterName} + cmd := "ceph" + exportList, err := runCommand(ctx, clientsets, OperatorNamespace, CephClusterNamespace, cmd, args) + if err != nil || exportList == "" { + logging.Info("No export path found for cluster %s: %s", clusterName, err) + return "" + } + + var unmarshalexportpath []string + var exportPath string + + err = json.Unmarshal([]byte(exportList), &unmarshalexportpath) + if err != nil { + logging.Info("failed to unmarshal export list: %q", err) + return "" + } + + // Extract the value from the unmarshalcluster + if len(unmarshalexportpath) > 0 { + exportPath = unmarshalexportpath[0] + } + + return exportPath +} + // func getOmapVal is used to get the omapval from the given subvolume // omapval is of format csi.volume.427774b4-340b-11ed-8d66-0242ac110005 // which is similar to volume name csi-vol-427774b4-340b-11ed-8d66-0242ac110005