Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(backup,restore): adding validation for backup and restore #221

Merged
merged 1 commit into from
Sep 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelogs/unreleased/221-pawanpraka1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
adding validation for backup and restore
8 changes: 8 additions & 0 deletions deploy/yamls/zfsbackup-crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ spec:
backupDest:
description: BackupDest is the remote address for backup transfer
minLength: 1
pattern: ^([0-9]+.[0-9]+.[0-9]+.[0-9]+:[0-9]+)$
type: string
ownerNodeID:
description: OwnerNodeID is a name of the nodes where the source volume
Expand All @@ -90,6 +91,13 @@ spec:
type: object
status:
description: ZFSBackupStatus is to hold status of backup
enum:
- Init
- Done
- Failed
- Pending
- InProgress
- Invalid
type: string
required:
- spec
Expand Down
8 changes: 8 additions & 0 deletions deploy/yamls/zfsrestore-crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ spec:
description: it can be ip:port in case of restore from remote or volumeName
in case of local restore
minLength: 1
pattern: ^([0-9]+.[0-9]+.[0-9]+.[0-9]+:[0-9]+)$
type: string
volumeName:
description: volume name to where restore has to be performed
Expand All @@ -67,6 +68,13 @@ spec:
type: object
status:
description: ZFSRestoreStatus is to hold result of action.
enum:
- Init
- Done
- Failed
- Pending
- InProgress
- Invalid
type: string
required:
- spec
Expand Down
16 changes: 16 additions & 0 deletions deploy/zfs-operator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,7 @@ spec:
backupDest:
description: BackupDest is the remote address for backup transfer
minLength: 1
pattern: ^([0-9]+.[0-9]+.[0-9]+.[0-9]+:[0-9]+)$
type: string
ownerNodeID:
description: OwnerNodeID is a name of the nodes where the source volume
Expand All @@ -920,6 +921,13 @@ spec:
type: object
status:
description: ZFSBackupStatus is to hold status of backup
enum:
- Init
- Done
- Failed
- Pending
- InProgress
- Invalid
type: string
required:
- spec
Expand Down Expand Up @@ -993,6 +1001,7 @@ spec:
description: it can be ip:port in case of restore from remote or volumeName
in case of local restore
minLength: 1
pattern: ^([0-9]+.[0-9]+.[0-9]+.[0-9]+:[0-9]+)$
type: string
volumeName:
description: volume name to where restore has to be performed
Expand All @@ -1005,6 +1014,13 @@ spec:
type: object
status:
description: ZFSRestoreStatus is to hold result of action.
enum:
- Init
- Done
- Failed
- Pending
- InProgress
- Invalid
type: string
required:
- spec
Expand Down
10 changes: 5 additions & 5 deletions pkg/apis/openebs.io/zfs/v1/zfsbackup.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ import (
type ZFSBackup struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec ZFSBackupSpec `json:"spec"`
Status ZFSBackupStatus `json:"status"`
Spec ZFSBackupSpec `json:"spec"`
// +kubebuilder:validation:Required
// +kubebuilder:validation:Enum=Init;Done;Failed;Pending;InProgress;Invalid
Status ZFSBackupStatus `json:"status"`
}

// ZFSBackupSpec is the spec for a ZFSBackup resource
Expand All @@ -61,6 +63,7 @@ type ZFSBackupSpec struct {
// BackupDest is the remote address for backup transfer
// +kubebuilder:validation:Required
// +kubebuilder:validation:MinLength=1
// +kubebuilder:validation:Pattern="^([0-9]+.[0-9]+.[0-9]+.[0-9]+:[0-9]+)$"
BackupDest string `json:"backupDest"`
}

Expand All @@ -69,9 +72,6 @@ type ZFSBackupStatus string

// Status written onto ZFSBackup objects.
const (
// BKPZFSStatusEmpty ensures the create operation is to be done, if import fails.
BKPZFSStatusEmpty ZFSBackupStatus = ""

// BKPZFSStatusDone , backup is completed.
BKPZFSStatusDone ZFSBackupStatus = "Done"

Expand Down
8 changes: 4 additions & 4 deletions pkg/apis/openebs.io/zfs/v1/zfsrestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ type ZFSRestore struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"` // set name to restore name + volume name + something like csp tag
Spec ZFSRestoreSpec `json:"spec"`
Status ZFSRestoreStatus `json:"status"`
// +kubebuilder:validation:Required
// +kubebuilder:validation:Enum=Init;Done;Failed;Pending;InProgress;Invalid
Status ZFSRestoreStatus `json:"status"`
}

// ZFSRestoreSpec is the spec for a ZFSRestore resource
Expand All @@ -46,6 +48,7 @@ type ZFSRestoreSpec struct {
// it can be ip:port in case of restore from remote or volumeName in case of local restore
// +kubebuilder:validation:Required
// +kubebuilder:validation:MinLength=1
// +kubebuilder:validation:Pattern="^([0-9]+.[0-9]+.[0-9]+.[0-9]+:[0-9]+)$"
RestoreSrc string `json:"restoreSrc"`
}

Expand All @@ -54,9 +57,6 @@ type ZFSRestoreStatus string

// Status written onto CStrorRestore object.
const (
// RSTZFSStatusEmpty ensures the create operation is to be done, if import fails.
RSTZFSStatusEmpty ZFSRestoreStatus = ""

// RSTZFSStatusDone , restore operation is completed.
RSTZFSStatusDone ZFSRestoreStatus = "Done"

Expand Down
26 changes: 20 additions & 6 deletions pkg/zfs/zfs_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,11 +297,14 @@ func buildVolumeResizeArgs(vol *apis.ZFSVolume) []string {
}

// builldVolumeBackupArgs returns volume send command for sending the zfs volume
func buildVolumeBackupArgs(bkp *apis.ZFSBackup, vol *apis.ZFSVolume) []string {
func buildVolumeBackupArgs(bkp *apis.ZFSBackup, vol *apis.ZFSVolume) ([]string, error) {
var ZFSVolArg []string
backupDest := bkp.Spec.BackupDest

bkpAddr := strings.Split(backupDest, ":")
if len(bkpAddr) != 2 {
return ZFSVolArg, fmt.Errorf("zfs: invalid backup server address %s", backupDest)
}

curSnap := vol.Spec.PoolName + "/" + vol.Name + "@" + bkp.Spec.SnapName

Expand All @@ -319,24 +322,28 @@ func buildVolumeBackupArgs(bkp *apis.ZFSBackup, vol *apis.ZFSVolume) []string {

ZFSVolArg = append(ZFSVolArg, "-c", cmd)

return ZFSVolArg
return ZFSVolArg, nil
}

// builldVolumeRestoreArgs returns volume recv command for receiving the zfs volume
func buildVolumeRestoreArgs(rstr *apis.ZFSRestore, vol *apis.ZFSVolume) []string {
func buildVolumeRestoreArgs(rstr *apis.ZFSRestore, vol *apis.ZFSVolume) ([]string, error) {
var ZFSVolArg []string
restoreSrc := rstr.Spec.RestoreSrc

volume := vol.Spec.PoolName + "/" + vol.Name

rstrAddr := strings.Split(restoreSrc, ":")
if len(rstrAddr) != 2 {
return ZFSVolArg, fmt.Errorf("zfs: invalid restore server address %s", restoreSrc)
}

source := "nc -w 3 " + rstrAddr[0] + " " + rstrAddr[1] + " | "

cmd := source + ZFSVolCmd + " " + ZFSRecvArg + " -F " + volume

ZFSVolArg = append(ZFSVolArg, "-c", cmd)

return ZFSVolArg
return ZFSVolArg, nil
}

// builldVolumeDestroyArgs returns volume destroy command along with attributes as a string array
Expand Down Expand Up @@ -692,7 +699,10 @@ func CreateBackup(bkp *apis.ZFSBackup) error {
return err
}

args := buildVolumeBackupArgs(bkp, vol)
args, err := buildVolumeBackupArgs(bkp, vol)
if err != nil {
return err
}
cmd := exec.Command("bash", args...)
out, err := cmd.CombinedOutput()

Expand Down Expand Up @@ -741,7 +751,11 @@ func CreateRestore(rstr *apis.ZFSRestore) error {
return err
}
volume := vol.Spec.PoolName + "/" + vol.Name
args := buildVolumeRestoreArgs(rstr, vol)
args, err := buildVolumeRestoreArgs(rstr, vol)
if err != nil {
return err
}

cmd := exec.Command("bash", args...)
out, err := cmd.CombinedOutput()

Expand Down