Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add falg to report node internal IP address in ingress status #1503

Merged
merged 1 commit into from
Oct 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pkg/ingress/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,11 @@ type Configuration struct {
Backend ingress.Controller

UpdateStatus bool
UseNodeInternalIP bool
ElectionID string
UpdateStatusOnShutdown bool
SortBackends bool

SortBackends bool
}

// newIngressController creates an Ingress controller
Expand Down Expand Up @@ -174,6 +176,7 @@ func newIngressController(config *Configuration) *GenericController {
DefaultIngressClass: config.DefaultIngressClass,
UpdateStatusOnShutdown: config.UpdateStatusOnShutdown,
CustomIngressStatus: ic.cfg.Backend.UpdateIngressStatus,
UseNodeInternalIP: ic.cfg.UseNodeInternalIP,
})
} else {
glog.Warning("Update of ingress status is disabled (flag --update-status=false was specified)")
Expand Down
4 changes: 4 additions & 0 deletions pkg/ingress/controller/launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ func NewIngressController(backend ingress.Controller) *GenericController {

sortBackends = flags.Bool("sort-backends", false,
`Defines if backends and it's endpoints should be sorted`)

useNodeInternalIP = flags.Bool("report-node-internal-ip-address", false,
`Defines if the nodes IP address to be returned in the ingress status should be the internal instead of the external IP address`)
)

flags.AddGoFlagSet(flag.CommandLine)
Expand Down Expand Up @@ -198,6 +201,7 @@ func NewIngressController(backend ingress.Controller) *GenericController {
DisableNodeList: *disableNodeList,
UpdateStatusOnShutdown: *updateStatusOnShutdown,
SortBackends: *sortBackends,
UseNodeInternalIP: *useNodeInternalIP,
}

ic := newIngressController(config)
Expand Down
4 changes: 3 additions & 1 deletion pkg/ingress/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ type Config struct {

UpdateStatusOnShutdown bool

UseNodeInternalIP bool

IngressLister store.IngressLister

DefaultIngressClass string
Expand Down Expand Up @@ -267,7 +269,7 @@ func (s *statusSync) runningAddresses() ([]string, error) {

addrs := []string{}
for _, pod := range pods.Items {
name := k8s.GetNodeIP(s.Client, pod.Spec.NodeName)
name := k8s.GetNodeIP(s.Client, pod.Spec.NodeName, s.UseNodeInternalIP)
if !sliceutils.StringInSlice(name, addrs) {
addrs = append(addrs, name)
}
Expand Down
26 changes: 15 additions & 11 deletions pkg/k8s/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,30 @@ 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
func GetNodeIP(kubeClient clientset.Interface, name string, useInternalIP bool) string {
node, err := kubeClient.Core().Nodes().Get(name, metav1.GetOptions{})
if err != nil {
return externalIP
return ""
}

for _, address := range node.Status.Addresses {
if address.Type == apiv1.NodeExternalIP {
if address.Address != "" {
externalIP = address.Address
break
if useInternalIP {
if address.Type == apiv1.NodeInternalIP {
if address.Address != "" {
return address.Address
}
}
continue
}

if externalIP == "" && address.Type == apiv1.NodeInternalIP {
externalIP = address.Address
if address.Type == apiv1.NodeExternalIP {
if address.Address != "" {
return address.Address
}
}
}
return externalIP

return ""
}

// PodInfo contains runtime information about the pod running the Ingres controller
Expand Down Expand Up @@ -87,7 +91,7 @@ func GetPodDetails(kubeClient clientset.Interface) (*PodInfo, error) {
return &PodInfo{
Name: podName,
Namespace: podNs,
NodeIP: GetNodeIP(kubeClient, pod.Spec.NodeName),
NodeIP: GetNodeIP(kubeClient, pod.Spec.NodeName, true),
Labels: pod.GetLabels(),
}, nil
}
15 changes: 8 additions & 7 deletions pkg/k8s/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ func TestGetNodeIP(t *testing.T) {
cs *testclient.Clientset
n string
ea string
i bool
}{
// empty node list
{testclient.NewSimpleClientset(), "demo", ""},
{testclient.NewSimpleClientset(), "demo", "", true},

// node not exist
{testclient.NewSimpleClientset(&apiv1.NodeList{Items: []apiv1.Node{{
Expand All @@ -78,7 +79,7 @@ func TestGetNodeIP(t *testing.T) {
},
},
},
}}}), "notexistnode", ""},
}}}), "notexistnode", "", true},

// node exist
{testclient.NewSimpleClientset(&apiv1.NodeList{Items: []apiv1.Node{{
Expand All @@ -93,7 +94,7 @@ func TestGetNodeIP(t *testing.T) {
},
},
},
}}}), "demo", "10.0.0.1"},
}}}), "demo", "10.0.0.1", true},

// search the correct node
{testclient.NewSimpleClientset(&apiv1.NodeList{Items: []apiv1.Node{
Expand Down Expand Up @@ -123,7 +124,7 @@ func TestGetNodeIP(t *testing.T) {
},
},
},
}}), "demo2", "10.0.0.2"},
}}), "demo2", "10.0.0.2", true},

// get NodeExternalIP
{testclient.NewSimpleClientset(&apiv1.NodeList{Items: []apiv1.Node{{
Expand All @@ -141,7 +142,7 @@ func TestGetNodeIP(t *testing.T) {
},
},
},
}}}), "demo", "10.0.0.2"},
}}}), "demo", "10.0.0.2", false},

// get NodeInternalIP
{testclient.NewSimpleClientset(&apiv1.NodeList{Items: []apiv1.Node{{
Expand All @@ -159,11 +160,11 @@ func TestGetNodeIP(t *testing.T) {
},
},
},
}}}), "demo", "10.0.0.2"},
}}}), "demo", "10.0.0.2", true},
}

for _, fk := range fKNodes {
address := GetNodeIP(fk.cs, fk.n)
address := GetNodeIP(fk.cs, fk.n, fk.i)
if address != fk.ea {
t.Errorf("expected %s, but returned %s", fk.ea, address)
}
Expand Down