server.go 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // Package app does all of the work necessary to configure and run a
  14. // Kubernetes app process.
  15. package app
  16. import (
  17. "errors"
  18. "fmt"
  19. "io/ioutil"
  20. "net/http"
  21. "os"
  22. goruntime "runtime"
  23. "strings"
  24. "time"
  25. "github.com/fsnotify/fsnotify"
  26. "github.com/spf13/cobra"
  27. "github.com/spf13/pflag"
  28. gerrors "github.com/pkg/errors"
  29. v1 "k8s.io/api/core/v1"
  30. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  31. "k8s.io/apimachinery/pkg/fields"
  32. "k8s.io/apimachinery/pkg/labels"
  33. "k8s.io/apimachinery/pkg/runtime"
  34. "k8s.io/apimachinery/pkg/runtime/serializer"
  35. "k8s.io/apimachinery/pkg/selection"
  36. utilruntime "k8s.io/apimachinery/pkg/util/runtime"
  37. "k8s.io/apimachinery/pkg/util/wait"
  38. "k8s.io/apiserver/pkg/server/healthz"
  39. "k8s.io/apiserver/pkg/server/mux"
  40. "k8s.io/apiserver/pkg/server/routes"
  41. utilfeature "k8s.io/apiserver/pkg/util/feature"
  42. "k8s.io/client-go/informers"
  43. clientset "k8s.io/client-go/kubernetes"
  44. v1core "k8s.io/client-go/kubernetes/typed/core/v1"
  45. "k8s.io/client-go/rest"
  46. "k8s.io/client-go/tools/clientcmd"
  47. clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
  48. "k8s.io/client-go/tools/record"
  49. cliflag "k8s.io/component-base/cli/flag"
  50. componentbaseconfig "k8s.io/component-base/config"
  51. "k8s.io/component-base/metrics/legacyregistry"
  52. "k8s.io/component-base/version"
  53. "k8s.io/component-base/version/verflag"
  54. "k8s.io/klog"
  55. "k8s.io/kube-proxy/config/v1alpha1"
  56. api "k8s.io/kubernetes/pkg/apis/core"
  57. "k8s.io/kubernetes/pkg/features"
  58. "k8s.io/kubernetes/pkg/kubelet/qos"
  59. "k8s.io/kubernetes/pkg/master/ports"
  60. "k8s.io/kubernetes/pkg/proxy"
  61. "k8s.io/kubernetes/pkg/proxy/apis"
  62. kubeproxyconfig "k8s.io/kubernetes/pkg/proxy/apis/config"
  63. proxyconfigscheme "k8s.io/kubernetes/pkg/proxy/apis/config/scheme"
  64. kubeproxyconfigv1alpha1 "k8s.io/kubernetes/pkg/proxy/apis/config/v1alpha1"
  65. "k8s.io/kubernetes/pkg/proxy/apis/config/validation"
  66. "k8s.io/kubernetes/pkg/proxy/config"
  67. "k8s.io/kubernetes/pkg/proxy/healthcheck"
  68. "k8s.io/kubernetes/pkg/proxy/iptables"
  69. "k8s.io/kubernetes/pkg/proxy/ipvs"
  70. "k8s.io/kubernetes/pkg/proxy/userspace"
  71. proxyutil "k8s.io/kubernetes/pkg/proxy/util"
  72. "k8s.io/kubernetes/pkg/util/configz"
  73. "k8s.io/kubernetes/pkg/util/filesystem"
  74. utilflag "k8s.io/kubernetes/pkg/util/flag"
  75. utilipset "k8s.io/kubernetes/pkg/util/ipset"
  76. utiliptables "k8s.io/kubernetes/pkg/util/iptables"
  77. utilipvs "k8s.io/kubernetes/pkg/util/ipvs"
  78. "k8s.io/kubernetes/pkg/util/oom"
  79. "k8s.io/utils/exec"
  80. utilpointer "k8s.io/utils/pointer"
  81. )
  82. const (
  83. proxyModeUserspace = "userspace"
  84. proxyModeIPTables = "iptables"
  85. proxyModeIPVS = "ipvs"
  86. proxyModeKernelspace = "kernelspace"
  87. )
  88. // proxyRun defines the interface to run a specified ProxyServer
  89. type proxyRun interface {
  90. Run() error
  91. CleanupAndExit() error
  92. }
  93. // Options contains everything necessary to create and run a proxy server.
  94. type Options struct {
  95. // ConfigFile is the location of the proxy server's configuration file.
  96. ConfigFile string
  97. // WriteConfigTo is the path where the default configuration will be written.
  98. WriteConfigTo string
  99. // CleanupAndExit, when true, makes the proxy server clean up iptables and ipvs rules, then exit.
  100. CleanupAndExit bool
  101. // CleanupIPVS, when true, makes the proxy server clean up ipvs rules before running.
  102. CleanupIPVS bool
  103. // WindowsService should be set to true if kube-proxy is running as a service on Windows.
  104. // Its corresponding flag only gets registered in Windows builds
  105. WindowsService bool
  106. // config is the proxy server's configuration object.
  107. config *kubeproxyconfig.KubeProxyConfiguration
  108. // watcher is used to watch on the update change of ConfigFile
  109. watcher filesystem.FSWatcher
  110. // proxyServer is the interface to run the proxy server
  111. proxyServer proxyRun
  112. // errCh is the channel that errors will be sent
  113. errCh chan error
  114. // The fields below here are placeholders for flags that can't be directly mapped into
  115. // config.KubeProxyConfiguration.
  116. //
  117. // TODO remove these fields once the deprecated flags are removed.
  118. // master is used to override the kubeconfig's URL to the apiserver.
  119. master string
  120. // healthzPort is the port to be used by the healthz server.
  121. healthzPort int32
  122. // metricsPort is the port to be used by the metrics server.
  123. metricsPort int32
  124. // hostnameOverride, if set from the command line flag, takes precedence over the `HostnameOverride` value from the config file
  125. hostnameOverride string
  126. }
  127. // AddFlags adds flags to fs and binds them to options.
  128. func (o *Options) AddFlags(fs *pflag.FlagSet) {
  129. o.addOSFlags(fs)
  130. fs.StringVar(&o.ConfigFile, "config", o.ConfigFile, "The path to the configuration file.")
  131. fs.StringVar(&o.WriteConfigTo, "write-config-to", o.WriteConfigTo, "If set, write the default configuration values to this file and exit.")
  132. fs.StringVar(&o.config.ClientConnection.Kubeconfig, "kubeconfig", o.config.ClientConnection.Kubeconfig, "Path to kubeconfig file with authorization information (the master location is set by the master flag).")
  133. fs.StringVar(&o.config.ClusterCIDR, "cluster-cidr", o.config.ClusterCIDR, "The CIDR range of pods in the cluster. When configured, traffic sent to a Service cluster IP from outside this range will be masqueraded and traffic sent from pods to an external LoadBalancer IP will be directed to the respective cluster IP instead")
  134. fs.StringVar(&o.config.ClientConnection.ContentType, "kube-api-content-type", o.config.ClientConnection.ContentType, "Content type of requests sent to apiserver.")
  135. fs.StringVar(&o.master, "master", o.master, "The address of the Kubernetes API server (overrides any value in kubeconfig)")
  136. fs.StringVar(&o.hostnameOverride, "hostname-override", o.hostnameOverride, "If non-empty, will use this string as identification instead of the actual hostname.")
  137. fs.StringVar(&o.config.IPVS.Scheduler, "ipvs-scheduler", o.config.IPVS.Scheduler, "The ipvs scheduler type when proxy mode is ipvs")
  138. fs.StringVar(&o.config.ShowHiddenMetricsForVersion, "show-hidden-metrics-for-version", o.config.ShowHiddenMetricsForVersion,
  139. "The previous version for which you want to show hidden metrics. "+
  140. "Only the previous minor version is meaningful, other values will not be allowed. "+
  141. "The format is <major>.<minor>, e.g.: '1.16'. "+
  142. "The purpose of this format is make sure you have the opportunity to notice if the next release hides additional metrics, "+
  143. "rather than being surprised when they are permanently removed in the release after that.")
  144. fs.StringSliceVar(&o.config.IPVS.ExcludeCIDRs, "ipvs-exclude-cidrs", o.config.IPVS.ExcludeCIDRs, "A comma-separated list of CIDR's which the ipvs proxier should not touch when cleaning up IPVS rules.")
  145. fs.StringSliceVar(&o.config.NodePortAddresses, "nodeport-addresses", o.config.NodePortAddresses,
  146. "A string slice of values which specify the addresses to use for NodePorts. Values may be valid IP blocks (e.g. 1.2.3.0/24, 1.2.3.4/32). The default empty string slice ([]) means to use all local addresses.")
  147. fs.BoolVar(&o.CleanupAndExit, "cleanup", o.CleanupAndExit, "If true cleanup iptables and ipvs rules and exit.")
  148. fs.BoolVar(&o.CleanupIPVS, "cleanup-ipvs", o.CleanupIPVS, "If true and --cleanup is specified, kube-proxy will also flush IPVS rules, in addition to normal cleanup.")
  149. fs.MarkDeprecated("cleanup-ipvs", "In a future release, running --cleanup will always flush IPVS rules")
  150. fs.Var(utilflag.IPVar{Val: &o.config.BindAddress}, "bind-address", "The IP address for the proxy server to serve on (set to `0.0.0.0` for all IPv4 interfaces and `::` for all IPv6 interfaces)")
  151. fs.Var(utilflag.IPVar{Val: &o.config.HealthzBindAddress}, "healthz-bind-address", "The IP address for the health check server to serve on (set to `0.0.0.0` for all IPv4 interfaces and `::` for all IPv6 interfaces)")
  152. fs.Var(utilflag.IPVar{Val: &o.config.MetricsBindAddress}, "metrics-bind-address", "The IP address for the metrics server to serve on (set to `0.0.0.0` for all IPv4 interfaces and `::` for all IPv6 interfaces)")
  153. fs.Var(utilflag.PortRangeVar{Val: &o.config.PortRange}, "proxy-port-range", "Range of host ports (beginPort-endPort, single port or beginPort+offset, inclusive) that may be consumed in order to proxy service traffic. If (unspecified, 0, or 0-0) then ports will be randomly chosen.")
  154. fs.Var(&o.config.Mode, "proxy-mode", "Which proxy mode to use: 'userspace' (older) or 'iptables' (faster) or 'ipvs'. If blank, use the best-available proxy (currently iptables). If the iptables proxy is selected, regardless of how, but the system's kernel or iptables versions are insufficient, this always falls back to the userspace proxy.")
  155. fs.Var(cliflag.NewMapStringBool(&o.config.FeatureGates), "feature-gates", "A set of key=value pairs that describe feature gates for alpha/experimental features. "+
  156. "Options are:\n"+strings.Join(utilfeature.DefaultFeatureGate.KnownFeatures(), "\n"))
  157. fs.Int32Var(&o.healthzPort, "healthz-port", o.healthzPort, "The port to bind the health check server. Use 0 to disable.")
  158. fs.Int32Var(&o.metricsPort, "metrics-port", o.metricsPort, "The port to bind the metrics server. Use 0 to disable.")
  159. fs.Int32Var(o.config.OOMScoreAdj, "oom-score-adj", utilpointer.Int32PtrDerefOr(o.config.OOMScoreAdj, int32(qos.KubeProxyOOMScoreAdj)), "The oom-score-adj value for kube-proxy process. Values must be within the range [-1000, 1000]")
  160. fs.Int32Var(o.config.IPTables.MasqueradeBit, "iptables-masquerade-bit", utilpointer.Int32PtrDerefOr(o.config.IPTables.MasqueradeBit, 14), "If using the pure iptables proxy, the bit of the fwmark space to mark packets requiring SNAT with. Must be within the range [0, 31].")
  161. fs.Int32Var(o.config.Conntrack.MaxPerCore, "conntrack-max-per-core", *o.config.Conntrack.MaxPerCore,
  162. "Maximum number of NAT connections to track per CPU core (0 to leave the limit as-is and ignore conntrack-min).")
  163. fs.Int32Var(o.config.Conntrack.Min, "conntrack-min", *o.config.Conntrack.Min,
  164. "Minimum number of conntrack entries to allocate, regardless of conntrack-max-per-core (set conntrack-max-per-core=0 to leave the limit as-is).")
  165. fs.Int32Var(&o.config.ClientConnection.Burst, "kube-api-burst", o.config.ClientConnection.Burst, "Burst to use while talking with kubernetes apiserver")
  166. fs.DurationVar(&o.config.IPTables.SyncPeriod.Duration, "iptables-sync-period", o.config.IPTables.SyncPeriod.Duration, "The maximum interval of how often iptables rules are refreshed (e.g. '5s', '1m', '2h22m'). Must be greater than 0.")
  167. fs.DurationVar(&o.config.IPTables.MinSyncPeriod.Duration, "iptables-min-sync-period", o.config.IPTables.MinSyncPeriod.Duration, "The minimum interval of how often the iptables rules can be refreshed as endpoints and services change (e.g. '5s', '1m', '2h22m').")
  168. fs.DurationVar(&o.config.IPVS.SyncPeriod.Duration, "ipvs-sync-period", o.config.IPVS.SyncPeriod.Duration, "The maximum interval of how often ipvs rules are refreshed (e.g. '5s', '1m', '2h22m'). Must be greater than 0.")
  169. fs.DurationVar(&o.config.IPVS.MinSyncPeriod.Duration, "ipvs-min-sync-period", o.config.IPVS.MinSyncPeriod.Duration, "The minimum interval of how often the ipvs rules can be refreshed as endpoints and services change (e.g. '5s', '1m', '2h22m').")
  170. fs.DurationVar(&o.config.IPVS.TCPTimeout.Duration, "ipvs-tcp-timeout", o.config.IPVS.TCPTimeout.Duration, "The timeout for idle IPVS TCP connections, 0 to leave as-is. (e.g. '5s', '1m', '2h22m').")
  171. fs.DurationVar(&o.config.IPVS.TCPFinTimeout.Duration, "ipvs-tcpfin-timeout", o.config.IPVS.TCPFinTimeout.Duration, "The timeout for IPVS TCP connections after receiving a FIN packet, 0 to leave as-is. (e.g. '5s', '1m', '2h22m').")
  172. fs.DurationVar(&o.config.IPVS.UDPTimeout.Duration, "ipvs-udp-timeout", o.config.IPVS.UDPTimeout.Duration, "The timeout for IPVS UDP packets, 0 to leave as-is. (e.g. '5s', '1m', '2h22m').")
  173. fs.DurationVar(&o.config.Conntrack.TCPEstablishedTimeout.Duration, "conntrack-tcp-timeout-established", o.config.Conntrack.TCPEstablishedTimeout.Duration, "Idle timeout for established TCP connections (0 to leave as-is)")
  174. fs.DurationVar(
  175. &o.config.Conntrack.TCPCloseWaitTimeout.Duration, "conntrack-tcp-timeout-close-wait",
  176. o.config.Conntrack.TCPCloseWaitTimeout.Duration,
  177. "NAT timeout for TCP connections in the CLOSE_WAIT state")
  178. fs.DurationVar(&o.config.ConfigSyncPeriod.Duration, "config-sync-period", o.config.ConfigSyncPeriod.Duration, "How often configuration from the apiserver is refreshed. Must be greater than 0.")
  179. fs.DurationVar(&o.config.UDPIdleTimeout.Duration, "udp-timeout", o.config.UDPIdleTimeout.Duration, "How long an idle UDP connection will be kept open (e.g. '250ms', '2s'). Must be greater than 0. Only applicable for proxy-mode=userspace")
  180. fs.BoolVar(&o.config.IPVS.StrictARP, "ipvs-strict-arp", o.config.IPVS.StrictARP, "Enable strict ARP by setting arp_ignore to 1 and arp_announce to 2")
  181. fs.BoolVar(&o.config.IPTables.MasqueradeAll, "masquerade-all", o.config.IPTables.MasqueradeAll, "If using the pure iptables proxy, SNAT all traffic sent via Service cluster IPs (this not commonly needed)")
  182. fs.BoolVar(&o.config.EnableProfiling, "profiling", o.config.EnableProfiling, "If true enables profiling via web interface on /debug/pprof handler.")
  183. fs.Float32Var(&o.config.ClientConnection.QPS, "kube-api-qps", o.config.ClientConnection.QPS, "QPS to use while talking with kubernetes apiserver")
  184. }
  185. // NewOptions returns initialized Options
  186. func NewOptions() *Options {
  187. return &Options{
  188. config: new(kubeproxyconfig.KubeProxyConfiguration),
  189. healthzPort: ports.ProxyHealthzPort,
  190. metricsPort: ports.ProxyStatusPort,
  191. CleanupIPVS: true,
  192. errCh: make(chan error),
  193. }
  194. }
  195. // Complete completes all the required options.
  196. func (o *Options) Complete() error {
  197. if len(o.ConfigFile) == 0 && len(o.WriteConfigTo) == 0 {
  198. klog.Warning("WARNING: all flags other than --config, --write-config-to, and --cleanup are deprecated. Please begin using a config file ASAP.")
  199. o.config.HealthzBindAddress = addressFromDeprecatedFlags(o.config.HealthzBindAddress, o.healthzPort)
  200. o.config.MetricsBindAddress = addressFromDeprecatedFlags(o.config.MetricsBindAddress, o.metricsPort)
  201. }
  202. // Load the config file here in Complete, so that Validate validates the fully-resolved config.
  203. if len(o.ConfigFile) > 0 {
  204. c, err := o.loadConfigFromFile(o.ConfigFile)
  205. if err != nil {
  206. return err
  207. }
  208. o.config = c
  209. if err := o.initWatcher(); err != nil {
  210. return err
  211. }
  212. }
  213. if err := o.processHostnameOverrideFlag(); err != nil {
  214. return err
  215. }
  216. return utilfeature.DefaultMutableFeatureGate.SetFromMap(o.config.FeatureGates)
  217. }
  218. // Creates a new filesystem watcher and adds watches for the config file.
  219. func (o *Options) initWatcher() error {
  220. fswatcher := filesystem.NewFsnotifyWatcher()
  221. err := fswatcher.Init(o.eventHandler, o.errorHandler)
  222. if err != nil {
  223. return err
  224. }
  225. err = fswatcher.AddWatch(o.ConfigFile)
  226. if err != nil {
  227. return err
  228. }
  229. o.watcher = fswatcher
  230. return nil
  231. }
  232. func (o *Options) eventHandler(ent fsnotify.Event) {
  233. eventOpIs := func(Op fsnotify.Op) bool {
  234. return ent.Op&Op == Op
  235. }
  236. if eventOpIs(fsnotify.Write) || eventOpIs(fsnotify.Rename) {
  237. // error out when ConfigFile is updated
  238. o.errCh <- fmt.Errorf("content of the proxy server's configuration file was updated")
  239. return
  240. }
  241. o.errCh <- nil
  242. }
  243. func (o *Options) errorHandler(err error) {
  244. o.errCh <- err
  245. }
  246. // processHostnameOverrideFlag processes hostname-override flag
  247. func (o *Options) processHostnameOverrideFlag() error {
  248. // Check if hostname-override flag is set and use value since configFile always overrides
  249. if len(o.hostnameOverride) > 0 {
  250. hostName := strings.TrimSpace(o.hostnameOverride)
  251. if len(hostName) == 0 {
  252. return fmt.Errorf("empty hostname-override is invalid")
  253. }
  254. o.config.HostnameOverride = strings.ToLower(hostName)
  255. }
  256. return nil
  257. }
  258. // Validate validates all the required options.
  259. func (o *Options) Validate(args []string) error {
  260. if len(args) != 0 {
  261. return errors.New("no arguments are supported")
  262. }
  263. if errs := validation.Validate(o.config); len(errs) != 0 {
  264. return errs.ToAggregate()
  265. }
  266. return nil
  267. }
  268. // Run runs the specified ProxyServer.
  269. func (o *Options) Run() error {
  270. defer close(o.errCh)
  271. if len(o.WriteConfigTo) > 0 {
  272. return o.writeConfigFile()
  273. }
  274. proxyServer, err := NewProxyServer(o)
  275. if err != nil {
  276. return err
  277. }
  278. if o.CleanupAndExit {
  279. return proxyServer.CleanupAndExit()
  280. }
  281. o.proxyServer = proxyServer
  282. return o.runLoop()
  283. }
  284. // runLoop will watch on the update change of the proxy server's configuration file.
  285. // Return an error when updated
  286. func (o *Options) runLoop() error {
  287. if o.watcher != nil {
  288. o.watcher.Run()
  289. }
  290. // run the proxy in goroutine
  291. go func() {
  292. err := o.proxyServer.Run()
  293. o.errCh <- err
  294. }()
  295. for {
  296. err := <-o.errCh
  297. if err != nil {
  298. return err
  299. }
  300. }
  301. }
  302. func (o *Options) writeConfigFile() (err error) {
  303. const mediaType = runtime.ContentTypeYAML
  304. info, ok := runtime.SerializerInfoForMediaType(proxyconfigscheme.Codecs.SupportedMediaTypes(), mediaType)
  305. if !ok {
  306. return fmt.Errorf("unable to locate encoder -- %q is not a supported media type", mediaType)
  307. }
  308. encoder := proxyconfigscheme.Codecs.EncoderForVersion(info.Serializer, v1alpha1.SchemeGroupVersion)
  309. configFile, err := os.Create(o.WriteConfigTo)
  310. if err != nil {
  311. return err
  312. }
  313. defer func() {
  314. ferr := configFile.Close()
  315. if ferr != nil && err == nil {
  316. err = ferr
  317. }
  318. }()
  319. if err = encoder.Encode(o.config, configFile); err != nil {
  320. return err
  321. }
  322. klog.Infof("Wrote configuration to: %s\n", o.WriteConfigTo)
  323. return nil
  324. }
  325. // addressFromDeprecatedFlags returns server address from flags
  326. // passed on the command line based on the following rules:
  327. // 1. If port is 0, disable the server (e.g. set address to empty).
  328. // 2. Otherwise, set the port portion of the config accordingly.
  329. func addressFromDeprecatedFlags(addr string, port int32) string {
  330. if port == 0 {
  331. return ""
  332. }
  333. return proxyutil.AppendPortIfNeeded(addr, port)
  334. }
  335. // newLenientSchemeAndCodecs returns a scheme that has only v1alpha1 registered into
  336. // it and a CodecFactory with strict decoding disabled.
  337. func newLenientSchemeAndCodecs() (*runtime.Scheme, *serializer.CodecFactory, error) {
  338. lenientScheme := runtime.NewScheme()
  339. if err := kubeproxyconfig.AddToScheme(lenientScheme); err != nil {
  340. return nil, nil, fmt.Errorf("failed to add kube-proxy config API to lenient scheme: %v", err)
  341. }
  342. if err := kubeproxyconfigv1alpha1.AddToScheme(lenientScheme); err != nil {
  343. return nil, nil, fmt.Errorf("failed to add kube-proxy config v1alpha1 API to lenient scheme: %v", err)
  344. }
  345. lenientCodecs := serializer.NewCodecFactory(lenientScheme, serializer.DisableStrict)
  346. return lenientScheme, &lenientCodecs, nil
  347. }
  348. // loadConfigFromFile loads the contents of file and decodes it as a
  349. // KubeProxyConfiguration object.
  350. func (o *Options) loadConfigFromFile(file string) (*kubeproxyconfig.KubeProxyConfiguration, error) {
  351. data, err := ioutil.ReadFile(file)
  352. if err != nil {
  353. return nil, err
  354. }
  355. return o.loadConfig(data)
  356. }
  357. // loadConfig decodes a serialized KubeProxyConfiguration to the internal type.
  358. func (o *Options) loadConfig(data []byte) (*kubeproxyconfig.KubeProxyConfiguration, error) {
  359. configObj, gvk, err := proxyconfigscheme.Codecs.UniversalDecoder().Decode(data, nil, nil)
  360. if err != nil {
  361. // Try strict decoding first. If that fails decode with a lenient
  362. // decoder, which has only v1alpha1 registered, and log a warning.
  363. // The lenient path is to be dropped when support for v1alpha1 is dropped.
  364. if !runtime.IsStrictDecodingError(err) {
  365. return nil, gerrors.Wrap(err, "failed to decode")
  366. }
  367. _, lenientCodecs, lenientErr := newLenientSchemeAndCodecs()
  368. if lenientErr != nil {
  369. return nil, lenientErr
  370. }
  371. configObj, gvk, lenientErr = lenientCodecs.UniversalDecoder().Decode(data, nil, nil)
  372. if lenientErr != nil {
  373. // Lenient decoding failed with the current version, return the
  374. // original strict error.
  375. return nil, fmt.Errorf("failed lenient decoding: %v", err)
  376. }
  377. // Continue with the v1alpha1 object that was decoded leniently, but emit a warning.
  378. klog.Warningf("using lenient decoding as strict decoding failed: %v", err)
  379. }
  380. proxyConfig, ok := configObj.(*kubeproxyconfig.KubeProxyConfiguration)
  381. if !ok {
  382. return nil, fmt.Errorf("got unexpected config type: %v", gvk)
  383. }
  384. return proxyConfig, nil
  385. }
  386. // ApplyDefaults applies the default values to Options.
  387. func (o *Options) ApplyDefaults(in *kubeproxyconfig.KubeProxyConfiguration) (*kubeproxyconfig.KubeProxyConfiguration, error) {
  388. external, err := proxyconfigscheme.Scheme.ConvertToVersion(in, v1alpha1.SchemeGroupVersion)
  389. if err != nil {
  390. return nil, err
  391. }
  392. proxyconfigscheme.Scheme.Default(external)
  393. internal, err := proxyconfigscheme.Scheme.ConvertToVersion(external, kubeproxyconfig.SchemeGroupVersion)
  394. if err != nil {
  395. return nil, err
  396. }
  397. out := internal.(*kubeproxyconfig.KubeProxyConfiguration)
  398. return out, nil
  399. }
  400. // NewProxyCommand creates a *cobra.Command object with default parameters
  401. func NewProxyCommand() *cobra.Command {
  402. opts := NewOptions()
  403. cmd := &cobra.Command{
  404. Use: "kube-proxy",
  405. Long: `The Kubernetes network proxy runs on each node. This
  406. reflects services as defined in the Kubernetes API on each node and can do simple
  407. TCP, UDP, and SCTP stream forwarding or round robin TCP, UDP, and SCTP forwarding across a set of backends.
  408. Service cluster IPs and ports are currently found through Docker-links-compatible
  409. environment variables specifying ports opened by the service proxy. There is an optional
  410. addon that provides cluster DNS for these cluster IPs. The user must create a service
  411. with the apiserver API to configure the proxy.`,
  412. Run: func(cmd *cobra.Command, args []string) {
  413. verflag.PrintAndExitIfRequested()
  414. utilflag.PrintFlags(cmd.Flags())
  415. if err := initForOS(opts.WindowsService); err != nil {
  416. klog.Fatalf("failed OS init: %v", err)
  417. }
  418. if err := opts.Complete(); err != nil {
  419. klog.Fatalf("failed complete: %v", err)
  420. }
  421. if err := opts.Validate(args); err != nil {
  422. klog.Fatalf("failed validate: %v", err)
  423. }
  424. if err := opts.Run(); err != nil {
  425. klog.Exit(err)
  426. }
  427. },
  428. }
  429. var err error
  430. opts.config, err = opts.ApplyDefaults(opts.config)
  431. if err != nil {
  432. klog.Fatalf("unable to create flag defaults: %v", err)
  433. }
  434. opts.AddFlags(cmd.Flags())
  435. // TODO handle error
  436. cmd.MarkFlagFilename("config", "yaml", "yml", "json")
  437. return cmd
  438. }
  439. // ProxyServer represents all the parameters required to start the Kubernetes proxy server. All
  440. // fields are required.
  441. type ProxyServer struct {
  442. Client clientset.Interface
  443. EventClient v1core.EventsGetter
  444. IptInterface utiliptables.Interface
  445. IpvsInterface utilipvs.Interface
  446. IpsetInterface utilipset.Interface
  447. execer exec.Interface
  448. Proxier proxy.Provider
  449. Broadcaster record.EventBroadcaster
  450. Recorder record.EventRecorder
  451. ConntrackConfiguration kubeproxyconfig.KubeProxyConntrackConfiguration
  452. Conntracker Conntracker // if nil, ignored
  453. ProxyMode string
  454. NodeRef *v1.ObjectReference
  455. CleanupIPVS bool
  456. MetricsBindAddress string
  457. EnableProfiling bool
  458. UseEndpointSlices bool
  459. OOMScoreAdj *int32
  460. ConfigSyncPeriod time.Duration
  461. HealthzServer *healthcheck.ProxierHealthServer
  462. }
  463. // createClients creates a kube client and an event client from the given config and masterOverride.
  464. // TODO remove masterOverride when CLI flags are removed.
  465. func createClients(config componentbaseconfig.ClientConnectionConfiguration, masterOverride string) (clientset.Interface, v1core.EventsGetter, error) {
  466. var kubeConfig *rest.Config
  467. var err error
  468. if len(config.Kubeconfig) == 0 && len(masterOverride) == 0 {
  469. klog.Info("Neither kubeconfig file nor master URL was specified. Falling back to in-cluster config.")
  470. kubeConfig, err = rest.InClusterConfig()
  471. } else {
  472. // This creates a client, first loading any specified kubeconfig
  473. // file, and then overriding the Master flag, if non-empty.
  474. kubeConfig, err = clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
  475. &clientcmd.ClientConfigLoadingRules{ExplicitPath: config.Kubeconfig},
  476. &clientcmd.ConfigOverrides{ClusterInfo: clientcmdapi.Cluster{Server: masterOverride}}).ClientConfig()
  477. }
  478. if err != nil {
  479. return nil, nil, err
  480. }
  481. kubeConfig.AcceptContentTypes = config.AcceptContentTypes
  482. kubeConfig.ContentType = config.ContentType
  483. kubeConfig.QPS = config.QPS
  484. kubeConfig.Burst = int(config.Burst)
  485. client, err := clientset.NewForConfig(kubeConfig)
  486. if err != nil {
  487. return nil, nil, err
  488. }
  489. eventClient, err := clientset.NewForConfig(kubeConfig)
  490. if err != nil {
  491. return nil, nil, err
  492. }
  493. return client, eventClient.CoreV1(), nil
  494. }
  495. // Run runs the specified ProxyServer. This should never exit (unless CleanupAndExit is set).
  496. // 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.
  497. func (s *ProxyServer) Run() error {
  498. // To help debugging, immediately log version
  499. klog.Infof("Version: %+v", version.Get())
  500. // TODO(vmarmol): Use container config for this.
  501. var oomAdjuster *oom.OOMAdjuster
  502. if s.OOMScoreAdj != nil {
  503. oomAdjuster = oom.NewOOMAdjuster()
  504. if err := oomAdjuster.ApplyOOMScoreAdj(0, int(*s.OOMScoreAdj)); err != nil {
  505. klog.V(2).Info(err)
  506. }
  507. }
  508. if s.Broadcaster != nil && s.EventClient != nil {
  509. s.Broadcaster.StartRecordingToSink(&v1core.EventSinkImpl{Interface: s.EventClient.Events("")})
  510. }
  511. // Start up a healthz server if requested
  512. if s.HealthzServer != nil {
  513. s.HealthzServer.Run()
  514. }
  515. // Start up a metrics server if requested
  516. if len(s.MetricsBindAddress) > 0 {
  517. proxyMux := mux.NewPathRecorderMux("kube-proxy")
  518. healthz.InstallHandler(proxyMux)
  519. proxyMux.HandleFunc("/proxyMode", func(w http.ResponseWriter, r *http.Request) {
  520. w.Header().Set("Content-Type", "text/plain; charset=utf-8")
  521. w.Header().Set("X-Content-Type-Options", "nosniff")
  522. fmt.Fprintf(w, "%s", s.ProxyMode)
  523. })
  524. //lint:ignore SA1019 See the Metrics Stability Migration KEP
  525. proxyMux.Handle("/metrics", legacyregistry.Handler())
  526. if s.EnableProfiling {
  527. routes.Profiling{}.Install(proxyMux)
  528. }
  529. configz.InstallHandler(proxyMux)
  530. go wait.Until(func() {
  531. err := http.ListenAndServe(s.MetricsBindAddress, proxyMux)
  532. if err != nil {
  533. utilruntime.HandleError(fmt.Errorf("starting metrics server failed: %v", err))
  534. }
  535. }, 5*time.Second, wait.NeverStop)
  536. }
  537. // Tune conntrack, if requested
  538. // Conntracker is always nil for windows
  539. if s.Conntracker != nil {
  540. max, err := getConntrackMax(s.ConntrackConfiguration)
  541. if err != nil {
  542. return err
  543. }
  544. if max > 0 {
  545. err := s.Conntracker.SetMax(max)
  546. if err != nil {
  547. if err != errReadOnlySysFS {
  548. return err
  549. }
  550. // errReadOnlySysFS is caused by a known docker issue (https://github.com/docker/docker/issues/24000),
  551. // the only remediation we know is to restart the docker daemon.
  552. // Here we'll send an node event with specific reason and message, the
  553. // administrator should decide whether and how to handle this issue,
  554. // whether to drain the node and restart docker. Occurs in other container runtimes
  555. // as well.
  556. // TODO(random-liu): Remove this when the docker bug is fixed.
  557. const message = "CRI error: /sys is read-only: " +
  558. "cannot modify conntrack limits, problems may arise later (If running Docker, see docker issue #24000)"
  559. s.Recorder.Eventf(s.NodeRef, api.EventTypeWarning, err.Error(), message)
  560. }
  561. }
  562. if s.ConntrackConfiguration.TCPEstablishedTimeout != nil && s.ConntrackConfiguration.TCPEstablishedTimeout.Duration > 0 {
  563. timeout := int(s.ConntrackConfiguration.TCPEstablishedTimeout.Duration / time.Second)
  564. if err := s.Conntracker.SetTCPEstablishedTimeout(timeout); err != nil {
  565. return err
  566. }
  567. }
  568. if s.ConntrackConfiguration.TCPCloseWaitTimeout != nil && s.ConntrackConfiguration.TCPCloseWaitTimeout.Duration > 0 {
  569. timeout := int(s.ConntrackConfiguration.TCPCloseWaitTimeout.Duration / time.Second)
  570. if err := s.Conntracker.SetTCPCloseWaitTimeout(timeout); err != nil {
  571. return err
  572. }
  573. }
  574. }
  575. noProxyName, err := labels.NewRequirement(apis.LabelServiceProxyName, selection.DoesNotExist, nil)
  576. if err != nil {
  577. return err
  578. }
  579. noHeadlessEndpoints, err := labels.NewRequirement(v1.IsHeadlessService, selection.DoesNotExist, nil)
  580. if err != nil {
  581. return err
  582. }
  583. labelSelector := labels.NewSelector()
  584. labelSelector = labelSelector.Add(*noProxyName, *noHeadlessEndpoints)
  585. // Make informers that filter out objects that want a non-default service proxy.
  586. informerFactory := informers.NewSharedInformerFactoryWithOptions(s.Client, s.ConfigSyncPeriod,
  587. informers.WithTweakListOptions(func(options *metav1.ListOptions) {
  588. options.LabelSelector = labelSelector.String()
  589. }))
  590. // Create configs (i.e. Watches for Services and Endpoints or EndpointSlices)
  591. // Note: RegisterHandler() calls need to happen before creation of Sources because sources
  592. // only notify on changes, and the initial update (on process start) may be lost if no handlers
  593. // are registered yet.
  594. serviceConfig := config.NewServiceConfig(informerFactory.Core().V1().Services(), s.ConfigSyncPeriod)
  595. serviceConfig.RegisterEventHandler(s.Proxier)
  596. go serviceConfig.Run(wait.NeverStop)
  597. if s.UseEndpointSlices {
  598. endpointSliceConfig := config.NewEndpointSliceConfig(informerFactory.Discovery().V1beta1().EndpointSlices(), s.ConfigSyncPeriod)
  599. endpointSliceConfig.RegisterEventHandler(s.Proxier)
  600. go endpointSliceConfig.Run(wait.NeverStop)
  601. } else {
  602. endpointsConfig := config.NewEndpointsConfig(informerFactory.Core().V1().Endpoints(), s.ConfigSyncPeriod)
  603. endpointsConfig.RegisterEventHandler(s.Proxier)
  604. go endpointsConfig.Run(wait.NeverStop)
  605. }
  606. // This has to start after the calls to NewServiceConfig and NewEndpointsConfig because those
  607. // functions must configure their shared informer event handlers first.
  608. informerFactory.Start(wait.NeverStop)
  609. if utilfeature.DefaultFeatureGate.Enabled(features.ServiceTopology) {
  610. // Make an informer that selects for our nodename.
  611. currentNodeInformerFactory := informers.NewSharedInformerFactoryWithOptions(s.Client, s.ConfigSyncPeriod,
  612. informers.WithTweakListOptions(func(options *metav1.ListOptions) {
  613. options.FieldSelector = fields.OneTermEqualSelector("metadata.name", s.NodeRef.Name).String()
  614. }))
  615. nodeConfig := config.NewNodeConfig(currentNodeInformerFactory.Core().V1().Nodes(), s.ConfigSyncPeriod)
  616. nodeConfig.RegisterEventHandler(s.Proxier)
  617. go nodeConfig.Run(wait.NeverStop)
  618. // This has to start after the calls to NewNodeConfig because that must
  619. // configure the shared informer event handler first.
  620. currentNodeInformerFactory.Start(wait.NeverStop)
  621. }
  622. // Birth Cry after the birth is successful
  623. s.birthCry()
  624. // Just loop forever for now...
  625. s.Proxier.SyncLoop()
  626. return nil
  627. }
  628. func (s *ProxyServer) birthCry() {
  629. s.Recorder.Eventf(s.NodeRef, api.EventTypeNormal, "Starting", "Starting kube-proxy.")
  630. }
  631. func getConntrackMax(config kubeproxyconfig.KubeProxyConntrackConfiguration) (int, error) {
  632. if config.MaxPerCore != nil && *config.MaxPerCore > 0 {
  633. floor := 0
  634. if config.Min != nil {
  635. floor = int(*config.Min)
  636. }
  637. scaled := int(*config.MaxPerCore) * goruntime.NumCPU()
  638. if scaled > floor {
  639. klog.V(3).Infof("getConntrackMax: using scaled conntrack-max-per-core")
  640. return scaled, nil
  641. }
  642. klog.V(3).Infof("getConntrackMax: using conntrack-min")
  643. return floor, nil
  644. }
  645. return 0, nil
  646. }
  647. // CleanupAndExit remove iptables rules and exit if success return nil
  648. func (s *ProxyServer) CleanupAndExit() error {
  649. encounteredError := userspace.CleanupLeftovers(s.IptInterface)
  650. encounteredError = iptables.CleanupLeftovers(s.IptInterface) || encounteredError
  651. encounteredError = ipvs.CleanupLeftovers(s.IpvsInterface, s.IptInterface, s.IpsetInterface, s.CleanupIPVS) || encounteredError
  652. if encounteredError {
  653. return errors.New("encountered an error while tearing down rules")
  654. }
  655. return nil
  656. }