-
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.
Add k8s get node by name and list nodes
- Loading branch information
1 parent
a4b499e
commit 371abbf
Showing
7 changed files
with
277 additions
and
13 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
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 ( | ||
v1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes" | ||
) | ||
|
||
func GetNode(clientset *kubernetes.Clientset, name string) (*v1.Node, error) { | ||
return clientset.CoreV1().Nodes().Get(name, metav1.GetOptions{}) | ||
} | ||
|
||
func GetNodes(clientset *kubernetes.Clientset) ([]*v1.Node, error) { | ||
nodes := []*v1.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,39 @@ | ||
package kubernetes | ||
|
||
import ( | ||
"github.com/linkernetworks/config" | ||
"github.com/linkernetworks/service/kubernetes" | ||
"github.com/stretchr/testify/assert" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestGetNodeFail(t *testing.T) { | ||
if _, ok := os.LookupEnv("TEST_K8S"); !ok { | ||
t.SkipNow() | ||
} | ||
|
||
kubernetes := kubernetes.NewFromConfig(&config.KubernetesConfig{ | ||
Namespace: "default", | ||
}) | ||
clientset, err := kubernetes.NewClientset() | ||
assert.NoError(t, err) | ||
|
||
_, err = GetNode(clientset, "UnKnown_Name") | ||
assert.Error(t, err) | ||
} | ||
|
||
func TestGetNodes(t *testing.T) { | ||
if _, ok := os.LookupEnv("TEST_K8S"); !ok { | ||
t.SkipNow() | ||
} | ||
|
||
kubernetes := kubernetes.NewFromConfig(&config.KubernetesConfig{ | ||
Namespace: "default", | ||
}) | ||
clientset, err := kubernetes.NewClientset() | ||
assert.NoError(t, err) | ||
|
||
nodes, err := GetNodes(clientset) | ||
assert.NoError(t, err) | ||
} |
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.