Move proxy platformSetup call, and do LocalDetector setup from there

This commit is contained in:
Dan Winship 2023-06-30 17:39:36 -04:00
parent ebb0fdd4f6
commit 8abfa89e82
4 changed files with 41 additions and 24 deletions

View File

@ -567,6 +567,11 @@ func newProxyServer(config *kubeproxyconfig.KubeProxyConfiguration, master strin
s.HealthzServer = healthcheck.NewProxierHealthServer(config.HealthzBindAddress, 2*config.IPTables.SyncPeriod.Duration, s.Recorder, s.NodeRef) s.HealthzServer = healthcheck.NewProxierHealthServer(config.HealthzBindAddress, 2*config.IPTables.SyncPeriod.Duration, s.Recorder, s.NodeRef)
} }
err = s.platformSetup()
if err != nil {
return nil, err
}
s.Proxier, err = s.createProxier(config) s.Proxier, err = s.createProxier(config)
if err != nil { if err != nil {
return nil, err return nil, err
@ -706,12 +711,6 @@ func (s *ProxyServer) Run() error {
// Start up a metrics server if requested // Start up a metrics server if requested
serveMetrics(s.Config.MetricsBindAddress, s.Config.Mode, s.Config.EnableProfiling, errCh) serveMetrics(s.Config.MetricsBindAddress, s.Config.Mode, s.Config.EnableProfiling, errCh)
// Do platform-specific setup
err := s.platformSetup()
if err != nil {
return err
}
noProxyName, err := labels.NewRequirement(apis.LabelServiceProxyName, selection.DoesNotExist, nil) noProxyName, err := labels.NewRequirement(apis.LabelServiceProxyName, selection.DoesNotExist, nil)
if err != nil { if err != nil {
return err return err

View File

@ -63,6 +63,8 @@ import (
// node after it is registered. // node after it is registered.
var timeoutForNodePodCIDR = 5 * time.Minute var timeoutForNodePodCIDR = 5 * time.Minute
// platformApplyDefaults is called after parsing command-line flags and/or reading the
// config file, to apply platform-specific default values to config.
func (o *Options) platformApplyDefaults(config *proxyconfigapi.KubeProxyConfiguration) { func (o *Options) platformApplyDefaults(config *proxyconfigapi.KubeProxyConfiguration) {
if config.Mode == "" { if config.Mode == "" {
klog.InfoS("Using iptables proxy") klog.InfoS("Using iptables proxy")
@ -76,21 +78,34 @@ func (o *Options) platformApplyDefaults(config *proxyconfigapi.KubeProxyConfigur
klog.V(2).InfoS("DetectLocalMode", "localMode", string(config.DetectLocalMode)) klog.V(2).InfoS("DetectLocalMode", "localMode", string(config.DetectLocalMode))
} }
// createProxier creates the proxy.Provider // platformSetup is called after setting up the ProxyServer, but before creating the
func (s *ProxyServer) createProxier(config *proxyconfigapi.KubeProxyConfiguration) (proxy.Provider, error) { // Proxier. It should fill in any platform-specific fields and perform other
var proxier proxy.Provider // platform-specific setup.
var err error func (s *ProxyServer) platformSetup() error {
if s.Config.DetectLocalMode == proxyconfigapi.LocalModeNodeCIDR {
if config.DetectLocalMode == proxyconfigapi.LocalModeNodeCIDR {
klog.InfoS("Watching for node, awaiting podCIDR allocation", "hostname", s.Hostname) klog.InfoS("Watching for node, awaiting podCIDR allocation", "hostname", s.Hostname)
node, err := waitForPodCIDR(s.Client, s.Hostname) node, err := waitForPodCIDR(s.Client, s.Hostname)
if err != nil { if err != nil {
return nil, err return err
} }
s.podCIDRs = node.Spec.PodCIDRs s.podCIDRs = node.Spec.PodCIDRs
klog.InfoS("NodeInfo", "podCIDRs", node.Spec.PodCIDRs) klog.InfoS("NodeInfo", "podCIDRs", node.Spec.PodCIDRs)
} }
err := s.setupConntrack()
if err != nil {
return err
}
proxymetrics.RegisterMetrics()
return nil
}
// createProxier creates the proxy.Provider
func (s *ProxyServer) createProxier(config *proxyconfigapi.KubeProxyConfiguration) (proxy.Provider, error) {
var proxier proxy.Provider
var err error
primaryProtocol := utiliptables.ProtocolIPv4 primaryProtocol := utiliptables.ProtocolIPv4
if s.PrimaryIPFamily == v1.IPv6Protocol { if s.PrimaryIPFamily == v1.IPv6Protocol {
primaryProtocol = utiliptables.ProtocolIPv6 primaryProtocol = utiliptables.ProtocolIPv6
@ -271,7 +286,7 @@ func (s *ProxyServer) createProxier(config *proxyconfigapi.KubeProxyConfiguratio
return proxier, nil return proxier, nil
} }
func (s *ProxyServer) platformSetup() error { func (s *ProxyServer) setupConntrack() error {
ct := &realConntracker{} ct := &realConntracker{}
max, err := getConntrackMax(s.Config.Conntrack) max, err := getConntrackMax(s.Config.Conntrack)
@ -311,7 +326,6 @@ func (s *ProxyServer) platformSetup() error {
} }
} }
proxymetrics.RegisterMetrics()
return nil return nil
} }

View File

@ -646,7 +646,7 @@ func TestGetConntrackMax(t *testing.T) {
} }
} }
func TestProxyServer_createProxier(t *testing.T) { func TestProxyServer_platformSetup(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
node *v1.Node node *v1.Node
@ -683,9 +683,8 @@ func TestProxyServer_createProxier(t *testing.T) {
v1.IPv6Protocol: net.IPv6zero, v1.IPv6Protocol: net.IPv6zero,
}, },
} }
_, err := s.createProxier(tt.config) err := s.platformSetup()
// TODO: mock the exec.Interface to not fail probing iptables if err != nil {
if (err != nil) && !strings.Contains(err.Error(), "iptables is not supported for primary IP family") {
t.Errorf("ProxyServer.createProxier() error = %v", err) t.Errorf("ProxyServer.createProxier() error = %v", err)
return return
} }

View File

@ -36,12 +36,22 @@ import (
"k8s.io/kubernetes/pkg/proxy/winkernel" "k8s.io/kubernetes/pkg/proxy/winkernel"
) )
// platformApplyDefaults is called after parsing command-line flags and/or reading the
// config file, to apply platform-specific default values to config.
func (o *Options) platformApplyDefaults(config *proxyconfigapi.KubeProxyConfiguration) { func (o *Options) platformApplyDefaults(config *proxyconfigapi.KubeProxyConfiguration) {
if config.Mode == "" { if config.Mode == "" {
config.Mode = proxyconfigapi.ProxyModeKernelspace config.Mode = proxyconfigapi.ProxyModeKernelspace
} }
} }
// platformSetup is called after setting up the ProxyServer, but before creating the
// Proxier. It should fill in any platform-specific fields and perform other
// platform-specific setup.
func (s *ProxyServer) platformSetup() error {
winkernel.RegisterMetrics()
return nil
}
// createProxier creates the proxy.Provider // createProxier creates the proxy.Provider
func (s *ProxyServer) createProxier(config *proxyconfigapi.KubeProxyConfiguration) (proxy.Provider, error) { func (s *ProxyServer) createProxier(config *proxyconfigapi.KubeProxyConfiguration) (proxy.Provider, error) {
var healthzPort int var healthzPort int
@ -93,11 +103,6 @@ func (s *ProxyServer) createProxier(config *proxyconfigapi.KubeProxyConfiguratio
return proxier, nil return proxier, nil
} }
func (s *ProxyServer) platformSetup() error {
winkernel.RegisterMetrics()
return nil
}
func getDualStackMode(networkname string, compatTester winkernel.StackCompatTester) bool { func getDualStackMode(networkname string, compatTester winkernel.StackCompatTester) bool {
return compatTester.DualStackCompatible(networkname) return compatTester.DualStackCompatible(networkname)
} }