kubernetes/pkg/proxy/util/utils_test.go

692 lines
17 KiB
Go
Raw Normal View History

/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package util
import (
2018-01-16 21:23:33 -05:00
"net"
"reflect"
"testing"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2018-01-16 21:23:33 -05:00
"k8s.io/apimachinery/pkg/util/sets"
2021-08-19 19:16:14 -04:00
netutils "k8s.io/utils/net"
)
func TestShouldSkipService(t *testing.T) {
testCases := []struct {
2018-08-15 09:51:19 -04:00
service *v1.Service
shouldSkip bool
}{
{
// Cluster IP is None
2018-08-15 09:51:19 -04:00
service: &v1.Service{
ObjectMeta: metav1.ObjectMeta{Namespace: "foo", Name: "bar"},
2018-08-15 09:51:19 -04:00
Spec: v1.ServiceSpec{
ClusterIP: v1.ClusterIPNone,
},
},
shouldSkip: true,
},
{
// Cluster IP is empty
2018-08-15 09:51:19 -04:00
service: &v1.Service{
ObjectMeta: metav1.ObjectMeta{Namespace: "foo", Name: "bar"},
2018-08-15 09:51:19 -04:00
Spec: v1.ServiceSpec{
ClusterIP: "",
},
},
shouldSkip: true,
},
{
// ExternalName type service
2018-08-15 09:51:19 -04:00
service: &v1.Service{
ObjectMeta: metav1.ObjectMeta{Namespace: "foo", Name: "bar"},
2018-08-15 09:51:19 -04:00
Spec: v1.ServiceSpec{
ClusterIP: "1.2.3.4",
2018-08-15 09:51:19 -04:00
Type: v1.ServiceTypeExternalName,
},
},
shouldSkip: true,
},
{
// ClusterIP type service with ClusterIP set
2018-08-15 09:51:19 -04:00
service: &v1.Service{
ObjectMeta: metav1.ObjectMeta{Namespace: "foo", Name: "bar"},
2018-08-15 09:51:19 -04:00
Spec: v1.ServiceSpec{
ClusterIP: "1.2.3.4",
2018-08-15 09:51:19 -04:00
Type: v1.ServiceTypeClusterIP,
},
},
shouldSkip: false,
},
{
// NodePort type service with ClusterIP set
2018-08-15 09:51:19 -04:00
service: &v1.Service{
ObjectMeta: metav1.ObjectMeta{Namespace: "foo", Name: "bar"},
2018-08-15 09:51:19 -04:00
Spec: v1.ServiceSpec{
ClusterIP: "1.2.3.4",
2018-08-15 09:51:19 -04:00
Type: v1.ServiceTypeNodePort,
},
},
shouldSkip: false,
},
{
// LoadBalancer type service with ClusterIP set
2018-08-15 09:51:19 -04:00
service: &v1.Service{
ObjectMeta: metav1.ObjectMeta{Namespace: "foo", Name: "bar"},
2018-08-15 09:51:19 -04:00
Spec: v1.ServiceSpec{
ClusterIP: "1.2.3.4",
2018-08-15 09:51:19 -04:00
Type: v1.ServiceTypeLoadBalancer,
},
},
shouldSkip: false,
},
}
for i := range testCases {
skip := ShouldSkipService(testCases[i].service)
if skip != testCases[i].shouldSkip {
t.Errorf("case %d: expect %v, got %v", i, testCases[i].shouldSkip, skip)
}
}
}
2018-01-16 21:23:33 -05:00
func TestAppendPortIfNeeded(t *testing.T) {
testCases := []struct {
name string
addr string
port int32
expect string
}{
{
name: "IPv4 all-zeros bind address has port",
addr: "0.0.0.0:12345",
port: 23456,
expect: "0.0.0.0:12345",
},
{
name: "non-zeros IPv4 config",
addr: "9.8.7.6",
port: 12345,
expect: "9.8.7.6:12345",
},
{
name: "IPv6 \"[::]\" bind address has port",
addr: "[::]:12345",
port: 23456,
expect: "[::]:12345",
},
{
name: "IPv6 config",
addr: "fd00:1::5",
port: 23456,
expect: "[fd00:1::5]:23456",
},
{
name: "Invalid IPv6 Config",
addr: "[fd00:1::5]",
port: 12345,
expect: "[fd00:1::5]",
},
}
for i := range testCases {
got := AppendPortIfNeeded(testCases[i].addr, testCases[i].port)
if testCases[i].expect != got {
t.Errorf("case %s: expected %v, got %v", testCases[i].name, testCases[i].expect, got)
}
}
}
2020-11-17 02:13:51 -05:00
func TestMapIPsByIPFamily(t *testing.T) {
testCases := []struct {
desc string
ipString []string
wantIPv6 bool
expectCorrect []string
expectIncorrect []string
}{
{
desc: "empty input IPv4",
ipString: []string{},
wantIPv6: false,
expectCorrect: nil,
expectIncorrect: nil,
},
{
desc: "empty input IPv6",
ipString: []string{},
wantIPv6: true,
expectCorrect: nil,
expectIncorrect: nil,
},
{
desc: "want IPv4 and receive IPv6",
ipString: []string{"fd00:20::1"},
wantIPv6: false,
expectCorrect: nil,
expectIncorrect: []string{"fd00:20::1"},
},
{
desc: "want IPv6 and receive IPv4",
ipString: []string{"192.168.200.2"},
wantIPv6: true,
expectCorrect: nil,
expectIncorrect: []string{"192.168.200.2"},
},
{
desc: "want IPv6 and receive IPv4 and IPv6",
ipString: []string{"192.168.200.2", "192.1.34.23", "fd00:20::1", "2001:db9::3"},
wantIPv6: true,
expectCorrect: []string{"fd00:20::1", "2001:db9::3"},
expectIncorrect: []string{"192.168.200.2", "192.1.34.23"},
},
{
desc: "want IPv4 and receive IPv4 and IPv6",
ipString: []string{"192.168.200.2", "192.1.34.23", "fd00:20::1", "2001:db9::3"},
wantIPv6: false,
expectCorrect: []string{"192.168.200.2", "192.1.34.23"},
expectIncorrect: []string{"fd00:20::1", "2001:db9::3"},
},
{
desc: "want IPv4 and receive IPv4 only",
ipString: []string{"192.168.200.2", "192.1.34.23"},
wantIPv6: false,
expectCorrect: []string{"192.168.200.2", "192.1.34.23"},
expectIncorrect: nil,
},
{
desc: "want IPv6 and receive IPv4 only",
ipString: []string{"192.168.200.2", "192.1.34.23"},
wantIPv6: true,
expectCorrect: nil,
expectIncorrect: []string{"192.168.200.2", "192.1.34.23"},
},
{
desc: "want IPv4 and receive IPv6 only",
ipString: []string{"fd00:20::1", "2001:db9::3"},
wantIPv6: false,
expectCorrect: nil,
expectIncorrect: []string{"fd00:20::1", "2001:db9::3"},
},
{
desc: "want IPv6 and receive IPv6 only",
ipString: []string{"fd00:20::1", "2001:db9::3"},
wantIPv6: true,
expectCorrect: []string{"fd00:20::1", "2001:db9::3"},
expectIncorrect: nil,
},
}
for _, testcase := range testCases {
t.Run(testcase.desc, func(t *testing.T) {
dual stack services (#91824) * api: structure change * api: defaulting, conversion, and validation * [FIX] validation: auto remove second ip/family when service changes to SingleStack * [FIX] api: defaulting, conversion, and validation * api-server: clusterIPs alloc, printers, storage and strategy * [FIX] clusterIPs default on read * alloc: auto remove second ip/family when service changes to SingleStack * api-server: repair loop handling for clusterIPs * api-server: force kubernetes default service into single stack * api-server: tie dualstack feature flag with endpoint feature flag * controller-manager: feature flag, endpoint, and endpointSlice controllers handling multi family service * [FIX] controller-manager: feature flag, endpoint, and endpointSlicecontrollers handling multi family service * kube-proxy: feature-flag, utils, proxier, and meta proxier * [FIX] kubeproxy: call both proxier at the same time * kubenet: remove forced pod IP sorting * kubectl: modify describe to include ClusterIPs, IPFamilies, and IPFamilyPolicy * e2e: fix tests that depends on IPFamily field AND add dual stack tests * e2e: fix expected error message for ClusterIP immutability * add integration tests for dualstack the third phase of dual stack is a very complex change in the API, basically it introduces Dual Stack services. Main changes are: - It pluralizes the Service IPFamily field to IPFamilies, and removes the singular field. - It introduces a new field IPFamilyPolicyType that can take 3 values to express the "dual-stack(mad)ness" of the cluster: SingleStack, PreferDualStack and RequireDualStack - It pluralizes ClusterIP to ClusterIPs. The goal is to add coverage to the services API operations, taking into account the 6 different modes a cluster can have: - single stack: IP4 or IPv6 (as of today) - dual stack: IPv4 only, IPv6 only, IPv4 - IPv6, IPv6 - IPv4 * [FIX] add integration tests for dualstack * generated data * generated files Co-authored-by: Antonio Ojea <aojea@redhat.com>
2020-10-26 16:15:59 -04:00
ipFamily := v1.IPv4Protocol
otherIPFamily := v1.IPv6Protocol
dual stack services (#91824) * api: structure change * api: defaulting, conversion, and validation * [FIX] validation: auto remove second ip/family when service changes to SingleStack * [FIX] api: defaulting, conversion, and validation * api-server: clusterIPs alloc, printers, storage and strategy * [FIX] clusterIPs default on read * alloc: auto remove second ip/family when service changes to SingleStack * api-server: repair loop handling for clusterIPs * api-server: force kubernetes default service into single stack * api-server: tie dualstack feature flag with endpoint feature flag * controller-manager: feature flag, endpoint, and endpointSlice controllers handling multi family service * [FIX] controller-manager: feature flag, endpoint, and endpointSlicecontrollers handling multi family service * kube-proxy: feature-flag, utils, proxier, and meta proxier * [FIX] kubeproxy: call both proxier at the same time * kubenet: remove forced pod IP sorting * kubectl: modify describe to include ClusterIPs, IPFamilies, and IPFamilyPolicy * e2e: fix tests that depends on IPFamily field AND add dual stack tests * e2e: fix expected error message for ClusterIP immutability * add integration tests for dualstack the third phase of dual stack is a very complex change in the API, basically it introduces Dual Stack services. Main changes are: - It pluralizes the Service IPFamily field to IPFamilies, and removes the singular field. - It introduces a new field IPFamilyPolicyType that can take 3 values to express the "dual-stack(mad)ness" of the cluster: SingleStack, PreferDualStack and RequireDualStack - It pluralizes ClusterIP to ClusterIPs. The goal is to add coverage to the services API operations, taking into account the 6 different modes a cluster can have: - single stack: IP4 or IPv6 (as of today) - dual stack: IPv4 only, IPv6 only, IPv4 - IPv6, IPv6 - IPv4 * [FIX] add integration tests for dualstack * generated data * generated files Co-authored-by: Antonio Ojea <aojea@redhat.com>
2020-10-26 16:15:59 -04:00
if testcase.wantIPv6 {
ipFamily = v1.IPv6Protocol
otherIPFamily = v1.IPv4Protocol
dual stack services (#91824) * api: structure change * api: defaulting, conversion, and validation * [FIX] validation: auto remove second ip/family when service changes to SingleStack * [FIX] api: defaulting, conversion, and validation * api-server: clusterIPs alloc, printers, storage and strategy * [FIX] clusterIPs default on read * alloc: auto remove second ip/family when service changes to SingleStack * api-server: repair loop handling for clusterIPs * api-server: force kubernetes default service into single stack * api-server: tie dualstack feature flag with endpoint feature flag * controller-manager: feature flag, endpoint, and endpointSlice controllers handling multi family service * [FIX] controller-manager: feature flag, endpoint, and endpointSlicecontrollers handling multi family service * kube-proxy: feature-flag, utils, proxier, and meta proxier * [FIX] kubeproxy: call both proxier at the same time * kubenet: remove forced pod IP sorting * kubectl: modify describe to include ClusterIPs, IPFamilies, and IPFamilyPolicy * e2e: fix tests that depends on IPFamily field AND add dual stack tests * e2e: fix expected error message for ClusterIP immutability * add integration tests for dualstack the third phase of dual stack is a very complex change in the API, basically it introduces Dual Stack services. Main changes are: - It pluralizes the Service IPFamily field to IPFamilies, and removes the singular field. - It introduces a new field IPFamilyPolicyType that can take 3 values to express the "dual-stack(mad)ness" of the cluster: SingleStack, PreferDualStack and RequireDualStack - It pluralizes ClusterIP to ClusterIPs. The goal is to add coverage to the services API operations, taking into account the 6 different modes a cluster can have: - single stack: IP4 or IPv6 (as of today) - dual stack: IPv4 only, IPv6 only, IPv4 - IPv6, IPv6 - IPv4 * [FIX] add integration tests for dualstack * generated data * generated files Co-authored-by: Antonio Ojea <aojea@redhat.com>
2020-10-26 16:15:59 -04:00
}
2020-11-17 02:13:51 -05:00
ipMap := MapIPsByIPFamily(testcase.ipString)
var ipStr []string
for _, ip := range ipMap[ipFamily] {
ipStr = append(ipStr, ip.String())
}
if !reflect.DeepEqual(testcase.expectCorrect, ipStr) {
t.Errorf("Test %v failed: expected %v, got %v", testcase.desc, testcase.expectCorrect, ipMap[ipFamily])
}
ipStr = nil
for _, ip := range ipMap[otherIPFamily] {
ipStr = append(ipStr, ip.String())
}
if !reflect.DeepEqual(testcase.expectIncorrect, ipStr) {
t.Errorf("Test %v failed: expected %v, got %v", testcase.desc, testcase.expectIncorrect, ipMap[otherIPFamily])
}
})
}
}
dual stack services (#91824) * api: structure change * api: defaulting, conversion, and validation * [FIX] validation: auto remove second ip/family when service changes to SingleStack * [FIX] api: defaulting, conversion, and validation * api-server: clusterIPs alloc, printers, storage and strategy * [FIX] clusterIPs default on read * alloc: auto remove second ip/family when service changes to SingleStack * api-server: repair loop handling for clusterIPs * api-server: force kubernetes default service into single stack * api-server: tie dualstack feature flag with endpoint feature flag * controller-manager: feature flag, endpoint, and endpointSlice controllers handling multi family service * [FIX] controller-manager: feature flag, endpoint, and endpointSlicecontrollers handling multi family service * kube-proxy: feature-flag, utils, proxier, and meta proxier * [FIX] kubeproxy: call both proxier at the same time * kubenet: remove forced pod IP sorting * kubectl: modify describe to include ClusterIPs, IPFamilies, and IPFamilyPolicy * e2e: fix tests that depends on IPFamily field AND add dual stack tests * e2e: fix expected error message for ClusterIP immutability * add integration tests for dualstack the third phase of dual stack is a very complex change in the API, basically it introduces Dual Stack services. Main changes are: - It pluralizes the Service IPFamily field to IPFamilies, and removes the singular field. - It introduces a new field IPFamilyPolicyType that can take 3 values to express the "dual-stack(mad)ness" of the cluster: SingleStack, PreferDualStack and RequireDualStack - It pluralizes ClusterIP to ClusterIPs. The goal is to add coverage to the services API operations, taking into account the 6 different modes a cluster can have: - single stack: IP4 or IPv6 (as of today) - dual stack: IPv4 only, IPv6 only, IPv4 - IPv6, IPv6 - IPv4 * [FIX] add integration tests for dualstack * generated data * generated files Co-authored-by: Antonio Ojea <aojea@redhat.com>
2020-10-26 16:15:59 -04:00
2020-11-17 02:35:50 -05:00
func TestMapCIDRsByIPFamily(t *testing.T) {
testCases := []struct {
desc string
ipString []string
wantIPv6 bool
expectCorrect []string
expectIncorrect []string
}{
{
desc: "empty input IPv4",
ipString: []string{},
wantIPv6: false,
expectCorrect: nil,
expectIncorrect: nil,
},
{
desc: "empty input IPv6",
ipString: []string{},
wantIPv6: true,
expectCorrect: nil,
expectIncorrect: nil,
},
{
desc: "want IPv4 and receive IPv6",
ipString: []string{"fd00:20::/64"},
2020-11-17 02:35:50 -05:00
wantIPv6: false,
expectCorrect: nil,
expectIncorrect: []string{"fd00:20::/64"},
2020-11-17 02:35:50 -05:00
},
{
desc: "want IPv6 and receive IPv4",
ipString: []string{"192.168.200.0/24"},
2020-11-17 02:35:50 -05:00
wantIPv6: true,
expectCorrect: nil,
expectIncorrect: []string{"192.168.200.0/24"},
2020-11-17 02:35:50 -05:00
},
{
desc: "want IPv6 and receive IPv4 and IPv6",
ipString: []string{"192.168.200.0/24", "192.1.34.0/24", "fd00:20::/64", "2001:db9::/64"},
2020-11-17 02:35:50 -05:00
wantIPv6: true,
expectCorrect: []string{"fd00:20::/64", "2001:db9::/64"},
expectIncorrect: []string{"192.168.200.0/24", "192.1.34.0/24"},
2020-11-17 02:35:50 -05:00
},
{
desc: "want IPv4 and receive IPv4 and IPv6",
ipString: []string{"192.168.200.0/24", "192.1.34.0/24", "fd00:20::/64", "2001:db9::/64"},
2020-11-17 02:35:50 -05:00
wantIPv6: false,
expectCorrect: []string{"192.168.200.0/24", "192.1.34.0/24"},
expectIncorrect: []string{"fd00:20::/64", "2001:db9::/64"},
2020-11-17 02:35:50 -05:00
},
{
desc: "want IPv4 and receive IPv4 only",
ipString: []string{"192.168.200.0/24", "192.1.34.0/24"},
2020-11-17 02:35:50 -05:00
wantIPv6: false,
expectCorrect: []string{"192.168.200.0/24", "192.1.34.0/24"},
2020-11-17 02:35:50 -05:00
expectIncorrect: nil,
},
{
desc: "want IPv6 and receive IPv4 only",
ipString: []string{"192.168.200.0/24", "192.1.34.0/24"},
2020-11-17 02:35:50 -05:00
wantIPv6: true,
expectCorrect: nil,
expectIncorrect: []string{"192.168.200.0/24", "192.1.34.0/24"},
2020-11-17 02:35:50 -05:00
},
{
desc: "want IPv4 and receive IPv6 only",
ipString: []string{"fd00:20::/64", "2001:db9::/64"},
2020-11-17 02:35:50 -05:00
wantIPv6: false,
expectCorrect: nil,
expectIncorrect: []string{"fd00:20::/64", "2001:db9::/64"},
2020-11-17 02:35:50 -05:00
},
{
desc: "want IPv6 and receive IPv6 only",
ipString: []string{"fd00:20::/64", "2001:db9::/64"},
2020-11-17 02:35:50 -05:00
wantIPv6: true,
expectCorrect: []string{"fd00:20::/64", "2001:db9::/64"},
2020-11-17 02:35:50 -05:00
expectIncorrect: nil,
},
}
for _, testcase := range testCases {
t.Run(testcase.desc, func(t *testing.T) {
ipFamily := v1.IPv4Protocol
otherIPFamily := v1.IPv6Protocol
if testcase.wantIPv6 {
ipFamily = v1.IPv6Protocol
otherIPFamily = v1.IPv4Protocol
}
cidrMap := MapCIDRsByIPFamily(testcase.ipString)
var cidrStr []string
for _, cidr := range cidrMap[ipFamily] {
cidrStr = append(cidrStr, cidr.String())
2020-11-17 02:35:50 -05:00
}
var cidrStrOther []string
for _, cidr := range cidrMap[otherIPFamily] {
cidrStrOther = append(cidrStrOther, cidr.String())
}
if !reflect.DeepEqual(testcase.expectCorrect, cidrStr) {
t.Errorf("Test %v failed: expected %v, got %v", testcase.desc, testcase.expectCorrect, cidrStr)
}
if !reflect.DeepEqual(testcase.expectIncorrect, cidrStrOther) {
t.Errorf("Test %v failed: expected %v, got %v", testcase.desc, testcase.expectIncorrect, cidrStrOther)
2020-11-17 02:35:50 -05:00
}
})
}
}
dual stack services (#91824) * api: structure change * api: defaulting, conversion, and validation * [FIX] validation: auto remove second ip/family when service changes to SingleStack * [FIX] api: defaulting, conversion, and validation * api-server: clusterIPs alloc, printers, storage and strategy * [FIX] clusterIPs default on read * alloc: auto remove second ip/family when service changes to SingleStack * api-server: repair loop handling for clusterIPs * api-server: force kubernetes default service into single stack * api-server: tie dualstack feature flag with endpoint feature flag * controller-manager: feature flag, endpoint, and endpointSlice controllers handling multi family service * [FIX] controller-manager: feature flag, endpoint, and endpointSlicecontrollers handling multi family service * kube-proxy: feature-flag, utils, proxier, and meta proxier * [FIX] kubeproxy: call both proxier at the same time * kubenet: remove forced pod IP sorting * kubectl: modify describe to include ClusterIPs, IPFamilies, and IPFamilyPolicy * e2e: fix tests that depends on IPFamily field AND add dual stack tests * e2e: fix expected error message for ClusterIP immutability * add integration tests for dualstack the third phase of dual stack is a very complex change in the API, basically it introduces Dual Stack services. Main changes are: - It pluralizes the Service IPFamily field to IPFamilies, and removes the singular field. - It introduces a new field IPFamilyPolicyType that can take 3 values to express the "dual-stack(mad)ness" of the cluster: SingleStack, PreferDualStack and RequireDualStack - It pluralizes ClusterIP to ClusterIPs. The goal is to add coverage to the services API operations, taking into account the 6 different modes a cluster can have: - single stack: IP4 or IPv6 (as of today) - dual stack: IPv4 only, IPv6 only, IPv4 - IPv6, IPv6 - IPv4 * [FIX] add integration tests for dualstack * generated data * generated files Co-authored-by: Antonio Ojea <aojea@redhat.com>
2020-10-26 16:15:59 -04:00
func TestGetClusterIPByFamily(t *testing.T) {
testCases := []struct {
name string
service v1.Service
requestFamily v1.IPFamily
expectedResult string
}{
{
name: "old style service ipv4. want ipv4",
requestFamily: v1.IPv4Protocol,
expectedResult: "10.0.0.10",
service: v1.Service{
Spec: v1.ServiceSpec{
ClusterIP: "10.0.0.10",
},
},
},
{
name: "old style service ipv4. want ipv6",
requestFamily: v1.IPv6Protocol,
expectedResult: "",
service: v1.Service{
Spec: v1.ServiceSpec{
ClusterIP: "10.0.0.10",
},
},
},
{
name: "old style service ipv6. want ipv6",
requestFamily: v1.IPv6Protocol,
expectedResult: "2000::1",
service: v1.Service{
Spec: v1.ServiceSpec{
ClusterIP: "2000::1",
},
},
},
{
name: "old style service ipv6. want ipv4",
requestFamily: v1.IPv4Protocol,
expectedResult: "",
service: v1.Service{
Spec: v1.ServiceSpec{
ClusterIP: "2000::1",
},
},
},
{
name: "service single stack ipv4. want ipv4",
requestFamily: v1.IPv4Protocol,
expectedResult: "10.0.0.10",
service: v1.Service{
Spec: v1.ServiceSpec{
ClusterIPs: []string{"10.0.0.10"},
IPFamilies: []v1.IPFamily{v1.IPv4Protocol},
},
},
},
{
name: "service single stack ipv4. want ipv6",
requestFamily: v1.IPv6Protocol,
expectedResult: "",
service: v1.Service{
Spec: v1.ServiceSpec{
ClusterIPs: []string{"10.0.0.10"},
IPFamilies: []v1.IPFamily{v1.IPv4Protocol},
},
},
},
{
name: "service single stack ipv6. want ipv6",
requestFamily: v1.IPv6Protocol,
expectedResult: "2000::1",
service: v1.Service{
Spec: v1.ServiceSpec{
ClusterIPs: []string{"2000::1"},
IPFamilies: []v1.IPFamily{v1.IPv6Protocol},
},
},
},
{
name: "service single stack ipv6. want ipv4",
requestFamily: v1.IPv4Protocol,
expectedResult: "",
service: v1.Service{
Spec: v1.ServiceSpec{
ClusterIPs: []string{"2000::1"},
IPFamilies: []v1.IPFamily{v1.IPv6Protocol},
},
},
},
// dual stack
{
name: "service dual stack ipv4,6. want ipv4",
requestFamily: v1.IPv4Protocol,
expectedResult: "10.0.0.10",
service: v1.Service{
Spec: v1.ServiceSpec{
ClusterIPs: []string{"10.0.0.10", "2000::1"},
IPFamilies: []v1.IPFamily{v1.IPv4Protocol, v1.IPv6Protocol},
},
},
},
{
name: "service dual stack ipv4,6. want ipv6",
requestFamily: v1.IPv6Protocol,
expectedResult: "2000::1",
service: v1.Service{
Spec: v1.ServiceSpec{
ClusterIPs: []string{"10.0.0.10", "2000::1"},
IPFamilies: []v1.IPFamily{v1.IPv4Protocol, v1.IPv6Protocol},
},
},
},
{
name: "service dual stack ipv6,4. want ipv6",
requestFamily: v1.IPv6Protocol,
expectedResult: "2000::1",
service: v1.Service{
Spec: v1.ServiceSpec{
ClusterIPs: []string{"2000::1", "10.0.0.10"},
IPFamilies: []v1.IPFamily{v1.IPv6Protocol, v1.IPv4Protocol},
},
},
},
{
name: "service dual stack ipv6,4. want ipv4",
requestFamily: v1.IPv4Protocol,
expectedResult: "10.0.0.10",
service: v1.Service{
Spec: v1.ServiceSpec{
ClusterIPs: []string{"2000::1", "10.0.0.10"},
IPFamilies: []v1.IPFamily{v1.IPv6Protocol, v1.IPv4Protocol},
},
},
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
ip := GetClusterIPByFamily(testCase.requestFamily, &testCase.service)
if ip != testCase.expectedResult {
t.Fatalf("expected ip:%v got %v", testCase.expectedResult, ip)
}
})
}
}
func mustParseIPAddr(str string) net.Addr {
a, err := net.ResolveIPAddr("ip", str)
if err != nil {
panic("mustParseIPAddr")
}
return a
}
func mustParseIPNet(str string) net.Addr {
_, n, err := netutils.ParseCIDRSloppy(str)
if err != nil {
panic("mustParseIPNet")
}
return n
}
func mustParseUnix(str string) net.Addr {
n, err := net.ResolveUnixAddr("unix", str)
if err != nil {
panic("mustParseUnix")
}
return n
}
type cidrValidator struct {
cidr *net.IPNet
}
func (v *cidrValidator) isValid(ip net.IP) bool {
return v.cidr.Contains(ip)
}
func newCidrValidator(cidr string) func(ip net.IP) bool {
_, n, err := netutils.ParseCIDRSloppy(cidr)
if err != nil {
panic("mustParseIPNet")
}
obj := cidrValidator{n}
return obj.isValid
}
func TestAddressSet(t *testing.T) {
testCases := []struct {
name string
validator func(ip net.IP) bool
input []net.Addr
2023-02-19 12:08:57 -05:00
expected sets.Set[string]
}{
{
"Empty",
func(ip net.IP) bool { return false },
nil,
2023-02-19 12:08:57 -05:00
nil,
},
{
"Reject IPAddr x 2",
func(ip net.IP) bool { return false },
[]net.Addr{
mustParseIPAddr("8.8.8.8"),
mustParseIPAddr("1000::"),
},
2023-02-19 12:08:57 -05:00
nil,
},
{
"Accept IPAddr x 2",
func(ip net.IP) bool { return true },
[]net.Addr{
mustParseIPAddr("8.8.8.8"),
mustParseIPAddr("1000::"),
},
2023-02-19 12:08:57 -05:00
sets.New("8.8.8.8", "1000::"),
},
{
"Accept IPNet x 2",
func(ip net.IP) bool { return true },
[]net.Addr{
mustParseIPNet("8.8.8.8/32"),
mustParseIPNet("1000::/128"),
},
2023-02-19 12:08:57 -05:00
sets.New("8.8.8.8", "1000::"),
},
{
"Accept Unix x 2",
func(ip net.IP) bool { return true },
[]net.Addr{
mustParseUnix("/tmp/sock1"),
mustParseUnix("/tmp/sock2"),
},
2023-02-19 12:08:57 -05:00
nil,
},
{
"Cidr IPv4",
newCidrValidator("192.168.1.0/24"),
[]net.Addr{
mustParseIPAddr("8.8.8.8"),
mustParseIPAddr("1000::"),
mustParseIPAddr("192.168.1.1"),
},
2023-02-19 12:08:57 -05:00
sets.New("192.168.1.1"),
},
{
"Cidr IPv6",
newCidrValidator("1000::/64"),
[]net.Addr{
mustParseIPAddr("8.8.8.8"),
mustParseIPAddr("1000::"),
mustParseIPAddr("192.168.1.1"),
},
2023-02-19 12:08:57 -05:00
sets.New("1000::"),
},
}
for _, tc := range testCases {
if !tc.expected.Equal(AddressSet(tc.validator, tc.input)) {
t.Errorf("%s", tc.name)
}
}
}
func TestIsZeroCIDR(t *testing.T) {
testCases := []struct {
name string
input string
expected bool
}{
{
name: "invalide cidr",
input: "",
expected: false,
},
{
name: "ipv4 cidr",
input: "172.10.0.0/16",
expected: false,
},
{
name: "ipv4 zero cidr",
input: IPv4ZeroCIDR,
expected: true,
},
{
name: "ipv6 cidr",
input: "::/128",
expected: false,
},
{
name: "ipv6 zero cidr",
input: IPv6ZeroCIDR,
expected: true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
_, cidr, _ := netutils.ParseCIDRSloppy(tc.input)
if got := IsZeroCIDR(cidr); tc.expected != got {
t.Errorf("IsZeroCIDR() = %t, want %t", got, tc.expected)
}
})
}
}