Skip to content

Commit 2c5f950

Browse files
prateekpandey14Kiran Mova
authored andcommitted
feat(topology): add support for custom topology keys
Signed-off-by: prateekpandey14 <[email protected]>
1 parent 034d231 commit 2c5f950

File tree

2 files changed

+74
-11
lines changed

2 files changed

+74
-11
lines changed

pkg/driver/controller.go

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/container-storage-interface/spec/lib/go/csi"
2525
apisv1 "github.com/openebs/api/v2/pkg/apis/cstor/v1"
2626
"github.com/openebs/cstor-csi/pkg/env"
27+
k8snode "github.com/openebs/cstor-csi/pkg/kubernetes/node"
2728
csipayload "github.com/openebs/cstor-csi/pkg/payload"
2829
analytics "github.com/openebs/cstor-csi/pkg/usage"
2930
utils "github.com/openebs/cstor-csi/pkg/utils"
@@ -33,6 +34,7 @@ import (
3334
"google.golang.org/grpc/codes"
3435
"google.golang.org/grpc/status"
3536
k8serror "k8s.io/apimachinery/pkg/api/errors"
37+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3638
)
3739

3840
// controller is the server implementation
@@ -106,7 +108,10 @@ func (cs *controller) CreateVolume(
106108
pvcName := req.GetParameters()[pvcNameKey]
107109
pvcNamespace := req.GetParameters()[pvcNamespaceKey]
108110

109-
nodeID = getAccessibilityRequirements(req.GetAccessibilityRequirements())
111+
nodeID, err = getAccessibilityRequirements(req.GetAccessibilityRequirements())
112+
if err != nil {
113+
return nil, err
114+
}
110115

111116
contentSource := req.GetVolumeContentSource()
112117
if contentSource != nil && contentSource.GetSnapshot() != nil {
@@ -337,20 +342,20 @@ func (cs *controller) ListVolumes(
337342
return nil, status.Error(codes.Unimplemented, "")
338343
}
339344

340-
func getAccessibilityRequirements(requirement *csi.TopologyRequirement) string {
345+
func getAccessibilityRequirements(requirement *csi.TopologyRequirement) (string, error) {
341346
if requirement == nil {
342-
return ""
347+
return "", status.Error(codes.Internal, "accessibility_requirements not found")
343348
}
344349

345-
preferredNode, exists := requirement.GetPreferred()[0].GetSegments()[TopologyNodeKey]
346-
if exists {
347-
return preferredNode
350+
node, err := getNode(requirement)
351+
if err != nil {
352+
return "", status.Errorf(codes.Internal, "failed to get the accessibility_requirements node %v", err)
348353
}
349-
preferredNode, exists = requirement.GetRequisite()[0].GetSegments()[TopologyNodeKey]
350-
if exists {
351-
return preferredNode
354+
355+
if len(node) == 0 {
356+
return "", status.Error(codes.Internal, "can not find any node")
352357
}
353-
return ""
358+
return node, nil
354359
}
355360

356361
// sendEventOrIgnore sends anonymous cstor provision/delete events
@@ -366,3 +371,28 @@ func sendEventOrIgnore(pvcName, pvName, capacity, replicaCount, stgType, method
366371
SetVolumeCapacity(capacity).Send()
367372
}
368373
}
374+
375+
// getNode gets the node which satisfies the topology info
376+
func getNode(topo *csi.TopologyRequirement) (string, error) {
377+
378+
list, err := k8snode.NewKubeClient().List(metav1.ListOptions{})
379+
if err != nil {
380+
return "", err
381+
}
382+
383+
for _, prf := range topo.Preferred {
384+
for _, node := range list.Items {
385+
nodeFiltered := false
386+
for key, value := range prf.Segments {
387+
if node.Labels[key] != value {
388+
nodeFiltered = true
389+
break
390+
}
391+
}
392+
if nodeFiltered == false {
393+
return node.Name, nil
394+
}
395+
}
396+
}
397+
return "", nil
398+
}

pkg/driver/node.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@ import (
2525
"github.com/container-storage-interface/spec/lib/go/csi"
2626
apis "github.com/openebs/cstor-csi/pkg/apis/cstor/v1"
2727
iscsiutils "github.com/openebs/cstor-csi/pkg/iscsi"
28+
k8snode "github.com/openebs/cstor-csi/pkg/kubernetes/node"
2829
utils "github.com/openebs/cstor-csi/pkg/utils"
2930
"github.com/sirupsen/logrus"
3031
"golang.org/x/net/context"
3132
"golang.org/x/sys/unix"
3233
"google.golang.org/grpc/codes"
3334
"google.golang.org/grpc/status"
35+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3436
)
3537

3638
// node is the server implementation
@@ -64,8 +66,39 @@ func (ns *node) NodeGetInfo(
6466
ctx context.Context,
6567
req *csi.NodeGetInfoRequest,
6668
) (*csi.NodeGetInfoResponse, error) {
69+
node, err := k8snode.NewKubeClient().Get(ns.driver.config.NodeID, metav1.GetOptions{})
70+
if err != nil {
71+
logrus.Errorf("failed to get the node %s", ns.driver.config.NodeID)
72+
return nil, err
73+
}
74+
75+
/*
76+
* The driver will support all the keys and values defined in the node's label.
77+
* if nodes are labeled with the below keys and values
78+
* map[beta.kubernetes.io/arch:amd64 beta.kubernetes.io/os:linux
79+
* kubernetes.io/arch:amd64 kubernetes.io/hostname:storage-node-1
80+
* kubernetes.io/os:linux node-role.kubernetes.io/worker:true
81+
* openebs.io/zone:zone1 openebs.io/zpool:ssd]
82+
* The driver will support below key and values
83+
*
84+
* {
85+
* beta.kubernetes.io/arch:amd64
86+
* beta.kubernetes.io/os:linux
87+
* kubernetes.io/arch:amd64
88+
* kubernetes.io/hostname:storage-node-1
89+
* kubernetes.io/os:linux
90+
* node-role.kubernetes.io/worker:true
91+
* openebs.io/zone:zone1
92+
* openebs.io/zpool:ssd
93+
* }
94+
*/
95+
96+
// support all the keys that node has
97+
topology := node.Labels
98+
99+
// add driver's topology key
100+
topology[TopologyNodeKey] = ns.driver.config.NodeID
67101

68-
topology := map[string]string{TopologyNodeKey: ns.driver.config.NodeID}
69102
return &csi.NodeGetInfoResponse{
70103
NodeId: ns.driver.config.NodeID,
71104
AccessibleTopology: &csi.Topology{

0 commit comments

Comments
 (0)