Merge pull request #118408 from danwinship/local-detector

kube-proxy local traffic detector single-vs-dual-stack cleanup
This commit is contained in:
Kubernetes Prow Robot
2023-07-11 21:19:11 -07:00
committed by GitHub
5 changed files with 156 additions and 207 deletions

View File

@@ -291,7 +291,7 @@ func NewFakeProxier(ipt utiliptables.Interface) *Proxier {
ipfamily = v1.IPv6Protocol
podCIDR = "fd00::/64"
}
detectLocal, _ := proxyutiliptables.NewDetectLocalByCIDR(podCIDR, ipt)
detectLocal, _ := proxyutiliptables.NewDetectLocalByCIDR(podCIDR)
networkInterfacer := proxyutiltest.NewFakeNetwork()
itf := net.Interface{Index: 0, MTU: 0, Name: "lo", HardwareAddr: nil, Flags: 0}

View File

@@ -19,7 +19,6 @@ package iptables
import (
"fmt"
utiliptables "k8s.io/kubernetes/pkg/util/iptables"
netutils "k8s.io/utils/net"
)
@@ -62,10 +61,7 @@ type detectLocalByCIDR struct {
// NewDetectLocalByCIDR implements the LocalTrafficDetector interface using a CIDR. This can be used when a single CIDR
// range can be used to capture the notion of local traffic.
func NewDetectLocalByCIDR(cidr string, ipt utiliptables.Interface) (LocalTrafficDetector, error) {
if netutils.IsIPv6CIDRString(cidr) != ipt.IsIPv6() {
return nil, fmt.Errorf("CIDR %s has incorrect IP version: expect isIPv6=%t", cidr, ipt.IsIPv6())
}
func NewDetectLocalByCIDR(cidr string) (LocalTrafficDetector, error) {
_, _, err := netutils.ParseCIDRSloppy(cidr)
if err != nil {
return nil, err

View File

@@ -19,9 +19,6 @@ package iptables
import (
"reflect"
"testing"
utiliptables "k8s.io/kubernetes/pkg/util/iptables"
iptablestest "k8s.io/kubernetes/pkg/util/iptables/testing"
)
func TestNoOpLocalDetector(t *testing.T) {
@@ -44,52 +41,35 @@ func TestNoOpLocalDetector(t *testing.T) {
func TestNewDetectLocalByCIDR(t *testing.T) {
cases := []struct {
cidr string
ipt utiliptables.Interface
errExpected bool
}{
{
cidr: "10.0.0.0/14",
ipt: iptablestest.NewFake(),
errExpected: false,
},
{
cidr: "2002::1234:abcd:ffff:c0a8:101/64",
ipt: iptablestest.NewIPv6Fake(),
errExpected: false,
},
{
cidr: "10.0.0.0/14",
ipt: iptablestest.NewIPv6Fake(),
errExpected: true,
},
{
cidr: "2002::1234:abcd:ffff:c0a8:101/64",
ipt: iptablestest.NewFake(),
errExpected: true,
},
{
cidr: "10.0.0.0",
ipt: iptablestest.NewFake(),
errExpected: true,
},
{
cidr: "2002::1234:abcd:ffff:c0a8:101",
ipt: iptablestest.NewIPv6Fake(),
errExpected: true,
},
{
cidr: "",
ipt: iptablestest.NewFake(),
errExpected: true,
},
{
cidr: "",
ipt: iptablestest.NewIPv6Fake(),
errExpected: true,
},
}
for i, c := range cases {
r, err := NewDetectLocalByCIDR(c.cidr, c.ipt)
r, err := NewDetectLocalByCIDR(c.cidr)
if c.errExpected {
if err == nil {
t.Errorf("Case[%d] expected error, but succeeded with: %q", i, r)
@@ -105,25 +85,22 @@ func TestNewDetectLocalByCIDR(t *testing.T) {
func TestDetectLocalByCIDR(t *testing.T) {
cases := []struct {
cidr string
ipt utiliptables.Interface
expectedIfLocalOutput []string
expectedIfNotLocalOutput []string
}{
{
cidr: "10.0.0.0/14",
ipt: iptablestest.NewFake(),
expectedIfLocalOutput: []string{"-s", "10.0.0.0/14"},
expectedIfNotLocalOutput: []string{"!", "-s", "10.0.0.0/14"},
},
{
cidr: "2002::1234:abcd:ffff:c0a8:101/64",
ipt: iptablestest.NewIPv6Fake(),
expectedIfLocalOutput: []string{"-s", "2002::1234:abcd:ffff:c0a8:101/64"},
expectedIfNotLocalOutput: []string{"!", "-s", "2002::1234:abcd:ffff:c0a8:101/64"},
},
}
for _, c := range cases {
localDetector, err := NewDetectLocalByCIDR(c.cidr, c.ipt)
localDetector, err := NewDetectLocalByCIDR(c.cidr)
if err != nil {
t.Errorf("Error initializing localDetector: %v", err)
continue