feat(zfspv): adding backup and restore support (#162)

This commit adds support for Backup and Restore controller, which will be watching for
the events. The velero plugin will create a Backup CR to create a backup
with the remote location information, the controller will send the data
to that remote location.

In the same way, the velero plugin will create a Restore CR to restore the
volume from the the remote location and the restore controller will restore
the data.

Steps to use velero plugin for ZFS-LocalPV are :

1. install velero

2. add openebs plugin

velero plugin add openebs/velero-plugin:latest

3. Create the volumesnapshot location :

for full backup :-

```yaml
apiVersion: velero.io/v1
kind: VolumeSnapshotLocation
metadata:
  name: default
  namespace: velero
spec:
  provider: openebs.io/zfspv-blockstore
  config:
    bucket: velero
    prefix: zfs
    namespace: openebs
    provider: aws
    region: minio
    s3ForcePathStyle: "true"
    s3Url: http://minio.velero.svc:9000
```

for incremental backup :-

```yaml
apiVersion: velero.io/v1
kind: VolumeSnapshotLocation
metadata:
  name: default
  namespace: velero
spec:
  provider: openebs.io/zfspv-blockstore
  config:
    bucket: velero
    prefix: zfs
    backup: incremental
    namespace: openebs
    provider: aws
    region: minio
    s3ForcePathStyle: "true"
    s3Url: http://minio.velero.svc:9000
```

4. Create backup

velero backup create my-backup --snapshot-volumes --include-namespaces=velero-ns --volume-snapshot-locations=aws-cloud-default --storage-location=default

5. Create Schedule

velero create schedule newschedule  --schedule="*/1 * * * *" --snapshot-volumes --include-namespaces=velero-ns --volume-snapshot-locations=aws-local-default --storage-location=default

6. Restore from backup

velero restore create --from-backup my-backup --restore-volumes=true --namespace-mappings velero-ns:ns1



Signed-off-by: Pawan <pawan@mayadata.io>
This commit is contained in:
Pawan Prakash Sharma 2020-09-08 13:44:39 +05:30 committed by GitHub
parent a5e645b43d
commit e40026c98a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
48 changed files with 5148 additions and 7 deletions

View file

@ -0,0 +1,117 @@
// Copyright © 2020 The OpenEBS Authors
//
// 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 bkpbuilder
import (
apis "github.com/openebs/zfs-localpv/pkg/apis/openebs.io/zfs/v1"
)
// ZFSBackup is a wrapper over
// ZFSBackup API instance
type ZFSBackup struct {
// ZFSSnap object
Object *apis.ZFSBackup
}
// From returns a new instance of
// zfsbkp bkpume
func From(bkp *apis.ZFSBackup) *ZFSBackup {
return &ZFSBackup{
Object: bkp,
}
}
// Predicate defines an abstraction
// to determine conditional checks
// against the provided pod instance
type Predicate func(*ZFSBackup) bool
// PredicateList holds a list of predicate
type predicateList []Predicate
// ZFSBackupList holds the list
// of zfs backup instances
type ZFSBackupList struct {
// List contains list of backups
List apis.ZFSBackupList
}
// Len returns the number of items present
// in the ZFSBackupList
func (bkpList *ZFSBackupList) Len() int {
return len(bkpList.List.Items)
}
// all returns true if all the predicates
// succeed against the provided ZFSBackup
// instance
func (l predicateList) all(bkp *ZFSBackup) bool {
for _, pred := range l {
if !pred(bkp) {
return false
}
}
return true
}
// HasLabels returns true if provided labels
// are present in the provided ZFSBackup instance
func HasLabels(keyValuePair map[string]string) Predicate {
return func(bkp *ZFSBackup) bool {
for key, value := range keyValuePair {
if !bkp.HasLabel(key, value) {
return false
}
}
return true
}
}
// HasLabel returns true if provided label
// is present in the provided ZFSBackup instance
func (bkp *ZFSBackup) HasLabel(key, value string) bool {
val, ok := bkp.Object.GetLabels()[key]
if ok {
return val == value
}
return false
}
// HasLabel returns true if provided label
// is present in the provided ZFSBackup instance
func HasLabel(key, value string) Predicate {
return func(bkp *ZFSBackup) bool {
return bkp.HasLabel(key, value)
}
}
// IsNil returns true if the zfsbkp bkpume instance
// is nil
func (bkp *ZFSBackup) IsNil() bool {
return bkp.Object == nil
}
// IsNil is predicate to filter out nil zfsbkp bkpume
// instances
func IsNil() Predicate {
return func(bkp *ZFSBackup) bool {
return bkp.IsNil()
}
}
// GetAPIObject returns zfsbkp bkpume's API instance
func (bkp *ZFSBackup) GetAPIObject() *apis.ZFSBackup {
return bkp.Object
}

View file

@ -0,0 +1,190 @@
/*
Copyright 2020 The OpenEBS Authors
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 bkpbuilder
import (
apis "github.com/openebs/zfs-localpv/pkg/apis/openebs.io/zfs/v1"
"github.com/openebs/zfs-localpv/pkg/common/errors"
)
// Builder is the builder object for ZFSBackup
type Builder struct {
bkp *ZFSBackup
errs []error
}
// NewBuilder returns new instance of Builder
func NewBuilder() *Builder {
return &Builder{
bkp: &ZFSBackup{
Object: &apis.ZFSBackup{},
},
}
}
// BuildFrom returns new instance of Builder
// from the provided api instance
func BuildFrom(bkp *apis.ZFSBackup) *Builder {
if bkp == nil {
b := NewBuilder()
b.errs = append(
b.errs,
errors.New("failed to build bkp object: nil bkp"),
)
return b
}
return &Builder{
bkp: &ZFSBackup{
Object: bkp,
},
}
}
// WithNamespace sets the namespace of ZFSBackup
func (b *Builder) WithNamespace(namespace string) *Builder {
if namespace == "" {
b.errs = append(
b.errs,
errors.New(
"failed to build csi bkp object: missing namespace",
),
)
return b
}
b.bkp.Object.Namespace = namespace
return b
}
// WithName sets the name of ZFSBackup
func (b *Builder) WithName(name string) *Builder {
if name == "" {
b.errs = append(
b.errs,
errors.New(
"failed to build csi bkp object: missing name",
),
)
return b
}
b.bkp.Object.Name = name
return b
}
// WithPrevSnap sets the previous snapshot for ZFSBackup
func (b *Builder) WithPrevSnap(snap string) *Builder {
b.bkp.Object.Spec.PrevSnapName = snap
return b
}
// WithSnap sets the snapshot for ZFSBackup
func (b *Builder) WithSnap(snap string) *Builder {
b.bkp.Object.Spec.SnapName = snap
return b
}
// WithVolume sets the volume name of ZFSBackup
func (b *Builder) WithVolume(volume string) *Builder {
if volume == "" {
b.errs = append(
b.errs,
errors.New(
"failed to build csi bkp object: missing volume name",
),
)
return b
}
b.bkp.Object.Spec.VolumeName = volume
return b
}
// WithNode sets the owenr node for the ZFSBackup
func (b *Builder) WithNode(node string) *Builder {
if node == "" {
b.errs = append(
b.errs,
errors.New(
"failed to build bkp object: missing node id",
),
)
return b
}
b.bkp.Object.Spec.OwnerNodeID = node
return b
}
// WithStatus sets the status of the Backup progress
func (b *Builder) WithStatus(status apis.ZFSBackupStatus) *Builder {
if status == "" {
b.errs = append(
b.errs,
errors.New(
"failed to build bkp object: missing snap name",
),
)
return b
}
b.bkp.Object.Status = status
return b
}
// WithRemote sets the remote address for the ZFSBackup
func (b *Builder) WithRemote(server string) *Builder {
if server == "" {
b.errs = append(
b.errs,
errors.New(
"failed to build bkp object: missing remote",
),
)
return b
}
b.bkp.Object.Spec.BackupDest = server
return b
}
// WithLabels merges existing labels if any
// with the ones that are provided here
func (b *Builder) WithLabels(labels map[string]string) *Builder {
if len(labels) == 0 {
return b
}
if b.bkp.Object.Labels == nil {
b.bkp.Object.Labels = map[string]string{}
}
for key, value := range labels {
b.bkp.Object.Labels[key] = value
}
return b
}
// WithFinalizer merge existing finalizers if any
// with the ones that are provided here
func (b *Builder) WithFinalizer(finalizer []string) *Builder {
b.bkp.Object.Finalizers = append(b.bkp.Object.Finalizers, finalizer...)
return b
}
// Build returns ZFSBackup API object
func (b *Builder) Build() (*apis.ZFSBackup, error) {
if len(b.errs) > 0 {
return nil, errors.Errorf("%+v", b.errs)
}
return b.bkp.Object, nil
}

View file

@ -0,0 +1,72 @@
/*
Copyright 2020 The OpenEBS Authors
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 bkpbuilder
import (
apis "github.com/openebs/zfs-localpv/pkg/apis/openebs.io/zfs/v1"
)
// ListBuilder enables building an instance of
// ZFSBackupList
type ListBuilder struct {
list *apis.ZFSBackupList
filters predicateList
}
// NewListBuilder returns a new instance of ListBuilder
func NewListBuilder() *ListBuilder {
return &ListBuilder{
list: &apis.ZFSBackupList{},
}
}
// ListBuilderFrom returns a new instance of
// ListBuilder from API list instance
func ListBuilderFrom(bkps apis.ZFSBackupList) *ListBuilder {
b := &ListBuilder{list: &apis.ZFSBackupList{}}
if len(bkps.Items) == 0 {
return b
}
b.list.Items = append(b.list.Items, bkps.Items...)
return b
}
// List returns the list of pod
// instances that was built by this
// builder
func (b *ListBuilder) List() *apis.ZFSBackupList {
if b.filters == nil || len(b.filters) == 0 {
return b.list
}
filtered := &apis.ZFSBackupList{}
for _, bkp := range b.list.Items {
bkp := bkp // pin it
if b.filters.all(From(&bkp)) {
filtered.Items = append(filtered.Items, bkp)
}
}
return filtered
}
// WithFilter add filters on which the pod
// has to be filtered
func (b *ListBuilder) WithFilter(pred ...Predicate) *ListBuilder {
b.filters = append(b.filters, pred...)
return b
}

View file

@ -0,0 +1,427 @@
// Copyright © 2020 The OpenEBS Authors
//
// 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 bkpbuilder
import (
"encoding/json"
apis "github.com/openebs/zfs-localpv/pkg/apis/openebs.io/zfs/v1"
client "github.com/openebs/zfs-localpv/pkg/common/kubernetes/client"
clientset "github.com/openebs/zfs-localpv/pkg/generated/clientset/internalclientset"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// getClientsetFn is a typed function that
// abstracts fetching of internal clientset
type getClientsetFn func() (clientset *clientset.Clientset, err error)
// getClientsetFromPathFn is a typed function that
// abstracts fetching of clientset from kubeConfigPath
type getClientsetForPathFn func(kubeConfigPath string) (
clientset *clientset.Clientset,
err error,
)
// createFn is a typed function that abstracts
// creating zfsbkp bkpume instance
type createFn func(
cs *clientset.Clientset,
upgradeResultObj *apis.ZFSBackup,
namespace string,
) (*apis.ZFSBackup, error)
// getFn is a typed function that abstracts
// fetching a zfsbkp bkpume instance
type getFn func(
cli *clientset.Clientset,
name,
namespace string,
opts metav1.GetOptions,
) (*apis.ZFSBackup, error)
// listFn is a typed function that abstracts
// listing of zfsbkp bkpume instances
type listFn func(
cli *clientset.Clientset,
namespace string,
opts metav1.ListOptions,
) (*apis.ZFSBackupList, error)
// delFn is a typed function that abstracts
// deleting a zfsbkp bkpume instance
type delFn func(
cli *clientset.Clientset,
name,
namespace string,
opts *metav1.DeleteOptions,
) error
// updateFn is a typed function that abstracts
// updating zfsbkp bkpume instance
type updateFn func(
cs *clientset.Clientset,
bkp *apis.ZFSBackup,
namespace string,
) (*apis.ZFSBackup, error)
// Kubeclient enables kubernetes API operations
// on zfsbkp bkpume instance
type Kubeclient struct {
// clientset refers to zfsbkp bkpume's
// clientset that will be responsible to
// make kubernetes API calls
clientset *clientset.Clientset
kubeConfigPath string
// namespace holds the namespace on which
// kubeclient has to operate
namespace string
// functions useful during mocking
getClientset getClientsetFn
getClientsetForPath getClientsetForPathFn
get getFn
list listFn
del delFn
create createFn
update updateFn
}
// KubeclientBuildOption defines the abstraction
// to build a kubeclient instance
type KubeclientBuildOption func(*Kubeclient)
// defaultGetClientset is the default implementation to
// get kubernetes clientset instance
func defaultGetClientset() (clients *clientset.Clientset, err error) {
config, err := client.GetConfig(client.New())
if err != nil {
return nil, err
}
return clientset.NewForConfig(config)
}
// defaultGetClientsetForPath is the default implementation to
// get kubernetes clientset instance based on the given
// kubeconfig path
func defaultGetClientsetForPath(
kubeConfigPath string,
) (clients *clientset.Clientset, err error) {
config, err := client.GetConfig(
client.New(client.WithKubeConfigPath(kubeConfigPath)))
if err != nil {
return nil, err
}
return clientset.NewForConfig(config)
}
// defaultGet is the default implementation to get
// a zfsbkp bkpume instance in kubernetes cluster
func defaultGet(
cli *clientset.Clientset,
name, namespace string,
opts metav1.GetOptions,
) (*apis.ZFSBackup, error) {
return cli.ZfsV1().
ZFSBackups(namespace).
Get(name, opts)
}
// defaultList is the default implementation to list
// zfsbkp bkpume instances in kubernetes cluster
func defaultList(
cli *clientset.Clientset,
namespace string,
opts metav1.ListOptions,
) (*apis.ZFSBackupList, error) {
return cli.ZfsV1().
ZFSBackups(namespace).
List(opts)
}
// defaultCreate is the default implementation to delete
// a zfsbkp bkpume instance in kubernetes cluster
func defaultDel(
cli *clientset.Clientset,
name, namespace string,
opts *metav1.DeleteOptions,
) error {
deletePropagation := metav1.DeletePropagationForeground
opts.PropagationPolicy = &deletePropagation
err := cli.ZfsV1().
ZFSBackups(namespace).
Delete(name, opts)
return err
}
// defaultCreate is the default implementation to create
// a zfsbkp bkpume instance in kubernetes cluster
func defaultCreate(
cli *clientset.Clientset,
bkp *apis.ZFSBackup,
namespace string,
) (*apis.ZFSBackup, error) {
return cli.ZfsV1().
ZFSBackups(namespace).
Create(bkp)
}
// defaultUpdate is the default implementation to update
// a zfsbkp bkpume instance in kubernetes cluster
func defaultUpdate(
cli *clientset.Clientset,
bkp *apis.ZFSBackup,
namespace string,
) (*apis.ZFSBackup, error) {
return cli.ZfsV1().
ZFSBackups(namespace).
Update(bkp)
}
// withDefaults sets the default options
// of kubeclient instance
func (k *Kubeclient) withDefaults() {
if k.getClientset == nil {
k.getClientset = defaultGetClientset
}
if k.getClientsetForPath == nil {
k.getClientsetForPath = defaultGetClientsetForPath
}
if k.get == nil {
k.get = defaultGet
}
if k.list == nil {
k.list = defaultList
}
if k.del == nil {
k.del = defaultDel
}
if k.create == nil {
k.create = defaultCreate
}
if k.update == nil {
k.update = defaultUpdate
}
}
// WithClientSet sets the kubernetes client against
// the kubeclient instance
func WithClientSet(c *clientset.Clientset) KubeclientBuildOption {
return func(k *Kubeclient) {
k.clientset = c
}
}
// WithNamespace sets the kubernetes client against
// the provided namespace
func WithNamespace(namespace string) KubeclientBuildOption {
return func(k *Kubeclient) {
k.namespace = namespace
}
}
// WithNamespace sets the provided namespace
// against this Kubeclient instance
func (k *Kubeclient) WithNamespace(namespace string) *Kubeclient {
k.namespace = namespace
return k
}
// WithKubeConfigPath sets the kubernetes client
// against the provided path
func WithKubeConfigPath(path string) KubeclientBuildOption {
return func(k *Kubeclient) {
k.kubeConfigPath = path
}
}
// NewKubeclient returns a new instance of
// kubeclient meant for zfsbkp bkpume operations
func NewKubeclient(opts ...KubeclientBuildOption) *Kubeclient {
k := &Kubeclient{}
for _, o := range opts {
o(k)
}
k.withDefaults()
return k
}
func (k *Kubeclient) getClientsetForPathOrDirect() (
*clientset.Clientset,
error,
) {
if k.kubeConfigPath != "" {
return k.getClientsetForPath(k.kubeConfigPath)
}
return k.getClientset()
}
// getClientOrCached returns either a new instance
// of kubernetes client or its cached copy
func (k *Kubeclient) getClientOrCached() (*clientset.Clientset, error) {
if k.clientset != nil {
return k.clientset, nil
}
c, err := k.getClientsetForPathOrDirect()
if err != nil {
return nil,
errors.Wrapf(
err,
"failed to get clientset",
)
}
k.clientset = c
return k.clientset, nil
}
// Create creates a zfsbkp bkpume instance
// in kubernetes cluster
func (k *Kubeclient) Create(bkp *apis.ZFSBackup) (*apis.ZFSBackup, error) {
if bkp == nil {
return nil,
errors.New(
"failed to create csibkpume: nil bkp object",
)
}
cs, err := k.getClientOrCached()
if err != nil {
return nil, errors.Wrapf(
err,
"failed to create zfsbkp bkpume {%s} in namespace {%s}",
bkp.Name,
k.namespace,
)
}
return k.create(cs, bkp, k.namespace)
}
// Get returns zfsbkp bkpume object for given name
func (k *Kubeclient) Get(
name string,
opts metav1.GetOptions,
) (*apis.ZFSBackup, error) {
if name == "" {
return nil,
errors.New(
"failed to get zfsbkp bkpume: missing zfsbkp bkpume name",
)
}
cli, err := k.getClientOrCached()
if err != nil {
return nil, errors.Wrapf(
err,
"failed to get zfsbkp bkpume {%s} in namespace {%s}",
name,
k.namespace,
)
}
return k.get(cli, name, k.namespace, opts)
}
// GetRaw returns zfsbkp bkpume instance
// in bytes
func (k *Kubeclient) GetRaw(
name string,
opts metav1.GetOptions,
) ([]byte, error) {
if name == "" {
return nil, errors.New(
"failed to get raw zfsbkp bkpume: missing bkp name",
)
}
csiv, err := k.Get(name, opts)
if err != nil {
return nil, errors.Wrapf(
err,
"failed to get zfsbkp bkpume {%s} in namespace {%s}",
name,
k.namespace,
)
}
return json.Marshal(csiv)
}
// List returns a list of zfsbkp bkpume
// instances present in kubernetes cluster
func (k *Kubeclient) List(opts metav1.ListOptions) (*apis.ZFSBackupList, error) {
cli, err := k.getClientOrCached()
if err != nil {
return nil, errors.Wrapf(
err,
"failed to list zfsbkp bkpumes in namespace {%s}",
k.namespace,
)
}
return k.list(cli, k.namespace, opts)
}
// Delete deletes the zfsbkp bkpume from
// kubernetes
func (k *Kubeclient) Delete(name string) error {
if name == "" {
return errors.New(
"failed to delete csibkpume: missing bkp name",
)
}
cli, err := k.getClientOrCached()
if err != nil {
return errors.Wrapf(
err,
"failed to delete csibkpume {%s} in namespace {%s}",
name,
k.namespace,
)
}
return k.del(cli, name, k.namespace, &metav1.DeleteOptions{})
}
// Update updates this zfsbkp bkpume instance
// against kubernetes cluster
func (k *Kubeclient) Update(bkp *apis.ZFSBackup) (*apis.ZFSBackup, error) {
if bkp == nil {
return nil,
errors.New(
"failed to update csibkpume: nil bkp object",
)
}
cs, err := k.getClientOrCached()
if err != nil {
return nil, errors.Wrapf(
err,
"failed to update csibkpume {%s} in namespace {%s}",
bkp.Name,
bkp.Namespace,
)
}
return k.update(cs, bkp, k.namespace)
}