@@ -197,7 +197,7 @@ type command struct {
197
197
func (ncp * NetworkCaptureProvider ) CollectMetadata () error {
198
198
ncp .l .Info ("Start to collect network metadata" )
199
199
200
- iptablesMode := obtainIptablesMode ()
200
+ iptablesMode := obtainIptablesMode (ncp . l )
201
201
ncp .l .Info (fmt .Sprintf ("Iptables mode %s is used" , iptablesMode ))
202
202
iptablesSaveCmdName := fmt .Sprintf ("iptables-%s-save" , iptablesMode )
203
203
iptablesCmdName := fmt .Sprintf ("iptables-%s" , iptablesMode )
@@ -371,27 +371,29 @@ const (
371
371
nftIptablesMode iptablesMode = "nft"
372
372
)
373
373
374
- func obtainIptablesMode () iptablesMode {
374
+ func obtainIptablesMode (logger * log. ZapLogger ) iptablesMode {
375
375
// Since iptables v1.8, nf_tables are introduced as an improvement of legacy iptables, but provides the same user
376
376
// interface as legacy iptables through iptables-nft command.
377
377
// based on: https://github.com/kubernetes-sigs/iptables-wrappers/blob/97b01f43a8e8db07840fc4b95e833a37c0d36b12/iptables-wrapper-installer.sh
378
378
379
- // when both iptables modes available, we choose the one with more rules.
379
+ // When both iptables modes available, we choose the one with more rules, because the other one normally outputs empty rules.
380
380
nftIptablesModeAvaiable := true
381
381
legacyIptablesModeAvaiable := true
382
382
legacySaveOut , err := exec .Command ("iptables-legacy-save" ).CombinedOutput ()
383
- if err != nil && strings .Contains (err .Error (), "command not found" ) {
384
- legacyIptablesModeAvaiable = false
383
+ if err != nil {
384
+ nftIptablesModeAvaiable = false
385
+ logger .Error ("Failed to run iptables-legacy-save" , zap .Error (err ))
385
386
}
386
-
387
387
legacySaveLineNum := len (strings .Split (string (legacySaveOut ), "\n " ))
388
+
388
389
nftSaveOut , err := exec .Command ("iptables-nft-save" ).CombinedOutput ()
389
- if err != nil && strings . Contains ( err . Error (), "command not found" ) {
390
+ if err != nil {
390
391
nftIptablesModeAvaiable = false
392
+ logger .Error ("Failed to run iptables-nft-save" , zap .Error (err ))
391
393
}
394
+ nftSaveLineNum := len (strings .Split (string (nftSaveOut ), "\n " ))
392
395
393
396
if nftIptablesModeAvaiable && legacyIptablesModeAvaiable {
394
- nftSaveLineNum := len (strings .Split (string (nftSaveOut ), "\n " ))
395
397
if legacySaveLineNum > nftSaveLineNum {
396
398
return legacyIptablesMode
397
399
}
0 commit comments