-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from linkernetworks/alex/AddK8sGetNode
Add k8s get node by name and list nodes
- Loading branch information
Showing
6 changed files
with
490 additions
and
7 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
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,25 @@ | ||
package kubernetes | ||
|
||
import ( | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes" | ||
) | ||
|
||
func GetNode(clientset kubernetes.Interface, name string) (*corev1.Node, error) { | ||
return clientset.CoreV1().Nodes().Get(name, metav1.GetOptions{}) | ||
} | ||
|
||
func GetNodes(clientset kubernetes.Interface) ([]*corev1.Node, error) { | ||
nodes := []*corev1.Node{} | ||
nodesList, err := clientset.CoreV1().Nodes().List(metav1.ListOptions{}) | ||
if err != nil { | ||
return nodes, err | ||
} | ||
|
||
for _, n := range nodesList.Items { | ||
nodes = append(nodes, &n) | ||
} | ||
|
||
return nodes, nil | ||
} |
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,56 @@ | ||
package kubernetes | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
fakeclientset "k8s.io/client-go/kubernetes/fake" | ||
"testing" | ||
) | ||
|
||
func TestGetNode(t *testing.T) { | ||
clientset := fakeclientset.NewSimpleClientset() | ||
|
||
node := corev1.Node{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "K8S-Node", | ||
}, | ||
} | ||
_, err := clientset.CoreV1().Nodes().Create(&node) | ||
assert.NoError(t, err) | ||
|
||
result, err := GetNode(clientset, "K8S-Node") | ||
assert.NoError(t, err) | ||
assert.Equal(t, node.GetName(), result.GetName()) | ||
} | ||
|
||
func TestGetNodeFail(t *testing.T) { | ||
clientset := fakeclientset.NewSimpleClientset() | ||
|
||
_, err := GetNode(clientset, "UnKnown_Name") | ||
assert.Error(t, err) | ||
} | ||
|
||
func TestGetNodes(t *testing.T) { | ||
clientset := fakeclientset.NewSimpleClientset() | ||
|
||
node := corev1.Node{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "K8S-Node1", | ||
}, | ||
} | ||
_, err := clientset.CoreV1().Nodes().Create(&node) | ||
assert.NoError(t, err) | ||
|
||
node = corev1.Node{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "K8S-Node2", | ||
}, | ||
} | ||
_, err = clientset.CoreV1().Nodes().Create(&node) | ||
assert.NoError(t, err) | ||
|
||
nodes, err := GetNodes(clientset) | ||
assert.NoError(t, err) | ||
assert.Equal(t, 2, len(nodes)) | ||
} |
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
Oops, something went wrong.