kube-proxy: use API constants for proxy modes rather than local redefinitions

This commit is contained in:
Dan Winship 2022-08-16 09:23:20 -04:00
parent 1609017f2b
commit 946ce55b04
3 changed files with 19 additions and 26 deletions

View File

@ -86,13 +86,6 @@ import (
utilpointer "k8s.io/utils/pointer"
)
const (
proxyModeUserspace = "userspace"
proxyModeIPTables = "iptables"
proxyModeIPVS = "ipvs"
proxyModeKernelspace = "kernelspace" //nolint:deadcode,varcheck
)
// proxyRun defines the interface to run a specified ProxyServer
type proxyRun interface {
Run() error
@ -540,7 +533,7 @@ type ProxyServer struct {
Recorder events.EventRecorder
ConntrackConfiguration kubeproxyconfig.KubeProxyConntrackConfiguration
Conntracker Conntracker // if nil, ignored
ProxyMode string
ProxyMode kubeproxyconfig.ProxyMode
NodeRef *v1.ObjectReference
MetricsBindAddress string
BindAddressHardFail bool
@ -611,7 +604,7 @@ func serveHealthz(hz healthcheck.ProxierHealthUpdater, errCh chan error) {
go wait.Until(fn, 5*time.Second, wait.NeverStop)
}
func serveMetrics(bindAddress, proxyMode string, enableProfiling bool, errCh chan error) {
func serveMetrics(bindAddress string, proxyMode kubeproxyconfig.ProxyMode, enableProfiling bool, errCh chan error) {
if len(bindAddress) == 0 {
return
}

View File

@ -129,7 +129,7 @@ func newProxyServer(
var proxier proxy.Provider
var detectLocalMode proxyconfigapi.LocalMode
proxyMode := getProxyMode(string(config.Mode))
proxyMode := getProxyMode(config.Mode)
detectLocalMode, err = getDetectLocalMode(config)
if err != nil {
return nil, fmt.Errorf("cannot determine detect-local-mode: %v", err)
@ -157,7 +157,7 @@ func newProxyServer(
var ipt [2]utiliptables.Interface
dualStack := true // While we assume that node supports, we do further checks below
if proxyMode != proxyModeUserspace {
if proxyMode != proxyconfigapi.ProxyModeUserspace {
// Create iptables handlers for both families, one is already created
// Always ordered as IPv4, IPv6
if primaryProtocol == utiliptables.ProtocolIPv4 {
@ -176,7 +176,7 @@ func newProxyServer(
}
}
if proxyMode == proxyModeIPTables {
if proxyMode == proxyconfigapi.ProxyModeIPTables {
klog.V(0).InfoS("Using iptables Proxier")
if config.IPTables.MasqueradeBit == nil {
// MasqueradeBit must be specified or defaulted.
@ -239,7 +239,7 @@ func newProxyServer(
return nil, fmt.Errorf("unable to create proxier: %v", err)
}
proxymetrics.RegisterMetrics()
} else if proxyMode == proxyModeIPVS {
} else if proxyMode == proxyconfigapi.ProxyModeIPVS {
kernelHandler := ipvs.NewLinuxKernelHandler()
ipsetInterface = utilipset.New(execer)
if err := ipvs.CanUseIPVSProxier(kernelHandler, ipsetInterface, config.IPVS.Scheduler); err != nil {
@ -342,7 +342,7 @@ func newProxyServer(
}
useEndpointSlices := true
if proxyMode == proxyModeUserspace {
if proxyMode == proxyconfigapi.ProxyModeUserspace {
// userspace mode doesn't support endpointslice.
useEndpointSlices = false
}
@ -547,10 +547,10 @@ func cidrTuple(cidrList string) [2]string {
return cidrs
}
func getProxyMode(proxyMode string) string {
func getProxyMode(proxyMode proxyconfigapi.ProxyMode) proxyconfigapi.ProxyMode {
if proxyMode == "" {
klog.InfoS("Using iptables proxy")
return proxyModeIPTables
return proxyconfigapi.ProxyModeIPTables
} else {
return proxyMode
}

View File

@ -102,9 +102,9 @@ func newProxyServer(config *proxyconfigapi.KubeProxyConfiguration, master string
}
var proxier proxy.Provider
proxyMode := getProxyMode(string(config.Mode), winkernel.WindowsKernelCompatTester{})
proxyMode := getProxyMode(config.Mode, winkernel.WindowsKernelCompatTester{})
dualStackMode := getDualStackMode(config.Winkernel.NetworkName, winkernel.DualStackCompatTester{})
if proxyMode == proxyModeKernelspace {
if proxyMode == proxyconfigapi.ProxyModeKernelspace {
klog.V(0).InfoS("Using Kernelspace Proxier.")
if dualStackMode {
klog.V(0).InfoS("Creating dualStackProxier for Windows kernel.")
@ -166,7 +166,7 @@ func newProxyServer(config *proxyconfigapi.KubeProxyConfiguration, master string
}
}
useEndpointSlices := true
if proxyMode == proxyModeUserspace {
if proxyMode == proxyconfigapi.ProxyModeUserspace {
// userspace mode doesn't support endpointslice.
useEndpointSlices = false
}
@ -192,18 +192,18 @@ func getDualStackMode(networkname string, compatTester winkernel.StackCompatTest
return compatTester.DualStackCompatible(networkname)
}
func getProxyMode(proxyMode string, kcompat winkernel.KernelCompatTester) string {
if proxyMode == proxyModeKernelspace {
func getProxyMode(proxyMode proxyconfigapi.ProxyMode, kcompat winkernel.KernelCompatTester) proxyconfigapi.ProxyMode {
if proxyMode == proxyconfigapi.ProxyModeKernelspace {
return tryWinKernelSpaceProxy(kcompat)
}
return proxyModeUserspace
return proxyconfigapi.ProxyModeUserspace
}
func detectNumCPU() int {
return goruntime.NumCPU()
}
func tryWinKernelSpaceProxy(kcompat winkernel.KernelCompatTester) string {
func tryWinKernelSpaceProxy(kcompat winkernel.KernelCompatTester) proxyconfigapi.ProxyMode {
// Check for Windows Kernel Version if we can support Kernel Space proxy
// Check for Windows Version
@ -211,14 +211,14 @@ func tryWinKernelSpaceProxy(kcompat winkernel.KernelCompatTester) string {
useWinKernelProxy, err := winkernel.CanUseWinKernelProxier(kcompat)
if err != nil {
klog.ErrorS(err, "Can't determine whether to use windows kernel proxy, using userspace proxier")
return proxyModeUserspace
return proxyconfigapi.ProxyModeUserspace
}
if useWinKernelProxy {
return proxyModeKernelspace
return proxyconfigapi.ProxyModeKernelspace
}
// Fallback.
klog.V(1).InfoS("Can't use winkernel proxy, using userspace proxier")
return proxyModeUserspace
return proxyconfigapi.ProxyModeUserspace
}
// cleanupAndExit cleans up after a previous proxy run