Skip to content

Commit 61bd182

Browse files
author
Harsh Vardhan
authored
Add support to list jiva volumes along with CStor volumes (#28)
* Add jiva CRDs to module from openebs/jiva-operator Co-authored-by: Shubham Bajpai (@shubham14bajpai) <[email protected]> * Add support to get jiva volumes along with CStor volumes Signed-off-by: Harsh Vardhan <[email protected]>
1 parent a95f52d commit 61bd182

File tree

6 files changed

+368
-57
lines changed

6 files changed

+368
-57
lines changed

client/k8s.go

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/openebs/api/v2/pkg/apis/openebs.io/v1alpha1"
2929
cstortypes "github.com/openebs/api/v2/pkg/apis/types"
3030
openebsclientset "github.com/openebs/api/v2/pkg/client/clientset/versioned"
31+
jiva "github.com/openebs/jiva-operator/pkg/apis/openebs/v1alpha1"
3132
"github.com/openebs/openebsctl/kubectl-openebs/cli/util"
3233
"github.com/pkg/errors"
3334
corev1 "k8s.io/api/core/v1"
@@ -141,6 +142,28 @@ func (k K8sClient) GetOpenEBSNamespace(casType string) (string, error) {
141142
return pods.Items[0].Namespace, nil
142143
}
143144

145+
// GetOpenEBSNamespaceMap maps the cas-type to it's namespace, e.g. n[cstor] = cstor-ns
146+
func (k K8sClient) GetOpenEBSNamespaceMap() (map[string]string, error) {
147+
label := "openebs.io/component-name in ("
148+
for _, v := range util.CasTypeAndComponentNameMap {
149+
label = label + v + ","
150+
}
151+
label += ")"
152+
pods, err := k.K8sCS.CoreV1().Pods("").List(context.TODO(), metav1.ListOptions{LabelSelector: label})
153+
if err != nil || pods == nil || len(pods.Items) == 0 {
154+
return nil, errors.New("unable to determine openebs namespace")
155+
}
156+
NSmap := make(map[string]string)
157+
for _, pod := range pods.Items {
158+
ns := pod.Namespace
159+
cas, ok := util.ComponentNameToCasTypeMap[pod.Labels["openebs.io/component-name"]]
160+
if ok {
161+
NSmap[cas] = ns
162+
}
163+
}
164+
return NSmap, nil
165+
}
166+
144167
// GetStorageClass using the K8sClient's storage class client
145168
func (k K8sClient) GetStorageClass(driver string) (*v1.StorageClass, error) {
146169
scs, err := k.K8sCS.StorageV1().StorageClasses().Get(context.TODO(), driver, metav1.GetOptions{})
@@ -418,3 +441,98 @@ func (k K8sClient) GetCstorVolumeRestores(pvName string) (*cstorv1.CStorRestoreL
418441
return cStorRestoreList, nil
419442

420443
}
444+
445+
// GetPVs returns a list of PersistentVolumes
446+
func (k K8sClient) GetPVs() (*corev1.PersistentVolumeList, error) {
447+
pvs, err := k.K8sCS.CoreV1().PersistentVolumes().List(context.TODO(), metav1.ListOptions{})
448+
if err != nil {
449+
return nil, err
450+
}
451+
return pvs, nil
452+
}
453+
454+
// GetJivaVolumes returns a list of jivavolumes
455+
func (k K8sClient) GetJivaVolumes() (*jiva.JivaVolumeList, error) {
456+
jv := jiva.JivaVolumeList{}
457+
// NOTE: The resource name must be plural and the API-group should be present for getting CRs
458+
err := k.K8sCS.Discovery().RESTClient().Get().AbsPath("/apis/openebs.io/v1alpha1").
459+
Resource("jivavolumes").Do(context.TODO()).Into(&jv)
460+
if err != nil {
461+
return nil, err
462+
}
463+
return &jv, nil
464+
}
465+
466+
// GetPVbyName gets a list of PVs by the with name in vols in order
467+
func (k K8sClient) GetPVbyName(vols []string) (*corev1.PersistentVolumeList, error) {
468+
pv, err := k.GetPVs()
469+
if err != nil {
470+
return nil, err
471+
}
472+
volMap := make(map[string]corev1.PersistentVolume)
473+
for _, vol := range pv.Items {
474+
volMap[vol.Name] = vol
475+
}
476+
var list []corev1.PersistentVolume
477+
for _, name := range vols {
478+
if pool, ok := volMap[name]; ok {
479+
list = append(list, pool)
480+
} else {
481+
fmt.Printf("Error from server (NotFound): PV %s not found\n", name)
482+
}
483+
}
484+
return &corev1.PersistentVolumeList{
485+
Items: list,
486+
}, nil
487+
}
488+
489+
// GetJivaVolume gets a single JivaVolume jv
490+
func (k K8sClient) GetJivaVolume(jv string) (*jiva.JivaVolume, error) {
491+
var j jiva.JivaVolume
492+
err := k.K8sCS.Discovery().RESTClient().Get().Namespace(k.Ns).Name(jv).AbsPath("/apis/openebs.io/v1alpha1").
493+
Resource("jivavolumes").Do(context.TODO()).Into(&j)
494+
if err != nil {
495+
return nil, err
496+
}
497+
return &j, nil
498+
}
499+
500+
// GetJivaVolumeMap returns a map[jvName] -> jv from all namespaces
501+
func (k K8sClient) GetJivaVolumeMap() (map[string]jiva.JivaVolume, error) {
502+
jvs, err := k.GetJivaVolumes()
503+
if err != nil {
504+
return nil, err
505+
}
506+
jvMap := make(map[string]jiva.JivaVolume)
507+
for _, jv := range jvs.Items {
508+
jvMap[jv.Name] = jv
509+
}
510+
return jvMap, nil
511+
}
512+
513+
// GetCStorVolumeMap returns a map[cvName] -> cv from all namespaces
514+
func (k K8sClient) GetCStorVolumeMap() (map[string]cstorv1.CStorVolume, error) {
515+
cvs, err := k.GetcStorVolumes()
516+
if err != nil {
517+
return nil, err
518+
}
519+
cvMap := make(map[string]cstorv1.CStorVolume)
520+
for _, cv := range cvs.Items {
521+
cvMap[cv.Name] = cv
522+
}
523+
return cvMap, nil
524+
}
525+
526+
// GetCStorVolumeAttachmentMap returns a map[volName] -> cva from all namespaces
527+
func (k K8sClient) GetCStorVolumeAttachmentMap() (map[string]cstorv1.CStorVolumeAttachment, error) {
528+
cvs, err := k.OpenebsCS.CstorV1().CStorVolumeAttachments("").List(context.TODO(), metav1.ListOptions{})
529+
if err != nil {
530+
return nil, err
531+
}
532+
cvMap := make(map[string]cstorv1.CStorVolumeAttachment)
533+
for _, cv := range cvs.Items {
534+
vol := cv.Labels["Volname"]
535+
cvMap[vol] = cv
536+
}
537+
return cvMap, nil
538+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.14
55
require (
66
github.com/dustin/go-humanize v1.0.0
77
github.com/openebs/api/v2 v2.3.0
8+
github.com/openebs/jiva-operator v1.12.2-0.20210607114402-811a3af7c34a
89
github.com/pkg/errors v0.9.1
910
github.com/ryanuber/columnize v2.1.0+incompatible
1011
github.com/spf13/cobra v1.1.1

0 commit comments

Comments
 (0)