-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add tests for custom rbd export-diff command
Signed-off-by: Yuji Ito <[email protected]>
- Loading branch information
1 parent
87ac222
commit 93f1358
Showing
10 changed files
with
360 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
apiVersion: ceph.rook.io/v1 | ||
kind: CephBlockPool | ||
metadata: | ||
name: test-pool | ||
namespace: rook-ceph | ||
spec: | ||
failureDomain: osd | ||
replicated: | ||
size: 1 | ||
requireSafeReplicaSize: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
apiVersion: storage.k8s.io/v1 | ||
kind: StorageClass | ||
metadata: | ||
name: test-sc | ||
provisioner: rook-ceph.rbd.csi.ceph.com | ||
parameters: | ||
clusterID: rook-ceph | ||
pool: test-pool | ||
imageFormat: "2" | ||
imageFeatures: layering | ||
csi.storage.k8s.io/provisioner-secret-name: rook-csi-rbd-provisioner | ||
csi.storage.k8s.io/provisioner-secret-namespace: rook-ceph | ||
csi.storage.k8s.io/controller-expand-secret-name: rook-csi-rbd-provisioner | ||
csi.storage.k8s.io/controller-expand-secret-namespace: rook-ceph | ||
csi.storage.k8s.io/node-stage-secret-name: rook-csi-rbd-node | ||
csi.storage.k8s.io/node-stage-secret-namespace: rook-ceph | ||
csi.storage.k8s.io/fstype: ext4 | ||
allowVolumeExpansion: true | ||
reclaimPolicy: Delete | ||
--- | ||
apiVersion: v1 | ||
kind: PersistentVolumeClaim | ||
metadata: | ||
name: test-pvc | ||
namespace: default | ||
spec: | ||
accessModes: | ||
- ReadWriteOnce | ||
resources: | ||
requests: | ||
storage: 1Gi | ||
storageClassName: test-sc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package cluster | ||
|
||
import ( | ||
"bytes" | ||
_ "embed" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
) | ||
|
||
var ( | ||
//go:embed pvc-pod-template.yaml | ||
templatePVCPod string | ||
) | ||
|
||
var kubectlCmd = os.Getenv("KUBECTL") | ||
|
||
func Kubectl(args ...string) ([]byte, error) { | ||
if len(kubectlCmd) == 0 { | ||
return nil, fmt.Errorf("KUBECTL environment variable should be set") | ||
} | ||
|
||
var stdout bytes.Buffer | ||
command := exec.Command(kubectlCmd, args...) | ||
command.Stdout = &stdout | ||
|
||
err := command.Run() | ||
return stdout.Bytes(), err | ||
} | ||
|
||
func KubectlWithInput(stdin []byte, args ...string) ([]byte, error) { | ||
if len(kubectlCmd) == 0 { | ||
return nil, fmt.Errorf("KUBECTL environment variable should be set") | ||
} | ||
|
||
var stdout bytes.Buffer | ||
command := exec.Command(kubectlCmd, args...) | ||
command.Stdin = bytes.NewReader(stdin) | ||
command.Stdout = &stdout | ||
|
||
err := command.Run() | ||
return stdout.Bytes(), err | ||
} | ||
|
||
func CreateNamespace(namespace string) error { | ||
_, err := Kubectl("create", "namespace", namespace) | ||
if err != nil { | ||
return fmt.Errorf("failed to create namespace: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
func DeleteNamespace(namespace string) error { | ||
_, err := Kubectl("delete", "namespace", namespace) | ||
if err != nil { | ||
return fmt.Errorf("failed to delete namespace: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
func CreatePodPVC(namespace, podName, pvcName string) error { | ||
manifest := fmt.Sprintf(templatePVCPod, podName, namespace, pvcName, pvcName, namespace) | ||
_, err := KubectlWithInput([]byte(manifest), "apply", "-f", "-") | ||
if err != nil { | ||
return fmt.Errorf("failed to create pod and pvc: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
func DeletePodPVC(namespace, podName, pvcName string) error { | ||
_, err := Kubectl("delete", "pod", podName, "-n", namespace) | ||
if err != nil { | ||
return fmt.Errorf("failed to delete pod: %w", err) | ||
} | ||
_, err = Kubectl("delete", "pvc", pvcName, "-n", namespace) | ||
if err != nil { | ||
return fmt.Errorf("failed to delete pvc: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
func GetImageNameByPVC(namespace, pvcName string) (string, error) { | ||
stdout, err := Kubectl("get", "-n", namespace, "pvc", pvcName, "-o", "jsonpath={.spec.volumeName}") | ||
if err != nil { | ||
return "", fmt.Errorf("failed to get volume name by PVC: %w", err) | ||
} | ||
|
||
volumeName := string(stdout) | ||
stdout, err = Kubectl("get", "pv", volumeName, "-o", "jsonpath={.spec.csi.volumeAttributes.imageName}") | ||
if err != nil { | ||
return "", fmt.Errorf("failed to get image name by volume: %w", err) | ||
} | ||
|
||
return string(stdout), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
name: %s | ||
namespace: %s | ||
spec: | ||
containers: | ||
- name: ubuntu | ||
image: ubuntu:20.04 | ||
command: ["/usr/bin/sleep", "infinity"] | ||
volumeMounts: | ||
- mountPath: /mnt | ||
name: my-volume | ||
volumes: | ||
- name: my-volume | ||
persistentVolumeClaim: | ||
claimName: %s | ||
--- | ||
apiVersion: v1 | ||
kind: PersistentVolumeClaim | ||
metadata: | ||
name: %s | ||
namespace: %s | ||
spec: | ||
accessModes: | ||
- ReadWriteOnce | ||
resources: | ||
requests: | ||
storage: 1Gi | ||
storageClassName: test-sc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package cluster | ||
|
||
import "fmt" | ||
|
||
func Rbd(args ...string) ([]byte, error) { | ||
return Kubectl(append([]string{"exec", "-n", "rook-ceph", "deploy/rook-ceph-tools", "--", "rbd"}, args...)...) | ||
} | ||
|
||
func ExportDiff(args ...string) error { | ||
stdout, err := Rbd(append([]string{"export-diff"}, args...)...) | ||
if err != nil { | ||
return fmt.Errorf("failed to run rbd export-diff command: %w, %s", err, string(stdout)) | ||
} | ||
return nil | ||
} | ||
|
||
func SnapCreate(pool, image, snap string) error { | ||
stdout, err := Rbd("snap", "create", pool+"/"+image+"@"+snap) | ||
if err != nil { | ||
return fmt.Errorf("failed to create snapshot: %w, %s", err, string(stdout)) | ||
} | ||
return nil | ||
} | ||
|
||
func SnapRemove(pool, image, snap string) error { | ||
stdout, err := Rbd("snap", "rm", pool+"/"+image+"@"+snap) | ||
if err != nil { | ||
return fmt.Errorf("failed to remove snapshot: %w, %s", err, string(stdout)) | ||
} | ||
return nil | ||
} |
Oops, something went wrong.