From 8abfa89e82b242abae47575c8ef6c15a56b516b8 Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Fri, 30 Jun 2023 17:39:36 -0400 Subject: [PATCH] Move proxy platformSetup call, and do LocalDetector setup from there --- cmd/kube-proxy/app/server.go | 11 ++++---- cmd/kube-proxy/app/server_others.go | 32 +++++++++++++++++------- cmd/kube-proxy/app/server_others_test.go | 7 +++--- cmd/kube-proxy/app/server_windows.go | 15 +++++++---- 4 files changed, 41 insertions(+), 24 deletions(-) diff --git a/cmd/kube-proxy/app/server.go b/cmd/kube-proxy/app/server.go index 89468c59175..6b12ab4e6cb 100644 --- a/cmd/kube-proxy/app/server.go +++ b/cmd/kube-proxy/app/server.go @@ -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) } + err = s.platformSetup() + if err != nil { + return nil, err + } + s.Proxier, err = s.createProxier(config) if err != nil { return nil, err @@ -706,12 +711,6 @@ func (s *ProxyServer) Run() error { // Start up a metrics server if requested 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) if err != nil { return err diff --git a/cmd/kube-proxy/app/server_others.go b/cmd/kube-proxy/app/server_others.go index c5961ee035a..3ad02d7840f 100644 --- a/cmd/kube-proxy/app/server_others.go +++ b/cmd/kube-proxy/app/server_others.go @@ -63,6 +63,8 @@ import ( // node after it is registered. 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) { if config.Mode == "" { 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)) } -// createProxier creates the proxy.Provider -func (s *ProxyServer) createProxier(config *proxyconfigapi.KubeProxyConfiguration) (proxy.Provider, error) { - var proxier proxy.Provider - var err error - - if config.DetectLocalMode == proxyconfigapi.LocalModeNodeCIDR { +// 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 { + if s.Config.DetectLocalMode == proxyconfigapi.LocalModeNodeCIDR { klog.InfoS("Watching for node, awaiting podCIDR allocation", "hostname", s.Hostname) node, err := waitForPodCIDR(s.Client, s.Hostname) if err != nil { - return nil, err + return err } s.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 if s.PrimaryIPFamily == v1.IPv6Protocol { primaryProtocol = utiliptables.ProtocolIPv6 @@ -271,7 +286,7 @@ func (s *ProxyServer) createProxier(config *proxyconfigapi.KubeProxyConfiguratio return proxier, nil } -func (s *ProxyServer) platformSetup() error { +func (s *ProxyServer) setupConntrack() error { ct := &realConntracker{} max, err := getConntrackMax(s.Config.Conntrack) @@ -311,7 +326,6 @@ func (s *ProxyServer) platformSetup() error { } } - proxymetrics.RegisterMetrics() return nil } diff --git a/cmd/kube-proxy/app/server_others_test.go b/cmd/kube-proxy/app/server_others_test.go index 620ad82fd60..9323f8327a1 100644 --- a/cmd/kube-proxy/app/server_others_test.go +++ b/cmd/kube-proxy/app/server_others_test.go @@ -646,7 +646,7 @@ func TestGetConntrackMax(t *testing.T) { } } -func TestProxyServer_createProxier(t *testing.T) { +func TestProxyServer_platformSetup(t *testing.T) { tests := []struct { name string node *v1.Node @@ -683,9 +683,8 @@ func TestProxyServer_createProxier(t *testing.T) { v1.IPv6Protocol: net.IPv6zero, }, } - _, err := s.createProxier(tt.config) - // TODO: mock the exec.Interface to not fail probing iptables - if (err != nil) && !strings.Contains(err.Error(), "iptables is not supported for primary IP family") { + err := s.platformSetup() + if err != nil { t.Errorf("ProxyServer.createProxier() error = %v", err) return } diff --git a/cmd/kube-proxy/app/server_windows.go b/cmd/kube-proxy/app/server_windows.go index 4c3ef13340f..c076abf90dc 100644 --- a/cmd/kube-proxy/app/server_windows.go +++ b/cmd/kube-proxy/app/server_windows.go @@ -36,12 +36,22 @@ import ( "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) { if config.Mode == "" { 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 func (s *ProxyServer) createProxier(config *proxyconfigapi.KubeProxyConfiguration) (proxy.Provider, error) { var healthzPort int @@ -93,11 +103,6 @@ func (s *ProxyServer) createProxier(config *proxyconfigapi.KubeProxyConfiguratio return proxier, nil } -func (s *ProxyServer) platformSetup() error { - winkernel.RegisterMetrics() - return nil -} - func getDualStackMode(networkname string, compatTester winkernel.StackCompatTester) bool { return compatTester.DualStackCompatible(networkname) }