proxier_test.go 34 KB

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