Skip to content

Commit

Permalink
refactor: replace the net.ParseCIDR for pkg/capture (istio#41332)
Browse files Browse the repository at this point in the history
  • Loading branch information
saltbo authored Oct 10, 2022
1 parent eed4d4b commit 86b4f3d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
23 changes: 12 additions & 11 deletions tools/istio-iptables/pkg/capture/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"bufio"
"fmt"
"net"
"net/netip"
"os"
"strings"
"time"
Expand Down Expand Up @@ -66,7 +67,7 @@ func NewIptablesConfigurator(cfg *config.Config, ext dep.Dependencies) *Iptables

type NetworkRange struct {
IsWildcard bool
IPNets []*net.IPNet
CIDRs []netip.Prefix
HasLoopBackIP bool
}

Expand All @@ -81,22 +82,22 @@ func (cfg *IptablesConfigurator) separateV4V6(cidrList string) (NetworkRange, Ne
ipv6Ranges := NetworkRange{}
ipv4Ranges := NetworkRange{}
for _, ipRange := range split(cidrList) {
ip, ipNet, err := net.ParseCIDR(ipRange)
ipp, err := netip.ParsePrefix(ipRange)
if err != nil {
_, err = fmt.Fprintf(os.Stderr, "Ignoring error for bug compatibility with istio-iptables: %s\n", err.Error())
if err != nil {
return ipv4Ranges, ipv6Ranges, err
}
continue
}
if ip.To4() != nil {
ipv4Ranges.IPNets = append(ipv4Ranges.IPNets, ipNet)
if ip.IsLoopback() {
if ipp.Addr().Is4() {
ipv4Ranges.CIDRs = append(ipv4Ranges.CIDRs, ipp)
if ipp.Addr().IsLoopback() {
ipv4Ranges.HasLoopBackIP = true
}
} else {
ipv6Ranges.IPNets = append(ipv6Ranges.IPNets, ipNet)
if ip.IsLoopback() {
ipv6Ranges.CIDRs = append(ipv6Ranges.CIDRs, ipp)
if ipp.Addr().IsLoopback() {
ipv6Ranges.HasLoopBackIP = true
}
}
Expand Down Expand Up @@ -205,9 +206,9 @@ func (cfg *IptablesConfigurator) handleOutboundIncludeRules(
insert(iptableslog.KubevirtCommand,
constants.PREROUTING, constants.NAT, 1, "-i", internalInterface, "-j", constants.ISTIOREDIRECT)
}
} else if len(rangeInclude.IPNets) > 0 {
} else if len(rangeInclude.CIDRs) > 0 {
// User has specified a non-empty list of cidrs to be redirected to Envoy.
for _, cidr := range rangeInclude.IPNets {
for _, cidr := range rangeInclude.CIDRs {
for _, internalInterface := range split(cfg.cfg.KubeVirtInterfaces) {
insert(iptableslog.KubevirtCommand, constants.PREROUTING, constants.NAT, 1, "-i", internalInterface,
"-d", cidr.String(), "-j", constants.ISTIOREDIRECT)
Expand Down Expand Up @@ -491,10 +492,10 @@ func (cfg *IptablesConfigurator) Run() {
cfg.iptables.AppendVersionedRule("127.0.0.1/32", "::1/128", iptableslog.UndefinedCommand, constants.ISTIOOUTPUT, constants.NAT,
"-d", constants.IPVersionSpecific, "-j", constants.RETURN)
// Apply outbound IPv4 exclusions. Must be applied before inclusions.
for _, cidr := range ipv4RangesExclude.IPNets {
for _, cidr := range ipv4RangesExclude.CIDRs {
cfg.iptables.AppendRuleV4(iptableslog.UndefinedCommand, constants.ISTIOOUTPUT, constants.NAT, "-d", cidr.String(), "-j", constants.RETURN)
}
for _, cidr := range ipv6RangesExclude.IPNets {
for _, cidr := range ipv6RangesExclude.CIDRs {
cfg.iptables.AppendRuleV6(iptableslog.UndefinedCommand, constants.ISTIOOUTPUT, constants.NAT, "-d", cidr.String(), "-j", constants.RETURN)
}

Expand Down
14 changes: 7 additions & 7 deletions tools/istio-iptables/pkg/capture/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package capture

import (
"net"
"net/netip"
"path/filepath"
"reflect"
"strings"
Expand Down Expand Up @@ -280,14 +280,14 @@ func TestIptables(t *testing.T) {
}

func TestSeparateV4V6(t *testing.T) {
mkIPList := func(ips ...string) []*net.IPNet {
ret := []*net.IPNet{}
mkIPList := func(ips ...string) []netip.Prefix {
ret := []netip.Prefix{}
for _, ip := range ips {
_, network, err := net.ParseCIDR(ip)
ipp, err := netip.ParsePrefix(ip)
if err != nil {
panic(err.Error())
}
ret = append(ret, network)
ret = append(ret, ipp)
}
return ret
}
Expand All @@ -306,12 +306,12 @@ func TestSeparateV4V6(t *testing.T) {
{
name: "v4 only",
cidr: "10.0.0.0/8,172.16.0.0/16",
v4: NetworkRange{IPNets: mkIPList("10.0.0.0/8", "172.16.0.0/16")},
v4: NetworkRange{CIDRs: mkIPList("10.0.0.0/8", "172.16.0.0/16")},
},
{
name: "v6 only",
cidr: "fd04:3e42:4a4e:3381::/64,ffff:ffff:ac10:ac10::/64",
v6: NetworkRange{IPNets: mkIPList("fd04:3e42:4a4e:3381::/64", "ffff:ffff:ac10:ac10::/64")},
v6: NetworkRange{CIDRs: mkIPList("fd04:3e42:4a4e:3381::/64", "ffff:ffff:ac10:ac10::/64")},
},
}
for _, tt := range cases {
Expand Down

0 comments on commit 86b4f3d

Please sign in to comment.