Skip to content

Commit

Permalink
log iptables command error
Browse files Browse the repository at this point in the history
  • Loading branch information
mainred committed Oct 30, 2024
1 parent 6ef95a3 commit 30b2834
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions pkg/capture/provider/network_capture_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ type command struct {
func (ncp *NetworkCaptureProvider) CollectMetadata() error {
ncp.l.Info("Start to collect network metadata")

iptablesMode := obtainIptablesMode()
iptablesMode := obtainIptablesMode(ncp.l)
ncp.l.Info(fmt.Sprintf("Iptables mode %s is used", iptablesMode))
iptablesSaveCmdName := fmt.Sprintf("iptables-%s-save", iptablesMode)
iptablesCmdName := fmt.Sprintf("iptables-%s", iptablesMode)
Expand Down Expand Up @@ -371,27 +371,29 @@ const (
nftIptablesMode iptablesMode = "nft"
)

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

// when both iptables modes available, we choose the one with more rules.
// When both iptables modes available, we choose the one with more rules, because the other one normally outputs empty rules.
nftIptablesModeAvaiable := true
legacyIptablesModeAvaiable := true
legacySaveOut, err := exec.Command("iptables-legacy-save").CombinedOutput()
if err != nil && strings.Contains(err.Error(), "command not found") {
legacyIptablesModeAvaiable = false
if err != nil {
nftIptablesModeAvaiable = false
logger.Error("Failed to write command run failure", zap.Error(err))
}

legacySaveLineNum := len(strings.Split(string(legacySaveOut), "\n"))

nftSaveOut, err := exec.Command("iptables-nft-save").CombinedOutput()
if err != nil && strings.Contains(err.Error(), "command not found") {
if err != nil {
nftIptablesModeAvaiable = false
logger.Error("Failed to write command run failure", zap.Error(err))
}
nftSaveLineNum := len(strings.Split(string(nftSaveOut), "\n"))

if nftIptablesModeAvaiable && legacyIptablesModeAvaiable {
nftSaveLineNum := len(strings.Split(string(nftSaveOut), "\n"))
if legacySaveLineNum > nftSaveLineNum {
return legacyIptablesMode
}
Expand Down

0 comments on commit 30b2834

Please sign in to comment.