proxier_test.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  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 userspace
  14. import (
  15. "fmt"
  16. "io/ioutil"
  17. "net"
  18. "net/http"
  19. "net/http/httptest"
  20. "net/url"
  21. "os"
  22. "strconv"
  23. "sync/atomic"
  24. "testing"
  25. "time"
  26. "k8s.io/kubernetes/pkg/api"
  27. "k8s.io/kubernetes/pkg/proxy"
  28. "k8s.io/kubernetes/pkg/types"
  29. ipttest "k8s.io/kubernetes/pkg/util/iptables/testing"
  30. "k8s.io/kubernetes/pkg/util/runtime"
  31. )
  32. const (
  33. udpIdleTimeoutForTest = 250 * time.Millisecond
  34. )
  35. func joinHostPort(host string, port int) string {
  36. return net.JoinHostPort(host, fmt.Sprintf("%d", port))
  37. }
  38. func waitForClosedPortTCP(p *Proxier, proxyPort int) error {
  39. for i := 0; i < 50; i++ {
  40. conn, err := net.Dial("tcp", joinHostPort("", proxyPort))
  41. if err != nil {
  42. return nil
  43. }
  44. conn.Close()
  45. time.Sleep(1 * time.Millisecond)
  46. }
  47. return fmt.Errorf("port %d still open", proxyPort)
  48. }
  49. func waitForClosedPortUDP(p *Proxier, proxyPort int) error {
  50. for i := 0; i < 50; i++ {
  51. conn, err := net.Dial("udp", joinHostPort("", proxyPort))
  52. if err != nil {
  53. return nil
  54. }
  55. conn.SetReadDeadline(time.Now().Add(10 * time.Millisecond))
  56. // To detect a closed UDP port write, then read.
  57. _, err = conn.Write([]byte("x"))
  58. if err != nil {
  59. if e, ok := err.(net.Error); ok && !e.Timeout() {
  60. return nil
  61. }
  62. }
  63. var buf [4]byte
  64. _, err = conn.Read(buf[0:])
  65. if err != nil {
  66. if e, ok := err.(net.Error); ok && !e.Timeout() {
  67. return nil
  68. }
  69. }
  70. conn.Close()
  71. time.Sleep(1 * time.Millisecond)
  72. }
  73. return fmt.Errorf("port %d still open", proxyPort)
  74. }
  75. var tcpServerPort int32
  76. var udpServerPort int32
  77. func TestMain(m *testing.M) {
  78. // Don't handle panics
  79. runtime.ReallyCrash = true
  80. // TCP setup.
  81. tcp := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  82. w.WriteHeader(http.StatusOK)
  83. w.Write([]byte(r.URL.Path[1:]))
  84. }))
  85. defer tcp.Close()
  86. u, err := url.Parse(tcp.URL)
  87. if err != nil {
  88. panic(fmt.Sprintf("failed to parse: %v", err))
  89. }
  90. _, port, err := net.SplitHostPort(u.Host)
  91. if err != nil {
  92. panic(fmt.Sprintf("failed to parse: %v", err))
  93. }
  94. tcpServerPortValue, err := strconv.Atoi(port)
  95. if err != nil {
  96. panic(fmt.Sprintf("failed to atoi(%s): %v", port, err))
  97. }
  98. tcpServerPort = int32(tcpServerPortValue)
  99. // UDP setup.
  100. udp, err := newUDPEchoServer()
  101. if err != nil {
  102. panic(fmt.Sprintf("failed to make a UDP server: %v", err))
  103. }
  104. _, port, err = net.SplitHostPort(udp.LocalAddr().String())
  105. if err != nil {
  106. panic(fmt.Sprintf("failed to parse: %v", err))
  107. }
  108. udpServerPortValue, err := strconv.Atoi(port)
  109. if err != nil {
  110. panic(fmt.Sprintf("failed to atoi(%s): %v", port, err))
  111. }
  112. udpServerPort = int32(udpServerPortValue)
  113. go udp.Loop()
  114. ret := m.Run()
  115. // it should be safe to call Close() multiple times.
  116. tcp.Close()
  117. os.Exit(ret)
  118. }
  119. func testEchoTCP(t *testing.T, address string, port int) {
  120. path := "aaaaa"
  121. res, err := http.Get("http://" + address + ":" + fmt.Sprintf("%d", port) + "/" + path)
  122. if err != nil {
  123. t.Fatalf("error connecting to server: %v", err)
  124. }
  125. defer res.Body.Close()
  126. data, err := ioutil.ReadAll(res.Body)
  127. if err != nil {
  128. t.Errorf("error reading data: %v %v", err, string(data))
  129. }
  130. if string(data) != path {
  131. t.Errorf("expected: %s, got %s", path, string(data))
  132. }
  133. }
  134. func testEchoUDP(t *testing.T, address string, port int) {
  135. data := "abc123"
  136. conn, err := net.Dial("udp", joinHostPort(address, port))
  137. if err != nil {
  138. t.Fatalf("error connecting to server: %v", err)
  139. }
  140. if _, err := conn.Write([]byte(data)); err != nil {
  141. t.Fatalf("error sending to server: %v", err)
  142. }
  143. var resp [1024]byte
  144. n, err := conn.Read(resp[0:])
  145. if err != nil {
  146. t.Errorf("error receiving data: %v", err)
  147. }
  148. if string(resp[0:n]) != data {
  149. t.Errorf("expected: %s, got %s", data, string(resp[0:n]))
  150. }
  151. }
  152. func waitForNumProxyLoops(t *testing.T, p *Proxier, want int32) {
  153. var got int32
  154. for i := 0; i < 600; i++ {
  155. got = atomic.LoadInt32(&p.numProxyLoops)
  156. if got == want {
  157. return
  158. }
  159. time.Sleep(100 * time.Millisecond)
  160. }
  161. t.Errorf("expected %d ProxyLoops running, got %d", want, got)
  162. }
  163. func waitForNumProxyClients(t *testing.T, s *serviceInfo, want int, timeout time.Duration) {
  164. var got int
  165. now := time.Now()
  166. deadline := now.Add(timeout)
  167. for time.Now().Before(deadline) {
  168. s.activeClients.mu.Lock()
  169. got = len(s.activeClients.clients)
  170. s.activeClients.mu.Unlock()
  171. if got == want {
  172. return
  173. }
  174. time.Sleep(500 * time.Millisecond)
  175. }
  176. t.Errorf("expected %d ProxyClients live, got %d", want, got)
  177. }
  178. func TestTCPProxy(t *testing.T) {
  179. lb := NewLoadBalancerRR()
  180. service := proxy.ServicePortName{NamespacedName: types.NamespacedName{Namespace: "testnamespace", Name: "echo"}, Port: "p"}
  181. lb.OnEndpointsUpdate([]api.Endpoints{
  182. {
  183. ObjectMeta: api.ObjectMeta{Name: service.Name, Namespace: service.Namespace},
  184. Subsets: []api.EndpointSubset{{
  185. Addresses: []api.EndpointAddress{{IP: "127.0.0.1"}},
  186. Ports: []api.EndpointPort{{Name: "p", Port: tcpServerPort}},
  187. }},
  188. },
  189. })
  190. p, err := createProxier(lb, net.ParseIP("0.0.0.0"), ipttest.NewFake(), net.ParseIP("127.0.0.1"), nil, time.Minute, udpIdleTimeoutForTest)
  191. if err != nil {
  192. t.Fatal(err)
  193. }
  194. waitForNumProxyLoops(t, p, 0)
  195. svcInfo, err := p.addServiceOnPort(service, "TCP", 0, time.Second)
  196. if err != nil {
  197. t.Fatalf("error adding new service: %#v", err)
  198. }
  199. testEchoTCP(t, "127.0.0.1", svcInfo.proxyPort)
  200. waitForNumProxyLoops(t, p, 1)
  201. }
  202. func TestUDPProxy(t *testing.T) {
  203. lb := NewLoadBalancerRR()
  204. service := proxy.ServicePortName{NamespacedName: types.NamespacedName{Namespace: "testnamespace", Name: "echo"}, Port: "p"}
  205. lb.OnEndpointsUpdate([]api.Endpoints{
  206. {
  207. ObjectMeta: api.ObjectMeta{Name: service.Name, Namespace: service.Namespace},
  208. Subsets: []api.EndpointSubset{{
  209. Addresses: []api.EndpointAddress{{IP: "127.0.0.1"}},
  210. Ports: []api.EndpointPort{{Name: "p", Port: udpServerPort}},
  211. }},
  212. },
  213. })
  214. p, err := createProxier(lb, net.ParseIP("0.0.0.0"), ipttest.NewFake(), net.ParseIP("127.0.0.1"), nil, time.Minute, udpIdleTimeoutForTest)
  215. if err != nil {
  216. t.Fatal(err)
  217. }
  218. waitForNumProxyLoops(t, p, 0)
  219. svcInfo, err := p.addServiceOnPort(service, "UDP", 0, time.Second)
  220. if err != nil {
  221. t.Fatalf("error adding new service: %#v", err)
  222. }
  223. testEchoUDP(t, "127.0.0.1", svcInfo.proxyPort)
  224. waitForNumProxyLoops(t, p, 1)
  225. }
  226. func TestUDPProxyTimeout(t *testing.T) {
  227. lb := NewLoadBalancerRR()
  228. service := proxy.ServicePortName{NamespacedName: types.NamespacedName{Namespace: "testnamespace", Name: "echo"}, Port: "p"}
  229. lb.OnEndpointsUpdate([]api.Endpoints{
  230. {
  231. ObjectMeta: api.ObjectMeta{Name: service.Name, Namespace: service.Namespace},
  232. Subsets: []api.EndpointSubset{{
  233. Addresses: []api.EndpointAddress{{IP: "127.0.0.1"}},
  234. Ports: []api.EndpointPort{{Name: "p", Port: udpServerPort}},
  235. }},
  236. },
  237. })
  238. p, err := createProxier(lb, net.ParseIP("0.0.0.0"), ipttest.NewFake(), net.ParseIP("127.0.0.1"), nil, time.Minute, udpIdleTimeoutForTest)
  239. if err != nil {
  240. t.Fatal(err)
  241. }
  242. waitForNumProxyLoops(t, p, 0)
  243. svcInfo, err := p.addServiceOnPort(service, "UDP", 0, time.Second)
  244. if err != nil {
  245. t.Fatalf("error adding new service: %#v", err)
  246. }
  247. waitForNumProxyLoops(t, p, 1)
  248. testEchoUDP(t, "127.0.0.1", svcInfo.proxyPort)
  249. // When connecting to a UDP service endpoint, there should be a Conn for proxy.
  250. waitForNumProxyClients(t, svcInfo, 1, time.Second)
  251. // If conn has no activity for serviceInfo.timeout since last Read/Write, it should be closed because of timeout.
  252. waitForNumProxyClients(t, svcInfo, 0, 2*time.Second)
  253. }
  254. func TestMultiPortProxy(t *testing.T) {
  255. lb := NewLoadBalancerRR()
  256. serviceP := proxy.ServicePortName{NamespacedName: types.NamespacedName{Namespace: "testnamespace", Name: "echo-p"}, Port: "p"}
  257. serviceQ := proxy.ServicePortName{NamespacedName: types.NamespacedName{Namespace: "testnamespace", Name: "echo-q"}, Port: "q"}
  258. lb.OnEndpointsUpdate([]api.Endpoints{{
  259. ObjectMeta: api.ObjectMeta{Name: serviceP.Name, Namespace: serviceP.Namespace},
  260. Subsets: []api.EndpointSubset{{
  261. Addresses: []api.EndpointAddress{{IP: "127.0.0.1"}},
  262. Ports: []api.EndpointPort{{Name: "p", Protocol: "TCP", Port: tcpServerPort}},
  263. }},
  264. }, {
  265. ObjectMeta: api.ObjectMeta{Name: serviceQ.Name, Namespace: serviceQ.Namespace},
  266. Subsets: []api.EndpointSubset{{
  267. Addresses: []api.EndpointAddress{{IP: "127.0.0.1"}},
  268. Ports: []api.EndpointPort{{Name: "q", Protocol: "UDP", Port: udpServerPort}},
  269. }},
  270. }})
  271. p, err := createProxier(lb, net.ParseIP("0.0.0.0"), ipttest.NewFake(), net.ParseIP("127.0.0.1"), nil, time.Minute, udpIdleTimeoutForTest)
  272. if err != nil {
  273. t.Fatal(err)
  274. }
  275. waitForNumProxyLoops(t, p, 0)
  276. svcInfoP, err := p.addServiceOnPort(serviceP, "TCP", 0, time.Second)
  277. if err != nil {
  278. t.Fatalf("error adding new service: %#v", err)
  279. }
  280. testEchoTCP(t, "127.0.0.1", svcInfoP.proxyPort)
  281. waitForNumProxyLoops(t, p, 1)
  282. svcInfoQ, err := p.addServiceOnPort(serviceQ, "UDP", 0, time.Second)
  283. if err != nil {
  284. t.Fatalf("error adding new service: %#v", err)
  285. }
  286. testEchoUDP(t, "127.0.0.1", svcInfoQ.proxyPort)
  287. waitForNumProxyLoops(t, p, 2)
  288. }
  289. func TestMultiPortOnServiceUpdate(t *testing.T) {
  290. lb := NewLoadBalancerRR()
  291. serviceP := proxy.ServicePortName{NamespacedName: types.NamespacedName{Namespace: "testnamespace", Name: "echo"}, Port: "p"}
  292. serviceQ := proxy.ServicePortName{NamespacedName: types.NamespacedName{Namespace: "testnamespace", Name: "echo"}, Port: "q"}
  293. serviceX := proxy.ServicePortName{NamespacedName: types.NamespacedName{Namespace: "testnamespace", Name: "echo"}, Port: "x"}
  294. p, err := createProxier(lb, net.ParseIP("0.0.0.0"), ipttest.NewFake(), net.ParseIP("127.0.0.1"), nil, time.Minute, udpIdleTimeoutForTest)
  295. if err != nil {
  296. t.Fatal(err)
  297. }
  298. waitForNumProxyLoops(t, p, 0)
  299. p.OnServiceUpdate([]api.Service{{
  300. ObjectMeta: api.ObjectMeta{Name: serviceP.Name, Namespace: serviceP.Namespace},
  301. Spec: api.ServiceSpec{ClusterIP: "1.2.3.4", Ports: []api.ServicePort{{
  302. Name: "p",
  303. Port: 80,
  304. Protocol: "TCP",
  305. }, {
  306. Name: "q",
  307. Port: 81,
  308. Protocol: "UDP",
  309. }}},
  310. }})
  311. waitForNumProxyLoops(t, p, 2)
  312. svcInfo, exists := p.getServiceInfo(serviceP)
  313. if !exists {
  314. t.Fatalf("can't find serviceInfo for %s", serviceP)
  315. }
  316. if svcInfo.portal.ip.String() != "1.2.3.4" || svcInfo.portal.port != 80 || svcInfo.protocol != "TCP" {
  317. t.Errorf("unexpected serviceInfo for %s: %#v", serviceP, svcInfo)
  318. }
  319. svcInfo, exists = p.getServiceInfo(serviceQ)
  320. if !exists {
  321. t.Fatalf("can't find serviceInfo for %s", serviceQ)
  322. }
  323. if svcInfo.portal.ip.String() != "1.2.3.4" || svcInfo.portal.port != 81 || svcInfo.protocol != "UDP" {
  324. t.Errorf("unexpected serviceInfo for %s: %#v", serviceQ, svcInfo)
  325. }
  326. svcInfo, exists = p.getServiceInfo(serviceX)
  327. if exists {
  328. t.Fatalf("found unwanted serviceInfo for %s: %#v", serviceX, svcInfo)
  329. }
  330. }
  331. // Helper: Stops the proxy for the named service.
  332. func stopProxyByName(proxier *Proxier, service proxy.ServicePortName) error {
  333. info, found := proxier.getServiceInfo(service)
  334. if !found {
  335. return fmt.Errorf("unknown service: %s", service)
  336. }
  337. return proxier.stopProxy(service, info)
  338. }
  339. func TestTCPProxyStop(t *testing.T) {
  340. lb := NewLoadBalancerRR()
  341. service := proxy.ServicePortName{NamespacedName: types.NamespacedName{Namespace: "testnamespace", Name: "echo"}, Port: "p"}
  342. lb.OnEndpointsUpdate([]api.Endpoints{
  343. {
  344. ObjectMeta: api.ObjectMeta{Namespace: service.Namespace, Name: service.Name},
  345. Subsets: []api.EndpointSubset{{
  346. Addresses: []api.EndpointAddress{{IP: "127.0.0.1"}},
  347. Ports: []api.EndpointPort{{Name: "p", Port: tcpServerPort}},
  348. }},
  349. },
  350. })
  351. p, err := createProxier(lb, net.ParseIP("0.0.0.0"), ipttest.NewFake(), net.ParseIP("127.0.0.1"), nil, time.Minute, udpIdleTimeoutForTest)
  352. if err != nil {
  353. t.Fatal(err)
  354. }
  355. waitForNumProxyLoops(t, p, 0)
  356. svcInfo, err := p.addServiceOnPort(service, "TCP", 0, time.Second)
  357. if err != nil {
  358. t.Fatalf("error adding new service: %#v", err)
  359. }
  360. if !svcInfo.isAlive() {
  361. t.Fatalf("wrong value for isAlive(): expected true")
  362. }
  363. conn, err := net.Dial("tcp", joinHostPort("", svcInfo.proxyPort))
  364. if err != nil {
  365. t.Fatalf("error connecting to proxy: %v", err)
  366. }
  367. conn.Close()
  368. waitForNumProxyLoops(t, p, 1)
  369. stopProxyByName(p, service)
  370. if svcInfo.isAlive() {
  371. t.Fatalf("wrong value for isAlive(): expected false")
  372. }
  373. // Wait for the port to really close.
  374. if err := waitForClosedPortTCP(p, svcInfo.proxyPort); err != nil {
  375. t.Fatalf(err.Error())
  376. }
  377. waitForNumProxyLoops(t, p, 0)
  378. }
  379. func TestUDPProxyStop(t *testing.T) {
  380. lb := NewLoadBalancerRR()
  381. service := proxy.ServicePortName{NamespacedName: types.NamespacedName{Namespace: "testnamespace", Name: "echo"}, Port: "p"}
  382. lb.OnEndpointsUpdate([]api.Endpoints{
  383. {
  384. ObjectMeta: api.ObjectMeta{Namespace: service.Namespace, Name: service.Name},
  385. Subsets: []api.EndpointSubset{{
  386. Addresses: []api.EndpointAddress{{IP: "127.0.0.1"}},
  387. Ports: []api.EndpointPort{{Name: "p", Port: udpServerPort}},
  388. }},
  389. },
  390. })
  391. p, err := createProxier(lb, net.ParseIP("0.0.0.0"), ipttest.NewFake(), net.ParseIP("127.0.0.1"), nil, time.Minute, udpIdleTimeoutForTest)
  392. if err != nil {
  393. t.Fatal(err)
  394. }
  395. waitForNumProxyLoops(t, p, 0)
  396. svcInfo, err := p.addServiceOnPort(service, "UDP", 0, time.Second)
  397. if err != nil {
  398. t.Fatalf("error adding new service: %#v", err)
  399. }
  400. conn, err := net.Dial("udp", joinHostPort("", svcInfo.proxyPort))
  401. if err != nil {
  402. t.Fatalf("error connecting to proxy: %v", err)
  403. }
  404. conn.Close()
  405. waitForNumProxyLoops(t, p, 1)
  406. stopProxyByName(p, service)
  407. // Wait for the port to really close.
  408. if err := waitForClosedPortUDP(p, svcInfo.proxyPort); err != nil {
  409. t.Fatalf(err.Error())
  410. }
  411. waitForNumProxyLoops(t, p, 0)
  412. }
  413. func TestTCPProxyUpdateDelete(t *testing.T) {
  414. lb := NewLoadBalancerRR()
  415. service := proxy.ServicePortName{NamespacedName: types.NamespacedName{Namespace: "testnamespace", Name: "echo"}, Port: "p"}
  416. lb.OnEndpointsUpdate([]api.Endpoints{
  417. {
  418. ObjectMeta: api.ObjectMeta{Namespace: service.Namespace, Name: service.Name},
  419. Subsets: []api.EndpointSubset{{
  420. Addresses: []api.EndpointAddress{{IP: "127.0.0.1"}},
  421. Ports: []api.EndpointPort{{Name: "p", Port: tcpServerPort}},
  422. }},
  423. },
  424. })
  425. p, err := createProxier(lb, net.ParseIP("0.0.0.0"), ipttest.NewFake(), net.ParseIP("127.0.0.1"), nil, time.Minute, udpIdleTimeoutForTest)
  426. if err != nil {
  427. t.Fatal(err)
  428. }
  429. waitForNumProxyLoops(t, p, 0)
  430. svcInfo, err := p.addServiceOnPort(service, "TCP", 0, time.Second)
  431. if err != nil {
  432. t.Fatalf("error adding new service: %#v", err)
  433. }
  434. conn, err := net.Dial("tcp", joinHostPort("", svcInfo.proxyPort))
  435. if err != nil {
  436. t.Fatalf("error connecting to proxy: %v", err)
  437. }
  438. conn.Close()
  439. waitForNumProxyLoops(t, p, 1)
  440. p.OnServiceUpdate([]api.Service{})
  441. if err := waitForClosedPortTCP(p, svcInfo.proxyPort); err != nil {
  442. t.Fatalf(err.Error())
  443. }
  444. waitForNumProxyLoops(t, p, 0)
  445. }
  446. func TestUDPProxyUpdateDelete(t *testing.T) {
  447. lb := NewLoadBalancerRR()
  448. service := proxy.ServicePortName{NamespacedName: types.NamespacedName{Namespace: "testnamespace", Name: "echo"}, Port: "p"}
  449. lb.OnEndpointsUpdate([]api.Endpoints{
  450. {
  451. ObjectMeta: api.ObjectMeta{Namespace: service.Namespace, Name: service.Name},
  452. Subsets: []api.EndpointSubset{{
  453. Addresses: []api.EndpointAddress{{IP: "127.0.0.1"}},
  454. Ports: []api.EndpointPort{{Name: "p", Port: udpServerPort}},
  455. }},
  456. },
  457. })
  458. p, err := createProxier(lb, net.ParseIP("0.0.0.0"), ipttest.NewFake(), net.ParseIP("127.0.0.1"), nil, time.Minute, udpIdleTimeoutForTest)
  459. if err != nil {
  460. t.Fatal(err)
  461. }
  462. waitForNumProxyLoops(t, p, 0)
  463. svcInfo, err := p.addServiceOnPort(service, "UDP", 0, time.Second)
  464. if err != nil {
  465. t.Fatalf("error adding new service: %#v", err)
  466. }
  467. conn, err := net.Dial("udp", joinHostPort("", svcInfo.proxyPort))
  468. if err != nil {
  469. t.Fatalf("error connecting to proxy: %v", err)
  470. }
  471. conn.Close()
  472. waitForNumProxyLoops(t, p, 1)
  473. p.OnServiceUpdate([]api.Service{})
  474. if err := waitForClosedPortUDP(p, svcInfo.proxyPort); err != nil {
  475. t.Fatalf(err.Error())
  476. }
  477. waitForNumProxyLoops(t, p, 0)
  478. }
  479. func TestTCPProxyUpdateDeleteUpdate(t *testing.T) {
  480. lb := NewLoadBalancerRR()
  481. service := proxy.ServicePortName{NamespacedName: types.NamespacedName{Namespace: "testnamespace", Name: "echo"}, Port: "p"}
  482. endpoint := api.Endpoints{
  483. ObjectMeta: api.ObjectMeta{Name: service.Name, Namespace: service.Namespace},
  484. Subsets: []api.EndpointSubset{{
  485. Addresses: []api.EndpointAddress{{IP: "127.0.0.1"}},
  486. Ports: []api.EndpointPort{{Name: "p", Port: tcpServerPort}},
  487. }},
  488. }
  489. lb.OnEndpointsUpdate([]api.Endpoints{endpoint})
  490. p, err := createProxier(lb, net.ParseIP("0.0.0.0"), ipttest.NewFake(), net.ParseIP("127.0.0.1"), nil, time.Minute, udpIdleTimeoutForTest)
  491. if err != nil {
  492. t.Fatal(err)
  493. }
  494. waitForNumProxyLoops(t, p, 0)
  495. svcInfo, err := p.addServiceOnPort(service, "TCP", 0, time.Second)
  496. if err != nil {
  497. t.Fatalf("error adding new service: %#v", err)
  498. }
  499. conn, err := net.Dial("tcp", joinHostPort("", svcInfo.proxyPort))
  500. if err != nil {
  501. t.Fatalf("error connecting to proxy: %v", err)
  502. }
  503. conn.Close()
  504. waitForNumProxyLoops(t, p, 1)
  505. p.OnServiceUpdate([]api.Service{})
  506. if err := waitForClosedPortTCP(p, svcInfo.proxyPort); err != nil {
  507. t.Fatalf(err.Error())
  508. }
  509. waitForNumProxyLoops(t, p, 0)
  510. // need to add endpoint here because it got clean up during service delete
  511. lb.OnEndpointsUpdate([]api.Endpoints{endpoint})
  512. p.OnServiceUpdate([]api.Service{{
  513. ObjectMeta: api.ObjectMeta{Name: service.Name, Namespace: service.Namespace},
  514. Spec: api.ServiceSpec{ClusterIP: "1.2.3.4", Ports: []api.ServicePort{{
  515. Name: "p",
  516. Port: int32(svcInfo.proxyPort),
  517. Protocol: "TCP",
  518. }}},
  519. }})
  520. svcInfo, exists := p.getServiceInfo(service)
  521. if !exists {
  522. t.Fatalf("can't find serviceInfo for %s", service)
  523. }
  524. testEchoTCP(t, "127.0.0.1", svcInfo.proxyPort)
  525. waitForNumProxyLoops(t, p, 1)
  526. }
  527. func TestUDPProxyUpdateDeleteUpdate(t *testing.T) {
  528. lb := NewLoadBalancerRR()
  529. service := proxy.ServicePortName{NamespacedName: types.NamespacedName{Namespace: "testnamespace", Name: "echo"}, Port: "p"}
  530. endpoint := api.Endpoints{
  531. ObjectMeta: api.ObjectMeta{Name: service.Name, Namespace: service.Namespace},
  532. Subsets: []api.EndpointSubset{{
  533. Addresses: []api.EndpointAddress{{IP: "127.0.0.1"}},
  534. Ports: []api.EndpointPort{{Name: "p", Port: udpServerPort}},
  535. }},
  536. }
  537. lb.OnEndpointsUpdate([]api.Endpoints{endpoint})
  538. p, err := createProxier(lb, net.ParseIP("0.0.0.0"), ipttest.NewFake(), net.ParseIP("127.0.0.1"), nil, time.Minute, udpIdleTimeoutForTest)
  539. if err != nil {
  540. t.Fatal(err)
  541. }
  542. waitForNumProxyLoops(t, p, 0)
  543. svcInfo, err := p.addServiceOnPort(service, "UDP", 0, time.Second)
  544. if err != nil {
  545. t.Fatalf("error adding new service: %#v", err)
  546. }
  547. conn, err := net.Dial("udp", joinHostPort("", svcInfo.proxyPort))
  548. if err != nil {
  549. t.Fatalf("error connecting to proxy: %v", err)
  550. }
  551. conn.Close()
  552. waitForNumProxyLoops(t, p, 1)
  553. p.OnServiceUpdate([]api.Service{})
  554. if err := waitForClosedPortUDP(p, svcInfo.proxyPort); err != nil {
  555. t.Fatalf(err.Error())
  556. }
  557. waitForNumProxyLoops(t, p, 0)
  558. // need to add endpoint here because it got clean up during service delete
  559. lb.OnEndpointsUpdate([]api.Endpoints{endpoint})
  560. p.OnServiceUpdate([]api.Service{{
  561. ObjectMeta: api.ObjectMeta{Name: service.Name, Namespace: service.Namespace},
  562. Spec: api.ServiceSpec{ClusterIP: "1.2.3.4", Ports: []api.ServicePort{{
  563. Name: "p",
  564. Port: int32(svcInfo.proxyPort),
  565. Protocol: "UDP",
  566. }}},
  567. }})
  568. svcInfo, exists := p.getServiceInfo(service)
  569. if !exists {
  570. t.Fatalf("can't find serviceInfo")
  571. }
  572. testEchoUDP(t, "127.0.0.1", svcInfo.proxyPort)
  573. waitForNumProxyLoops(t, p, 1)
  574. }
  575. func TestTCPProxyUpdatePort(t *testing.T) {
  576. lb := NewLoadBalancerRR()
  577. service := proxy.ServicePortName{NamespacedName: types.NamespacedName{Namespace: "testnamespace", Name: "echo"}, Port: "p"}
  578. lb.OnEndpointsUpdate([]api.Endpoints{
  579. {
  580. ObjectMeta: api.ObjectMeta{Name: service.Name, Namespace: service.Namespace},
  581. Subsets: []api.EndpointSubset{{
  582. Addresses: []api.EndpointAddress{{IP: "127.0.0.1"}},
  583. Ports: []api.EndpointPort{{Name: "p", Port: tcpServerPort}},
  584. }},
  585. },
  586. })
  587. p, err := createProxier(lb, net.ParseIP("0.0.0.0"), ipttest.NewFake(), net.ParseIP("127.0.0.1"), nil, time.Minute, udpIdleTimeoutForTest)
  588. if err != nil {
  589. t.Fatal(err)
  590. }
  591. waitForNumProxyLoops(t, p, 0)
  592. svcInfo, err := p.addServiceOnPort(service, "TCP", 0, time.Second)
  593. if err != nil {
  594. t.Fatalf("error adding new service: %#v", err)
  595. }
  596. testEchoTCP(t, "127.0.0.1", svcInfo.proxyPort)
  597. waitForNumProxyLoops(t, p, 1)
  598. p.OnServiceUpdate([]api.Service{{
  599. ObjectMeta: api.ObjectMeta{Name: service.Name, Namespace: service.Namespace},
  600. Spec: api.ServiceSpec{ClusterIP: "1.2.3.4", Ports: []api.ServicePort{{
  601. Name: "p",
  602. Port: 99,
  603. Protocol: "TCP",
  604. }}},
  605. }})
  606. // Wait for the socket to actually get free.
  607. if err := waitForClosedPortTCP(p, svcInfo.proxyPort); err != nil {
  608. t.Fatalf(err.Error())
  609. }
  610. svcInfo, exists := p.getServiceInfo(service)
  611. if !exists {
  612. t.Fatalf("can't find serviceInfo")
  613. }
  614. testEchoTCP(t, "127.0.0.1", svcInfo.proxyPort)
  615. // This is a bit async, but this should be sufficient.
  616. time.Sleep(500 * time.Millisecond)
  617. waitForNumProxyLoops(t, p, 1)
  618. }
  619. func TestUDPProxyUpdatePort(t *testing.T) {
  620. lb := NewLoadBalancerRR()
  621. service := proxy.ServicePortName{NamespacedName: types.NamespacedName{Namespace: "testnamespace", Name: "echo"}, Port: "p"}
  622. lb.OnEndpointsUpdate([]api.Endpoints{
  623. {
  624. ObjectMeta: api.ObjectMeta{Name: service.Name, Namespace: service.Namespace},
  625. Subsets: []api.EndpointSubset{{
  626. Addresses: []api.EndpointAddress{{IP: "127.0.0.1"}},
  627. Ports: []api.EndpointPort{{Name: "p", Port: udpServerPort}},
  628. }},
  629. },
  630. })
  631. p, err := createProxier(lb, net.ParseIP("0.0.0.0"), ipttest.NewFake(), net.ParseIP("127.0.0.1"), nil, time.Minute, udpIdleTimeoutForTest)
  632. if err != nil {
  633. t.Fatal(err)
  634. }
  635. waitForNumProxyLoops(t, p, 0)
  636. svcInfo, err := p.addServiceOnPort(service, "UDP", 0, time.Second)
  637. if err != nil {
  638. t.Fatalf("error adding new service: %#v", err)
  639. }
  640. waitForNumProxyLoops(t, p, 1)
  641. p.OnServiceUpdate([]api.Service{{
  642. ObjectMeta: api.ObjectMeta{Name: service.Name, Namespace: service.Namespace},
  643. Spec: api.ServiceSpec{ClusterIP: "1.2.3.4", Ports: []api.ServicePort{{
  644. Name: "p",
  645. Port: 99,
  646. Protocol: "UDP",
  647. }}},
  648. }})
  649. // Wait for the socket to actually get free.
  650. if err := waitForClosedPortUDP(p, svcInfo.proxyPort); err != nil {
  651. t.Fatalf(err.Error())
  652. }
  653. svcInfo, exists := p.getServiceInfo(service)
  654. if !exists {
  655. t.Fatalf("can't find serviceInfo")
  656. }
  657. testEchoUDP(t, "127.0.0.1", svcInfo.proxyPort)
  658. waitForNumProxyLoops(t, p, 1)
  659. }
  660. func TestProxyUpdatePublicIPs(t *testing.T) {
  661. lb := NewLoadBalancerRR()
  662. service := proxy.ServicePortName{NamespacedName: types.NamespacedName{Namespace: "testnamespace", Name: "echo"}, Port: "p"}
  663. lb.OnEndpointsUpdate([]api.Endpoints{
  664. {
  665. ObjectMeta: api.ObjectMeta{Name: service.Name, Namespace: service.Namespace},
  666. Subsets: []api.EndpointSubset{{
  667. Addresses: []api.EndpointAddress{{IP: "127.0.0.1"}},
  668. Ports: []api.EndpointPort{{Name: "p", Port: tcpServerPort}},
  669. }},
  670. },
  671. })
  672. p, err := createProxier(lb, net.ParseIP("0.0.0.0"), ipttest.NewFake(), net.ParseIP("127.0.0.1"), nil, time.Minute, udpIdleTimeoutForTest)
  673. if err != nil {
  674. t.Fatal(err)
  675. }
  676. waitForNumProxyLoops(t, p, 0)
  677. svcInfo, err := p.addServiceOnPort(service, "TCP", 0, time.Second)
  678. if err != nil {
  679. t.Fatalf("error adding new service: %#v", err)
  680. }
  681. testEchoTCP(t, "127.0.0.1", svcInfo.proxyPort)
  682. waitForNumProxyLoops(t, p, 1)
  683. p.OnServiceUpdate([]api.Service{{
  684. ObjectMeta: api.ObjectMeta{Name: service.Name, Namespace: service.Namespace},
  685. Spec: api.ServiceSpec{
  686. Ports: []api.ServicePort{{
  687. Name: "p",
  688. Port: int32(svcInfo.portal.port),
  689. Protocol: "TCP",
  690. }},
  691. ClusterIP: svcInfo.portal.ip.String(),
  692. ExternalIPs: []string{"4.3.2.1"},
  693. },
  694. }})
  695. // Wait for the socket to actually get free.
  696. if err := waitForClosedPortTCP(p, svcInfo.proxyPort); err != nil {
  697. t.Fatalf(err.Error())
  698. }
  699. svcInfo, exists := p.getServiceInfo(service)
  700. if !exists {
  701. t.Fatalf("can't find serviceInfo")
  702. }
  703. testEchoTCP(t, "127.0.0.1", svcInfo.proxyPort)
  704. // This is a bit async, but this should be sufficient.
  705. time.Sleep(500 * time.Millisecond)
  706. waitForNumProxyLoops(t, p, 1)
  707. }
  708. func TestProxyUpdatePortal(t *testing.T) {
  709. lb := NewLoadBalancerRR()
  710. service := proxy.ServicePortName{NamespacedName: types.NamespacedName{Namespace: "testnamespace", Name: "echo"}, Port: "p"}
  711. endpoint := api.Endpoints{
  712. ObjectMeta: api.ObjectMeta{Name: service.Name, Namespace: service.Namespace},
  713. Subsets: []api.EndpointSubset{{
  714. Addresses: []api.EndpointAddress{{IP: "127.0.0.1"}},
  715. Ports: []api.EndpointPort{{Name: "p", Port: tcpServerPort}},
  716. }},
  717. }
  718. lb.OnEndpointsUpdate([]api.Endpoints{endpoint})
  719. p, err := createProxier(lb, net.ParseIP("0.0.0.0"), ipttest.NewFake(), net.ParseIP("127.0.0.1"), nil, time.Minute, udpIdleTimeoutForTest)
  720. if err != nil {
  721. t.Fatal(err)
  722. }
  723. waitForNumProxyLoops(t, p, 0)
  724. svcInfo, err := p.addServiceOnPort(service, "TCP", 0, time.Second)
  725. if err != nil {
  726. t.Fatalf("error adding new service: %#v", err)
  727. }
  728. testEchoTCP(t, "127.0.0.1", svcInfo.proxyPort)
  729. waitForNumProxyLoops(t, p, 1)
  730. p.OnServiceUpdate([]api.Service{{
  731. ObjectMeta: api.ObjectMeta{Name: service.Name, Namespace: service.Namespace},
  732. Spec: api.ServiceSpec{ClusterIP: "", Ports: []api.ServicePort{{
  733. Name: "p",
  734. Port: int32(svcInfo.proxyPort),
  735. Protocol: "TCP",
  736. }}},
  737. }})
  738. _, exists := p.getServiceInfo(service)
  739. if exists {
  740. t.Fatalf("service with empty ClusterIP should not be included in the proxy")
  741. }
  742. p.OnServiceUpdate([]api.Service{{
  743. ObjectMeta: api.ObjectMeta{Name: service.Name, Namespace: service.Namespace},
  744. Spec: api.ServiceSpec{ClusterIP: "None", Ports: []api.ServicePort{{
  745. Name: "p",
  746. Port: int32(svcInfo.proxyPort),
  747. Protocol: "TCP",
  748. }}},
  749. }})
  750. _, exists = p.getServiceInfo(service)
  751. if exists {
  752. t.Fatalf("service with 'None' as ClusterIP should not be included in the proxy")
  753. }
  754. p.OnServiceUpdate([]api.Service{{
  755. ObjectMeta: api.ObjectMeta{Name: service.Name, Namespace: service.Namespace},
  756. Spec: api.ServiceSpec{ClusterIP: "1.2.3.4", Ports: []api.ServicePort{{
  757. Name: "p",
  758. Port: int32(svcInfo.proxyPort),
  759. Protocol: "TCP",
  760. }}},
  761. }})
  762. lb.OnEndpointsUpdate([]api.Endpoints{endpoint})
  763. svcInfo, exists = p.getServiceInfo(service)
  764. if !exists {
  765. t.Fatalf("service with ClusterIP set not found in the proxy")
  766. }
  767. testEchoTCP(t, "127.0.0.1", svcInfo.proxyPort)
  768. waitForNumProxyLoops(t, p, 1)
  769. }
  770. // TODO(justinsb): Add test for nodePort conflict detection, once we have nodePort wired in