@@ -25,7 +25,7 @@ const (
2525 // transferred from iptm.go and not sure why this length is important
2626 minLineNumberStringLength int = 3
2727
28- detectingErrMsg = "failed to detect iptables version. failed to find KUBE chains in iptables-legacy-save and iptables-nft-save and failed to get kernel version. NPM will crash to retry"
28+ detectingErrMsg = "failed to detect iptables version. failed to run iptables-legacy-save, run iptables-nft-save, and get kernel version. NPM will crash to retry"
2929)
3030
3131var (
@@ -249,53 +249,54 @@ func (pMgr *PolicyManager) bootupAfterDetectAndCleanup() error {
249249func (pMgr * PolicyManager ) detectIptablesVersion () error {
250250 klog .Info ("first attempt detecting iptables version. running: iptables-nft-save -t mangle" )
251251 cmd := pMgr .ioShim .Exec .Command (util .IptablesSaveNft , "-t" , "mangle" )
252- output , err := cmd .CombinedOutput ()
253- if err == nil && strings .Contains (string (output ), "KUBE-IPTABLES-HINT" ) || strings .Contains (string (output ), "KUBE-KUBELET-CANARY" ) {
252+ output , nftErr := cmd .CombinedOutput ()
253+ if nftErr != nil {
254+ msg := "failed to detect iptables version on first attempt. error running iptables-nft-save. will try detecting using iptables-legacy-save. err: %s"
255+ metrics .SendErrorLogAndMetric (util .IptmID , msg , nftErr .Error ())
256+ } else if strings .Contains (string (output ), "KUBE-IPTABLES-HINT" ) || strings .Contains (string (output ), "KUBE-KUBELET-CANARY" ) {
254257 msg := "detected iptables version on first attempt. found KUBE chains in nft iptables. NPM will use iptables-nft"
255258 klog .Info (msg )
256259 metrics .SendLog (util .IptmID , msg , metrics .DonotPrint )
257260 util .SetIptablesToNft ()
258261 return nil
259262 }
260263
261- if err != nil {
262- msg := "failed to detect iptables version on first attempt. error running iptables-nft-save. will try detecting using iptables-legacy-save. err: %s"
263- metrics .SendErrorLogAndMetric (util .IptmID , msg , err .Error ())
264- }
265-
266264 klog .Info ("second attempt detecting iptables version. running: iptables-legacy-save -t mangle" )
267265 lCmd := pMgr .ioShim .Exec .Command (util .IptablesSaveLegacy , "-t" , "mangle" )
268- loutput , err := lCmd .CombinedOutput ()
269- if err == nil && strings .Contains (string (loutput ), "KUBE-IPTABLES-HINT" ) || strings .Contains (string (loutput ), "KUBE-KUBELET-CANARY" ) {
270- msg := "detected iptables version on second attempt. found KUBE chains in legacy tables. NPM will use iptables-legacy"
271- klog .Info (msg )
272- metrics .SendLog (util .IptmID , msg , metrics .DonotPrint )
273- util .SetIptablesToLegacy ()
274- return nil
275- }
276-
277- if err != nil {
266+ loutput , legacyErr := lCmd .CombinedOutput ()
267+ if legacyErr != nil {
278268 msg := "failed to detect iptables version on second attempt. error running iptables-legacy-save. will try detecting using kernel version. err: %s"
279- metrics .SendErrorLogAndMetric (util .IptmID , msg , err .Error ())
269+ metrics .SendErrorLogAndMetric (util .IptmID , msg , legacyErr .Error ())
270+ } else {
271+ if strings .Contains (string (loutput ), "KUBE-IPTABLES-HINT" ) || strings .Contains (string (loutput ), "KUBE-KUBELET-CANARY" ) {
272+ msg := "detected iptables version on second attempt. found KUBE chains in legacy tables. NPM will use iptables-legacy"
273+ klog .Info (msg )
274+ metrics .SendLog (util .IptmID , msg , metrics .DonotPrint )
275+ util .SetIptablesToLegacy ()
276+ return nil
277+ } else if nftErr != nil {
278+ msg := "NPM will use iptables-nft. iptables-nft-save failed earlier, but iptables-legacy-save didn't have KUBE chains"
279+ klog .Info (msg )
280+ metrics .SendLog (util .IptmID , msg , metrics .DonotPrint )
281+ util .SetIptablesToNft ()
282+ return nil
283+ }
280284 }
281285
286+ // we are here if either:
287+ // 1. both nft and legacy save commands failed
288+ // 2. both nft and legacy save commands didn't have KUBE chains
282289 klog .Info ("third attempt detecting iptables version. getting kernel version" )
283- kernelRelease := ""
290+ var majorVersion int
291+ var versionError error
284292 if pMgr .debug {
285293 // for testing purposes
286- kernelRelease = pMgr .debugKernelVersion
294+ majorVersion = pMgr .debugKernelVersion
295+ versionError = pMgr .debugKernelVersionErr
287296 } else {
288- kernelRelease = util .KernelRelease ()
289- }
290- kernelVersion := strings .Split (kernelRelease , "." )[0 ]
291- if kernelVersion == "" {
292- metrics .SendErrorLogAndMetric (util .IptmID , "failed to detect iptables version on third attempt. error getting kernel version" )
293- return errDetectingIptablesVersion
297+ majorVersion , versionError = util .KernelReleaseMajorVersion ()
294298 }
295-
296- majorVersion , err := strconv .Atoi (kernelVersion )
297- if err != nil {
298- metrics .SendErrorLogAndMetric (util .IptmID , "failed to detect iptables version on third attempt. error converting kernel version to int. err: %s" , err .Error ())
299+ if versionError != nil {
299300 return errDetectingIptablesVersion
300301 }
301302
0 commit comments