@@ -909,58 +909,69 @@ func TestDetectIptablesVersion(t *testing.T) {
909909
910910 tests := []args {
911911 {
912- name : "iptables- nft-save returns kube chains " ,
912+ name : "nft has hint chain " ,
913913 calls : []testutils.TestCmd {
914914 {
915- Cmd : []string {"iptables-nft-save " , "-t" , "mangle" },
916- Stdout : iptablesSaveMangleOutput ,
915+ Cmd : []string {"iptables-nft" , "-w " , "60" , " -t" , "mangle" , "-n" , "-L" , "KUBE-IPTABLES-HINT " },
916+ ExitCode : 0 ,
917917 },
918918 },
919919 expectedErr : false ,
920920 expectedIptablesVersion : util .IptablesNft ,
921921 },
922922 {
923- name : "iptables-save returns kube chains " ,
923+ name : "nft has only canary chain " ,
924924 calls : []testutils.TestCmd {
925925 {
926- Cmd : []string {"iptables-nft-save " , "-t" , "mangle" },
927- Stdout : "" ,
926+ Cmd : []string {"iptables-nft" , "-w " , "60" , " -t" , "mangle" , "-n" , "-L" , "KUBE-IPTABLES-HINT " },
927+ ExitCode : 1 ,
928928 },
929929 {
930- Cmd : []string {"iptables-save " , "-t" , "mangle" },
931- Stdout : iptablesSaveMangleOutput ,
930+ Cmd : []string {"iptables-nft " , "-w" , "60" , "- t" , "mangle" , "-n" , "-L" , "KUBE-KUBELET-CANARY " },
931+ ExitCode : 0 ,
932932 },
933933 },
934934 expectedErr : false ,
935- expectedIptablesVersion : util .IptablesLegacy ,
935+ expectedIptablesVersion : util .IptablesNft ,
936936 },
937937 {
938- name : "iptables-nft-save and iptables-save both fail: kernel version >= 5" ,
939- kernelVersion : 5 ,
938+ name : "legacy has hint chain" ,
940939 calls : []testutils.TestCmd {
941940 {
942- Cmd : []string {"iptables-nft-save " , "-t" , "mangle" },
941+ Cmd : []string {"iptables-nft" , "-w " , "60" , " -t" , "mangle" , "-n" , "-L" , "KUBE-IPTABLES-HINT " },
943942 ExitCode : 1 ,
944943 },
945944 {
946- Cmd : []string {"iptables-save " , "-t" , "mangle" },
945+ Cmd : []string {"iptables-nft " , "-w" , "60" , "- t" , "mangle" , "-n" , "-L" , "KUBE-KUBELET-CANARY " },
947946 ExitCode : 1 ,
948947 },
948+ {
949+ Cmd : []string {"iptables" , "-w" , "60" , "-t" , "mangle" , "-n" , "-L" , "KUBE-IPTABLES-HINT" },
950+ ExitCode : 0 ,
951+ },
949952 },
950953 expectedErr : false ,
951- expectedIptablesVersion : util .IptablesNft ,
954+ expectedIptablesVersion : util .IptablesLegacy ,
952955 },
953956 {
954- name : "no kube chains : kernel version >= 5" ,
957+ name : "nft and legacy both fail : kernel version >= 5" ,
955958 kernelVersion : 5 ,
956959 calls : []testutils.TestCmd {
957960 {
958- Cmd : []string {"iptables-nft-save " , "-t" , "mangle" },
959- Stdout : "" ,
961+ Cmd : []string {"iptables-nft" , "-w " , "60" , " -t" , "mangle" , "-n" , "-L" , "KUBE-IPTABLES-HINT " },
962+ ExitCode : 2 ,
960963 },
961964 {
962- Cmd : []string {"iptables-save" , "-t" , "mangle" },
963- Stdout : "" ,
965+ Cmd : []string {"iptables-nft" , "-w" , "60" , "-t" , "mangle" , "-n" , "-L" , "KUBE-KUBELET-CANARY" },
966+ ExitCode : 2 ,
967+ },
968+ {
969+ Cmd : []string {"iptables" , "-w" , "60" , "-t" , "mangle" , "-n" , "-L" , "KUBE-IPTABLES-HINT" },
970+ ExitCode : 2 ,
971+ },
972+ {
973+ Cmd : []string {"iptables" , "-w" , "60" , "-t" , "mangle" , "-n" , "-L" , "KUBE-KUBELET-CANARY" },
974+ ExitCode : 2 ,
964975 },
965976 },
966977 expectedErr : false ,
@@ -971,12 +982,20 @@ func TestDetectIptablesVersion(t *testing.T) {
971982 kernelVersion : 4 ,
972983 calls : []testutils.TestCmd {
973984 {
974- Cmd : []string {"iptables-nft-save " , "-t" , "mangle" },
975- Stdout : "" ,
985+ Cmd : []string {"iptables-nft" , "-w " , "60" , " -t" , "mangle" , "-n" , "-L" , "KUBE-IPTABLES-HINT " },
986+ ExitCode : 1 ,
976987 },
977988 {
978- Cmd : []string {"iptables-save" , "-t" , "mangle" },
979- Stdout : "" ,
989+ Cmd : []string {"iptables-nft" , "-w" , "60" , "-t" , "mangle" , "-n" , "-L" , "KUBE-KUBELET-CANARY" },
990+ ExitCode : 1 ,
991+ },
992+ {
993+ Cmd : []string {"iptables" , "-w" , "60" , "-t" , "mangle" , "-n" , "-L" , "KUBE-IPTABLES-HINT" },
994+ ExitCode : 1 ,
995+ },
996+ {
997+ Cmd : []string {"iptables" , "-w" , "60" , "-t" , "mangle" , "-n" , "-L" , "KUBE-KUBELET-CANARY" },
998+ ExitCode : 1 ,
980999 },
9811000 },
9821001 expectedErr : false ,
@@ -987,12 +1006,20 @@ func TestDetectIptablesVersion(t *testing.T) {
9871006 kernelVersionErr : fmt .Errorf ("kernel version error" ),
9881007 calls : []testutils.TestCmd {
9891008 {
990- Cmd : []string {"iptables-nft-save" , "-t" , "mangle" },
991- Stdout : "" ,
1009+ Cmd : []string {"iptables-nft" , "-w" , "60" , "-t" , "mangle" , "-n" , "-L" , "KUBE-IPTABLES-HINT" },
1010+ ExitCode : 1 ,
1011+ },
1012+ {
1013+ Cmd : []string {"iptables-nft" , "-w" , "60" , "-t" , "mangle" , "-n" , "-L" , "KUBE-KUBELET-CANARY" },
1014+ ExitCode : 1 ,
1015+ },
1016+ {
1017+ Cmd : []string {"iptables" , "-w" , "60" , "-t" , "mangle" , "-n" , "-L" , "KUBE-IPTABLES-HINT" },
1018+ ExitCode : 1 ,
9921019 },
9931020 {
994- Cmd : []string {"iptables-save " , "-t" , "mangle" },
995- Stdout : "" ,
1021+ Cmd : []string {"iptables" , "-w " , "60" , " -t" , "mangle" , "-n" , "-L" , "KUBE-KUBELET-CANARY " },
1022+ ExitCode : 1 ,
9961023 },
9971024 },
9981025 expectedErr : true ,
@@ -1001,7 +1028,8 @@ func TestDetectIptablesVersion(t *testing.T) {
10011028
10021029 for _ , tt := range tests {
10031030 tt := tt
1004- if tt .name != "no kube chains: kernel version is empty" {
1031+
1032+ if tt .name != "no kube chains: kernel version error" {
10051033 continue
10061034 }
10071035
@@ -1012,11 +1040,12 @@ func TestDetectIptablesVersion(t *testing.T) {
10121040 ioshim := common .NewMockIOShim (tt .calls )
10131041 defer ioshim .VerifyCalls (t , tt .calls )
10141042 cfg := & PolicyManagerCfg {
1015- debug : true ,
1016- debugKernelVersion : tt .kernelVersion ,
1017- NodeIP : "6.7.8.9" ,
1018- PolicyMode : IPSetPolicyMode ,
1019- PlaceAzureChainFirst : util .PlaceAzureChainFirst ,
1043+ debug : true ,
1044+ debugKernelVersion : tt .kernelVersion ,
1045+ debugKernelVersionErr : tt .kernelVersionErr ,
1046+ NodeIP : "6.7.8.9" ,
1047+ PolicyMode : IPSetPolicyMode ,
1048+ PlaceAzureChainFirst : util .PlaceAzureChainFirst ,
10201049 }
10211050 pMgr := NewPolicyManager (ioshim , cfg )
10221051 err := pMgr .detectIptablesVersion ()
0 commit comments