@@ -44,7 +44,74 @@ type IPTablesManager struct{}
44
44
45
45
const kubeProxyMark string = "0x4000/0x4000"
46
46
47
- func (iptm IPTablesManager ) MasqRules (cluster_cidrs []ip.IP4Net , lease * lease.Lease ) []trafficmngr.IPTablesRule {
47
+ func (iptm IPTablesManager ) SetupAndEnsureMasqRules (flannelIPv4Net , prevSubnet ip.IP4Net ,
48
+ prevNetworks []ip.IP4Net ,
49
+ flannelIPv6Net , prevIPv6Subnet ip.IP6Net ,
50
+ prevIPv6Networks []ip.IP6Net ,
51
+ currentlease * lease.Lease ,
52
+ resyncPeriod int ) error {
53
+ if flannelIPv4Net .String () != "" {
54
+ //Find the cidr in FLANNEL_NETWORK which contains the podCIDR (i.e. FLANNEL_SUBNET) of this node
55
+ prevNetwork := ip.IP4Net {}
56
+ for _ , net := range prevNetworks {
57
+ if net .ContainsCIDR (& prevSubnet ) {
58
+ prevNetwork = net
59
+ break
60
+ }
61
+ }
62
+ // recycle iptables rules only when network configured or subnet leased is not equal to current one.
63
+ if prevNetwork != flannelIPv4Net && prevSubnet != currentlease .Subnet {
64
+ log .Infof ("Current network or subnet (%v, %v) is not equal to previous one (%v, %v), trying to recycle old iptables rules" ,
65
+ flannelIPv4Net , currentlease .Subnet , prevNetwork , prevSubnet )
66
+ newLease := & lease.Lease {
67
+ Subnet : prevSubnet ,
68
+ }
69
+ if err := iptm .deleteIP4Tables (iptm .masqRules (prevNetworks , newLease )); err != nil {
70
+ return err
71
+ }
72
+ }
73
+
74
+ log .Infof ("Setting up masking rules" )
75
+ iptm .CreateIP4Chain ("nat" , "FLANNEL-POSTRTG" )
76
+ //Note: doesn't work for multiple networks but we disabled MultiClusterCIDR anyway
77
+ getRules := func () []trafficmngr.IPTablesRule {
78
+ return iptm .masqRules ([]ip.IP4Net {flannelIPv4Net }, currentlease )
79
+ }
80
+ go iptm .setupAndEnsureIP4Tables (getRules , resyncPeriod )
81
+ }
82
+ if flannelIPv6Net .String () != "" {
83
+ //Find the cidr in FLANNEL_IPV6_NETWORK which contains the podCIDR (i.e. FLANNEL_IPV6_SUBNET) of this node
84
+ prevIPv6Network := ip.IP6Net {}
85
+ for _ , net := range prevIPv6Networks {
86
+ if net .ContainsCIDR (& prevIPv6Subnet ) {
87
+ prevIPv6Network = net
88
+ break
89
+ }
90
+ }
91
+ // recycle iptables rules only when network configured or subnet leased is not equal to current one.
92
+ if prevIPv6Network != flannelIPv6Net && prevIPv6Subnet != currentlease .IPv6Subnet {
93
+ log .Infof ("Current network or subnet (%v, %v) is not equal to previous one (%v, %v), trying to recycle old iptables rules" ,
94
+ flannelIPv6Net , currentlease .IPv6Subnet , prevIPv6Network , prevIPv6Subnet )
95
+ newLease := & lease.Lease {
96
+ IPv6Subnet : prevIPv6Subnet ,
97
+ }
98
+ if err := iptm .deleteIP6Tables (iptm .masqIP6Rules (prevIPv6Networks , newLease )); err != nil {
99
+ return err
100
+ }
101
+ }
102
+
103
+ log .Infof ("Setting up masking rules for IPv6" )
104
+ iptm .CreateIP6Chain ("nat" , "FLANNEL-POSTRTG" )
105
+ //Note: doesn't work for multiple networks but we disabled MultiClusterCIDR anyway
106
+ getRules := func () []trafficmngr.IPTablesRule {
107
+ return iptm .masqIP6Rules ([]ip.IP6Net {flannelIPv6Net }, currentlease )
108
+ }
109
+ go iptm .setupAndEnsureIP6Tables (getRules , resyncPeriod )
110
+ }
111
+ return nil
112
+ }
113
+
114
+ func (iptm IPTablesManager ) masqRules (cluster_cidrs []ip.IP4Net , lease * lease.Lease ) []trafficmngr.IPTablesRule {
48
115
pod_cidr := lease .Subnet .String ()
49
116
ipt , err := iptables .New ()
50
117
supports_random_fully := false
@@ -90,7 +157,7 @@ func (iptm IPTablesManager) MasqRules(cluster_cidrs []ip.IP4Net, lease *lease.Le
90
157
return rules
91
158
}
92
159
93
- func (iptm IPTablesManager ) MasqIP6Rules (cluster_cidrs []ip.IP6Net , lease * lease.Lease ) []trafficmngr.IPTablesRule {
160
+ func (iptm IPTablesManager ) masqIP6Rules (cluster_cidrs []ip.IP6Net , lease * lease.Lease ) []trafficmngr.IPTablesRule {
94
161
pod_cidr := lease .IPv6Subnet .String ()
95
162
ipt , err := iptables .NewWithProtocol (iptables .ProtocolIPv6 )
96
163
supports_random_fully := false
@@ -141,7 +208,26 @@ func (iptm IPTablesManager) MasqIP6Rules(cluster_cidrs []ip.IP6Net, lease *lease
141
208
return rules
142
209
}
143
210
144
- func (iptm IPTablesManager ) ForwardRules (flannelNetwork string ) []trafficmngr.IPTablesRule {
211
+ func (iptm IPTablesManager ) SetupAndEnsureForwardRules (flannelIPv4Network ip.IP4Net , flannelIPv6Network ip.IP6Net , resyncPeriod int ) {
212
+ if flannelIPv4Network .String () != "" {
213
+ log .Infof ("Changing default FORWARD chain policy to ACCEPT" )
214
+ iptm .CreateIP4Chain ("filter" , "FLANNEL-FWD" )
215
+ getRules := func () []trafficmngr.IPTablesRule {
216
+ return iptm .forwardRules (flannelIPv4Network .String ())
217
+ }
218
+ go iptm .setupAndEnsureIP4Tables (getRules , resyncPeriod )
219
+ }
220
+ if flannelIPv6Network .String () != "" {
221
+ log .Infof ("IPv6: Changing default FORWARD chain policy to ACCEPT" )
222
+ iptm .CreateIP6Chain ("filter" , "FLANNEL-FWD" )
223
+ getRules := func () []trafficmngr.IPTablesRule {
224
+ return iptm .forwardRules (flannelIPv6Network .String ())
225
+ }
226
+ go iptm .setupAndEnsureIP6Tables (getRules , resyncPeriod )
227
+ }
228
+ }
229
+
230
+ func (iptm IPTablesManager ) forwardRules (flannelNetwork string ) []trafficmngr.IPTablesRule {
145
231
return []trafficmngr.IPTablesRule {
146
232
// This rule ensure that the flannel iptables rules are executed before other rules on the node
147
233
{Table : "filter" , Action : "-A" , Chain : "FORWARD" , Rulespec : []string {"-m" , "comment" , "--comment" , "flanneld forward" , "-j" , "FLANNEL-FWD" }},
@@ -281,7 +367,7 @@ func ipTablesBootstrap(ipt IPTables, iptRestore IPTablesRestore, rules []traffic
281
367
return nil
282
368
}
283
369
284
- func (iptm IPTablesManager ) SetupAndEnsureIP4Tables (getRules func () []trafficmngr.IPTablesRule , resyncPeriod int ) {
370
+ func (iptm IPTablesManager ) setupAndEnsureIP4Tables (getRules func () []trafficmngr.IPTablesRule , resyncPeriod int ) {
285
371
rules := getRules ()
286
372
log .Infof ("generated %d rules" , len (rules ))
287
373
ipt , err := iptables .New ()
@@ -320,7 +406,7 @@ func (iptm IPTablesManager) SetupAndEnsureIP4Tables(getRules func() []trafficmng
320
406
}
321
407
}
322
408
323
- func (iptm IPTablesManager ) SetupAndEnsureIP6Tables (getRules func () []trafficmngr.IPTablesRule , resyncPeriod int ) {
409
+ func (iptm IPTablesManager ) setupAndEnsureIP6Tables (getRules func () []trafficmngr.IPTablesRule , resyncPeriod int ) {
324
410
rules := getRules ()
325
411
ipt , err := iptables .NewWithProtocol (iptables .ProtocolIPv6 )
326
412
if err != nil {
@@ -358,8 +444,8 @@ func (iptm IPTablesManager) SetupAndEnsureIP6Tables(getRules func() []trafficmng
358
444
}
359
445
}
360
446
361
- // DeleteIP4Tables delete specified iptables rules
362
- func (iptm IPTablesManager ) DeleteIP4Tables (rules []trafficmngr.IPTablesRule ) error {
447
+ // deleteIP4Tables delete specified iptables rules
448
+ func (iptm IPTablesManager ) deleteIP4Tables (rules []trafficmngr.IPTablesRule ) error {
363
449
ipt , err := iptables .New ()
364
450
if err != nil {
365
451
// if we can't find iptables, give up and return
@@ -380,8 +466,8 @@ func (iptm IPTablesManager) DeleteIP4Tables(rules []trafficmngr.IPTablesRule) er
380
466
return nil
381
467
}
382
468
383
- // DeleteIP6Tables delete specified iptables rules
384
- func (iptm IPTablesManager ) DeleteIP6Tables (rules []trafficmngr.IPTablesRule ) error {
469
+ // deleteIP6Tables delete specified iptables rules
470
+ func (iptm IPTablesManager ) deleteIP6Tables (rules []trafficmngr.IPTablesRule ) error {
385
471
ipt , err := iptables .NewWithProtocol (iptables .ProtocolIPv6 )
386
472
if err != nil {
387
473
// if we can't find iptables, give up and return
0 commit comments