proxier.go 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322
  1. /*
  2. Copyright 2015 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 iptables
  14. //
  15. // NOTE: this needs to be tested in e2e since it uses iptables for everything.
  16. //
  17. import (
  18. "bytes"
  19. "crypto/sha256"
  20. "encoding/base32"
  21. "fmt"
  22. "net"
  23. "reflect"
  24. "strconv"
  25. "strings"
  26. "sync"
  27. "time"
  28. "github.com/coreos/go-semver/semver"
  29. "github.com/davecgh/go-spew/spew"
  30. "github.com/golang/glog"
  31. "k8s.io/kubernetes/pkg/api"
  32. apiservice "k8s.io/kubernetes/pkg/api/service"
  33. "k8s.io/kubernetes/pkg/proxy"
  34. "k8s.io/kubernetes/pkg/proxy/healthcheck"
  35. "k8s.io/kubernetes/pkg/types"
  36. featuregate "k8s.io/kubernetes/pkg/util/config"
  37. utilexec "k8s.io/kubernetes/pkg/util/exec"
  38. utiliptables "k8s.io/kubernetes/pkg/util/iptables"
  39. "k8s.io/kubernetes/pkg/util/sets"
  40. "k8s.io/kubernetes/pkg/util/slice"
  41. utilsysctl "k8s.io/kubernetes/pkg/util/sysctl"
  42. )
  43. const (
  44. // iptablesMinVersion is the minimum version of iptables for which we will use the Proxier
  45. // from this package instead of the userspace Proxier. While most of the
  46. // features we need were available earlier, the '-C' flag was added more
  47. // recently. We use that indirectly in Ensure* functions, and if we don't
  48. // have it, we have to be extra careful about the exact args we feed in being
  49. // the same as the args we read back (iptables itself normalizes some args).
  50. // This is the "new" Proxier, so we require "new" versions of tools.
  51. iptablesMinVersion = utiliptables.MinCheckVersion
  52. // the services chain
  53. kubeServicesChain utiliptables.Chain = "KUBE-SERVICES"
  54. // the nodeports chain
  55. kubeNodePortsChain utiliptables.Chain = "KUBE-NODEPORTS"
  56. // the kubernetes postrouting chain
  57. kubePostroutingChain utiliptables.Chain = "KUBE-POSTROUTING"
  58. // the mark-for-masquerade chain
  59. KubeMarkMasqChain utiliptables.Chain = "KUBE-MARK-MASQ"
  60. // the mark-for-drop chain
  61. KubeMarkDropChain utiliptables.Chain = "KUBE-MARK-DROP"
  62. )
  63. // IptablesVersioner can query the current iptables version.
  64. type IptablesVersioner interface {
  65. // returns "X.Y.Z"
  66. GetVersion() (string, error)
  67. }
  68. // KernelCompatTester tests whether the required kernel capabilities are
  69. // present to run the iptables proxier.
  70. type KernelCompatTester interface {
  71. IsCompatible() error
  72. }
  73. // CanUseIptablesProxier returns true if we should use the iptables Proxier
  74. // instead of the "classic" userspace Proxier. This is determined by checking
  75. // the iptables version and for the existence of kernel features. It may return
  76. // an error if it fails to get the iptables version without error, in which
  77. // case it will also return false.
  78. func CanUseIptablesProxier(iptver IptablesVersioner, kcompat KernelCompatTester) (bool, error) {
  79. minVersion, err := semver.NewVersion(iptablesMinVersion)
  80. if err != nil {
  81. return false, err
  82. }
  83. // returns "X.Y.Z"
  84. versionString, err := iptver.GetVersion()
  85. if err != nil {
  86. return false, err
  87. }
  88. version, err := semver.NewVersion(versionString)
  89. if err != nil {
  90. return false, err
  91. }
  92. if version.LessThan(*minVersion) {
  93. return false, nil
  94. }
  95. // Check that the kernel supports what we need.
  96. if err := kcompat.IsCompatible(); err != nil {
  97. return false, err
  98. }
  99. return true, nil
  100. }
  101. type LinuxKernelCompatTester struct{}
  102. func (lkct LinuxKernelCompatTester) IsCompatible() error {
  103. // Check for the required sysctls. We don't care about the value, just
  104. // that it exists. If this Proxier is chosen, we'll initialize it as we
  105. // need.
  106. _, err := utilsysctl.New().GetSysctl(sysctlRouteLocalnet)
  107. return err
  108. }
  109. const sysctlRouteLocalnet = "net/ipv4/conf/all/route_localnet"
  110. const sysctlBridgeCallIptables = "net/bridge/bridge-nf-call-iptables"
  111. // internal struct for string service information
  112. type serviceInfo struct {
  113. clusterIP net.IP
  114. port int
  115. protocol api.Protocol
  116. nodePort int
  117. loadBalancerStatus api.LoadBalancerStatus
  118. sessionAffinityType api.ServiceAffinity
  119. stickyMaxAgeSeconds int
  120. externalIPs []string
  121. loadBalancerSourceRanges []string
  122. onlyNodeLocalEndpoints bool
  123. healthCheckNodePort int
  124. }
  125. // internal struct for endpoints information
  126. type endpointsInfo struct {
  127. ip string
  128. localEndpoint bool
  129. }
  130. // returns a new serviceInfo struct
  131. func newServiceInfo(service proxy.ServicePortName) *serviceInfo {
  132. return &serviceInfo{
  133. sessionAffinityType: api.ServiceAffinityNone, // default
  134. stickyMaxAgeSeconds: 180, // TODO: paramaterize this in the API.
  135. }
  136. }
  137. // Proxier is an iptables based proxy for connections between a localhost:lport
  138. // and services that provide the actual backends.
  139. type Proxier struct {
  140. mu sync.Mutex // protects the following fields
  141. serviceMap map[proxy.ServicePortName]*serviceInfo
  142. endpointsMap map[proxy.ServicePortName][]*endpointsInfo
  143. portsMap map[localPort]closeable
  144. haveReceivedServiceUpdate bool // true once we've seen an OnServiceUpdate event
  145. haveReceivedEndpointsUpdate bool // true once we've seen an OnEndpointsUpdate event
  146. // These are effectively const and do not need the mutex to be held.
  147. syncPeriod time.Duration
  148. iptables utiliptables.Interface
  149. masqueradeAll bool
  150. masqueradeMark string
  151. exec utilexec.Interface
  152. clusterCIDR string
  153. hostname string
  154. nodeIP net.IP
  155. }
  156. type localPort struct {
  157. desc string
  158. ip string
  159. port int
  160. protocol string
  161. }
  162. func (lp *localPort) String() string {
  163. return fmt.Sprintf("%q (%s:%d/%s)", lp.desc, lp.ip, lp.port, lp.protocol)
  164. }
  165. type closeable interface {
  166. Close() error
  167. }
  168. // Proxier implements ProxyProvider
  169. var _ proxy.ProxyProvider = &Proxier{}
  170. // NewProxier returns a new Proxier given an iptables Interface instance.
  171. // Because of the iptables logic, it is assumed that there is only a single Proxier active on a machine.
  172. // An error will be returned if iptables fails to update or acquire the initial lock.
  173. // Once a proxier is created, it will keep iptables up to date in the background and
  174. // will not terminate if a particular iptables call fails.
  175. func NewProxier(ipt utiliptables.Interface, sysctl utilsysctl.Interface, exec utilexec.Interface, syncPeriod time.Duration, masqueradeAll bool, masqueradeBit int, clusterCIDR string, hostname string, nodeIP net.IP) (*Proxier, error) {
  176. // Set the route_localnet sysctl we need for
  177. if err := sysctl.SetSysctl(sysctlRouteLocalnet, 1); err != nil {
  178. return nil, fmt.Errorf("can't set sysctl %s: %v", sysctlRouteLocalnet, err)
  179. }
  180. // Proxy needs br_netfilter and bridge-nf-call-iptables=1 when containers
  181. // are connected to a Linux bridge (but not SDN bridges). Until most
  182. // plugins handle this, log when config is missing
  183. if val, err := sysctl.GetSysctl(sysctlBridgeCallIptables); err == nil && val != 1 {
  184. glog.Infof("missing br-netfilter module or unset sysctl br-nf-call-iptables; proxy may not work as intended")
  185. }
  186. // Generate the masquerade mark to use for SNAT rules.
  187. if masqueradeBit < 0 || masqueradeBit > 31 {
  188. return nil, fmt.Errorf("invalid iptables-masquerade-bit %v not in [0, 31]", masqueradeBit)
  189. }
  190. masqueradeValue := 1 << uint(masqueradeBit)
  191. masqueradeMark := fmt.Sprintf("%#08x/%#08x", masqueradeValue, masqueradeValue)
  192. if nodeIP == nil {
  193. glog.Warningf("invalid nodeIP, initialize kube-proxy with 127.0.0.1 as nodeIP")
  194. nodeIP = net.ParseIP("127.0.0.1")
  195. }
  196. go healthcheck.Run()
  197. return &Proxier{
  198. serviceMap: make(map[proxy.ServicePortName]*serviceInfo),
  199. endpointsMap: make(map[proxy.ServicePortName][]*endpointsInfo),
  200. portsMap: make(map[localPort]closeable),
  201. syncPeriod: syncPeriod,
  202. iptables: ipt,
  203. masqueradeAll: masqueradeAll,
  204. masqueradeMark: masqueradeMark,
  205. exec: exec,
  206. clusterCIDR: clusterCIDR,
  207. hostname: hostname,
  208. nodeIP: nodeIP,
  209. }, nil
  210. }
  211. // CleanupLeftovers removes all iptables rules and chains created by the Proxier
  212. // It returns true if an error was encountered. Errors are logged.
  213. func CleanupLeftovers(ipt utiliptables.Interface) (encounteredError bool) {
  214. // Unlink the services chain.
  215. args := []string{
  216. "-m", "comment", "--comment", "kubernetes service portals",
  217. "-j", string(kubeServicesChain),
  218. }
  219. tableChainsWithJumpServices := []struct {
  220. table utiliptables.Table
  221. chain utiliptables.Chain
  222. }{
  223. {utiliptables.TableFilter, utiliptables.ChainOutput},
  224. {utiliptables.TableNAT, utiliptables.ChainOutput},
  225. {utiliptables.TableNAT, utiliptables.ChainPrerouting},
  226. }
  227. for _, tc := range tableChainsWithJumpServices {
  228. if err := ipt.DeleteRule(tc.table, tc.chain, args...); err != nil {
  229. if !utiliptables.IsNotFoundError(err) {
  230. glog.Errorf("Error removing pure-iptables proxy rule: %v", err)
  231. encounteredError = true
  232. }
  233. }
  234. }
  235. // Unlink the postrouting chain.
  236. args = []string{
  237. "-m", "comment", "--comment", "kubernetes postrouting rules",
  238. "-j", string(kubePostroutingChain),
  239. }
  240. if err := ipt.DeleteRule(utiliptables.TableNAT, utiliptables.ChainPostrouting, args...); err != nil {
  241. if !utiliptables.IsNotFoundError(err) {
  242. glog.Errorf("Error removing pure-iptables proxy rule: %v", err)
  243. encounteredError = true
  244. }
  245. }
  246. // Flush and remove all of our chains.
  247. if iptablesSaveRaw, err := ipt.Save(utiliptables.TableNAT); err != nil {
  248. glog.Errorf("Failed to execute iptables-save for %s: %v", utiliptables.TableNAT, err)
  249. encounteredError = true
  250. } else {
  251. existingNATChains := utiliptables.GetChainLines(utiliptables.TableNAT, iptablesSaveRaw)
  252. natChains := bytes.NewBuffer(nil)
  253. natRules := bytes.NewBuffer(nil)
  254. writeLine(natChains, "*nat")
  255. // Start with chains we know we need to remove.
  256. for _, chain := range []utiliptables.Chain{kubeServicesChain, kubeNodePortsChain, kubePostroutingChain, KubeMarkMasqChain} {
  257. if _, found := existingNATChains[chain]; found {
  258. chainString := string(chain)
  259. writeLine(natChains, existingNATChains[chain]) // flush
  260. writeLine(natRules, "-X", chainString) // delete
  261. }
  262. }
  263. // Hunt for service and endpoint chains.
  264. for chain := range existingNATChains {
  265. chainString := string(chain)
  266. if strings.HasPrefix(chainString, "KUBE-SVC-") || strings.HasPrefix(chainString, "KUBE-SEP-") || strings.HasPrefix(chainString, "KUBE-FW-") || strings.HasPrefix(chainString, "KUBE-XLB-") {
  267. writeLine(natChains, existingNATChains[chain]) // flush
  268. writeLine(natRules, "-X", chainString) // delete
  269. }
  270. }
  271. writeLine(natRules, "COMMIT")
  272. natLines := append(natChains.Bytes(), natRules.Bytes()...)
  273. // Write it.
  274. err = ipt.Restore(utiliptables.TableNAT, natLines, utiliptables.NoFlushTables, utiliptables.RestoreCounters)
  275. if err != nil {
  276. glog.Errorf("Failed to execute iptables-restore for %s: %v", utiliptables.TableNAT, err)
  277. encounteredError = true
  278. }
  279. }
  280. {
  281. filterBuf := bytes.NewBuffer(nil)
  282. writeLine(filterBuf, "*filter")
  283. writeLine(filterBuf, fmt.Sprintf(":%s - [0:0]", kubeServicesChain))
  284. writeLine(filterBuf, fmt.Sprintf("-X %s", kubeServicesChain))
  285. writeLine(filterBuf, "COMMIT")
  286. // Write it.
  287. if err := ipt.Restore(utiliptables.TableFilter, filterBuf.Bytes(), utiliptables.NoFlushTables, utiliptables.RestoreCounters); err != nil {
  288. glog.Errorf("Failed to execute iptables-restore for %s: %v", utiliptables.TableFilter, err)
  289. encounteredError = true
  290. }
  291. }
  292. return encounteredError
  293. }
  294. func (proxier *Proxier) sameConfig(info *serviceInfo, service *api.Service, port *api.ServicePort) bool {
  295. if info.protocol != port.Protocol || info.port != int(port.Port) || info.nodePort != int(port.NodePort) {
  296. return false
  297. }
  298. if !info.clusterIP.Equal(net.ParseIP(service.Spec.ClusterIP)) {
  299. return false
  300. }
  301. if !ipsEqual(info.externalIPs, service.Spec.ExternalIPs) {
  302. return false
  303. }
  304. if !api.LoadBalancerStatusEqual(&info.loadBalancerStatus, &service.Status.LoadBalancer) {
  305. return false
  306. }
  307. if info.sessionAffinityType != service.Spec.SessionAffinity {
  308. return false
  309. }
  310. return true
  311. }
  312. func ipsEqual(lhs, rhs []string) bool {
  313. if len(lhs) != len(rhs) {
  314. return false
  315. }
  316. for i := range lhs {
  317. if lhs[i] != rhs[i] {
  318. return false
  319. }
  320. }
  321. return true
  322. }
  323. // Sync is called to immediately synchronize the proxier state to iptables
  324. func (proxier *Proxier) Sync() {
  325. proxier.mu.Lock()
  326. defer proxier.mu.Unlock()
  327. proxier.syncProxyRules()
  328. }
  329. // SyncLoop runs periodic work. This is expected to run as a goroutine or as the main loop of the app. It does not return.
  330. func (proxier *Proxier) SyncLoop() {
  331. t := time.NewTicker(proxier.syncPeriod)
  332. defer t.Stop()
  333. for {
  334. <-t.C
  335. glog.V(6).Infof("Periodic sync")
  336. proxier.Sync()
  337. }
  338. }
  339. // OnServiceUpdate tracks the active set of service proxies.
  340. // They will be synchronized using syncProxyRules()
  341. func (proxier *Proxier) OnServiceUpdate(allServices []api.Service) {
  342. start := time.Now()
  343. defer func() {
  344. glog.V(4).Infof("OnServiceUpdate took %v for %d services", time.Since(start), len(allServices))
  345. }()
  346. proxier.mu.Lock()
  347. defer proxier.mu.Unlock()
  348. proxier.haveReceivedServiceUpdate = true
  349. activeServices := make(map[proxy.ServicePortName]bool) // use a map as a set
  350. for i := range allServices {
  351. service := &allServices[i]
  352. svcName := types.NamespacedName{
  353. Namespace: service.Namespace,
  354. Name: service.Name,
  355. }
  356. // if ClusterIP is "None" or empty, skip proxying
  357. if !api.IsServiceIPSet(service) {
  358. glog.V(3).Infof("Skipping service %s due to clusterIP = %q", svcName, service.Spec.ClusterIP)
  359. continue
  360. }
  361. for i := range service.Spec.Ports {
  362. servicePort := &service.Spec.Ports[i]
  363. serviceName := proxy.ServicePortName{
  364. NamespacedName: svcName,
  365. Port: servicePort.Name,
  366. }
  367. activeServices[serviceName] = true
  368. info, exists := proxier.serviceMap[serviceName]
  369. if exists && proxier.sameConfig(info, service, servicePort) {
  370. // Nothing changed.
  371. continue
  372. }
  373. if exists {
  374. // Something changed.
  375. glog.V(3).Infof("Something changed for service %q: removing it", serviceName)
  376. delete(proxier.serviceMap, serviceName)
  377. }
  378. serviceIP := net.ParseIP(service.Spec.ClusterIP)
  379. glog.V(1).Infof("Adding new service %q at %s:%d/%s", serviceName, serviceIP, servicePort.Port, servicePort.Protocol)
  380. info = newServiceInfo(serviceName)
  381. info.clusterIP = serviceIP
  382. info.port = int(servicePort.Port)
  383. info.protocol = servicePort.Protocol
  384. info.nodePort = int(servicePort.NodePort)
  385. info.externalIPs = service.Spec.ExternalIPs
  386. // Deep-copy in case the service instance changes
  387. info.loadBalancerStatus = *api.LoadBalancerStatusDeepCopy(&service.Status.LoadBalancer)
  388. info.sessionAffinityType = service.Spec.SessionAffinity
  389. info.loadBalancerSourceRanges = service.Spec.LoadBalancerSourceRanges
  390. info.onlyNodeLocalEndpoints = apiservice.NeedsHealthCheck(service) && featuregate.DefaultFeatureGate.ExternalTrafficLocalOnly()
  391. if info.onlyNodeLocalEndpoints {
  392. p := apiservice.GetServiceHealthCheckNodePort(service)
  393. if p == 0 {
  394. glog.Errorf("Service does not contain necessary annotation %v",
  395. apiservice.AnnotationHealthCheckNodePort)
  396. } else {
  397. info.healthCheckNodePort = int(p)
  398. // Turn on healthcheck responder to listen on the health check nodePort
  399. healthcheck.AddServiceListener(serviceName.NamespacedName, info.healthCheckNodePort)
  400. }
  401. }
  402. proxier.serviceMap[serviceName] = info
  403. glog.V(4).Infof("added serviceInfo(%s): %s", serviceName, spew.Sdump(info))
  404. }
  405. }
  406. staleUDPServices := sets.NewString()
  407. // Remove serviceports missing from the update.
  408. for name, info := range proxier.serviceMap {
  409. if !activeServices[name] {
  410. glog.V(1).Infof("Removing service %q", name)
  411. if info.protocol == api.ProtocolUDP {
  412. staleUDPServices.Insert(info.clusterIP.String())
  413. }
  414. delete(proxier.serviceMap, name)
  415. if info.onlyNodeLocalEndpoints && info.healthCheckNodePort > 0 {
  416. // Remove ServiceListener health check nodePorts from the health checker
  417. // TODO - Stats
  418. healthcheck.DeleteServiceListener(name.NamespacedName, info.healthCheckNodePort)
  419. }
  420. }
  421. }
  422. proxier.syncProxyRules()
  423. proxier.deleteServiceConnections(staleUDPServices.List())
  424. }
  425. // Generate a list of ip strings from the list of endpoint infos
  426. func flattenEndpointsInfo(endPoints []*endpointsInfo) []string {
  427. var endpointIPs []string
  428. for _, ep := range endPoints {
  429. endpointIPs = append(endpointIPs, ep.ip)
  430. }
  431. return endpointIPs
  432. }
  433. // Reconstruct the list of endpoint infos from the endpointIP list
  434. // Use the slice of endpointIPs to rebuild a slice of corresponding {endpointIP, localEndpointOnly} infos
  435. // from the full []hostPortInfo slice.
  436. //
  437. // For e.g. if input is
  438. // endpoints = []hostPortInfo{ {host="1.1.1.1", port=22, localEndpointOnly=<bool>}, {host="2.2.2.2", port=80, localEndpointOnly=<bool>} }
  439. // endpointIPs = []string{ "2.2.2.2:80" }
  440. //
  441. // then output will be
  442. //
  443. // []endpointsInfo{ {"2.2.2.2:80", localEndpointOnly=<bool>} }
  444. func (proxier *Proxier) buildEndpointInfoList(endPoints []hostPortInfo, endpointIPs []string) []*endpointsInfo {
  445. lookupSet := sets.NewString()
  446. for _, ip := range endpointIPs {
  447. lookupSet.Insert(ip)
  448. }
  449. var filteredEndpoints []*endpointsInfo
  450. for _, hpp := range endPoints {
  451. key := net.JoinHostPort(hpp.host, strconv.Itoa(hpp.port))
  452. if lookupSet.Has(key) {
  453. filteredEndpoints = append(filteredEndpoints, &endpointsInfo{ip: key, localEndpoint: hpp.localEndpoint})
  454. }
  455. }
  456. return filteredEndpoints
  457. }
  458. // OnEndpointsUpdate takes in a slice of updated endpoints.
  459. func (proxier *Proxier) OnEndpointsUpdate(allEndpoints []api.Endpoints) {
  460. start := time.Now()
  461. defer func() {
  462. glog.V(4).Infof("OnEndpointsUpdate took %v for %d endpoints", time.Since(start), len(allEndpoints))
  463. }()
  464. proxier.mu.Lock()
  465. defer proxier.mu.Unlock()
  466. proxier.haveReceivedEndpointsUpdate = true
  467. activeEndpoints := make(map[proxy.ServicePortName]bool) // use a map as a set
  468. staleConnections := make(map[endpointServicePair]bool)
  469. svcPortToInfoMap := make(map[proxy.ServicePortName][]hostPortInfo)
  470. // Update endpoints for services.
  471. for i := range allEndpoints {
  472. svcEndpoints := &allEndpoints[i]
  473. // We need to build a map of portname -> all ip:ports for that
  474. // portname. Explode Endpoints.Subsets[*] into this structure.
  475. portsToEndpoints := map[string][]hostPortInfo{}
  476. for i := range svcEndpoints.Subsets {
  477. ss := &svcEndpoints.Subsets[i]
  478. for i := range ss.Ports {
  479. port := &ss.Ports[i]
  480. for i := range ss.Addresses {
  481. addr := &ss.Addresses[i]
  482. var isLocalEndpoint bool
  483. if addr.NodeName != nil {
  484. isLocalEndpoint = *addr.NodeName == proxier.hostname
  485. isLocalEndpoint = featuregate.DefaultFeatureGate.ExternalTrafficLocalOnly() && isLocalEndpoint
  486. }
  487. hostPortObject := hostPortInfo{
  488. host: addr.IP,
  489. port: int(port.Port),
  490. localEndpoint: isLocalEndpoint,
  491. }
  492. portsToEndpoints[port.Name] = append(portsToEndpoints[port.Name], hostPortObject)
  493. }
  494. }
  495. }
  496. for portname := range portsToEndpoints {
  497. svcPort := proxy.ServicePortName{NamespacedName: types.NamespacedName{Namespace: svcEndpoints.Namespace, Name: svcEndpoints.Name}, Port: portname}
  498. svcPortToInfoMap[svcPort] = portsToEndpoints[portname]
  499. curEndpoints := proxier.endpointsMap[svcPort]
  500. newEndpoints := flattenValidEndpoints(portsToEndpoints[portname])
  501. // Flatten the list of current endpoint infos to just a list of ips as strings
  502. curEndpointIPs := flattenEndpointsInfo(curEndpoints)
  503. if len(curEndpointIPs) != len(newEndpoints) || !slicesEquiv(slice.CopyStrings(curEndpointIPs), newEndpoints) {
  504. removedEndpoints := getRemovedEndpoints(curEndpointIPs, newEndpoints)
  505. for _, ep := range removedEndpoints {
  506. staleConnections[endpointServicePair{endpoint: ep, servicePortName: svcPort}] = true
  507. }
  508. glog.V(3).Infof("Setting endpoints for %q to %+v", svcPort, newEndpoints)
  509. // Once the set operations using the list of ips are complete, build the list of endpoint infos
  510. proxier.endpointsMap[svcPort] = proxier.buildEndpointInfoList(portsToEndpoints[portname], newEndpoints)
  511. }
  512. activeEndpoints[svcPort] = true
  513. }
  514. }
  515. // Remove endpoints missing from the update.
  516. for svcPort := range proxier.endpointsMap {
  517. if !activeEndpoints[svcPort] {
  518. // record endpoints of unactive service to stale connections
  519. for _, ep := range proxier.endpointsMap[svcPort] {
  520. staleConnections[endpointServicePair{endpoint: ep.ip, servicePortName: svcPort}] = true
  521. }
  522. glog.V(2).Infof("Removing endpoints for %q", svcPort)
  523. delete(proxier.endpointsMap, svcPort)
  524. }
  525. proxier.updateHealthCheckEntries(svcPort.NamespacedName, svcPortToInfoMap[svcPort])
  526. }
  527. proxier.syncProxyRules()
  528. proxier.deleteEndpointConnections(staleConnections)
  529. }
  530. // updateHealthCheckEntries - send the new set of local endpoints to the health checker
  531. func (proxier *Proxier) updateHealthCheckEntries(name types.NamespacedName, hostPorts []hostPortInfo) {
  532. // Use a set instead of a slice to provide deduplication
  533. endpoints := sets.NewString()
  534. for _, portInfo := range hostPorts {
  535. if portInfo.localEndpoint {
  536. // kube-proxy health check only needs local endpoints
  537. endpoints.Insert(fmt.Sprintf("%s/%s", name.Namespace, name.Name))
  538. }
  539. }
  540. healthcheck.UpdateEndpoints(name, endpoints)
  541. }
  542. // used in OnEndpointsUpdate
  543. type hostPortInfo struct {
  544. host string
  545. port int
  546. localEndpoint bool
  547. }
  548. func isValidEndpoint(hpp *hostPortInfo) bool {
  549. return hpp.host != "" && hpp.port > 0
  550. }
  551. // Tests whether two slices are equivalent. This sorts both slices in-place.
  552. func slicesEquiv(lhs, rhs []string) bool {
  553. if len(lhs) != len(rhs) {
  554. return false
  555. }
  556. if reflect.DeepEqual(slice.SortStrings(lhs), slice.SortStrings(rhs)) {
  557. return true
  558. }
  559. return false
  560. }
  561. func flattenValidEndpoints(endpoints []hostPortInfo) []string {
  562. // Convert Endpoint objects into strings for easier use later.
  563. var result []string
  564. for i := range endpoints {
  565. hpp := &endpoints[i]
  566. if isValidEndpoint(hpp) {
  567. result = append(result, net.JoinHostPort(hpp.host, strconv.Itoa(hpp.port)))
  568. } else {
  569. glog.Warningf("got invalid endpoint: %+v", *hpp)
  570. }
  571. }
  572. return result
  573. }
  574. // portProtoHash takes the ServicePortName and protocol for a service
  575. // returns the associated 16 character hash. This is computed by hashing (sha256)
  576. // then encoding to base32 and truncating to 16 chars. We do this because Iptables
  577. // Chain Names must be <= 28 chars long, and the longer they are the harder they are to read.
  578. func portProtoHash(s proxy.ServicePortName, protocol string) string {
  579. hash := sha256.Sum256([]byte(s.String() + protocol))
  580. encoded := base32.StdEncoding.EncodeToString(hash[:])
  581. return encoded[:16]
  582. }
  583. // servicePortChainName takes the ServicePortName for a service and
  584. // returns the associated iptables chain. This is computed by hashing (sha256)
  585. // then encoding to base32 and truncating with the prefix "KUBE-SVC-".
  586. func servicePortChainName(s proxy.ServicePortName, protocol string) utiliptables.Chain {
  587. return utiliptables.Chain("KUBE-SVC-" + portProtoHash(s, protocol))
  588. }
  589. // serviceFirewallChainName takes the ServicePortName for a service and
  590. // returns the associated iptables chain. This is computed by hashing (sha256)
  591. // then encoding to base32 and truncating with the prefix "KUBE-FW-".
  592. func serviceFirewallChainName(s proxy.ServicePortName, protocol string) utiliptables.Chain {
  593. return utiliptables.Chain("KUBE-FW-" + portProtoHash(s, protocol))
  594. }
  595. // serviceLBPortChainName takes the ServicePortName for a service and
  596. // returns the associated iptables chain. This is computed by hashing (sha256)
  597. // then encoding to base32 and truncating with the prefix "KUBE-XLB-". We do
  598. // this because Iptables Chain Names must be <= 28 chars long, and the longer
  599. // they are the harder they are to read.
  600. func serviceLBChainName(s proxy.ServicePortName, protocol string) utiliptables.Chain {
  601. return utiliptables.Chain("KUBE-XLB-" + portProtoHash(s, protocol))
  602. }
  603. // This is the same as servicePortChainName but with the endpoint included.
  604. func servicePortEndpointChainName(s proxy.ServicePortName, protocol string, endpoint string) utiliptables.Chain {
  605. hash := sha256.Sum256([]byte(s.String() + protocol + endpoint))
  606. encoded := base32.StdEncoding.EncodeToString(hash[:])
  607. return utiliptables.Chain("KUBE-SEP-" + encoded[:16])
  608. }
  609. // getRemovedEndpoints returns the endpoint IPs that are missing in the new endpoints
  610. func getRemovedEndpoints(curEndpoints, newEndpoints []string) []string {
  611. return sets.NewString(curEndpoints...).Difference(sets.NewString(newEndpoints...)).List()
  612. }
  613. type endpointServicePair struct {
  614. endpoint string
  615. servicePortName proxy.ServicePortName
  616. }
  617. const noConnectionToDelete = "0 flow entries have been deleted"
  618. // After a UDP endpoint has been removed, we must flush any pending conntrack entries to it, or else we
  619. // risk sending more traffic to it, all of which will be lost (because UDP).
  620. // This assumes the proxier mutex is held
  621. func (proxier *Proxier) deleteEndpointConnections(connectionMap map[endpointServicePair]bool) {
  622. for epSvcPair := range connectionMap {
  623. if svcInfo, ok := proxier.serviceMap[epSvcPair.servicePortName]; ok && svcInfo.protocol == api.ProtocolUDP {
  624. endpointIP := strings.Split(epSvcPair.endpoint, ":")[0]
  625. glog.V(2).Infof("Deleting connection tracking state for service IP %s, endpoint IP %s", svcInfo.clusterIP.String(), endpointIP)
  626. err := proxier.execConntrackTool("-D", "--orig-dst", svcInfo.clusterIP.String(), "--dst-nat", endpointIP, "-p", "udp")
  627. if err != nil && !strings.Contains(err.Error(), noConnectionToDelete) {
  628. // TODO: Better handling for deletion failure. When failure occur, stale udp connection may not get flushed.
  629. // These stale udp connection will keep black hole traffic. Making this a best effort operation for now, since it
  630. // is expensive to baby sit all udp connections to kubernetes services.
  631. glog.Errorf("conntrack return with error: %v", err)
  632. }
  633. }
  634. }
  635. }
  636. // deleteServiceConnection use conntrack-tool to delete UDP connection specified by service ip
  637. func (proxier *Proxier) deleteServiceConnections(svcIPs []string) {
  638. for _, ip := range svcIPs {
  639. glog.V(2).Infof("Deleting connection tracking state for service IP %s", ip)
  640. err := proxier.execConntrackTool("-D", "--orig-dst", ip, "-p", "udp")
  641. if err != nil && !strings.Contains(err.Error(), noConnectionToDelete) {
  642. // TODO: Better handling for deletion failure. When failure occur, stale udp connection may not get flushed.
  643. // These stale udp connection will keep black hole traffic. Making this a best effort operation for now, since it
  644. // is expensive to baby sit all udp connections to kubernetes services.
  645. glog.Errorf("conntrack return with error: %v", err)
  646. }
  647. }
  648. }
  649. //execConntrackTool executes conntrack tool using given parameters
  650. func (proxier *Proxier) execConntrackTool(parameters ...string) error {
  651. conntrackPath, err := proxier.exec.LookPath("conntrack")
  652. if err != nil {
  653. return fmt.Errorf("Error looking for path of conntrack: %v", err)
  654. }
  655. output, err := proxier.exec.Command(conntrackPath, parameters...).CombinedOutput()
  656. if err != nil {
  657. return fmt.Errorf("Conntrack command returned: %q, error message: %s", string(output), err)
  658. }
  659. return nil
  660. }
  661. // This is where all of the iptables-save/restore calls happen.
  662. // The only other iptables rules are those that are setup in iptablesInit()
  663. // assumes proxier.mu is held
  664. func (proxier *Proxier) syncProxyRules() {
  665. start := time.Now()
  666. defer func() {
  667. glog.V(4).Infof("syncProxyRules took %v", time.Since(start))
  668. }()
  669. // don't sync rules till we've received services and endpoints
  670. if !proxier.haveReceivedEndpointsUpdate || !proxier.haveReceivedServiceUpdate {
  671. glog.V(2).Info("Not syncing iptables until Services and Endpoints have been received from master")
  672. return
  673. }
  674. glog.V(3).Infof("Syncing iptables rules")
  675. // Create and link the kube services chain.
  676. {
  677. tablesNeedServicesChain := []utiliptables.Table{utiliptables.TableFilter, utiliptables.TableNAT}
  678. for _, table := range tablesNeedServicesChain {
  679. if _, err := proxier.iptables.EnsureChain(table, kubeServicesChain); err != nil {
  680. glog.Errorf("Failed to ensure that %s chain %s exists: %v", table, kubeServicesChain, err)
  681. return
  682. }
  683. }
  684. tableChainsNeedJumpServices := []struct {
  685. table utiliptables.Table
  686. chain utiliptables.Chain
  687. }{
  688. {utiliptables.TableFilter, utiliptables.ChainOutput},
  689. {utiliptables.TableNAT, utiliptables.ChainOutput},
  690. {utiliptables.TableNAT, utiliptables.ChainPrerouting},
  691. }
  692. comment := "kubernetes service portals"
  693. args := []string{"-m", "comment", "--comment", comment, "-j", string(kubeServicesChain)}
  694. for _, tc := range tableChainsNeedJumpServices {
  695. if _, err := proxier.iptables.EnsureRule(utiliptables.Prepend, tc.table, tc.chain, args...); err != nil {
  696. glog.Errorf("Failed to ensure that %s chain %s jumps to %s: %v", tc.table, tc.chain, kubeServicesChain, err)
  697. return
  698. }
  699. }
  700. }
  701. // Create and link the kube postrouting chain.
  702. {
  703. if _, err := proxier.iptables.EnsureChain(utiliptables.TableNAT, kubePostroutingChain); err != nil {
  704. glog.Errorf("Failed to ensure that %s chain %s exists: %v", utiliptables.TableNAT, kubePostroutingChain, err)
  705. return
  706. }
  707. comment := "kubernetes postrouting rules"
  708. args := []string{"-m", "comment", "--comment", comment, "-j", string(kubePostroutingChain)}
  709. if _, err := proxier.iptables.EnsureRule(utiliptables.Prepend, utiliptables.TableNAT, utiliptables.ChainPostrouting, args...); err != nil {
  710. glog.Errorf("Failed to ensure that %s chain %s jumps to %s: %v", utiliptables.TableNAT, utiliptables.ChainPostrouting, kubePostroutingChain, err)
  711. return
  712. }
  713. }
  714. // Get iptables-save output so we can check for existing chains and rules.
  715. // This will be a map of chain name to chain with rules as stored in iptables-save/iptables-restore
  716. existingFilterChains := make(map[utiliptables.Chain]string)
  717. iptablesSaveRaw, err := proxier.iptables.Save(utiliptables.TableFilter)
  718. if err != nil { // if we failed to get any rules
  719. glog.Errorf("Failed to execute iptables-save, syncing all rules: %v", err)
  720. } else { // otherwise parse the output
  721. existingFilterChains = utiliptables.GetChainLines(utiliptables.TableFilter, iptablesSaveRaw)
  722. }
  723. existingNATChains := make(map[utiliptables.Chain]string)
  724. iptablesSaveRaw, err = proxier.iptables.Save(utiliptables.TableNAT)
  725. if err != nil { // if we failed to get any rules
  726. glog.Errorf("Failed to execute iptables-save, syncing all rules: %v", err)
  727. } else { // otherwise parse the output
  728. existingNATChains = utiliptables.GetChainLines(utiliptables.TableNAT, iptablesSaveRaw)
  729. }
  730. filterChains := bytes.NewBuffer(nil)
  731. filterRules := bytes.NewBuffer(nil)
  732. natChains := bytes.NewBuffer(nil)
  733. natRules := bytes.NewBuffer(nil)
  734. // Write table headers.
  735. writeLine(filterChains, "*filter")
  736. writeLine(natChains, "*nat")
  737. // Make sure we keep stats for the top-level chains, if they existed
  738. // (which most should have because we created them above).
  739. if chain, ok := existingFilterChains[kubeServicesChain]; ok {
  740. writeLine(filterChains, chain)
  741. } else {
  742. writeLine(filterChains, utiliptables.MakeChainLine(kubeServicesChain))
  743. }
  744. if chain, ok := existingNATChains[kubeServicesChain]; ok {
  745. writeLine(natChains, chain)
  746. } else {
  747. writeLine(natChains, utiliptables.MakeChainLine(kubeServicesChain))
  748. }
  749. if chain, ok := existingNATChains[kubeNodePortsChain]; ok {
  750. writeLine(natChains, chain)
  751. } else {
  752. writeLine(natChains, utiliptables.MakeChainLine(kubeNodePortsChain))
  753. }
  754. if chain, ok := existingNATChains[kubePostroutingChain]; ok {
  755. writeLine(natChains, chain)
  756. } else {
  757. writeLine(natChains, utiliptables.MakeChainLine(kubePostroutingChain))
  758. }
  759. if chain, ok := existingNATChains[KubeMarkMasqChain]; ok {
  760. writeLine(natChains, chain)
  761. } else {
  762. writeLine(natChains, utiliptables.MakeChainLine(KubeMarkMasqChain))
  763. }
  764. // Install the kubernetes-specific postrouting rules. We use a whole chain for
  765. // this so that it is easier to flush and change, for example if the mark
  766. // value should ever change.
  767. writeLine(natRules, []string{
  768. "-A", string(kubePostroutingChain),
  769. "-m", "comment", "--comment", `"kubernetes service traffic requiring SNAT"`,
  770. "-m", "mark", "--mark", proxier.masqueradeMark,
  771. "-j", "MASQUERADE",
  772. }...)
  773. // Install the kubernetes-specific masquerade mark rule. We use a whole chain for
  774. // this so that it is easier to flush and change, for example if the mark
  775. // value should ever change.
  776. writeLine(natRules, []string{
  777. "-A", string(KubeMarkMasqChain),
  778. "-j", "MARK", "--set-xmark", proxier.masqueradeMark,
  779. }...)
  780. // Accumulate NAT chains to keep.
  781. activeNATChains := map[utiliptables.Chain]bool{} // use a map as a set
  782. // Accumulate the set of local ports that we will be holding open once this update is complete
  783. replacementPortsMap := map[localPort]closeable{}
  784. // Build rules for each service.
  785. for svcName, svcInfo := range proxier.serviceMap {
  786. protocol := strings.ToLower(string(svcInfo.protocol))
  787. // Create the per-service chain, retaining counters if possible.
  788. svcChain := servicePortChainName(svcName, protocol)
  789. if chain, ok := existingNATChains[svcChain]; ok {
  790. writeLine(natChains, chain)
  791. } else {
  792. writeLine(natChains, utiliptables.MakeChainLine(svcChain))
  793. }
  794. activeNATChains[svcChain] = true
  795. svcXlbChain := serviceLBChainName(svcName, protocol)
  796. if svcInfo.onlyNodeLocalEndpoints {
  797. // Only for services with the externalTraffic annotation set to OnlyLocal
  798. // create the per-service LB chain, retaining counters if possible.
  799. if lbChain, ok := existingNATChains[svcXlbChain]; ok {
  800. writeLine(natChains, lbChain)
  801. } else {
  802. writeLine(natChains, utiliptables.MakeChainLine(svcXlbChain))
  803. }
  804. activeNATChains[svcXlbChain] = true
  805. }
  806. // Capture the clusterIP.
  807. args := []string{
  808. "-A", string(kubeServicesChain),
  809. "-m", "comment", "--comment", fmt.Sprintf(`"%s cluster IP"`, svcName.String()),
  810. "-m", protocol, "-p", protocol,
  811. "-d", fmt.Sprintf("%s/32", svcInfo.clusterIP.String()),
  812. "--dport", fmt.Sprintf("%d", svcInfo.port),
  813. }
  814. if proxier.masqueradeAll {
  815. writeLine(natRules, append(args, "-j", string(KubeMarkMasqChain))...)
  816. }
  817. if len(proxier.clusterCIDR) > 0 {
  818. writeLine(natRules, append(args, "! -s", proxier.clusterCIDR, "-j", string(KubeMarkMasqChain))...)
  819. }
  820. writeLine(natRules, append(args, "-j", string(svcChain))...)
  821. // Capture externalIPs.
  822. for _, externalIP := range svcInfo.externalIPs {
  823. // If the "external" IP happens to be an IP that is local to this
  824. // machine, hold the local port open so no other process can open it
  825. // (because the socket might open but it would never work).
  826. if local, err := isLocalIP(externalIP); err != nil {
  827. glog.Errorf("can't determine if IP is local, assuming not: %v", err)
  828. } else if local {
  829. lp := localPort{
  830. desc: "externalIP for " + svcName.String(),
  831. ip: externalIP,
  832. port: svcInfo.port,
  833. protocol: protocol,
  834. }
  835. if proxier.portsMap[lp] != nil {
  836. glog.V(4).Infof("Port %s was open before and is still needed", lp.String())
  837. replacementPortsMap[lp] = proxier.portsMap[lp]
  838. } else {
  839. socket, err := openLocalPort(&lp)
  840. if err != nil {
  841. glog.Errorf("can't open %s, skipping this externalIP: %v", lp.String(), err)
  842. continue
  843. }
  844. replacementPortsMap[lp] = socket
  845. }
  846. } // We're holding the port, so it's OK to install iptables rules.
  847. args := []string{
  848. "-A", string(kubeServicesChain),
  849. "-m", "comment", "--comment", fmt.Sprintf(`"%s external IP"`, svcName.String()),
  850. "-m", protocol, "-p", protocol,
  851. "-d", fmt.Sprintf("%s/32", externalIP),
  852. "--dport", fmt.Sprintf("%d", svcInfo.port),
  853. }
  854. // We have to SNAT packets to external IPs.
  855. writeLine(natRules, append(args, "-j", string(KubeMarkMasqChain))...)
  856. // Allow traffic for external IPs that does not come from a bridge (i.e. not from a container)
  857. // nor from a local process to be forwarded to the service.
  858. // This rule roughly translates to "all traffic from off-machine".
  859. // This is imperfect in the face of network plugins that might not use a bridge, but we can revisit that later.
  860. externalTrafficOnlyArgs := append(args,
  861. "-m", "physdev", "!", "--physdev-is-in",
  862. "-m", "addrtype", "!", "--src-type", "LOCAL")
  863. writeLine(natRules, append(externalTrafficOnlyArgs, "-j", string(svcChain))...)
  864. dstLocalOnlyArgs := append(args, "-m", "addrtype", "--dst-type", "LOCAL")
  865. // Allow traffic bound for external IPs that happen to be recognized as local IPs to stay local.
  866. // This covers cases like GCE load-balancers which get added to the local routing table.
  867. writeLine(natRules, append(dstLocalOnlyArgs, "-j", string(svcChain))...)
  868. }
  869. // Capture load-balancer ingress.
  870. for _, ingress := range svcInfo.loadBalancerStatus.Ingress {
  871. if ingress.IP != "" {
  872. // create service firewall chain
  873. fwChain := serviceFirewallChainName(svcName, protocol)
  874. if chain, ok := existingNATChains[fwChain]; ok {
  875. writeLine(natChains, chain)
  876. } else {
  877. writeLine(natChains, utiliptables.MakeChainLine(fwChain))
  878. }
  879. activeNATChains[fwChain] = true
  880. // The service firewall rules are created based on ServiceSpec.loadBalancerSourceRanges field.
  881. // This currently works for loadbalancers that preserves source ips.
  882. // For loadbalancers which direct traffic to service NodePort, the firewall rules will not apply.
  883. args := []string{
  884. "-A", string(kubeServicesChain),
  885. "-m", "comment", "--comment", fmt.Sprintf(`"%s loadbalancer IP"`, svcName.String()),
  886. "-m", protocol, "-p", protocol,
  887. "-d", fmt.Sprintf("%s/32", ingress.IP),
  888. "--dport", fmt.Sprintf("%d", svcInfo.port),
  889. }
  890. // jump to service firewall chain
  891. writeLine(natRules, append(args, "-j", string(fwChain))...)
  892. args = []string{
  893. "-A", string(fwChain),
  894. "-m", "comment", "--comment", fmt.Sprintf(`"%s loadbalancer IP"`, svcName.String()),
  895. }
  896. // Each source match rule in the FW chain may jump to either the SVC or the XLB chain
  897. chosenChain := svcXlbChain
  898. // If we are proxying globally, we need to masquerade in case we cross nodes.
  899. // If we are proxying only locally, we can retain the source IP.
  900. if !svcInfo.onlyNodeLocalEndpoints {
  901. writeLine(natRules, append(args, "-j", string(KubeMarkMasqChain))...)
  902. chosenChain = svcChain
  903. }
  904. if len(svcInfo.loadBalancerSourceRanges) == 0 {
  905. // allow all sources, so jump directly to the KUBE-SVC or KUBE-XLB chain
  906. writeLine(natRules, append(args, "-j", string(chosenChain))...)
  907. } else {
  908. // firewall filter based on each source range
  909. allowFromNode := false
  910. for _, src := range svcInfo.loadBalancerSourceRanges {
  911. writeLine(natRules, append(args, "-s", src, "-j", string(chosenChain))...)
  912. // ignore error because it has been validated
  913. _, cidr, _ := net.ParseCIDR(src)
  914. if cidr.Contains(proxier.nodeIP) {
  915. allowFromNode = true
  916. }
  917. }
  918. // generally, ip route rule was added to intercept request to loadbalancer vip from the
  919. // loadbalancer's backend hosts. In this case, request will not hit the loadbalancer but loop back directly.
  920. // Need to add the following rule to allow request on host.
  921. if allowFromNode {
  922. writeLine(natRules, append(args, "-s", fmt.Sprintf("%s/32", ingress.IP), "-j", string(chosenChain))...)
  923. }
  924. }
  925. // If the packet was able to reach the end of firewall chain, then it did not get DNATed.
  926. // It means the packet cannot go thru the firewall, then mark it for DROP
  927. writeLine(natRules, append(args, "-j", string(KubeMarkDropChain))...)
  928. }
  929. }
  930. // Capture nodeports. If we had more than 2 rules it might be
  931. // worthwhile to make a new per-service chain for nodeport rules, but
  932. // with just 2 rules it ends up being a waste and a cognitive burden.
  933. if svcInfo.nodePort != 0 {
  934. // Hold the local port open so no other process can open it
  935. // (because the socket might open but it would never work).
  936. lp := localPort{
  937. desc: "nodePort for " + svcName.String(),
  938. ip: "",
  939. port: svcInfo.nodePort,
  940. protocol: protocol,
  941. }
  942. if proxier.portsMap[lp] != nil {
  943. glog.V(4).Infof("Port %s was open before and is still needed", lp.String())
  944. replacementPortsMap[lp] = proxier.portsMap[lp]
  945. } else {
  946. socket, err := openLocalPort(&lp)
  947. if err != nil {
  948. glog.Errorf("can't open %s, skipping this nodePort: %v", lp.String(), err)
  949. continue
  950. }
  951. replacementPortsMap[lp] = socket
  952. } // We're holding the port, so it's OK to install iptables rules.
  953. args := []string{
  954. "-A", string(kubeNodePortsChain),
  955. "-m", "comment", "--comment", svcName.String(),
  956. "-m", protocol, "-p", protocol,
  957. "--dport", fmt.Sprintf("%d", svcInfo.nodePort),
  958. }
  959. // Nodeports need SNAT.
  960. writeLine(natRules, append(args, "-j", string(KubeMarkMasqChain))...)
  961. // Jump to the service chain.
  962. writeLine(natRules, append(args, "-j", string(svcChain))...)
  963. }
  964. // If the service has no endpoints then reject packets.
  965. if len(proxier.endpointsMap[svcName]) == 0 {
  966. writeLine(filterRules,
  967. "-A", string(kubeServicesChain),
  968. "-m", "comment", "--comment", fmt.Sprintf(`"%s has no endpoints"`, svcName.String()),
  969. "-m", protocol, "-p", protocol,
  970. "-d", fmt.Sprintf("%s/32", svcInfo.clusterIP.String()),
  971. "--dport", fmt.Sprintf("%d", svcInfo.port),
  972. "-j", "REJECT",
  973. )
  974. continue
  975. }
  976. // Generate the per-endpoint chains. We do this in multiple passes so we
  977. // can group rules together.
  978. // These two slices parallel each other - keep in sync
  979. endpoints := make([]*endpointsInfo, 0)
  980. endpointChains := make([]utiliptables.Chain, 0)
  981. for _, ep := range proxier.endpointsMap[svcName] {
  982. endpoints = append(endpoints, ep)
  983. endpointChain := servicePortEndpointChainName(svcName, protocol, ep.ip)
  984. endpointChains = append(endpointChains, endpointChain)
  985. // Create the endpoint chain, retaining counters if possible.
  986. if chain, ok := existingNATChains[utiliptables.Chain(endpointChain)]; ok {
  987. writeLine(natChains, chain)
  988. } else {
  989. writeLine(natChains, utiliptables.MakeChainLine(endpointChain))
  990. }
  991. activeNATChains[endpointChain] = true
  992. }
  993. // First write session affinity rules, if applicable.
  994. if svcInfo.sessionAffinityType == api.ServiceAffinityClientIP {
  995. for _, endpointChain := range endpointChains {
  996. writeLine(natRules,
  997. "-A", string(svcChain),
  998. "-m", "comment", "--comment", svcName.String(),
  999. "-m", "recent", "--name", string(endpointChain),
  1000. "--rcheck", "--seconds", fmt.Sprintf("%d", svcInfo.stickyMaxAgeSeconds), "--reap",
  1001. "-j", string(endpointChain))
  1002. }
  1003. }
  1004. // Now write loadbalancing & DNAT rules.
  1005. n := len(endpointChains)
  1006. for i, endpointChain := range endpointChains {
  1007. // Balancing rules in the per-service chain.
  1008. args := []string{
  1009. "-A", string(svcChain),
  1010. "-m", "comment", "--comment", svcName.String(),
  1011. }
  1012. if i < (n - 1) {
  1013. // Each rule is a probabilistic match.
  1014. args = append(args,
  1015. "-m", "statistic",
  1016. "--mode", "random",
  1017. "--probability", fmt.Sprintf("%0.5f", 1.0/float64(n-i)))
  1018. }
  1019. // The final (or only if n == 1) rule is a guaranteed match.
  1020. args = append(args, "-j", string(endpointChain))
  1021. writeLine(natRules, args...)
  1022. // Rules in the per-endpoint chain.
  1023. args = []string{
  1024. "-A", string(endpointChain),
  1025. "-m", "comment", "--comment", svcName.String(),
  1026. }
  1027. // Handle traffic that loops back to the originator with SNAT.
  1028. writeLine(natRules, append(args,
  1029. "-s", fmt.Sprintf("%s/32", strings.Split(endpoints[i].ip, ":")[0]),
  1030. "-j", string(KubeMarkMasqChain))...)
  1031. // Update client-affinity lists.
  1032. if svcInfo.sessionAffinityType == api.ServiceAffinityClientIP {
  1033. args = append(args, "-m", "recent", "--name", string(endpointChain), "--set")
  1034. }
  1035. // DNAT to final destination.
  1036. args = append(args, "-m", protocol, "-p", protocol, "-j", "DNAT", "--to-destination", endpoints[i].ip)
  1037. writeLine(natRules, args...)
  1038. }
  1039. // The logic below this applies only if this service is marked as OnlyLocal
  1040. if !svcInfo.onlyNodeLocalEndpoints {
  1041. continue
  1042. }
  1043. // Now write ingress loadbalancing & DNAT rules only for services that have a localOnly annotation
  1044. // TODO - This logic may be combinable with the block above that creates the svc balancer chain
  1045. localEndpoints := make([]*endpointsInfo, 0)
  1046. localEndpointChains := make([]utiliptables.Chain, 0)
  1047. for i := range endpointChains {
  1048. if endpoints[i].localEndpoint {
  1049. // These slices parallel each other; must be kept in sync
  1050. localEndpoints = append(localEndpoints, endpoints[i])
  1051. localEndpointChains = append(localEndpointChains, endpointChains[i])
  1052. }
  1053. }
  1054. numLocalEndpoints := len(localEndpointChains)
  1055. if numLocalEndpoints == 0 {
  1056. // Blackhole all traffic since there are no local endpoints
  1057. args := []string{
  1058. "-A", string(svcXlbChain),
  1059. "-m", "comment", "--comment",
  1060. fmt.Sprintf(`"%s has no local endpoints"`, svcName.String()),
  1061. "-j",
  1062. string(KubeMarkDropChain),
  1063. }
  1064. writeLine(natRules, args...)
  1065. } else {
  1066. // Setup probability filter rules only over local endpoints
  1067. for i, endpointChain := range localEndpointChains {
  1068. // Balancing rules in the per-service chain.
  1069. args := []string{
  1070. "-A", string(svcXlbChain),
  1071. "-m", "comment", "--comment",
  1072. fmt.Sprintf(`"Balancing rule %d for %s"`, i, svcName.String()),
  1073. }
  1074. if i < (numLocalEndpoints - 1) {
  1075. // Each rule is a probabilistic match.
  1076. args = append(args,
  1077. "-m", "statistic",
  1078. "--mode", "random",
  1079. "--probability", fmt.Sprintf("%0.5f", 1.0/float64(numLocalEndpoints-i)))
  1080. }
  1081. // The final (or only if n == 1) rule is a guaranteed match.
  1082. args = append(args, "-j", string(endpointChain))
  1083. writeLine(natRules, args...)
  1084. }
  1085. }
  1086. }
  1087. // Delete chains no longer in use.
  1088. for chain := range existingNATChains {
  1089. if !activeNATChains[chain] {
  1090. chainString := string(chain)
  1091. if !strings.HasPrefix(chainString, "KUBE-SVC-") && !strings.HasPrefix(chainString, "KUBE-SEP-") && !strings.HasPrefix(chainString, "KUBE-FW-") && !strings.HasPrefix(chainString, "KUBE-XLB-") {
  1092. // Ignore chains that aren't ours.
  1093. continue
  1094. }
  1095. // We must (as per iptables) write a chain-line for it, which has
  1096. // the nice effect of flushing the chain. Then we can remove the
  1097. // chain.
  1098. writeLine(natChains, existingNATChains[chain])
  1099. writeLine(natRules, "-X", chainString)
  1100. }
  1101. }
  1102. // Finally, tail-call to the nodeports chain. This needs to be after all
  1103. // other service portal rules.
  1104. writeLine(natRules,
  1105. "-A", string(kubeServicesChain),
  1106. "-m", "comment", "--comment", `"kubernetes service nodeports; NOTE: this must be the last rule in this chain"`,
  1107. "-m", "addrtype", "--dst-type", "LOCAL",
  1108. "-j", string(kubeNodePortsChain))
  1109. // Write the end-of-table markers.
  1110. writeLine(filterRules, "COMMIT")
  1111. writeLine(natRules, "COMMIT")
  1112. // Sync rules.
  1113. // NOTE: NoFlushTables is used so we don't flush non-kubernetes chains in the table.
  1114. filterLines := append(filterChains.Bytes(), filterRules.Bytes()...)
  1115. natLines := append(natChains.Bytes(), natRules.Bytes()...)
  1116. lines := append(filterLines, natLines...)
  1117. glog.V(3).Infof("Restoring iptables rules: %s", lines)
  1118. err = proxier.iptables.RestoreAll(lines, utiliptables.NoFlushTables, utiliptables.RestoreCounters)
  1119. if err != nil {
  1120. glog.Errorf("Failed to execute iptables-restore: %v", err)
  1121. // Revert new local ports.
  1122. revertPorts(replacementPortsMap, proxier.portsMap)
  1123. return
  1124. }
  1125. // Close old local ports and save new ones.
  1126. for k, v := range proxier.portsMap {
  1127. if replacementPortsMap[k] == nil {
  1128. v.Close()
  1129. }
  1130. }
  1131. proxier.portsMap = replacementPortsMap
  1132. }
  1133. // Join all words with spaces, terminate with newline and write to buf.
  1134. func writeLine(buf *bytes.Buffer, words ...string) {
  1135. buf.WriteString(strings.Join(words, " ") + "\n")
  1136. }
  1137. func isLocalIP(ip string) (bool, error) {
  1138. addrs, err := net.InterfaceAddrs()
  1139. if err != nil {
  1140. return false, err
  1141. }
  1142. for i := range addrs {
  1143. intf, _, err := net.ParseCIDR(addrs[i].String())
  1144. if err != nil {
  1145. return false, err
  1146. }
  1147. if net.ParseIP(ip).Equal(intf) {
  1148. return true, nil
  1149. }
  1150. }
  1151. return false, nil
  1152. }
  1153. func openLocalPort(lp *localPort) (closeable, error) {
  1154. // For ports on node IPs, open the actual port and hold it, even though we
  1155. // use iptables to redirect traffic.
  1156. // This ensures a) that it's safe to use that port and b) that (a) stays
  1157. // true. The risk is that some process on the node (e.g. sshd or kubelet)
  1158. // is using a port and we give that same port out to a Service. That would
  1159. // be bad because iptables would silently claim the traffic but the process
  1160. // would never know.
  1161. // NOTE: We should not need to have a real listen()ing socket - bind()
  1162. // should be enough, but I can't figure out a way to e2e test without
  1163. // it. Tools like 'ss' and 'netstat' do not show sockets that are
  1164. // bind()ed but not listen()ed, and at least the default debian netcat
  1165. // has no way to avoid about 10 seconds of retries.
  1166. var socket closeable
  1167. switch lp.protocol {
  1168. case "tcp":
  1169. listener, err := net.Listen("tcp", net.JoinHostPort(lp.ip, strconv.Itoa(lp.port)))
  1170. if err != nil {
  1171. return nil, err
  1172. }
  1173. socket = listener
  1174. case "udp":
  1175. addr, err := net.ResolveUDPAddr("udp", net.JoinHostPort(lp.ip, strconv.Itoa(lp.port)))
  1176. if err != nil {
  1177. return nil, err
  1178. }
  1179. conn, err := net.ListenUDP("udp", addr)
  1180. if err != nil {
  1181. return nil, err
  1182. }
  1183. socket = conn
  1184. default:
  1185. return nil, fmt.Errorf("unknown protocol %q", lp.protocol)
  1186. }
  1187. glog.V(2).Infof("Opened local port %s", lp.String())
  1188. return socket, nil
  1189. }
  1190. // revertPorts is closing ports in replacementPortsMap but not in originalPortsMap. In other words, it only
  1191. // closes the ports opened in this sync.
  1192. func revertPorts(replacementPortsMap, originalPortsMap map[localPort]closeable) {
  1193. for k, v := range replacementPortsMap {
  1194. // Only close newly opened local ports - leave ones that were open before this update
  1195. if originalPortsMap[k] == nil {
  1196. glog.V(2).Infof("Closing local port %s after iptables-restore failure", k.String())
  1197. v.Close()
  1198. }
  1199. }
  1200. }