Skip to content

Commit

Permalink
feat(config): namespace new parameter in volumesnapshotlocation (#36)
Browse files Browse the repository at this point in the history
* Adding support for new parameter `namespace` in volumesnapshotlocation

velero-plugin assumes that openebs is installed in namespace `openebs`.
If openebs is installed in different namespace then user can configure
this parameter to execute backup operation.

* Finding namespace for openebs if user has not given any

plugin will find maya-apiserver service using labelselector(for label `openebs.io/component-name=maya-apiserver-svc`)
or fieldselector(for name `maya-apiserver-svc`).

Signed-off-by: mayank <[email protected]>
  • Loading branch information
mynktl authored and vishnuitta committed Aug 9, 2019
1 parent 8d2dc78 commit 8ae13ad
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 16 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ spec:
provider: <GCP_OR_AWS>
region: <AWS_REGION>
```
If you have multiple installation of openebs then you need to add `spec.config.namespace: <OPENEBS_NAMESPACE>`.

*Note:*

- _`prefix` is for backup file name._
Expand Down
1 change: 1 addition & 0 deletions example/06-volumesnapshotlocation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ spec:
backupPathPrefix: <PREFIX_FOR_BACKUP_PATH>
provider: <GCP_OR_AWS>
region: <AWS_REGION>
namespace: <OPENEBS_NAMESPACE>
52 changes: 38 additions & 14 deletions pkg/cstor/cstor.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ import (

cloud "github.com/openebs/velero-plugin/pkg/clouduploader"
"github.com/pkg/errors"

/* Due to dependency conflict, please ensure openebs
* dependency manually instead of using dep
*/
v1alpha1 "github.com/openebs/maya/pkg/apis/openebs.io/v1alpha1"
"github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
k8serror "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -48,13 +48,17 @@ const (
mayaAPIServiceLabel = "openebs.io/component-name=maya-apiserver-svc"
backupEndpoint = "/latest/backups/"
restorePath = "/latest/restore/"
operator = "openebs"
casTypeCStor = "cstor"
backupStatusInterval = 5
restoreStatusInterval = 5
openebsVolumeLabel = "openebs.io/cas-type"
)

const (
// NAMESPACE config key for OpenEBS namespace
NAMESPACE = "namespace"
)

// Plugin defines snapshot plugin for CStor volume
type Plugin struct {
// Log is used for logging
Expand All @@ -66,6 +70,9 @@ type Plugin struct {
// config to store parameters from velero server
config map[string]string

// namespace in which openebs is installed, default is openebs
namespace string

// cl stores cloud connection information
cl *cloud.Conn

Expand Down Expand Up @@ -137,42 +144,59 @@ func (p *Plugin) getServerAddress() string {

// getMapiAddr return maya API server's ip address
func (p *Plugin) getMapiAddr() string {
sclist, err := p.K8sClient.Services(operator).List(
var openebsNs string

// check if user has provided openebs namespace
if p.namespace != "" {
openebsNs = p.namespace
} else {
openebsNs = metav1.NamespaceAll
}

sclist, err := p.K8sClient.Services(openebsNs).List(
metav1.ListOptions{
LabelSelector: mayaAPIServiceLabel,
},
)

if err != nil {
if k8serror.IsNotFound(err) {
goto usingname
}
p.Log.Errorf("Error getting maya-apiservice : %v", err.Error())
return ""
}

for _, s := range sclist.Items {
if len(s.Spec.ClusterIP) != 0 {
return "http://" + s.Spec.ClusterIP + ":" + strconv.FormatInt(int64(s.Spec.Ports[0].Port), 10)
}
if len(sclist.Items) != 0 {
goto fetchip
}

usingname:
// There are no any services having MayaApiService Label
// Let's try to find by name only..
sc, err := p.K8sClient.Services(operator).Get(mayaAPIServiceName, metav1.GetOptions{})
sclist, err = p.K8sClient.Services(openebsNs).List(
metav1.ListOptions{
FieldSelector: "metadata.name=" + mayaAPIServiceName,
})
if err != nil {
p.Log.Errorf("Error getting IP Address for service{%s} : %v", mayaAPIServiceName, err.Error())
return ""
}

if len(sc.Spec.ClusterIP) != 0 {
return "http://" + sc.Spec.ClusterIP + ":" + strconv.FormatInt(int64(sc.Spec.Ports[0].Port), 10)
fetchip:
for _, s := range sclist.Items {
if len(s.Spec.ClusterIP) != 0 {
// update the namespace
p.namespace = s.Namespace
return "http://" + s.Spec.ClusterIP + ":" + strconv.FormatInt(int64(s.Spec.Ports[0].Port), 10)
}
}

return ""
}

// Init CStor snapshot plugin
func (p *Plugin) Init(config map[string]string) error {
if ns, ok := config[NAMESPACE]; ok {
p.namespace = ns
}

conf, err := rest.InClusterConfig()
if err != nil {
p.Log.Errorf("Failed to get cluster config : %s", err.Error())
Expand Down
3 changes: 1 addition & 2 deletions pkg/snapshot/snap.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ var _ velero.VolumeSnapshotter = (*BlockStore)(nil)

// Init the plugin
func (p *BlockStore) Init(config map[string]string) error {
p.Log.Infof("Initializing velero plugin for CStor %v", config)
p.Log.Infof("Initializing velero plugin for CStor")

// TODO check for type of volume
p.plugin = &cstor.Plugin{Log: p.Log}
return p.plugin.Init(config)
}
Expand Down

0 comments on commit 8ae13ad

Please sign in to comment.