diff --git a/cmd/kube-proxy/app/server_others_test.go b/cmd/kube-proxy/app/server_others_test.go index c087621a498..5ee22b1de4b 100644 --- a/cmd/kube-proxy/app/server_others_test.go +++ b/cmd/kube-proxy/app/server_others_test.go @@ -21,7 +21,6 @@ package app import ( "fmt" - "net" "os" "path/filepath" "reflect" @@ -107,111 +106,6 @@ func Test_platformApplyDefaults(t *testing.T) { } } -func Test_detectNodeIP(t *testing.T) { - cases := []struct { - name string - nodeInfo *v1.Node - hostname string - bindAddress string - expectedIP net.IP - }{ - { - name: "Bind address IPv4 unicast address and no Node object", - nodeInfo: makeNodeWithAddresses("", "", ""), - hostname: "fakeHost", - bindAddress: "10.0.0.1", - expectedIP: netutils.ParseIPSloppy("10.0.0.1"), - }, - { - name: "Bind address IPv6 unicast address and no Node object", - nodeInfo: makeNodeWithAddresses("", "", ""), - hostname: "fakeHost", - bindAddress: "fd00:4321::2", - expectedIP: netutils.ParseIPSloppy("fd00:4321::2"), - }, - { - name: "No Valid IP found", - nodeInfo: makeNodeWithAddresses("", "", ""), - hostname: "fakeHost", - bindAddress: "", - expectedIP: netutils.ParseIPSloppy("127.0.0.1"), - }, - // Disabled because the GetNodeIP method has a backoff retry mechanism - // and the test takes more than 30 seconds - // ok k8s.io/kubernetes/cmd/kube-proxy/app 34.136s - // { - // name: "No Valid IP found and unspecified bind address", - // nodeInfo: makeNodeWithAddresses("", "", ""), - // hostname: "fakeHost", - // bindAddress: "0.0.0.0", - // expectedIP: net.IP{127,0,0,1), - // }, - { - name: "Bind address 0.0.0.0 and node with IPv4 InternalIP set", - nodeInfo: makeNodeWithAddresses("fakeHost", "192.168.1.1", "90.90.90.90"), - hostname: "fakeHost", - bindAddress: "0.0.0.0", - expectedIP: netutils.ParseIPSloppy("192.168.1.1"), - }, - { - name: "Bind address :: and node with IPv4 InternalIP set", - nodeInfo: makeNodeWithAddresses("fakeHost", "192.168.1.1", "90.90.90.90"), - hostname: "fakeHost", - bindAddress: "::", - expectedIP: netutils.ParseIPSloppy("192.168.1.1"), - }, - { - name: "Bind address 0.0.0.0 and node with IPv6 InternalIP set", - nodeInfo: makeNodeWithAddresses("fakeHost", "fd00:1234::1", "2001:db8::2"), - hostname: "fakeHost", - bindAddress: "0.0.0.0", - expectedIP: netutils.ParseIPSloppy("fd00:1234::1"), - }, - { - name: "Bind address :: and node with IPv6 InternalIP set", - nodeInfo: makeNodeWithAddresses("fakeHost", "fd00:1234::1", "2001:db8::2"), - hostname: "fakeHost", - bindAddress: "::", - expectedIP: netutils.ParseIPSloppy("fd00:1234::1"), - }, - { - name: "Bind address 0.0.0.0 and node with only IPv4 ExternalIP set", - nodeInfo: makeNodeWithAddresses("fakeHost", "", "90.90.90.90"), - hostname: "fakeHost", - bindAddress: "0.0.0.0", - expectedIP: netutils.ParseIPSloppy("90.90.90.90"), - }, - { - name: "Bind address :: and node with only IPv4 ExternalIP set", - nodeInfo: makeNodeWithAddresses("fakeHost", "", "90.90.90.90"), - hostname: "fakeHost", - bindAddress: "::", - expectedIP: netutils.ParseIPSloppy("90.90.90.90"), - }, - { - name: "Bind address 0.0.0.0 and node with only IPv6 ExternalIP set", - nodeInfo: makeNodeWithAddresses("fakeHost", "", "2001:db8::2"), - hostname: "fakeHost", - bindAddress: "0.0.0.0", - expectedIP: netutils.ParseIPSloppy("2001:db8::2"), - }, - { - name: "Bind address :: and node with only IPv6 ExternalIP set", - nodeInfo: makeNodeWithAddresses("fakeHost", "", "2001:db8::2"), - hostname: "fakeHost", - bindAddress: "::", - expectedIP: netutils.ParseIPSloppy("2001:db8::2"), - }, - } - for _, c := range cases { - client := clientsetfake.NewSimpleClientset(c.nodeInfo) - ip := detectNodeIP(client, c.hostname, c.bindAddress) - if !ip.Equal(c.expectedIP) { - t.Errorf("Case[%s] Expected IP %q got %q", c.name, c.expectedIP, ip) - } - } -} - func Test_getLocalDetector(t *testing.T) { cases := []struct { mode proxyconfigapi.LocalMode @@ -502,35 +396,6 @@ func Test_getDualStackLocalDetectorTuple(t *testing.T) { } } -func makeNodeWithAddresses(name, internal, external string) *v1.Node { - if name == "" { - return &v1.Node{} - } - - node := &v1.Node{ - ObjectMeta: metav1.ObjectMeta{ - Name: name, - }, - Status: v1.NodeStatus{ - Addresses: []v1.NodeAddress{}, - }, - } - - if internal != "" { - node.Status.Addresses = append(node.Status.Addresses, - v1.NodeAddress{Type: v1.NodeInternalIP, Address: internal}, - ) - } - - if external != "" { - node.Status.Addresses = append(node.Status.Addresses, - v1.NodeAddress{Type: v1.NodeExternalIP, Address: external}, - ) - } - - return node -} - func makeNodeWithPodCIDRs(cidrs ...string) *v1.Node { if len(cidrs) == 0 { return &v1.Node{} diff --git a/cmd/kube-proxy/app/server_test.go b/cmd/kube-proxy/app/server_test.go index 96b1e59872d..7ba0ee5fc15 100644 --- a/cmd/kube-proxy/app/server_test.go +++ b/cmd/kube-proxy/app/server_test.go @@ -19,19 +19,21 @@ package app import ( "errors" "fmt" + "net" "reflect" "testing" "time" "github.com/google/go-cmp/cmp" - "github.com/stretchr/testify/assert" - "k8s.io/utils/pointer" - + v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + clientsetfake "k8s.io/client-go/kubernetes/fake" componentbaseconfig "k8s.io/component-base/config" kubeproxyconfig "k8s.io/kubernetes/pkg/proxy/apis/config" + netutils "k8s.io/utils/net" + "k8s.io/utils/pointer" ) // TestLoadConfig tests proper operation of loadConfig() @@ -464,3 +466,137 @@ func TestAddressFromDeprecatedFlags(t *testing.T) { } } + +func makeNodeWithAddresses(name, internal, external string) *v1.Node { + if name == "" { + return &v1.Node{} + } + + node := &v1.Node{ + ObjectMeta: metav1.ObjectMeta{ + Name: name, + }, + Status: v1.NodeStatus{ + Addresses: []v1.NodeAddress{}, + }, + } + + if internal != "" { + node.Status.Addresses = append(node.Status.Addresses, + v1.NodeAddress{Type: v1.NodeInternalIP, Address: internal}, + ) + } + + if external != "" { + node.Status.Addresses = append(node.Status.Addresses, + v1.NodeAddress{Type: v1.NodeExternalIP, Address: external}, + ) + } + + return node +} + +func Test_detectNodeIP(t *testing.T) { + cases := []struct { + name string + nodeInfo *v1.Node + hostname string + bindAddress string + expectedIP net.IP + }{ + { + name: "Bind address IPv4 unicast address and no Node object", + nodeInfo: makeNodeWithAddresses("", "", ""), + hostname: "fakeHost", + bindAddress: "10.0.0.1", + expectedIP: netutils.ParseIPSloppy("10.0.0.1"), + }, + { + name: "Bind address IPv6 unicast address and no Node object", + nodeInfo: makeNodeWithAddresses("", "", ""), + hostname: "fakeHost", + bindAddress: "fd00:4321::2", + expectedIP: netutils.ParseIPSloppy("fd00:4321::2"), + }, + { + name: "No Valid IP found", + nodeInfo: makeNodeWithAddresses("", "", ""), + hostname: "fakeHost", + bindAddress: "", + expectedIP: netutils.ParseIPSloppy("127.0.0.1"), + }, + // Disabled because the GetNodeIP method has a backoff retry mechanism + // and the test takes more than 30 seconds + // ok k8s.io/kubernetes/cmd/kube-proxy/app 34.136s + // { + // name: "No Valid IP found and unspecified bind address", + // nodeInfo: makeNodeWithAddresses("", "", ""), + // hostname: "fakeHost", + // bindAddress: "0.0.0.0", + // expectedIP: net.IP{127,0,0,1), + // }, + { + name: "Bind address 0.0.0.0 and node with IPv4 InternalIP set", + nodeInfo: makeNodeWithAddresses("fakeHost", "192.168.1.1", "90.90.90.90"), + hostname: "fakeHost", + bindAddress: "0.0.0.0", + expectedIP: netutils.ParseIPSloppy("192.168.1.1"), + }, + { + name: "Bind address :: and node with IPv4 InternalIP set", + nodeInfo: makeNodeWithAddresses("fakeHost", "192.168.1.1", "90.90.90.90"), + hostname: "fakeHost", + bindAddress: "::", + expectedIP: netutils.ParseIPSloppy("192.168.1.1"), + }, + { + name: "Bind address 0.0.0.0 and node with IPv6 InternalIP set", + nodeInfo: makeNodeWithAddresses("fakeHost", "fd00:1234::1", "2001:db8::2"), + hostname: "fakeHost", + bindAddress: "0.0.0.0", + expectedIP: netutils.ParseIPSloppy("fd00:1234::1"), + }, + { + name: "Bind address :: and node with IPv6 InternalIP set", + nodeInfo: makeNodeWithAddresses("fakeHost", "fd00:1234::1", "2001:db8::2"), + hostname: "fakeHost", + bindAddress: "::", + expectedIP: netutils.ParseIPSloppy("fd00:1234::1"), + }, + { + name: "Bind address 0.0.0.0 and node with only IPv4 ExternalIP set", + nodeInfo: makeNodeWithAddresses("fakeHost", "", "90.90.90.90"), + hostname: "fakeHost", + bindAddress: "0.0.0.0", + expectedIP: netutils.ParseIPSloppy("90.90.90.90"), + }, + { + name: "Bind address :: and node with only IPv4 ExternalIP set", + nodeInfo: makeNodeWithAddresses("fakeHost", "", "90.90.90.90"), + hostname: "fakeHost", + bindAddress: "::", + expectedIP: netutils.ParseIPSloppy("90.90.90.90"), + }, + { + name: "Bind address 0.0.0.0 and node with only IPv6 ExternalIP set", + nodeInfo: makeNodeWithAddresses("fakeHost", "", "2001:db8::2"), + hostname: "fakeHost", + bindAddress: "0.0.0.0", + expectedIP: netutils.ParseIPSloppy("2001:db8::2"), + }, + { + name: "Bind address :: and node with only IPv6 ExternalIP set", + nodeInfo: makeNodeWithAddresses("fakeHost", "", "2001:db8::2"), + hostname: "fakeHost", + bindAddress: "::", + expectedIP: netutils.ParseIPSloppy("2001:db8::2"), + }, + } + for _, c := range cases { + client := clientsetfake.NewSimpleClientset(c.nodeInfo) + ip := detectNodeIP(client, c.hostname, c.bindAddress) + if !ip.Equal(c.expectedIP) { + t.Errorf("Case[%s] Expected IP %q got %q", c.name, c.expectedIP, ip) + } + } +}