Skip to content

Commit 0062a7d

Browse files
tedyuyutedz
authored andcommitted
Store parsed CIDRs at initialization of Proxier
1 parent cee320a commit 0062a7d

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

pkg/proxy/ipvs/proxier.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ type Proxier struct {
194194
syncPeriod time.Duration
195195
minSyncPeriod time.Duration
196196
// Values are CIDR's to exclude when cleaning up IPVS rules.
197-
excludeCIDRs []string
197+
excludeCIDRs []*net.IPNet
198198
// Set to true to set sysctls arp_ignore and arp_announce
199199
strictARP bool
200200
iptables utiliptables.Interface
@@ -274,6 +274,19 @@ func (r *realIPGetter) NodeIPs() (ips []net.IP, err error) {
274274
// Proxier implements ProxyProvider
275275
var _ proxy.ProxyProvider = &Proxier{}
276276

277+
// ParseExcludedCIDRs parses the input strings and returns net.IPNet
278+
// The validation has been done earlier so the error condition will never happen under normal conditions
279+
func ParseExcludedCIDRs(excludeCIDRStrs []string) []*net.IPNet {
280+
var cidrExclusions []*net.IPNet
281+
for _, excludedCIDR := range excludeCIDRStrs {
282+
_, n, err := net.ParseCIDR(excludedCIDR)
283+
if err == nil {
284+
cidrExclusions = append(cidrExclusions, n)
285+
}
286+
}
287+
return cidrExclusions
288+
}
289+
277290
// NewProxier returns a new Proxier given an iptables and ipvs Interface instance.
278291
// Because of the iptables and ipvs logic, it is assumed that there is only a single Proxier active on a machine.
279292
// An error will be returned if it fails to update or acquire the initial lock.
@@ -286,7 +299,7 @@ func NewProxier(ipt utiliptables.Interface,
286299
exec utilexec.Interface,
287300
syncPeriod time.Duration,
288301
minSyncPeriod time.Duration,
289-
excludeCIDRs []string,
302+
excludeCIDRStrs []string,
290303
strictARP bool,
291304
masqueradeAll bool,
292305
masqueradeBit int,
@@ -397,7 +410,7 @@ func NewProxier(ipt utiliptables.Interface,
397410
endpointsChanges: proxy.NewEndpointChangeTracker(hostname, nil, &isIPv6, recorder),
398411
syncPeriod: syncPeriod,
399412
minSyncPeriod: minSyncPeriod,
400-
excludeCIDRs: excludeCIDRs,
413+
excludeCIDRs: ParseExcludedCIDRs(excludeCIDRStrs),
401414
iptables: ipt,
402415
masqueradeAll: masqueradeAll,
403416
masqueradeMark: masqueradeMark,
@@ -1715,9 +1728,7 @@ func (proxier *Proxier) cleanLegacyService(activeServices map[string]bool, curre
17151728
func (proxier *Proxier) isIPInExcludeCIDRs(ip net.IP) bool {
17161729
// make sure it does not fall within an excluded CIDR range.
17171730
for _, excludedCIDR := range proxier.excludeCIDRs {
1718-
// Any validation of this CIDR already should have occurred.
1719-
_, n, _ := net.ParseCIDR(excludedCIDR)
1720-
if n.Contains(ip) {
1731+
if excludedCIDR.Contains(ip) {
17211732
return true
17221733
}
17231734
}

pkg/proxy/ipvs/proxier_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func (fakeSysctl *FakeSysctl) SetSysctl(sysctl string, newVal int) error {
125125
return nil
126126
}
127127

128-
func NewFakeProxier(ipt utiliptables.Interface, ipvs utilipvs.Interface, ipset utilipset.Interface, nodeIPs []net.IP, excludeCIDRs []string) *Proxier {
128+
func NewFakeProxier(ipt utiliptables.Interface, ipvs utilipvs.Interface, ipset utilipset.Interface, nodeIPs []net.IP, excludeCIDRs []*net.IPNet) *Proxier {
129129
fcmd := fakeexec.FakeCmd{
130130
CombinedOutputScript: []fakeexec.FakeCombinedOutputAction{
131131
func() ([]byte, error) { return []byte("dummy device have been created"), nil },
@@ -2823,7 +2823,7 @@ func TestCleanLegacyService(t *testing.T) {
28232823
ipt := iptablestest.NewFake()
28242824
ipvs := ipvstest.NewFake()
28252825
ipset := ipsettest.NewFake(testIPSetVersion)
2826-
fp := NewFakeProxier(ipt, ipvs, ipset, nil, []string{"3.3.3.0/24", "4.4.4.0/24"})
2826+
fp := NewFakeProxier(ipt, ipvs, ipset, nil, ParseExcludedCIDRs([]string{"3.3.3.0/24", "4.4.4.0/24"}))
28272827

28282828
// All ipvs services that were processed in the latest sync loop.
28292829
activeServices := map[string]bool{"ipvs0": true, "ipvs1": true}
@@ -2930,7 +2930,7 @@ func TestCleanLegacyRealServersExcludeCIDRs(t *testing.T) {
29302930
ipvs := ipvstest.NewFake()
29312931
ipset := ipsettest.NewFake(testIPSetVersion)
29322932
gtm := NewGracefulTerminationManager(ipvs)
2933-
fp := NewFakeProxier(ipt, ipvs, ipset, nil, []string{"4.4.4.4/32"})
2933+
fp := NewFakeProxier(ipt, ipvs, ipset, nil, ParseExcludedCIDRs([]string{"4.4.4.4/32"}))
29342934
fp.gracefuldeleteManager = gtm
29352935

29362936
vs := &utilipvs.VirtualServer{
@@ -2984,7 +2984,7 @@ func TestCleanLegacyService6(t *testing.T) {
29842984
ipt := iptablestest.NewFake()
29852985
ipvs := ipvstest.NewFake()
29862986
ipset := ipsettest.NewFake(testIPSetVersion)
2987-
fp := NewFakeProxier(ipt, ipvs, ipset, nil, []string{"3000::/64", "4000::/64"})
2987+
fp := NewFakeProxier(ipt, ipvs, ipset, nil, ParseExcludedCIDRs([]string{"3000::/64", "4000::/64"}))
29882988
fp.nodeIP = net.ParseIP("::1")
29892989

29902990
// All ipvs services that were processed in the latest sync loop.

0 commit comments

Comments
 (0)