Skip to content

Commit b173ff7

Browse files
committed
log iptables command error
Signed-off-by: Qingchuan Hao <[email protected]>
1 parent 6ef95a3 commit b173ff7

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

pkg/capture/provider/network_capture_unix.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ type command struct {
197197
func (ncp *NetworkCaptureProvider) CollectMetadata() error {
198198
ncp.l.Info("Start to collect network metadata")
199199

200-
iptablesMode := obtainIptablesMode()
200+
iptablesMode := obtainIptablesMode(ncp.l)
201201
ncp.l.Info(fmt.Sprintf("Iptables mode %s is used", iptablesMode))
202202
iptablesSaveCmdName := fmt.Sprintf("iptables-%s-save", iptablesMode)
203203
iptablesCmdName := fmt.Sprintf("iptables-%s", iptablesMode)
@@ -371,27 +371,29 @@ const (
371371
nftIptablesMode iptablesMode = "nft"
372372
)
373373

374-
func obtainIptablesMode() iptablesMode {
374+
func obtainIptablesMode(logger *log.ZapLogger) iptablesMode {
375375
// Since iptables v1.8, nf_tables are introduced as an improvement of legacy iptables, but provides the same user
376376
// interface as legacy iptables through iptables-nft command.
377377
// based on: https://github.com/kubernetes-sigs/iptables-wrappers/blob/97b01f43a8e8db07840fc4b95e833a37c0d36b12/iptables-wrapper-installer.sh
378378

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.
380380
nftIptablesModeAvaiable := true
381381
legacyIptablesModeAvaiable := true
382382
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))
385386
}
386-
387387
legacySaveLineNum := len(strings.Split(string(legacySaveOut), "\n"))
388+
388389
nftSaveOut, err := exec.Command("iptables-nft-save").CombinedOutput()
389-
if err != nil && strings.Contains(err.Error(), "command not found") {
390+
if err != nil {
390391
nftIptablesModeAvaiable = false
392+
logger.Error("Failed to run iptables-nft-save", zap.Error(err))
391393
}
394+
nftSaveLineNum := len(strings.Split(string(nftSaveOut), "\n"))
392395

393396
if nftIptablesModeAvaiable && legacyIptablesModeAvaiable {
394-
nftSaveLineNum := len(strings.Split(string(nftSaveOut), "\n"))
395397
if legacySaveLineNum > nftSaveLineNum {
396398
return legacyIptablesMode
397399
}

0 commit comments

Comments
 (0)