Skip to content

Commit

Permalink
core: add flatten-rbd-pvc command
Browse files Browse the repository at this point in the history
- Flatten an RBD image corrensponding to the target PVC
- Remove the corresponding temporary RBD image

Closes: rook#222

Signed-off-by: Satoru Takeuchi <satoru.takeuchi@gmail.com>
  • Loading branch information
satoru-takeuchi committed Jan 26, 2024
1 parent e1fe321 commit 998315d
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 2 deletions.
85 changes: 85 additions & 0 deletions cmd/commands/flatten_rbd_pvc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
Copyright 2023 The Rook Authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package command

import (
"encoding/json"
"fmt"

"github.com/rook/kubectl-rook-ceph/pkg/exec"
"github.com/rook/kubectl-rook-ceph/pkg/logging"
"github.com/spf13/cobra"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type TrashLsOutput struct {
ID string `json:"id"`
Name string `json:"name"`
}

var namespace string
var allowInUse bool

// FlattenRBDPVCCmd represents the rook commands
var FlattenRBDPVCCmd = &cobra.Command{
Use: "flatten-rbd-pvc",
Short: "Flatten the RBD image corresponding to the target RBD PVC",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
pvcName := args[0]
pvc, err := clientSets.Kube.CoreV1().PersistentVolumeClaims(namespace).Get(cmd.Context(), pvcName, v1.GetOptions{})
if err != nil {
logging.Fatal(err, "failed to get pvc %s/%s", namespace, pvcName)
return
}
pvName := pvc.Spec.VolumeName
pv, err := clientSets.Kube.CoreV1().PersistentVolumes().Get(cmd.Context(), pvName, v1.GetOptions{})
if err != nil {
logging.Fatal(fmt.Errorf("failed to get pv %s", pvName))
return
}
imageName := pv.Spec.CSI.VolumeAttributes["imageName"]
poolName := pv.Spec.CSI.VolumeAttributes["pool"]
tempImageName := imageName + "-temp"
if !allowInUse {
// TODO: check whether the target PVC is mounted
logging.Warning("can't flatten in-use pvc")
}
logging.Info("removing the temporary RBD image %s/%s if exist", poolName, tempImageName)
exec.RunCommandInOperatorPod(cmd.Context(), clientSets, "rbd", []string{"-p", poolName, "trash", "mv", tempImageName}, cephClusterNamespace, operatorNamespace, false, false)
out := exec.RunCommandInOperatorPod(cmd.Context(), clientSets, "rbd", []string{"-p", poolName, "trash", "ls", "--format=json"}, cephClusterNamespace, operatorNamespace, true, false)
var data []TrashLsOutput
json.Unmarshal([]byte(out), &data)
var id string
for _, d := range data {
if d.Name == tempImageName {
id = d.ID
break
}
}
if id != "" {
exec.RunCommandInOperatorPod(cmd.Context(), clientSets, "ceph", []string{"rbd", "task", "add", "trash", "remove", fmt.Sprintf("%s/%s", poolName, id)}, cephClusterNamespace, operatorNamespace, false, false)
}
logging.Info("flattening the target RBD image %s/%s", poolName, imageName)
exec.RunCommandInOperatorPod(cmd.Context(), clientSets, "rbd", []string{"-p", poolName, "flatten", imageName}, cephClusterNamespace, operatorNamespace, false, false)
},
}

func init() {
FlattenRBDPVCCmd.Flags().StringVarP(&namespace, "namespace", "n", "default", "pvc's namespace")
FlattenRBDPVCCmd.Flags().BoolVarP(&allowInUse, "allow-in-use", "a", false, "allow to flatten in-use image")
}
2 changes: 0 additions & 2 deletions cmd/commands/rook.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ import (
"github.com/spf13/cobra"
)

var json bool

// RookCmd represents the rook commands
var RookCmd = &cobra.Command{
Use: "rook",
Expand Down
1 change: 1 addition & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@ func addcommands() {
command.RestoreCmd,
command.DestroyClusterCmd,
command.SubvolumeCmd,
command.FlattenRBDPVCCmd,
)
}

0 comments on commit 998315d

Please sign in to comment.