Skip to content

Commit 10f1f14

Browse files
committed
fix: stop checking kernel version. default nft, never crash
Signed-off-by: Hunter Gregory <[email protected]>
1 parent 7c7ead6 commit 10f1f14

File tree

7 files changed

+43
-173
lines changed

7 files changed

+43
-173
lines changed

npm/pkg/dataplane/policies/chain-management_linux.go

Lines changed: 7 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,9 @@ const (
2424

2525
// transferred from iptm.go and not sure why this length is important
2626
minLineNumberStringLength int = 3
27-
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"
29-
30-
minNftKernelVersion = 5
3127
)
3228

3329
var (
34-
errDetectingIptablesVersion = errors.New(detectingErrMsg)
35-
3630
// Must loop through a slice because we need a deterministic order for fexec commands for UTs.
3731
iptablesAzureChains = []string{
3832
util.IptablesAzureChain,
@@ -194,9 +188,7 @@ func (pMgr *PolicyManager) bootup(_ []string) error {
194188
klog.Infof("booting up iptables Azure chains")
195189

196190
// 0.1. Detect iptables version
197-
if err := pMgr.detectIptablesVersion(); err != nil {
198-
return npmerrors.SimpleErrorWrapper("failed to detect iptables version", err)
199-
}
191+
pMgr.detectIptablesVersion()
200192

201193
// Stop reconciling so we don't contend for iptables, and so we don't update the staleChains at the same time as reconcile()
202194
// Reconciling would only be happening if this function were called to reset iptables well into the azure-npm pod lifecycle.
@@ -254,47 +246,22 @@ func (pMgr *PolicyManager) bootupAfterDetectAndCleanup() error {
254246
// NPM should use the same iptables version as kube-proxy.
255247
// kube-proxy creates an iptables chain as a hint for which version it uses.
256248
// For more details, see: https://kubernetes.io/blog/2022/09/07/iptables-chains-not-api/#use-case-iptables-mode
257-
func (pMgr *PolicyManager) detectIptablesVersion() error {
249+
func (pMgr *PolicyManager) detectIptablesVersion() {
258250
klog.Info("first attempt detecting iptables version. looking for hint/canary chain in iptables-nft")
259251
if pMgr.hintOrCanaryChainExist(util.IptablesNft) {
260252
util.SetIptablesToNft()
261-
return nil
253+
return
262254
}
263255

264256
klog.Info("second attempt detecting iptables version. looking for hint/canary chain in iptables-legacy")
265257
if pMgr.hintOrCanaryChainExist(util.IptablesLegacy) {
266258
util.SetIptablesToLegacy()
267-
return nil
268-
}
269-
270-
// at this point, chains do not exist in iptables legacy/nft and/or iptables commands have failed for other reasons
271-
klog.Info("third attempt detecting iptables version. getting kernel version")
272-
var majorVersion int
273-
var versionError error
274-
if pMgr.debug {
275-
// for testing purposes
276-
majorVersion = pMgr.debugKernelVersion
277-
versionError = pMgr.debugKernelVersionErr
278-
} else {
279-
majorVersion, versionError = util.KernelReleaseMajorVersion()
280-
}
281-
if versionError != nil {
282-
return errDetectingIptablesVersion
283-
}
284-
285-
if majorVersion >= minNftKernelVersion {
286-
msg := "detected iptables version on third attempt. found kernel version >= 5. NPM will use iptables-nft. kernel version: %d"
287-
klog.Infof(msg, majorVersion)
288-
metrics.SendLog(util.IptmID, fmt.Sprintf(msg, majorVersion), metrics.DonotPrint)
289-
util.SetIptablesToNft()
290-
return nil
259+
return
291260
}
292261

293-
msg := "detected iptables version on third attempt. found kernel version < 5. NPM will use iptables-legacy. kernel version: %d"
294-
klog.Infof(msg, majorVersion)
295-
metrics.SendLog(util.IptmID, fmt.Sprintf(msg, majorVersion), metrics.DonotPrint)
296-
util.SetIptablesToLegacy()
297-
return nil
262+
// default to nft if nothing is found
263+
util.SetIptablesToNft()
264+
return
298265
}
299266

300267
func (pMgr *PolicyManager) hintOrCanaryChainExist(iptablesCmd string) bool {

npm/pkg/dataplane/policies/chain-management_linux_test.go

Lines changed: 21 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package policies
22

33
import (
4-
"errors"
54
"fmt"
65
"sort"
76
"strings"
@@ -45,8 +44,6 @@ Chain AZURE-NPM-INGRESS (1 references)
4544
`
4645
)
4746

48-
var errKernelVersion = errors.New("kernel error")
49-
5047
func TestStaleChainsForceLock(t *testing.T) {
5148
testChains := []string{}
5249
for i := 0; i < 100000; i++ {
@@ -59,7 +56,6 @@ func TestStaleChainsForceLock(t *testing.T) {
5956
ioshim := common.NewMockIOShim(calls)
6057
// don't verify calls because there shouldn't be as many commands as we create if forceLock works properly
6158
pMgr := NewPolicyManager(ioshim, ipsetConfig)
62-
util.SetIptablesToNft()
6359

6460
start := make(chan struct{}, 1)
6561
done := make(chan struct{}, 1)
@@ -146,7 +142,6 @@ func TestCleanupChainsSuccess(t *testing.T) {
146142
ioshim := common.NewMockIOShim(calls)
147143
defer ioshim.VerifyCalls(t, calls)
148144
pMgr := NewPolicyManager(ioshim, ipsetConfig)
149-
util.SetIptablesToNft()
150145

151146
pMgr.staleChains.add(testChain1)
152147
pMgr.staleChains.add(testChain2)
@@ -165,7 +160,6 @@ func TestCleanupChainsFailure(t *testing.T) {
165160
ioshim := common.NewMockIOShim(calls)
166161
defer ioshim.VerifyCalls(t, calls)
167162
pMgr := NewPolicyManager(ioshim, ipsetConfig)
168-
util.SetIptablesToNft()
169163

170164
pMgr.staleChains.add(testChain1)
171165
pMgr.staleChains.add(testChain2)
@@ -484,7 +478,6 @@ func TestBootupLinux(t *testing.T) {
484478
ioshim := common.NewMockIOShim(tt.calls)
485479
defer ioshim.VerifyCalls(t, tt.calls)
486480
pMgr := NewPolicyManager(ioshim, ipsetConfig)
487-
util.SetIptablesToNft()
488481
err := pMgr.bootupAfterDetectAndCleanup()
489482
if tt.wantErr {
490483
require.Error(t, err)
@@ -773,7 +766,6 @@ func TestPositionAzureChainJumpRule(t *testing.T) {
773766
PlaceAzureChainFirst: tt.placeAzureChainFirst,
774767
}
775768
pMgr := NewPolicyManager(ioshim, cfg)
776-
util.SetIptablesToNft()
777769

778770
err := pMgr.positionAzureChainJumpRule()
779771
if tt.wantErr {
@@ -866,7 +858,6 @@ func TestChainLineNumber(t *testing.T) {
866858
ioshim := common.NewMockIOShim(tt.calls)
867859
defer ioshim.VerifyCalls(t, tt.calls)
868860
pMgr := NewPolicyManager(ioshim, ipsetConfig)
869-
util.SetIptablesToNft()
870861

871862
lineNum, err := pMgr.chainLineNumber(testChainName)
872863
if tt.wantErr {
@@ -903,10 +894,7 @@ func stringsToMap(items []string) map[string]struct{} {
903894
func TestDetectIptablesVersion(t *testing.T) {
904895
type args struct {
905896
name string
906-
kernelVersion int
907-
kernelVersionErr error
908897
calls []testutils.TestCmd
909-
expectedErr bool
910898
expectedIptablesVersion string
911899
}
912900

@@ -919,7 +907,6 @@ func TestDetectIptablesVersion(t *testing.T) {
919907
ExitCode: 0,
920908
},
921909
},
922-
expectedErr: false,
923910
expectedIptablesVersion: util.IptablesNft,
924911
},
925912
{
@@ -934,7 +921,6 @@ func TestDetectIptablesVersion(t *testing.T) {
934921
ExitCode: 0,
935922
},
936923
},
937-
expectedErr: false,
938924
expectedIptablesVersion: util.IptablesNft,
939925
},
940926
{
@@ -953,36 +939,10 @@ func TestDetectIptablesVersion(t *testing.T) {
953939
ExitCode: 0,
954940
},
955941
},
956-
expectedErr: false,
957942
expectedIptablesVersion: util.IptablesLegacy,
958943
},
959944
{
960-
name: "nft and legacy both fail: kernel version >= 5",
961-
kernelVersion: 5,
962-
calls: []testutils.TestCmd{
963-
{
964-
Cmd: []string{"iptables-nft", "-w", "60", "-L", "KUBE-IPTABLES-HINT", "-t", "mangle", "-n"},
965-
ExitCode: 2,
966-
},
967-
{
968-
Cmd: []string{"iptables-nft", "-w", "60", "-L", "KUBE-KUBELET-CANARY", "-t", "mangle", "-n"},
969-
ExitCode: 2,
970-
},
971-
{
972-
Cmd: []string{"iptables", "-w", "60", "-L", "KUBE-IPTABLES-HINT", "-t", "mangle", "-n"},
973-
ExitCode: 2,
974-
},
975-
{
976-
Cmd: []string{"iptables", "-w", "60", "-L", "KUBE-KUBELET-CANARY", "-t", "mangle", "-n"},
977-
ExitCode: 2,
978-
},
979-
},
980-
expectedErr: false,
981-
expectedIptablesVersion: util.IptablesNft,
982-
},
983-
{
984-
name: "no kube chains: kernel version < 5",
985-
kernelVersion: 4,
945+
name: "no kube chains: default nft",
986946
calls: []testutils.TestCmd{
987947
{
988948
Cmd: []string{"iptables-nft", "-w", "60", "-L", "KUBE-IPTABLES-HINT", "-t", "mangle", "-n"},
@@ -1001,64 +961,50 @@ func TestDetectIptablesVersion(t *testing.T) {
1001961
ExitCode: 1,
1002962
},
1003963
},
1004-
expectedErr: false,
1005-
expectedIptablesVersion: util.IptablesLegacy,
964+
expectedIptablesVersion: util.IptablesNft,
1006965
},
1007966
{
1008-
name: "no kube chains: kernel version error",
1009-
kernelVersionErr: errKernelVersion,
967+
name: "nft and legacy both fail: default nft",
1010968
calls: []testutils.TestCmd{
1011969
{
1012970
Cmd: []string{"iptables-nft", "-w", "60", "-L", "KUBE-IPTABLES-HINT", "-t", "mangle", "-n"},
1013-
ExitCode: 1,
971+
ExitCode: 2,
1014972
},
1015973
{
1016974
Cmd: []string{"iptables-nft", "-w", "60", "-L", "KUBE-KUBELET-CANARY", "-t", "mangle", "-n"},
1017-
ExitCode: 1,
975+
ExitCode: 2,
1018976
},
1019977
{
1020978
Cmd: []string{"iptables", "-w", "60", "-L", "KUBE-IPTABLES-HINT", "-t", "mangle", "-n"},
1021-
ExitCode: 1,
979+
ExitCode: 2,
1022980
},
1023981
{
1024982
Cmd: []string{"iptables", "-w", "60", "-L", "KUBE-KUBELET-CANARY", "-t", "mangle", "-n"},
1025-
ExitCode: 1,
983+
ExitCode: 2,
1026984
},
1027985
},
1028-
expectedErr: true,
986+
expectedIptablesVersion: util.IptablesNft,
1029987
},
1030988
}
1031989

1032990
for _, tt := range tests {
1033991
tt := tt
1034992

1035-
if tt.name != "no kube chains: kernel version error" {
1036-
continue
1037-
}
1038-
1039993
t.Run(tt.name, func(t *testing.T) {
1040994

1041995
metrics.InitializeAll()
1042996

1043997
ioshim := common.NewMockIOShim(tt.calls)
1044998
defer ioshim.VerifyCalls(t, tt.calls)
1045999
cfg := &PolicyManagerCfg{
1046-
debug: true,
1047-
debugKernelVersion: tt.kernelVersion,
1048-
debugKernelVersionErr: tt.kernelVersionErr,
1049-
NodeIP: "6.7.8.9",
1050-
PolicyMode: IPSetPolicyMode,
1051-
PlaceAzureChainFirst: util.PlaceAzureChainFirst,
1000+
NodeIP: "6.7.8.9",
1001+
PolicyMode: IPSetPolicyMode,
1002+
PlaceAzureChainFirst: util.PlaceAzureChainFirst,
10521003
}
10531004
pMgr := NewPolicyManager(ioshim, cfg)
1054-
err := pMgr.detectIptablesVersion()
1005+
pMgr.detectIptablesVersion()
10551006

1056-
if tt.expectedErr {
1057-
require.Error(t, err)
1058-
} else {
1059-
require.NoError(t, err)
1060-
require.Equal(t, tt.expectedIptablesVersion, util.Iptables)
1061-
}
1007+
require.Equal(t, tt.expectedIptablesVersion, util.Iptables)
10621008
})
10631009
}
10641010
}
@@ -1268,19 +1214,19 @@ func TestCleanupOtherChains(t *testing.T) {
12681214
},
12691215
{
12701216
Cmd: []string{"iptables", "-w", "60", "-F", "AZURE-NPM"},
1271-
ExitCode: 1,
1217+
ExitCode: 2,
12721218
},
12731219
{
12741220
Cmd: []string{"iptables", "-w", "60", "-F", "AZURE-NPM-INGRESS"},
1275-
ExitCode: 1,
1221+
ExitCode: 2,
12761222
},
12771223
{
12781224
Cmd: []string{"iptables", "-w", "60", "-X", "AZURE-NPM"},
1279-
ExitCode: 1,
1225+
ExitCode: 2,
12801226
},
12811227
{
12821228
Cmd: []string{"iptables", "-w", "60", "-X", "AZURE-NPM-INGRESS"},
1283-
ExitCode: 1,
1229+
ExitCode: 2,
12841230
},
12851231
},
12861232
expectedErr: false,
@@ -1402,6 +1348,10 @@ func TestCleanupOtherChains(t *testing.T) {
14021348
util.SetIptablesToNft()
14031349
} else {
14041350
util.SetIptablesToLegacy()
1351+
// set back to default
1352+
defer func() {
1353+
util.SetIptablesToNft()
1354+
}()
14051355
}
14061356

14071357
err := pMgr.cleanupOtherIptables()

npm/pkg/dataplane/policies/policymanager.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,6 @@ const (
2929
)
3030

3131
type PolicyManagerCfg struct {
32-
// debug is only used for testing. Currently just indicating whether to use debug kernel version
33-
debug bool //nolint:unused // only used in linux
34-
// debugKernelVersion is only used for testing
35-
debugKernelVersion int //nolint:unused // only used in linux
36-
// debugKernelVersionErr is only used for testing
37-
debugKernelVersionErr error //nolint:unused // only used in linux
38-
3932
// NodeIP is only used in Windows
4033
NodeIP string
4134
// PolicyMode only affects Windows

npm/pkg/dataplane/policies/policymanager_linux_test.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,6 @@ func TestAddPolicyFailure(t *testing.T) {
212212
ioshim := common.NewMockIOShim(calls)
213213
defer ioshim.VerifyCalls(t, calls)
214214
pMgr := NewPolicyManager(ioshim, ipsetConfig)
215-
util.SetIptablesToNft()
216215

217216
require.Error(t, pMgr.AddPolicies([]*NPMNetworkPolicy{testNetPol}, nil))
218217
_, ok := pMgr.GetPolicy(testNetPol.PolicyKey)
@@ -225,7 +224,6 @@ func TestCreatorForAddPolicies(t *testing.T) {
225224
ioshim := common.NewMockIOShim(calls)
226225
defer ioshim.VerifyCalls(t, calls)
227226
pMgr := NewPolicyManager(ioshim, ipsetConfig)
228-
util.SetIptablesToNft()
229227

230228
// 1. test with activation
231229
policies := []*NPMNetworkPolicy{allTestNetworkPolicies[0]}
@@ -289,7 +287,6 @@ func TestCreatorForRemovePolicies(t *testing.T) {
289287
ioshim := common.NewMockIOShim(calls)
290288
defer ioshim.VerifyCalls(t, calls)
291289
pMgr := NewPolicyManager(ioshim, ipsetConfig)
292-
util.SetIptablesToNft()
293290

294291
// 1. test without deactivation (i.e. flushing azure chain when removing the last policy)
295292
// hack: the cache is empty (and len(cache) != len(allTestNetworkPolicies)), so shouldDeactivate will be false
@@ -337,7 +334,6 @@ func TestRemovePoliciesAcceptableError(t *testing.T) {
337334
ioshim := common.NewMockIOShim(calls)
338335
defer ioshim.VerifyCalls(t, calls)
339336
pMgr := NewPolicyManager(ioshim, ipsetConfig)
340-
util.SetIptablesToNft()
341337
require.NoError(t, pMgr.AddPolicies([]*NPMNetworkPolicy{bothDirectionsNetPol}, epList))
342338
require.NoError(t, pMgr.RemovePolicy(bothDirectionsNetPol.PolicyKey))
343339
_, ok := pMgr.GetPolicy(bothDirectionsNetPol.PolicyKey)
@@ -384,7 +380,6 @@ func TestRemovePoliciesError(t *testing.T) {
384380
ioshim := common.NewMockIOShim(tt.calls)
385381
defer ioshim.VerifyCalls(t, tt.calls)
386382
pMgr := NewPolicyManager(ioshim, ipsetConfig)
387-
util.SetIptablesToNft()
388383
err := pMgr.AddPolicies([]*NPMNetworkPolicy{bothDirectionsNetPol}, nil)
389384
require.NoError(t, err)
390385
err = pMgr.RemovePolicy(bothDirectionsNetPol.PolicyKey)
@@ -407,7 +402,6 @@ func TestUpdatingStaleChains(t *testing.T) {
407402
ioshim := common.NewMockIOShim(calls)
408403
defer ioshim.VerifyCalls(t, calls)
409404
pMgr := NewPolicyManager(ioshim, ipsetConfig)
410-
util.SetIptablesToNft()
411405

412406
// add so we can remove. no stale chains to start
413407
require.NoError(t, pMgr.AddPolicies([]*NPMNetworkPolicy{bothDirectionsNetPol}, nil))

0 commit comments

Comments
 (0)