From 48f431aae581bce70dab0d98174c4a9c2bc071e2 Mon Sep 17 00:00:00 2001 From: JieJhih Jhang Date: Wed, 24 Apr 2019 16:32:11 +0800 Subject: [PATCH] os exit when option is true --- cmd/kube-proxy/app/server.go | 30 ++++++++++++++++++---------- cmd/kube-proxy/app/server_others.go | 1 - cmd/kube-proxy/app/server_test.go | 12 ++++++++++- cmd/kube-proxy/app/server_windows.go | 3 ++- 4 files changed, 32 insertions(+), 14 deletions(-) diff --git a/cmd/kube-proxy/app/server.go b/cmd/kube-proxy/app/server.go index efea3697fe9..2c71d45e076 100644 --- a/cmd/kube-proxy/app/server.go +++ b/cmd/kube-proxy/app/server.go @@ -91,6 +91,7 @@ const ( // proxyRun defines the interface to run a specified ProxyServer type proxyRun interface { Run() error + CleanupAndExit() error } // Options contains everything necessary to create and run a proxy server. @@ -307,6 +308,11 @@ func (o *Options) Run() error { if err != nil { return err } + + if o.CleanupAndExit { + return proxyServer.CleanupAndExit() + } + o.proxyServer = proxyServer return o.runLoop() } @@ -497,7 +503,6 @@ type ProxyServer struct { Conntracker Conntracker // if nil, ignored ProxyMode string NodeRef *v1.ObjectReference - CleanupAndExit bool CleanupIPVS bool MetricsBindAddress string EnableProfiling bool @@ -546,19 +551,10 @@ func createClients(config componentbaseconfig.ClientConnectionConfiguration, mas } // Run runs the specified ProxyServer. This should never exit (unless CleanupAndExit is set). +// TODO: At the moment, Run() cannot return a nil error, otherwise it's caller will never exit. Update callers of Run to handle nil errors. func (s *ProxyServer) Run() error { // To help debugging, immediately log version klog.Infof("Version: %+v", version.Get()) - // remove iptables rules and exit - if s.CleanupAndExit { - encounteredError := userspace.CleanupLeftovers(s.IptInterface) - encounteredError = iptables.CleanupLeftovers(s.IptInterface) || encounteredError - encounteredError = ipvs.CleanupLeftovers(s.IpvsInterface, s.IptInterface, s.IpsetInterface, s.CleanupIPVS) || encounteredError - if encounteredError { - return errors.New("encountered an error while tearing down rules") - } - return nil - } // TODO(vmarmol): Use container config for this. var oomAdjuster *oom.OOMAdjuster @@ -704,3 +700,15 @@ func getConntrackMax(config kubeproxyconfig.KubeProxyConntrackConfiguration) (in } return 0, nil } + +// CleanupAndExit remove iptables rules and exit if success return nil +func (s *ProxyServer) CleanupAndExit() error { + encounteredError := userspace.CleanupLeftovers(s.IptInterface) + encounteredError = iptables.CleanupLeftovers(s.IptInterface) || encounteredError + encounteredError = ipvs.CleanupLeftovers(s.IpvsInterface, s.IptInterface, s.IpsetInterface, s.CleanupIPVS) || encounteredError + if encounteredError { + return errors.New("encountered an error while tearing down rules") + } + + return nil +} diff --git a/cmd/kube-proxy/app/server_others.go b/cmd/kube-proxy/app/server_others.go index 0f0832c9bc4..1182c001668 100644 --- a/cmd/kube-proxy/app/server_others.go +++ b/cmd/kube-proxy/app/server_others.go @@ -102,7 +102,6 @@ func newProxyServer( IptInterface: iptInterface, IpvsInterface: ipvsInterface, IpsetInterface: ipsetInterface, - CleanupAndExit: cleanupAndExit, }, nil } diff --git a/cmd/kube-proxy/app/server_test.go b/cmd/kube-proxy/app/server_test.go index 345e783cf20..95644e2c582 100644 --- a/cmd/kube-proxy/app/server_test.go +++ b/cmd/kube-proxy/app/server_test.go @@ -17,6 +17,7 @@ limitations under the License. package app import ( + "errors" "fmt" "io/ioutil" "os" @@ -108,7 +109,6 @@ func TestProxyServerWithCleanupAndExit(t *testing.T) { assert.Nil(t, err, "unexpected error in NewProxyServer, addr: %s", addr) assert.NotNil(t, proxyserver, "nil proxy server obj, addr: %s", addr) assert.NotNil(t, proxyserver.IptInterface, "nil iptables intf, addr: %s", addr) - assert.True(t, proxyserver.CleanupAndExit, "false CleanupAndExit, addr: %s", addr) // Clean up config for next test case configz.Delete(kubeproxyconfig.GroupName) @@ -540,6 +540,11 @@ func (s *fakeProxyServerLongRun) Run() error { } } +// CleanupAndExit runs in the specified ProxyServer. +func (s *fakeProxyServerLongRun) CleanupAndExit() error { + return nil +} + type fakeProxyServerError struct{} // Run runs the specified ProxyServer. @@ -549,3 +554,8 @@ func (s *fakeProxyServerError) Run() error { return fmt.Errorf("mocking error from ProxyServer.Run()") } } + +// CleanupAndExit runs in the specified ProxyServer. +func (s *fakeProxyServerError) CleanupAndExit() error { + return errors.New("mocking error from ProxyServer.CleanupAndExit()") +} diff --git a/cmd/kube-proxy/app/server_windows.go b/cmd/kube-proxy/app/server_windows.go index 56a4a3b61f8..04ecf8fb0e1 100644 --- a/cmd/kube-proxy/app/server_windows.go +++ b/cmd/kube-proxy/app/server_windows.go @@ -24,6 +24,7 @@ import ( "errors" "fmt" "net" + // Enable pprof HTTP handlers. _ "net/http/pprof" @@ -63,7 +64,7 @@ func newProxyServer(config *proxyconfigapi.KubeProxyConfiguration, cleanupAndExi // We omit creation of pretty much everything if we run in cleanup mode if cleanupAndExit { - return &ProxyServer{CleanupAndExit: cleanupAndExit}, nil + return &ProxyServer{}, nil } client, eventClient, err := createClients(config.ClientConnection, master)