Skip to content

Commit

Permalink
Merge pull request #302 from yati1998/nfs
Browse files Browse the repository at this point in the history
delete nfs config for stale subvolume
  • Loading branch information
subhamkrai authored Jun 24, 2024
2 parents 808c626 + 56f58a4 commit 9c6a69e
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions pkg/filesystem/subvolume.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,21 @@ func deleteOmapForSubvolume(ctx context.Context, clientsets *k8sutil.Clientsets,
if err != nil || poolName == "" {
logging.Fatal(fmt.Errorf("pool name not found: %q", err))
}
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)
}
logging.Info("nfs export: %q %q deleted", nfsClusterName, exportPath)
}
}
if omapval != "" {
cmd := "rados"
args := []string{"rm", omapval, "-p", poolName, "--namespace", "csi"}
Expand Down Expand Up @@ -392,6 +407,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 unmarshalexportpath
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
Expand Down

0 comments on commit 9c6a69e

Please sign in to comment.