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