-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add topology awareness for node plugin
- Loading branch information
Amit Roushan
committed
Aug 31, 2021
1 parent
e59580d
commit ca8a32f
Showing
13 changed files
with
166 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,27 @@ | ||
package driver | ||
|
||
import ( | ||
"strings" | ||
"utils/k8sutils" | ||
) | ||
|
||
type Driver struct { | ||
name string | ||
version string | ||
useMultiPath bool | ||
isNeedMultiPath bool | ||
k8sUtils k8sutils.Interface | ||
nodeName string | ||
} | ||
|
||
func NewDriver(name, version string, useMultiPath, isNeedMultiPath bool) *Driver { | ||
func NewDriver(name, version string, useMultiPath, isNeedMultiPath bool, | ||
k8sUtils k8sutils.Interface, nodeName string) *Driver { | ||
return &Driver{ | ||
name: name, | ||
version: version, | ||
useMultiPath: useMultiPath, | ||
isNeedMultiPath: isNeedMultiPath, | ||
k8sUtils: k8sUtils, | ||
nodeName: strings.TrimSpace(nodeName), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package k8sutils | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"regexp" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/rest" | ||
"k8s.io/client-go/tools/clientcmd" | ||
) | ||
|
||
const ( | ||
topologyRegx = "topology.kubernetes.io/*" | ||
) | ||
|
||
type Interface interface { | ||
GetNodeTopology(nodeName string) (map[string]string, error) | ||
} | ||
|
||
type KubeClient struct { | ||
clientSet *kubernetes.Clientset | ||
} | ||
|
||
func NewK8SUtils(kubeConfig string) (Interface, error) { | ||
var clientset *kubernetes.Clientset | ||
|
||
if kubeConfig != "" { | ||
config, err := clientcmd.BuildConfigFromFlags("", kubeConfig) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
clientset, err = kubernetes.NewForConfig(config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} else { | ||
config, err := rest.InClusterConfig() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
clientset, err = kubernetes.NewForConfig(config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
return &KubeClient{ | ||
clientSet: clientset, | ||
}, nil | ||
} | ||
|
||
func (k *KubeClient) GetNodeTopology(nodeName string) (map[string]string, error) { | ||
k8sNode, err := k.getNode(nodeName) | ||
if err != nil { | ||
return nil, errors.New(fmt.Sprintf("failed to get node topology with error: %v", err)) | ||
} | ||
|
||
topology := make(map[string]string) | ||
for key, value := range k8sNode.Labels { | ||
if match, err := regexp.MatchString(topologyRegx, key); err == nil && match { | ||
topology[key] = value | ||
} | ||
} | ||
|
||
return topology, nil | ||
} | ||
|
||
func (k *KubeClient) getNode(nodeName string) (*corev1.Node, error) { | ||
return k.clientSet.CoreV1().Nodes().Get(nodeName, metav1.GetOptions{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters