@@ -31,11 +31,12 @@ import (
31
31
"github.com/coreos/pkg/flagutil"
32
32
"github.com/flannel-io/flannel/pkg/ip"
33
33
"github.com/flannel-io/flannel/pkg/ipmatch"
34
- "github.com/flannel-io/flannel/pkg/iptables"
35
34
"github.com/flannel-io/flannel/pkg/lease"
36
35
"github.com/flannel-io/flannel/pkg/subnet"
37
36
etcd "github.com/flannel-io/flannel/pkg/subnet/etcd"
38
37
"github.com/flannel-io/flannel/pkg/subnet/kube"
38
+ "github.com/flannel-io/flannel/pkg/trafficmngr"
39
+ "github.com/flannel-io/flannel/pkg/trafficmngr/iptables"
39
40
"github.com/flannel-io/flannel/pkg/version"
40
41
"golang.org/x/net/context"
41
42
log "k8s.io/klog/v2"
@@ -335,6 +336,8 @@ func main() {
335
336
os .Exit (1 )
336
337
}
337
338
339
+ //Create TrafficManager and instanciate it based on whether we use iptables or nftables
340
+ trafficMngr := newTrafficManager ()
338
341
// Set up ipMasq if needed
339
342
if opts .ipMasq {
340
343
if config .EnableIPv4 {
@@ -345,22 +348,22 @@ func main() {
345
348
wg .Wait ()
346
349
os .Exit (1 )
347
350
}
348
- if err = recycleIPTables (net , bn .Lease ()); err != nil {
351
+ if err = recycleIPTables (trafficMngr , net , bn .Lease ()); err != nil {
349
352
log .Errorf ("Failed to recycle IPTables rules, %v" , err )
350
353
cancel ()
351
354
wg .Wait ()
352
355
os .Exit (1 )
353
356
}
354
357
log .Infof ("Setting up masking rules" )
355
- iptables .CreateIP4Chain ("nat" , "FLANNEL-POSTRTG" )
356
- getRules := func () []iptables .IPTablesRule {
358
+ trafficMngr .CreateIP4Chain ("nat" , "FLANNEL-POSTRTG" )
359
+ getRules := func () []trafficmngr .IPTablesRule {
357
360
if config .HasNetworks () {
358
- return iptables .MasqRules (config .Networks , bn .Lease ())
361
+ return trafficMngr .MasqRules (config .Networks , bn .Lease ())
359
362
} else {
360
- return iptables .MasqRules ([]ip.IP4Net {config .Network }, bn .Lease ())
363
+ return trafficMngr .MasqRules ([]ip.IP4Net {config .Network }, bn .Lease ())
361
364
}
362
365
}
363
- go iptables .SetupAndEnsureIP4Tables (getRules , opts .iptablesResyncSeconds )
366
+ go trafficMngr .SetupAndEnsureIP4Tables (getRules , opts .iptablesResyncSeconds )
364
367
365
368
}
366
369
if config .EnableIPv6 {
@@ -371,22 +374,22 @@ func main() {
371
374
wg .Wait ()
372
375
os .Exit (1 )
373
376
}
374
- if err = recycleIP6Tables (ip6net , bn .Lease ()); err != nil {
377
+ if err = recycleIP6Tables (trafficMngr , ip6net , bn .Lease ()); err != nil {
375
378
log .Errorf ("Failed to recycle IP6Tables rules, %v" , err )
376
379
cancel ()
377
380
wg .Wait ()
378
381
os .Exit (1 )
379
382
}
380
383
log .Infof ("Setting up masking ip6 rules" )
381
- iptables .CreateIP6Chain ("nat" , "FLANNEL-POSTRTG" )
382
- getRules := func () []iptables .IPTablesRule {
384
+ trafficMngr .CreateIP6Chain ("nat" , "FLANNEL-POSTRTG" )
385
+ getRules := func () []trafficmngr .IPTablesRule {
383
386
if config .HasIPv6Networks () {
384
- return iptables .MasqIP6Rules (config .IPv6Networks , bn .Lease ())
387
+ return trafficMngr .MasqIP6Rules (config .IPv6Networks , bn .Lease ())
385
388
} else {
386
- return iptables .MasqIP6Rules ([]ip.IP6Net {config .IPv6Network }, bn .Lease ())
389
+ return trafficMngr .MasqIP6Rules ([]ip.IP6Net {config .IPv6Network }, bn .Lease ())
387
390
}
388
391
}
389
- go iptables .SetupAndEnsureIP6Tables (getRules , opts .iptablesResyncSeconds )
392
+ go trafficMngr .SetupAndEnsureIP6Tables (getRules , opts .iptablesResyncSeconds )
390
393
}
391
394
}
392
395
@@ -403,11 +406,11 @@ func main() {
403
406
os .Exit (1 )
404
407
}
405
408
log .Infof ("Changing default FORWARD chain policy to ACCEPT" )
406
- iptables .CreateIP4Chain ("filter" , "FLANNEL-FWD" )
407
- getRules := func () []iptables .IPTablesRule {
408
- return iptables .ForwardRules (net .String ())
409
+ trafficMngr .CreateIP4Chain ("filter" , "FLANNEL-FWD" )
410
+ getRules := func () []trafficmngr .IPTablesRule {
411
+ return trafficMngr .ForwardRules (net .String ())
409
412
}
410
- go iptables .SetupAndEnsureIP4Tables (getRules , opts .iptablesResyncSeconds )
413
+ go trafficMngr .SetupAndEnsureIP4Tables (getRules , opts .iptablesResyncSeconds )
411
414
}
412
415
if config .EnableIPv6 {
413
416
ip6net , err := config .GetFlannelIPv6Network (& bn .Lease ().IPv6Subnet )
@@ -418,11 +421,11 @@ func main() {
418
421
os .Exit (1 )
419
422
}
420
423
log .Infof ("IPv6: Changing default FORWARD chain policy to ACCEPT" )
421
- iptables .CreateIP6Chain ("filter" , "FLANNEL-FWD" )
422
- getRules := func () []iptables .IPTablesRule {
423
- return iptables .ForwardRules (ip6net .String ())
424
+ trafficMngr .CreateIP6Chain ("filter" , "FLANNEL-FWD" )
425
+ getRules := func () []trafficmngr .IPTablesRule {
426
+ return trafficMngr .ForwardRules (ip6net .String ())
424
427
}
425
- go iptables .SetupAndEnsureIP6Tables (getRules , opts .iptablesResyncSeconds )
428
+ go trafficMngr .SetupAndEnsureIP6Tables (getRules , opts .iptablesResyncSeconds )
426
429
}
427
430
}
428
431
@@ -462,7 +465,7 @@ func main() {
462
465
os .Exit (0 )
463
466
}
464
467
465
- func recycleIPTables (nw ip.IP4Net , myLease * lease.Lease ) error {
468
+ func recycleIPTables (tm trafficmngr. TrafficManager , nw ip.IP4Net , myLease * lease.Lease ) error {
466
469
prevNetworks := ReadCIDRsFromSubnetFile (opts .subnetFile , "FLANNEL_NETWORK" )
467
470
prevSubnet := ReadCIDRFromSubnetFile (opts .subnetFile , "FLANNEL_SUBNET" )
468
471
@@ -480,14 +483,14 @@ func recycleIPTables(nw ip.IP4Net, myLease *lease.Lease) error {
480
483
newLease := & lease.Lease {
481
484
Subnet : prevSubnet ,
482
485
}
483
- if err := iptables .DeleteIP4Tables (iptables .MasqRules (prevNetworks , newLease )); err != nil {
486
+ if err := tm .DeleteIP4Tables (tm .MasqRules (prevNetworks , newLease )); err != nil {
484
487
return err
485
488
}
486
489
}
487
490
return nil
488
491
}
489
492
490
- func recycleIP6Tables (nw ip.IP6Net , myLease * lease.Lease ) error {
493
+ func recycleIP6Tables (tm trafficmngr. TrafficManager , nw ip.IP6Net , myLease * lease.Lease ) error {
491
494
prevNetworks := ReadIP6CIDRsFromSubnetFile (opts .subnetFile , "FLANNEL_IPV6_NETWORK" )
492
495
prevSubnet := ReadIP6CIDRFromSubnetFile (opts .subnetFile , "FLANNEL_IPV6_SUBNET" )
493
496
@@ -506,7 +509,7 @@ func recycleIP6Tables(nw ip.IP6Net, myLease *lease.Lease) error {
506
509
lease := & lease.Lease {
507
510
IPv6Subnet : prevSubnet ,
508
511
}
509
- if err := iptables .DeleteIP6Tables (iptables .MasqIP6Rules (prevNetworks , lease )); err != nil {
512
+ if err := tm .DeleteIP6Tables (tm .MasqIP6Rules (prevNetworks , lease )); err != nil {
510
513
return err
511
514
}
512
515
}
@@ -656,3 +659,7 @@ func ReadIP6CIDRsFromSubnetFile(path string, CIDRKey string) []ip.IP6Net {
656
659
}
657
660
return prevCIDRs
658
661
}
662
+
663
+ func newTrafficManager () trafficmngr.TrafficManager {
664
+ return iptables.IPTablesManager {}
665
+ }
0 commit comments