This commit is contained in:
Manuel de Brito Fontes 2017-09-17 15:42:31 -03:00
parent f478084cd8
commit 0661eaa08c
29 changed files with 264 additions and 281 deletions

View file

@ -21,8 +21,8 @@ import (
"os"
"strings"
api "k8s.io/api/core/v1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clientset "k8s.io/client-go/kubernetes"
)
@ -39,20 +39,20 @@ func ParseNameNS(input string) (string, string, error) {
// GetNodeIP returns the IP address of a node in the cluster
func GetNodeIP(kubeClient clientset.Interface, name string) string {
var externalIP string
node, err := kubeClient.Core().Nodes().Get(name, meta_v1.GetOptions{})
node, err := kubeClient.Core().Nodes().Get(name, metav1.GetOptions{})
if err != nil {
return externalIP
}
for _, address := range node.Status.Addresses {
if address.Type == api.NodeExternalIP {
if address.Type == apiv1.NodeExternalIP {
if address.Address != "" {
externalIP = address.Address
break
}
}
if externalIP == "" && address.Type == api.NodeInternalIP {
if externalIP == "" && address.Type == apiv1.NodeInternalIP {
externalIP = address.Address
}
}
@ -79,7 +79,7 @@ func GetPodDetails(kubeClient clientset.Interface) (*PodInfo, error) {
return nil, fmt.Errorf("unable to get POD information (missing POD_NAME or POD_NAMESPACE environment variable")
}
pod, _ := kubeClient.Core().Pods(podNs).Get(podName, meta_v1.GetOptions{})
pod, _ := kubeClient.Core().Pods(podNs).Get(podName, metav1.GetOptions{})
if pod == nil {
return nil, fmt.Errorf("unable to get POD information")
}

View file

@ -20,8 +20,8 @@ import (
"os"
"testing"
api "k8s.io/api/core/v1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
testclient "k8s.io/client-go/kubernetes/fake"
)
@ -66,14 +66,14 @@ func TestGetNodeIP(t *testing.T) {
{testclient.NewSimpleClientset(), "demo", ""},
// node not exist
{testclient.NewSimpleClientset(&api.NodeList{Items: []api.Node{{
ObjectMeta: meta_v1.ObjectMeta{
{testclient.NewSimpleClientset(&apiv1.NodeList{Items: []apiv1.Node{{
ObjectMeta: metav1.ObjectMeta{
Name: "demo",
},
Status: api.NodeStatus{
Addresses: []api.NodeAddress{
Status: apiv1.NodeStatus{
Addresses: []apiv1.NodeAddress{
{
Type: api.NodeInternalIP,
Type: apiv1.NodeInternalIP,
Address: "10.0.0.1",
},
},
@ -81,14 +81,14 @@ func TestGetNodeIP(t *testing.T) {
}}}), "notexistnode", ""},
// node exist
{testclient.NewSimpleClientset(&api.NodeList{Items: []api.Node{{
ObjectMeta: meta_v1.ObjectMeta{
{testclient.NewSimpleClientset(&apiv1.NodeList{Items: []apiv1.Node{{
ObjectMeta: metav1.ObjectMeta{
Name: "demo",
},
Status: api.NodeStatus{
Addresses: []api.NodeAddress{
Status: apiv1.NodeStatus{
Addresses: []apiv1.NodeAddress{
{
Type: api.NodeInternalIP,
Type: apiv1.NodeInternalIP,
Address: "10.0.0.1",
},
},
@ -96,28 +96,28 @@ func TestGetNodeIP(t *testing.T) {
}}}), "demo", "10.0.0.1"},
// search the correct node
{testclient.NewSimpleClientset(&api.NodeList{Items: []api.Node{
{testclient.NewSimpleClientset(&apiv1.NodeList{Items: []apiv1.Node{
{
ObjectMeta: meta_v1.ObjectMeta{
ObjectMeta: metav1.ObjectMeta{
Name: "demo1",
},
Status: api.NodeStatus{
Addresses: []api.NodeAddress{
Status: apiv1.NodeStatus{
Addresses: []apiv1.NodeAddress{
{
Type: api.NodeInternalIP,
Type: apiv1.NodeInternalIP,
Address: "10.0.0.1",
},
},
},
},
{
ObjectMeta: meta_v1.ObjectMeta{
ObjectMeta: metav1.ObjectMeta{
Name: "demo2",
},
Status: api.NodeStatus{
Addresses: []api.NodeAddress{
Status: apiv1.NodeStatus{
Addresses: []apiv1.NodeAddress{
{
Type: api.NodeInternalIP,
Type: apiv1.NodeInternalIP,
Address: "10.0.0.2",
},
},
@ -126,17 +126,17 @@ func TestGetNodeIP(t *testing.T) {
}}), "demo2", "10.0.0.2"},
// get NodeExternalIP
{testclient.NewSimpleClientset(&api.NodeList{Items: []api.Node{{
ObjectMeta: meta_v1.ObjectMeta{
{testclient.NewSimpleClientset(&apiv1.NodeList{Items: []apiv1.Node{{
ObjectMeta: metav1.ObjectMeta{
Name: "demo",
},
Status: api.NodeStatus{
Addresses: []api.NodeAddress{
Status: apiv1.NodeStatus{
Addresses: []apiv1.NodeAddress{
{
Type: api.NodeInternalIP,
Type: apiv1.NodeInternalIP,
Address: "10.0.0.1",
}, {
Type: api.NodeExternalIP,
Type: apiv1.NodeExternalIP,
Address: "10.0.0.2",
},
},
@ -144,17 +144,17 @@ func TestGetNodeIP(t *testing.T) {
}}}), "demo", "10.0.0.2"},
// get NodeInternalIP
{testclient.NewSimpleClientset(&api.NodeList{Items: []api.Node{{
ObjectMeta: meta_v1.ObjectMeta{
{testclient.NewSimpleClientset(&apiv1.NodeList{Items: []apiv1.Node{{
ObjectMeta: metav1.ObjectMeta{
Name: "demo",
},
Status: api.NodeStatus{
Addresses: []api.NodeAddress{
Status: apiv1.NodeStatus{
Addresses: []apiv1.NodeAddress{
{
Type: api.NodeExternalIP,
Type: apiv1.NodeExternalIP,
Address: "",
}, {
Type: api.NodeInternalIP,
Type: apiv1.NodeInternalIP,
Address: "10.0.0.2",
},
},
@ -181,7 +181,7 @@ func TestGetPodDetails(t *testing.T) {
// POD_NAME not exist
os.Setenv("POD_NAME", "")
os.Setenv("POD_NAMESPACE", api.NamespaceDefault)
os.Setenv("POD_NAMESPACE", apiv1.NamespaceDefault)
_, err2 := GetPodDetails(testclient.NewSimpleClientset())
if err2 == nil {
t.Errorf("expected an error but returned nil")
@ -197,7 +197,7 @@ func TestGetPodDetails(t *testing.T) {
// POD not exist
os.Setenv("POD_NAME", "testpod")
os.Setenv("POD_NAMESPACE", api.NamespaceDefault)
os.Setenv("POD_NAMESPACE", apiv1.NamespaceDefault)
_, err4 := GetPodDetails(testclient.NewSimpleClientset())
if err4 == nil {
t.Errorf("expected an error but returned nil")
@ -205,24 +205,24 @@ func TestGetPodDetails(t *testing.T) {
// success to get PodInfo
fkClient := testclient.NewSimpleClientset(
&api.PodList{Items: []api.Pod{{
ObjectMeta: meta_v1.ObjectMeta{
&apiv1.PodList{Items: []apiv1.Pod{{
ObjectMeta: metav1.ObjectMeta{
Name: "testpod",
Namespace: api.NamespaceDefault,
Namespace: apiv1.NamespaceDefault,
Labels: map[string]string{
"first": "first_label",
"second": "second_label",
},
},
}}},
&api.NodeList{Items: []api.Node{{
ObjectMeta: meta_v1.ObjectMeta{
&apiv1.NodeList{Items: []apiv1.Node{{
ObjectMeta: metav1.ObjectMeta{
Name: "demo",
},
Status: api.NodeStatus{
Addresses: []api.NodeAddress{
Status: apiv1.NodeStatus{
Addresses: []apiv1.NodeAddress{
{
Type: api.NodeInternalIP,
Type: apiv1.NodeInternalIP,
Address: "10.0.0.1",
},
},