Skip to content

Commit

Permalink
core: fix purge_osd command for multiple OSD IDs
Browse files Browse the repository at this point in the history
Updating the command pre run to parse
the comma separated list of OSD IDs

Signed-off-by: Troy Partridge <[email protected]>
  • Loading branch information
troyApart committed Nov 26, 2024
1 parent 3ab36b9 commit d4bad27
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
23 changes: 13 additions & 10 deletions cmd/commands/rook.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package command
import (
"fmt"
"strconv"
"strings"

"github.com/rook/kubectl-rook-ceph/pkg/exec"
"github.com/rook/kubectl-rook-ceph/pkg/logging"
Expand Down Expand Up @@ -47,17 +48,17 @@ var versionCmd = &cobra.Command{

var purgeCmd = &cobra.Command{
Use: "purge-osd",
Short: "Permanently remove an OSD from the cluster. Multiple OSDs can be removed with a comma-separated list of IDs, for example, purge-osd 0,1",
PreRunE: validateOsdID,
Short: "Permanently remove an OSD from the cluster. Multiple OSDs can be removed in a single command with a comma-separated list of IDs, for example, purge-osd 0,1",
PreRunE: validateOsdIDs,
Args: cobra.ExactArgs(1),
Example: "kubectl rook-ceph rook purge-osd <OSD_ID>",
PreRun: func(cmd *cobra.Command, args []string) {
verifyOperatorPodIsRunning(cmd.Context(), clientSets)
},
Run: func(cmd *cobra.Command, args []string) {
forceflagValue := cmd.Flag("force").Value.String()
osdID := args[0]
rook.PurgeOsd(cmd.Context(), clientSets, operatorNamespace, cephClusterNamespace, osdID, forceflagValue)
osdIDs := args[0]
rook.PurgeOsds(cmd.Context(), clientSets, operatorNamespace, cephClusterNamespace, osdIDs, forceflagValue)
},
}

Expand All @@ -76,14 +77,16 @@ func init() {
RookCmd.AddCommand(statusCmd)
RookCmd.AddCommand(purgeCmd)
statusCmd.PersistentFlags().Bool("json", false, "print status in json format")
purgeCmd.PersistentFlags().Bool("force", false, "force deletion of an OSD if the OSD still contains data")
purgeCmd.PersistentFlags().Bool("force", false, "force deletion of OSD(s) with the risk that they could still contains data")
}

func validateOsdID(cmd *cobra.Command, args []string) error {
osdID := args[0]
_, err := strconv.Atoi(osdID)
if err != nil {
return fmt.Errorf("invalid ID %s, the OSD ID must be an integer. %v", osdID, err)
func validateOsdIDs(cmd *cobra.Command, args []string) error {
osdIDs := strings.Split(args[0], ",")
for _, osdID := range osdIDs {
_, err := strconv.Atoi(osdID)
if err != nil {
return fmt.Errorf("invalid ID %s, the OSD ID must be an integer. %v", osdID, err)
}
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions docs/rook.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ The `rook` command supports the following sub-commands:
4. `status all`: [status all](#status-all) print the phase and conditions of all CRs
5. `status <CR>`: [status cr](#status-cr-name) print the phase and conditions of CRs of a specific type, such as 'cephobjectstore', 'cephfilesystem', etc

## Purge an OSD
## Purge OSDs

Permanently remove an OSD from the cluster.
Permanently remove OSD(s) from the cluster.

!!! warning
Data loss is possible when passing the --force flag if the PGs are not healthy on other OSDs.
Expand Down
6 changes: 3 additions & 3 deletions pkg/rook/purge_osd.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func PurgeOsd(ctx context.Context, clientsets *k8sutil.Clientsets, operatorNamespace, clusterNamespace, osdId, flag string) {
func PurgeOsds(ctx context.Context, clientsets *k8sutil.Clientsets, operatorNamespace, clusterNamespace, osdIds, flag string) {
monCm, err := clientsets.Kube.CoreV1().ConfigMaps(clusterNamespace).Get(ctx, mons.MonConfigMap, v1.GetOptions{})
if err != nil {
logging.Fatal(fmt.Errorf("failed to get mon configmap %s %v", mons.MonConfigMap, err))
Expand All @@ -47,12 +47,12 @@ func PurgeOsd(ctx context.Context, clientsets *k8sutil.Clientsets, operatorNames
cmd := "/bin/sh"
args := []string{
"-c",
fmt.Sprintf("export ROOK_MON_ENDPOINTS=%s ROOK_CEPH_USERNAME=client.admin ROOK_CEPH_SECRET=%s ROOK_CONFIG_DIR=/var/lib/rook && rook ceph osd remove --osd-ids=%s --force-osd-removal=%s", monEndPoint, adminKey, osdId, flag),
fmt.Sprintf("export ROOK_MON_ENDPOINTS=%s ROOK_CEPH_USERNAME=client.admin ROOK_CEPH_SECRET=%s ROOK_CONFIG_DIR=/var/lib/rook && rook ceph osd remove --osd-ids=%s --force-osd-removal=%s", monEndPoint, adminKey, osdIds, flag),
}
logging.Info("Running purge osd command")

_, err = exec.RunCommandInOperatorPod(ctx, clientsets, cmd, args, operatorNamespace, clusterNamespace, false)
if err != nil {
logging.Fatal(err, "failed to remove osd %s", osdId)
logging.Fatal(err, "failed to remove osd %s", osdIds)
}
}

0 comments on commit d4bad27

Please sign in to comment.