From 335aefb4aecaff6801c60751d02a6805d03003b4 Mon Sep 17 00:00:00 2001 From: Pawan Date: Fri, 14 Aug 2020 00:52:35 +0530 Subject: [PATCH] code restructuring Signed-off-by: Pawan --- pkg/zfs/plugin/backup.go | 16 ++++------ pkg/zfs/plugin/restore.go | 58 +++++++++++++----------------------- pkg/zfs/plugin/zfs.go | 14 +-------- pkg/{ => zfs}/utils/utils.go | 26 ++++++++++++++++ 4 files changed, 53 insertions(+), 61 deletions(-) rename pkg/{ => zfs}/utils/utils.go (73%) diff --git a/pkg/zfs/plugin/backup.go b/pkg/zfs/plugin/backup.go index 7178a1ca..3b6608e3 100644 --- a/pkg/zfs/plugin/backup.go +++ b/pkg/zfs/plugin/backup.go @@ -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" @@ -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 { @@ -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 { @@ -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 } diff --git a/pkg/zfs/plugin/restore.go b/pkg/zfs/plugin/restore.go index c72beb6c..99257145 100644 --- a/pkg/zfs/plugin/restore.go +++ b/pkg/zfs/plugin/restore.go @@ -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" @@ -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{} @@ -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 } @@ -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 } @@ -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 { @@ -145,23 +127,23 @@ 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 { @@ -169,21 +151,21 @@ func (p *Plugin) doRestore(snapshotID string) (string, error) { } 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) } diff --git a/pkg/zfs/plugin/zfs.go b/pkg/zfs/plugin/zfs.go index e12e394d..8c456cca 100644 --- a/pkg/zfs/plugin/zfs.go +++ b/pkg/zfs/plugin/zfs.go @@ -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" ) @@ -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) } diff --git a/pkg/utils/utils.go b/pkg/zfs/utils/utils.go similarity index 73% rename from pkg/utils/utils.go rename to pkg/zfs/utils/utils.go index 9cf38062..9fe8e615 100644 --- a/pkg/utils/utils.go +++ b/pkg/zfs/utils/utils.go @@ -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() @@ -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 +}