mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-25 12:43:23 +00:00
Clean-up un-used code
This commit is contained in:
parent
3bd3759424
commit
cf214d0738
@ -24,7 +24,6 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
"regexp"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
@ -606,7 +605,6 @@ func newServiceInfo(port *v1.ServicePort, service *v1.Service, bsvcPortInfo *pro
|
|||||||
|
|
||||||
// KernelHandler can handle the current installed kernel modules.
|
// KernelHandler can handle the current installed kernel modules.
|
||||||
type KernelHandler interface {
|
type KernelHandler interface {
|
||||||
GetModules() ([]string, error)
|
|
||||||
GetKernelVersion() (string, error)
|
GetKernelVersion() (string, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -622,73 +620,6 @@ func NewLinuxKernelHandler() *LinuxKernelHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetModules returns all installed kernel modules.
|
|
||||||
func (handle *LinuxKernelHandler) GetModules() ([]string, error) {
|
|
||||||
// Check whether IPVS required kernel modules are built-in
|
|
||||||
kernelVersionStr, err := handle.GetKernelVersion()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
kernelVersion, err := version.ParseGeneric(kernelVersionStr)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("error parsing kernel version %q: %v", kernelVersionStr, err)
|
|
||||||
}
|
|
||||||
ipvsModules := utilipvs.GetRequiredIPVSModules(kernelVersion)
|
|
||||||
|
|
||||||
var bmods, lmods []string
|
|
||||||
|
|
||||||
// Find out loaded kernel modules. If this is a full static kernel it will try to verify if the module is compiled using /boot/config-KERNELVERSION
|
|
||||||
modulesFile, err := os.Open("/proc/modules")
|
|
||||||
if err == os.ErrNotExist {
|
|
||||||
klog.ErrorS(err, "Failed to read file /proc/modules, assuming this is a kernel without loadable modules support enabled")
|
|
||||||
kernelConfigFile := fmt.Sprintf("/boot/config-%s", kernelVersionStr)
|
|
||||||
kConfig, err := os.ReadFile(kernelConfigFile)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to read Kernel Config file %s with error %w", kernelConfigFile, err)
|
|
||||||
}
|
|
||||||
for _, module := range ipvsModules {
|
|
||||||
if match, _ := regexp.Match("CONFIG_"+strings.ToUpper(module)+"=y", kConfig); match {
|
|
||||||
bmods = append(bmods, module)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return bmods, nil
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to read file /proc/modules with error %w", err)
|
|
||||||
}
|
|
||||||
defer modulesFile.Close()
|
|
||||||
|
|
||||||
mods, err := getFirstColumn(modulesFile)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to find loaded kernel modules: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
builtinModsFilePath := fmt.Sprintf("/lib/modules/%s/modules.builtin", kernelVersionStr)
|
|
||||||
b, err := os.ReadFile(builtinModsFilePath)
|
|
||||||
if err != nil {
|
|
||||||
klog.ErrorS(err, "Failed to read builtin modules file, you can ignore this message when kube-proxy is running inside container without mounting /lib/modules", "filePath", builtinModsFilePath)
|
|
||||||
}
|
|
||||||
|
|
||||||
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.InfoS("Failed to load kernel module with modprobe, "+
|
|
||||||
"you can ignore this message when kube-proxy is running inside container without mounting /lib/modules", "moduleName", module)
|
|
||||||
} else {
|
|
||||||
lmods = append(lmods, module)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
mods = append(mods, bmods...)
|
|
||||||
mods = append(mods, lmods...)
|
|
||||||
return mods, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// getFirstColumn reads all the content from r into memory and return a
|
// getFirstColumn reads all the content from r into memory and return a
|
||||||
// slice which consists of the first word from each line.
|
// slice which consists of the first word from each line.
|
||||||
func getFirstColumn(r io.Reader) ([]string, error) {
|
func getFirstColumn(r io.Reader) ([]string, error) {
|
||||||
|
@ -21,8 +21,6 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"k8s.io/apimachinery/pkg/util/version"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Interface is an injectable interface for running ipvs commands. Implementations must be goroutine-safe.
|
// Interface is an injectable interface for running ipvs commands. Implementations must be goroutine-safe.
|
||||||
@ -71,22 +69,6 @@ const (
|
|||||||
FlagHashed = 0x2
|
FlagHashed = 0x2
|
||||||
)
|
)
|
||||||
|
|
||||||
// IPVS required kernel modules.
|
|
||||||
const (
|
|
||||||
// KernelModuleIPVS is the kernel module "ip_vs"
|
|
||||||
KernelModuleIPVS string = "ip_vs"
|
|
||||||
// KernelModuleIPVSRR is the kernel module "ip_vs_rr"
|
|
||||||
KernelModuleIPVSRR string = "ip_vs_rr"
|
|
||||||
// KernelModuleIPVSWRR is the kernel module "ip_vs_wrr"
|
|
||||||
KernelModuleIPVSWRR string = "ip_vs_wrr"
|
|
||||||
// KernelModuleIPVSSH is the kernel module "ip_vs_sh"
|
|
||||||
KernelModuleIPVSSH string = "ip_vs_sh"
|
|
||||||
// KernelModuleNfConntrackIPV4 is the module "nf_conntrack_ipv4"
|
|
||||||
KernelModuleNfConntrackIPV4 string = "nf_conntrack_ipv4"
|
|
||||||
// KernelModuleNfConntrack is the kernel module "nf_conntrack"
|
|
||||||
KernelModuleNfConntrack string = "nf_conntrack"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Equal check the equality of virtual server.
|
// Equal check the equality of virtual server.
|
||||||
// We don't use struct == since it doesn't work because of slice.
|
// We don't use struct == since it doesn't work because of slice.
|
||||||
func (svc *VirtualServer) Equal(other *VirtualServer) bool {
|
func (svc *VirtualServer) Equal(other *VirtualServer) bool {
|
||||||
@ -122,17 +104,6 @@ func (rs *RealServer) Equal(other *RealServer) bool {
|
|||||||
rs.Port == other.Port
|
rs.Port == other.Port
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetRequiredIPVSModules returns the required ipvs modules for the given linux kernel version.
|
|
||||||
func GetRequiredIPVSModules(kernelVersion *version.Version) []string {
|
|
||||||
// "nf_conntrack_ipv4" has been removed since v4.19
|
|
||||||
// see https://github.com/torvalds/linux/commit/a0ae2562c6c4b2721d9fddba63b7286c13517d9f
|
|
||||||
if kernelVersion.LessThan(version.MustParseGeneric("4.19")) {
|
|
||||||
return []string{KernelModuleIPVS, KernelModuleIPVSRR, KernelModuleIPVSWRR, KernelModuleIPVSSH, KernelModuleNfConntrackIPV4}
|
|
||||||
}
|
|
||||||
return []string{KernelModuleIPVS, KernelModuleIPVSRR, KernelModuleIPVSWRR, KernelModuleIPVSSH, KernelModuleNfConntrack}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// IsRsGracefulTerminationNeeded returns true if protocol requires graceful termination for the stale connections
|
// IsRsGracefulTerminationNeeded returns true if protocol requires graceful termination for the stale connections
|
||||||
func IsRsGracefulTerminationNeeded(proto string) bool {
|
func IsRsGracefulTerminationNeeded(proto string) bool {
|
||||||
return !strings.EqualFold(proto, "UDP") && !strings.EqualFold(proto, "SCTP")
|
return !strings.EqualFold(proto, "UDP") && !strings.EqualFold(proto, "SCTP")
|
||||||
|
Loading…
Reference in New Issue
Block a user