diff --git a/pkg/master/master.go b/pkg/master/master.go index 23188b49ab4..d7cf999a78f 100644 --- a/pkg/master/master.go +++ b/pkg/master/master.go @@ -758,12 +758,20 @@ func (m *Master) api_v1() *apiserver.APIGroupVersion { return version } +// findExternalAddress returns ExternalIP of provided node with fallback to LegacyHostIP. func findExternalAddress(node *api.Node) (string, error) { + var fallback string for ix := range node.Status.Addresses { addr := &node.Status.Addresses[ix] if addr.Type == api.NodeExternalIP { return addr.Address, nil } + if fallback == "" && addr.Type == api.NodeLegacyHostIP { + fallback = addr.Address + } + } + if fallback != "" { + return fallback, nil } return "", fmt.Errorf("Couldn't find external address: %v", node) } diff --git a/pkg/master/master_test.go b/pkg/master/master_test.go index ff15461a6ed..16ddfcce4fa 100644 --- a/pkg/master/master_test.go +++ b/pkg/master/master_test.go @@ -46,3 +46,27 @@ func TestGetServersToValidate(t *testing.T) { } } } + +func TestFindExternalAddress(t *testing.T) { + expectedIP := "172.0.0.1" + + nodes := []*api.Node{new(api.Node), new(api.Node), new(api.Node)} + nodes[0].Status.Addresses = []api.NodeAddress{{"ExternalIP", expectedIP}} + nodes[1].Status.Addresses = []api.NodeAddress{{"LegacyHostIP", expectedIP}} + nodes[2].Status.Addresses = []api.NodeAddress{{"ExternalIP", expectedIP}, {"LegacyHostIP", "172.0.0.2"}} + + for _, node := range nodes { + ip, err := findExternalAddress(node) + if err != nil { + t.Errorf("error getting node external address: %s", err) + } + if ip != expectedIP { + t.Errorf("expected ip to be %s, but was %s", expectedIP, ip) + } + } + + _, err := findExternalAddress(new(api.Node)) + if err == nil { + t.Errorf("expected findExternalAddress to fail on a node with missing ip information") + } +}