parent
aba58d67f2
commit
f0f9618a89
2 changed files with 136 additions and 89 deletions
|
@ -48,16 +48,20 @@ func GetNodeIPOrName(kubeClient clientset.Interface, name string, useInternalIP
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
if useInternalIP {
|
defaultOrInternalIP := ""
|
||||||
for _, address := range node.Status.Addresses {
|
for _, address := range node.Status.Addresses {
|
||||||
if address.Type == apiv1.NodeInternalIP {
|
if address.Type == apiv1.NodeInternalIP {
|
||||||
if address.Address != "" {
|
if address.Address != "" {
|
||||||
return address.Address
|
defaultOrInternalIP = address.Address
|
||||||
}
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if useInternalIP {
|
||||||
|
return defaultOrInternalIP
|
||||||
|
}
|
||||||
|
|
||||||
for _, address := range node.Status.Addresses {
|
for _, address := range node.Status.Addresses {
|
||||||
if address.Type == apiv1.NodeExternalIP {
|
if address.Type == apiv1.NodeExternalIP {
|
||||||
if address.Address != "" {
|
if address.Address != "" {
|
||||||
|
@ -66,7 +70,7 @@ func GetNodeIPOrName(kubeClient clientset.Interface, name string, useInternalIP
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return ""
|
return defaultOrInternalIP
|
||||||
}
|
}
|
||||||
|
|
||||||
// PodInfo contains runtime information about the pod running the Ingres controller
|
// PodInfo contains runtime information about the pod running the Ingres controller
|
||||||
|
|
|
@ -58,49 +58,22 @@ func TestParseNameNS(t *testing.T) {
|
||||||
|
|
||||||
func TestGetNodeIP(t *testing.T) {
|
func TestGetNodeIP(t *testing.T) {
|
||||||
fKNodes := []struct {
|
fKNodes := []struct {
|
||||||
cs *testclient.Clientset
|
name string
|
||||||
n string
|
cs *testclient.Clientset
|
||||||
ea string
|
nodeName string
|
||||||
i bool
|
ea string
|
||||||
|
useInternalIP bool
|
||||||
}{
|
}{
|
||||||
// empty node list
|
{
|
||||||
{testclient.NewSimpleClientset(), "demo", "", true},
|
"empty node list",
|
||||||
|
testclient.NewSimpleClientset(),
|
||||||
// node not exist
|
"demo", "", true,
|
||||||
{testclient.NewSimpleClientset(&apiv1.NodeList{Items: []apiv1.Node{{
|
},
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
{
|
||||||
Name: "demo",
|
"node does not exist",
|
||||||
},
|
testclient.NewSimpleClientset(&apiv1.NodeList{Items: []apiv1.Node{{
|
||||||
Status: apiv1.NodeStatus{
|
|
||||||
Addresses: []apiv1.NodeAddress{
|
|
||||||
{
|
|
||||||
Type: apiv1.NodeInternalIP,
|
|
||||||
Address: "10.0.0.1",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}}}), "notexistnode", "", true},
|
|
||||||
|
|
||||||
// node exist
|
|
||||||
{testclient.NewSimpleClientset(&apiv1.NodeList{Items: []apiv1.Node{{
|
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
|
||||||
Name: "demo",
|
|
||||||
},
|
|
||||||
Status: apiv1.NodeStatus{
|
|
||||||
Addresses: []apiv1.NodeAddress{
|
|
||||||
{
|
|
||||||
Type: apiv1.NodeInternalIP,
|
|
||||||
Address: "10.0.0.1",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}}}), "demo", "10.0.0.1", true},
|
|
||||||
|
|
||||||
// search the correct node
|
|
||||||
{testclient.NewSimpleClientset(&apiv1.NodeList{Items: []apiv1.Node{
|
|
||||||
{
|
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
Name: "demo1",
|
Name: "demo",
|
||||||
},
|
},
|
||||||
Status: apiv1.NodeStatus{
|
Status: apiv1.NodeStatus{
|
||||||
Addresses: []apiv1.NodeAddress{
|
Addresses: []apiv1.NodeAddress{
|
||||||
|
@ -110,63 +83,133 @@ func TestGetNodeIP(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
}}}), "notexistnode", "", true,
|
||||||
{
|
},
|
||||||
|
{
|
||||||
|
"node exist and only has an internal IP address (useInternalIP=false)",
|
||||||
|
testclient.NewSimpleClientset(&apiv1.NodeList{Items: []apiv1.Node{{
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
Name: "demo2",
|
Name: "demo",
|
||||||
},
|
},
|
||||||
Status: apiv1.NodeStatus{
|
Status: apiv1.NodeStatus{
|
||||||
Addresses: []apiv1.NodeAddress{
|
Addresses: []apiv1.NodeAddress{
|
||||||
{
|
{
|
||||||
Type: apiv1.NodeInternalIP,
|
Type: apiv1.NodeInternalIP,
|
||||||
|
Address: "10.0.0.1",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}}}), "demo", "10.0.0.1", false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"node exist and only has an internal IP address",
|
||||||
|
testclient.NewSimpleClientset(&apiv1.NodeList{Items: []apiv1.Node{{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "demo",
|
||||||
|
},
|
||||||
|
Status: apiv1.NodeStatus{
|
||||||
|
Addresses: []apiv1.NodeAddress{
|
||||||
|
{
|
||||||
|
Type: apiv1.NodeInternalIP,
|
||||||
|
Address: "10.0.0.1",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}}}), "demo", "10.0.0.1", true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"node exist and only has an external IP address",
|
||||||
|
testclient.NewSimpleClientset(&apiv1.NodeList{Items: []apiv1.Node{{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "demo",
|
||||||
|
},
|
||||||
|
Status: apiv1.NodeStatus{
|
||||||
|
Addresses: []apiv1.NodeAddress{
|
||||||
|
{
|
||||||
|
Type: apiv1.NodeExternalIP,
|
||||||
|
Address: "10.0.0.1",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}}}), "demo", "10.0.0.1", false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"multiple nodes - choose the right one",
|
||||||
|
testclient.NewSimpleClientset(&apiv1.NodeList{Items: []apiv1.Node{
|
||||||
|
{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "demo1",
|
||||||
|
},
|
||||||
|
Status: apiv1.NodeStatus{
|
||||||
|
Addresses: []apiv1.NodeAddress{
|
||||||
|
{
|
||||||
|
Type: apiv1.NodeInternalIP,
|
||||||
|
Address: "10.0.0.1",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "demo2",
|
||||||
|
},
|
||||||
|
Status: apiv1.NodeStatus{
|
||||||
|
Addresses: []apiv1.NodeAddress{
|
||||||
|
{
|
||||||
|
Type: apiv1.NodeInternalIP,
|
||||||
|
Address: "10.0.0.2",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}}),
|
||||||
|
"demo2", "10.0.0.2", true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"node with both IP internal and external IP address - returns external IP",
|
||||||
|
testclient.NewSimpleClientset(&apiv1.NodeList{Items: []apiv1.Node{{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "demo",
|
||||||
|
},
|
||||||
|
Status: apiv1.NodeStatus{
|
||||||
|
Addresses: []apiv1.NodeAddress{
|
||||||
|
{
|
||||||
|
Type: apiv1.NodeInternalIP,
|
||||||
|
Address: "10.0.0.1",
|
||||||
|
}, {
|
||||||
|
Type: apiv1.NodeExternalIP,
|
||||||
Address: "10.0.0.2",
|
Address: "10.0.0.2",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
}}}),
|
||||||
}}), "demo2", "10.0.0.2", true},
|
"demo", "10.0.0.2", false,
|
||||||
|
},
|
||||||
// get NodeExternalIP
|
{
|
||||||
{testclient.NewSimpleClientset(&apiv1.NodeList{Items: []apiv1.Node{{
|
"node with both IP internal and external IP address - returns internal IP",
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
testclient.NewSimpleClientset(&apiv1.NodeList{Items: []apiv1.Node{{
|
||||||
Name: "demo",
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
},
|
Name: "demo",
|
||||||
Status: apiv1.NodeStatus{
|
},
|
||||||
Addresses: []apiv1.NodeAddress{
|
Status: apiv1.NodeStatus{
|
||||||
{
|
Addresses: []apiv1.NodeAddress{
|
||||||
Type: apiv1.NodeInternalIP,
|
{
|
||||||
Address: "10.0.0.1",
|
Type: apiv1.NodeExternalIP,
|
||||||
}, {
|
Address: "",
|
||||||
Type: apiv1.NodeExternalIP,
|
}, {
|
||||||
Address: "10.0.0.2",
|
Type: apiv1.NodeInternalIP,
|
||||||
|
Address: "10.0.0.2",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
}}}),
|
||||||
}}}), "demo", "10.0.0.2", false},
|
"demo", "10.0.0.2", true},
|
||||||
|
|
||||||
// get NodeInternalIP
|
|
||||||
{testclient.NewSimpleClientset(&apiv1.NodeList{Items: []apiv1.Node{{
|
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
|
||||||
Name: "demo",
|
|
||||||
},
|
|
||||||
Status: apiv1.NodeStatus{
|
|
||||||
Addresses: []apiv1.NodeAddress{
|
|
||||||
{
|
|
||||||
Type: apiv1.NodeExternalIP,
|
|
||||||
Address: "",
|
|
||||||
}, {
|
|
||||||
Type: apiv1.NodeInternalIP,
|
|
||||||
Address: "10.0.0.2",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}}}), "demo", "10.0.0.2", true},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, fk := range fKNodes {
|
for _, fk := range fKNodes {
|
||||||
address := GetNodeIPOrName(fk.cs, fk.n, fk.i)
|
address := GetNodeIPOrName(fk.cs, fk.nodeName, fk.useInternalIP)
|
||||||
if address != fk.ea {
|
if address != fk.ea {
|
||||||
t.Errorf("expected %s, but returned %s", fk.ea, address)
|
t.Errorf("%v - expected %s, but returned %s", fk.name, fk.ea, address)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue