diff --git a/pkg/proxy/ipvs/proxier.go b/pkg/proxy/ipvs/proxier.go index e58782e4b82..9d3997c8ae6 100644 --- a/pkg/proxy/ipvs/proxier.go +++ b/pkg/proxy/ipvs/proxier.go @@ -578,30 +578,12 @@ func (handle *LinuxKernelHandler) GetModules() ([]string, error) { } ipvsModules := utilipvs.GetRequiredIPVSModules(kernelVersion) - builtinModsFilePath := fmt.Sprintf("/lib/modules/%s/modules.builtin", kernelVersionStr) - b, err := ioutil.ReadFile(builtinModsFilePath) - if err != nil { - klog.Warningf("Failed to read file %s with error %v. You can ignore this message when kube-proxy is running inside container without mounting /lib/modules", builtinModsFilePath, err) - } var bmods []string - for _, module := range ipvsModules { - if match, _ := regexp.Match(module+".ko", b); match { - bmods = append(bmods, module) - } - } - // Try to load IPVS required kernel modules using modprobe first - for _, kmod := range ipvsModules { - err := handle.executor.Command("modprobe", "--", kmod).Run() - if err != nil { - klog.Warningf("Failed to load kernel module %v with modprobe. "+ - "You can ignore this message when kube-proxy is running inside container without mounting /lib/modules", kmod) - } - } - - // Find out loaded kernel modules + // Find out loaded kernel modules. If this is a full static kernel it will thrown an error modulesFile, err := os.Open("/proc/modules") if err != nil { + klog.Warningf("Failed to read file /proc/modules with error %v. Kube-proxy requires loadable modules support enabled in the kernel", err) return nil, err } @@ -610,6 +592,25 @@ func (handle *LinuxKernelHandler) GetModules() ([]string, error) { return nil, fmt.Errorf("failed to find loaded kernel modules: %v", err) } + builtinModsFilePath := fmt.Sprintf("/lib/modules/%s/modules.builtin", kernelVersionStr) + b, err := ioutil.ReadFile(builtinModsFilePath) + if err != nil { + klog.Warningf("Failed to read file %s with error %v. You can ignore this message when kube-proxy is running inside container without mounting /lib/modules", builtinModsFilePath, err) + } + + for _, module := range ipvsModules { + if match, _ := regexp.Match(module+".ko", b); match { + bmods = append(bmods, module) + } else { + // Try to load the required IPVS kernel modules if not built in + err := handle.executor.Command("modprobe", "--", module).Run() + if err != nil { + klog.Warningf("Failed to load kernel module %v with modprobe. "+ + "You can ignore this message when kube-proxy is running inside container without mounting /lib/modules", module) + } + } + } + return append(mods, bmods...), nil }