Skip to content

Commit

Permalink
code restructuring
Browse files Browse the repository at this point in the history
Signed-off-by: Pawan <[email protected]>
  • Loading branch information
pawanpraka1 committed Aug 13, 2020
1 parent 869a25c commit 335aefb
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 61 deletions.
16 changes: 6 additions & 10 deletions pkg/zfs/plugin/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"encoding/json"
"strconv"
"github.com/pkg/errors"
"github.com/openebs/velero-plugin/pkg/utils"
"github.com/openebs/velero-plugin/pkg/zfs/utils"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/openebs/zfs-localpv/pkg/zfs"
"github.com/openebs/zfs-localpv/pkg/builder/volbuilder"
Expand All @@ -31,14 +31,9 @@ import (
)

const (
// IdentifierKey is a word to generate snapshotID from nodeid, volume name and backup name
IdentifierKey = "/"
VeleroBkpKey = "velero.io/bkpname"
)

func generateSnapshotID(nodeID, volumeID, backupName string) string {
return nodeID + IdentifierKey + volumeID + IdentifierKey + backupName
}

func (p *Plugin) uploadZFSVolume(vol *apis.ZFSVolume, bkpname string) error {
data, err := json.MarshalIndent(vol, "", "\t")
if err != nil {
Expand Down Expand Up @@ -82,15 +77,16 @@ func (p *Plugin) deleteSnapshot(snapshotID string) error {

func (p *Plugin) sendSnapshot(vol *apis.ZFSVolume, bkpname string, snapshotID string) error {
p.Log.Infof("zfs: creating ZFSBackup vol = %s bkp = %s snap = %s", vol.Name, bkpname, snapshotID)
labels := map[string]string{VeleroBkpKey: bkpname}

bkp, err := bkpbuilder.NewBuilder().
WithName(snapshotID).
WithBackup(bkpname).
WithLabels(labels).
WithVolume(vol.Name).
WithNode(vol.Spec.OwnerNodeID).
WithSnapshot(snapshotID).
WithStatus(apis.BKPZFSStatusInit).
WithRemote(p.remoteAddr).
WithLocal(false).
Build()

if err != nil {
Expand Down Expand Up @@ -181,5 +177,5 @@ func (p *Plugin) doBackup(volumeID string, snapshotID string) (string, error) {
return "", errors.Errorf("Failed to upload snapshot, status:{%v}", bkp.Status)
}

return generateSnapshotID(vol.Spec.OwnerNodeID, volumeID, snapshotID), nil
return utils.GenerateSnapshotID(volumeID, snapshotID), nil
}
58 changes: 20 additions & 38 deletions pkg/zfs/plugin/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ limitations under the License.
package plugin

import (
"strings"
"time"
"encoding/json"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/openebs/zfs-localpv/pkg/zfs"
"github.com/pkg/errors"
"github.com/openebs/velero-plugin/pkg/zfs/utils"
"github.com/openebs/zfs-localpv/pkg/builder/volbuilder"
"github.com/openebs/zfs-localpv/pkg/builder/restorebuilder"
apis "github.com/openebs/zfs-localpv/pkg/apis/openebs.io/zfs/v1"
Expand All @@ -32,24 +32,6 @@ const (
restoreStatusInterval = 5
)

// getInfoFromSnapshotID return node id, backup name and volume id from the given snapshotID
func getInfoFromSnapshotID(snapshotID string) (nodeID, volumeID, backupName string, err error) {
s := strings.Split(snapshotID, IdentifierKey)
if len(s) != 3 {
err = errors.New("invalid snapshot id")
return
}

nodeID = s[0]
volumeID = s[1]
backupName = s[2]

if nodeID == "" || volumeID == "" || backupName == "" {
err = errors.Errorf("invalid nodeID=%s volumeID=%s backupName=%s", nodeID, volumeID, backupName)
}
return
}

func (p *Plugin) downloadZFSVolume(volumeID, snapName string) (*apis.ZFSVolume, error) {
zv := &apis.ZFSVolume{}

Expand Down Expand Up @@ -89,16 +71,16 @@ func (p *Plugin) isVolumeReady(volumeID string) (ready bool, err error) {
return ready, nil
}

func (p *Plugin) checkRestoreStatus(bkpname string) {
func (p *Plugin) checkRestoreStatus(snapname string) {
rstrDone := false

for !rstrDone {
getOptions := metav1.GetOptions{}
bkp, err := restorebuilder.NewKubeclient().
WithNamespace(p.zfspvNamespace).Get(bkpname, getOptions)
WithNamespace(p.zfspvNamespace).Get(snapname, getOptions)

if err != nil {
p.Log.Errorf("zfs: Failed to fetch restore info {%s}", bkpname)
p.Log.Errorf("zfs: Failed to fetch restore info {%s}", snapname)
p.cl.ExitServer = true
return
}
Expand All @@ -113,10 +95,10 @@ func (p *Plugin) checkRestoreStatus(bkpname string) {
}
}

func (p *Plugin) restoreVolume(node, volname, bkpname string) error {
zv, err := p.downloadZFSVolume(volname, bkpname)
func (p *Plugin) restoreVolume(volname, snapname string) error {
zv, err := p.downloadZFSVolume(volname, snapname)
if err != nil {
p.Log.Errorf("zfs: download ZFSVolume failed vol %s bkp %s", volname, bkpname)
p.Log.Errorf("zfs: download ZFSVolume failed vol %s bkp %s", volname, snapname)
return err
}

Expand All @@ -126,14 +108,14 @@ func (p *Plugin) restoreVolume(node, volname, bkpname string) error {
return err
}

node := zv.Spec.OwnerNodeID

rstr, err := restorebuilder.NewBuilder().
WithName(bkpname).
WithRestoreName(bkpname).
WithName(snapname).
WithVolume(volname).
WithNode(node).
WithStatus(apis.RSTZFSStatusInit).
WithRemote(p.remoteAddr).
WithLocal(false).
Build()

if err != nil {
Expand All @@ -145,45 +127,45 @@ func (p *Plugin) restoreVolume(node, volname, bkpname string) error {

func (p *Plugin) doRestore(snapshotID string) (string, error) {

node, volname, bkpname, err := getInfoFromSnapshotID(snapshotID)
volname, snapname, err := utils.GetInfoFromSnapshotID(snapshotID)
if err != nil {
return "", err
}

err = p.restoreVolume(node, volname, bkpname)
err = p.restoreVolume(volname, snapname)
if err != nil {
p.Log.Errorf("zfs: restoreVolume failed vol %s bkp %s err: %v", volname, bkpname, err)
p.Log.Errorf("zfs: restoreVolume failed vol %s snap %s err: %v", volname, snapname, err)
return "", err
}

filename := p.cl.GenerateRemoteFilename(volname, bkpname)
filename := p.cl.GenerateRemoteFilename(volname, snapname)
if filename == "" {
return "", errors.Errorf("Error creating remote file name for restore")
}

go p.checkRestoreStatus(bkpname)
go p.checkRestoreStatus(snapname)

ret := p.cl.Download(filename)
if !ret {
return "", errors.New("failed to restore snapshot")
}

rstr, err := restorebuilder.NewKubeclient().
WithNamespace(p.zfspvNamespace).Get(bkpname, metav1.GetOptions{})
WithNamespace(p.zfspvNamespace).Get(snapname, metav1.GetOptions{})

if err != nil {
p.Log.Errorf("zfs: get restoreVolume failed bkp %s err: %v", bkpname, err)
p.Log.Errorf("zfs: get restoreVolume failed bkp %s err: %v", snapname, err)
return "", err
}

err = restorebuilder.NewKubeclient().WithNamespace(p.zfspvNamespace).Delete(bkpname)
err = restorebuilder.NewKubeclient().WithNamespace(p.zfspvNamespace).Delete(snapname)
if err != nil {
p.Log.Errorf("zfs: get restoreVolume delete failed bkp %s err: %v", bkpname, err)
p.Log.Errorf("zfs: get restoreVolume delete failed bkp %s err: %v", snapname, err)
return "", err
}

if rstr.Status != apis.RSTZFSStatusDone {
p.Log.Errorf("zfs: restoreVolume status failed bkp %s", bkpname)
p.Log.Errorf("zfs: restoreVolume status failed bkp %s", snapname)
return "", errors.Errorf("Failed to upload snapshot, status:{%v}", rstr.Status)
}

Expand Down
14 changes: 1 addition & 13 deletions pkg/zfs/plugin/zfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ import (
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"github.com/openebs/velero-plugin/pkg/utils"
"github.com/openebs/velero-plugin/pkg/velero"
"k8s.io/client-go/rest"
"github.com/openebs/velero-plugin/pkg/zfs/utils"
cloud "github.com/openebs/velero-plugin/pkg/clouduploader"
)

Expand Down Expand Up @@ -67,16 +65,6 @@ func (p *Plugin) Init(config map[string]string) error {
p.zfspvNamespace = ns
}

conf, err := rest.InClusterConfig()
if err != nil {
p.Log.Errorf("Failed to get cluster config : %s", err.Error())
return errors.New("error fetching cluster config")
}

if err := velero.InitializeClientSet(conf); err != nil {
return errors.Wrapf(err, "failed to initialize velero clientSet")
}

p.cl = &cloud.Conn{Log: p.Log}
return p.cl.Init(config)
}
Expand Down
26 changes: 26 additions & 0 deletions pkg/utils/utils.go → pkg/zfs/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ import (
cloud "github.com/openebs/velero-plugin/pkg/clouduploader"
)

const (
// IdentifierKey is a word to generate snapshotID from volume name and backup name
IdentifierKey = "@"
)

func GetServerAddress() (string, error) {
netInterfaceAddresses, err := net.InterfaceAddrs()

Expand Down Expand Up @@ -60,3 +65,24 @@ func GetScheduleName(backupName string) string {
}
return scheduleOrBackupName
}

func GenerateSnapshotID(volumeID, backupName string) string {
return volumeID + IdentifierKey + backupName
}

// GetInfoFromSnapshotID return backup name and volume id from the given snapshotID
func GetInfoFromSnapshotID(snapshotID string) (volumeID, backupName string, err error) {
s := strings.Split(snapshotID, IdentifierKey)
if len(s) != 2 {
err = errors.New("invalid snapshot id")
return
}

volumeID = s[0]
backupName = s[1]

if volumeID == "" || backupName == "" {
err = errors.Errorf("invalid volumeID=%s backupName=%s", volumeID, backupName)
}
return
}

0 comments on commit 335aefb

Please sign in to comment.