Skip to content

Commit

Permalink
delete nfs config for stale subvolume
Browse files Browse the repository at this point in the history
this commit deletes the nfs config entry for
the stale subvolume

Signed-off-by: yati1998 <[email protected]>
  • Loading branch information
yati1998 committed Jun 19, 2024
1 parent d06a1cf commit 58c2679
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions pkg/filesystem/subvolume.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
} 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", subVol)
}
}
}
}

// getOmapKey gets the omap key and value details for a given subvolume.
Expand Down Expand Up @@ -392,6 +406,60 @@ 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 |ocs-storageclust|
// 00000010 65 72 2d 63 65 70 68 6e 66 73 |er-cephnfs|
// 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", 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("No PV found for subvolume %s: %s", subVol, 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"
nfscluster, err := runCommand(ctx, clientsets, OperatorNamespace, CephClusterNamespace, cmd, args)
if err != nil || nfscluster == "" {
logging.Info("No path found for cluster %s: %s", clusterName, err)
return ""
}

var unmarshalcluster []string
var nfsCluster string

errc := json.Unmarshal([]byte(nfscluster), &unmarshalcluster)
if errc != nil {
fmt.Println("Error:", errc)
return ""
}

// Extract the value from the unmarshalcluster
if len(unmarshalcluster) > 0 {
nfsCluster = unmarshalcluster[0]
} else {
return ""
}

return nfsCluster
}

// 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 58c2679

Please sign in to comment.