@@ -31,6 +31,11 @@ import (
31
31
"github.com/kubeovn/kube-ovn/pkg/util"
32
32
)
33
33
34
+ const (
35
+ kernelModuleIPTables = "ip_tables"
36
+ kernelModuleIP6Tables = "ip6_tables"
37
+ )
38
+
34
39
// ControllerRuntime represents runtime specific controller members
35
40
type ControllerRuntime struct {
36
41
iptables map [string ]* iptables.IPTables
@@ -93,11 +98,17 @@ func (c *Controller) initRuntime() error {
93
98
}
94
99
c .iptables [kubeovnv1 .ProtocolIPv4 ] = ipt
95
100
if c .iptablesObsolete != nil {
96
- if ipt , err = iptables .NewWithProtocolAndMode (iptables .ProtocolIPv4 , "legacy" ); err != nil {
97
- klog .Error (err )
98
- return err
101
+ ok , err := kernelModuleLoaded (kernelModuleIPTables )
102
+ if err != nil {
103
+ klog .Errorf ("failed to check kernel module %s: %v" , kernelModuleIPTables , err )
104
+ }
105
+ if ok {
106
+ if ipt , err = iptables .NewWithProtocolAndMode (iptables .ProtocolIPv4 , "legacy" ); err != nil {
107
+ klog .Error (err )
108
+ return err
109
+ }
110
+ c .iptablesObsolete [kubeovnv1 .ProtocolIPv4 ] = ipt
99
111
}
100
- c .iptablesObsolete [kubeovnv1 .ProtocolIPv4 ] = ipt
101
112
}
102
113
c .ipsets [kubeovnv1 .ProtocolIPv4 ] = ipsets .NewIPSets (ipsets .NewIPVersionConfig (ipsets .IPFamilyV4 , IPSetPrefix , nil , nil ))
103
114
c .k8siptables [kubeovnv1 .ProtocolIPv4 ] = k8siptables .New (c .k8sExec , k8siptables .ProtocolIPv4 )
@@ -110,11 +121,17 @@ func (c *Controller) initRuntime() error {
110
121
}
111
122
c .iptables [kubeovnv1 .ProtocolIPv6 ] = ipt
112
123
if c .iptablesObsolete != nil {
113
- if ipt , err = iptables .NewWithProtocolAndMode (iptables .ProtocolIPv6 , "legacy" ); err != nil {
114
- klog .Error (err )
115
- return err
124
+ ok , err := kernelModuleLoaded (kernelModuleIP6Tables )
125
+ if err != nil {
126
+ klog .Errorf ("failed to check kernel module %s: %v" , kernelModuleIP6Tables , err )
127
+ }
128
+ if ok {
129
+ if ipt , err = iptables .NewWithProtocolAndMode (iptables .ProtocolIPv6 , "legacy" ); err != nil {
130
+ klog .Error (err )
131
+ return err
132
+ }
133
+ c .iptablesObsolete [kubeovnv1 .ProtocolIPv6 ] = ipt
116
134
}
117
- c .iptablesObsolete [kubeovnv1 .ProtocolIPv6 ] = ipt
118
135
}
119
136
c .ipsets [kubeovnv1 .ProtocolIPv6 ] = ipsets .NewIPSets (ipsets .NewIPVersionConfig (ipsets .IPFamilyV6 , IPSetPrefix , nil , nil ))
120
137
c .k8siptables [kubeovnv1 .ProtocolIPv6 ] = k8siptables .New (c .k8sExec , k8siptables .ProtocolIPv6 )
@@ -696,3 +713,19 @@ func rotateLog() {
696
713
klog .Errorf ("failed to rotate kube-ovn log %q" , output )
697
714
}
698
715
}
716
+
717
+ func kernelModuleLoaded (module string ) (bool , error ) {
718
+ data , err := os .ReadFile ("/proc/modules" )
719
+ if err != nil {
720
+ klog .Errorf ("failed to read /proc/modules: %v" , err )
721
+ return false , err
722
+ }
723
+
724
+ for _ , line := range strings .Split (string (data ), "\n " ) {
725
+ if fields := strings .Fields (line ); len (fields ) != 0 && fields [0 ] == module {
726
+ return true , nil
727
+ }
728
+ }
729
+
730
+ return false , nil
731
+ }
0 commit comments