controller_test.go 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  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 master
  14. import (
  15. "net"
  16. "reflect"
  17. "testing"
  18. corev1 "k8s.io/api/core/v1"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. "k8s.io/apimachinery/pkg/util/intstr"
  21. "k8s.io/client-go/kubernetes/fake"
  22. core "k8s.io/client-go/testing"
  23. "k8s.io/kubernetes/pkg/master/reconcilers"
  24. )
  25. func TestReconcileEndpoints(t *testing.T) {
  26. ns := metav1.NamespaceDefault
  27. om := func(name string) metav1.ObjectMeta {
  28. return metav1.ObjectMeta{Namespace: ns, Name: name}
  29. }
  30. reconcileTests := []struct {
  31. testName string
  32. serviceName string
  33. ip string
  34. endpointPorts []corev1.EndpointPort
  35. additionalMasters int
  36. endpoints *corev1.EndpointsList
  37. expectUpdate *corev1.Endpoints // nil means none expected
  38. expectCreate *corev1.Endpoints // nil means none expected
  39. }{
  40. {
  41. testName: "no existing endpoints",
  42. serviceName: "foo",
  43. ip: "1.2.3.4",
  44. endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
  45. endpoints: nil,
  46. expectCreate: &corev1.Endpoints{
  47. ObjectMeta: om("foo"),
  48. Subsets: []corev1.EndpointSubset{{
  49. Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
  50. Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
  51. }},
  52. },
  53. },
  54. {
  55. testName: "existing endpoints satisfy",
  56. serviceName: "foo",
  57. ip: "1.2.3.4",
  58. endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
  59. endpoints: &corev1.EndpointsList{
  60. Items: []corev1.Endpoints{{
  61. ObjectMeta: om("foo"),
  62. Subsets: []corev1.EndpointSubset{{
  63. Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
  64. Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
  65. }},
  66. }},
  67. },
  68. },
  69. {
  70. testName: "existing endpoints satisfy but too many",
  71. serviceName: "foo",
  72. ip: "1.2.3.4",
  73. endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
  74. endpoints: &corev1.EndpointsList{
  75. Items: []corev1.Endpoints{{
  76. ObjectMeta: om("foo"),
  77. Subsets: []corev1.EndpointSubset{{
  78. Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}, {IP: "4.3.2.1"}},
  79. Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
  80. }},
  81. }},
  82. },
  83. expectUpdate: &corev1.Endpoints{
  84. ObjectMeta: om("foo"),
  85. Subsets: []corev1.EndpointSubset{{
  86. Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
  87. Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
  88. }},
  89. },
  90. },
  91. {
  92. testName: "existing endpoints satisfy but too many + extra masters",
  93. serviceName: "foo",
  94. ip: "1.2.3.4",
  95. endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
  96. additionalMasters: 3,
  97. endpoints: &corev1.EndpointsList{
  98. Items: []corev1.Endpoints{{
  99. ObjectMeta: om("foo"),
  100. Subsets: []corev1.EndpointSubset{{
  101. Addresses: []corev1.EndpointAddress{
  102. {IP: "1.2.3.4"},
  103. {IP: "4.3.2.1"},
  104. {IP: "4.3.2.2"},
  105. {IP: "4.3.2.3"},
  106. {IP: "4.3.2.4"},
  107. },
  108. Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
  109. }},
  110. }},
  111. },
  112. expectUpdate: &corev1.Endpoints{
  113. ObjectMeta: om("foo"),
  114. Subsets: []corev1.EndpointSubset{{
  115. Addresses: []corev1.EndpointAddress{
  116. {IP: "1.2.3.4"},
  117. {IP: "4.3.2.2"},
  118. {IP: "4.3.2.3"},
  119. {IP: "4.3.2.4"},
  120. },
  121. Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
  122. }},
  123. },
  124. },
  125. {
  126. testName: "existing endpoints satisfy but too many + extra masters + delete first",
  127. serviceName: "foo",
  128. ip: "4.3.2.4",
  129. endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
  130. additionalMasters: 3,
  131. endpoints: &corev1.EndpointsList{
  132. Items: []corev1.Endpoints{{
  133. ObjectMeta: om("foo"),
  134. Subsets: []corev1.EndpointSubset{{
  135. Addresses: []corev1.EndpointAddress{
  136. {IP: "1.2.3.4"},
  137. {IP: "4.3.2.1"},
  138. {IP: "4.3.2.2"},
  139. {IP: "4.3.2.3"},
  140. {IP: "4.3.2.4"},
  141. },
  142. Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
  143. }},
  144. }},
  145. },
  146. expectUpdate: &corev1.Endpoints{
  147. ObjectMeta: om("foo"),
  148. Subsets: []corev1.EndpointSubset{{
  149. Addresses: []corev1.EndpointAddress{
  150. {IP: "4.3.2.1"},
  151. {IP: "4.3.2.2"},
  152. {IP: "4.3.2.3"},
  153. {IP: "4.3.2.4"},
  154. },
  155. Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
  156. }},
  157. },
  158. },
  159. {
  160. testName: "existing endpoints satisfy and endpoint addresses length less than master count",
  161. serviceName: "foo",
  162. ip: "4.3.2.2",
  163. endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
  164. additionalMasters: 3,
  165. endpoints: &corev1.EndpointsList{
  166. Items: []corev1.Endpoints{{
  167. ObjectMeta: om("foo"),
  168. Subsets: []corev1.EndpointSubset{{
  169. Addresses: []corev1.EndpointAddress{
  170. {IP: "4.3.2.1"},
  171. {IP: "4.3.2.2"},
  172. },
  173. Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
  174. }},
  175. }},
  176. },
  177. expectUpdate: nil,
  178. },
  179. {
  180. testName: "existing endpoints current IP missing and address length less than master count",
  181. serviceName: "foo",
  182. ip: "4.3.2.2",
  183. endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
  184. additionalMasters: 3,
  185. endpoints: &corev1.EndpointsList{
  186. Items: []corev1.Endpoints{{
  187. ObjectMeta: om("foo"),
  188. Subsets: []corev1.EndpointSubset{{
  189. Addresses: []corev1.EndpointAddress{
  190. {IP: "4.3.2.1"},
  191. },
  192. Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
  193. }},
  194. }},
  195. },
  196. expectUpdate: &corev1.Endpoints{
  197. ObjectMeta: om("foo"),
  198. Subsets: []corev1.EndpointSubset{{
  199. Addresses: []corev1.EndpointAddress{
  200. {IP: "4.3.2.1"},
  201. {IP: "4.3.2.2"},
  202. },
  203. Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
  204. }},
  205. },
  206. },
  207. {
  208. testName: "existing endpoints wrong name",
  209. serviceName: "foo",
  210. ip: "1.2.3.4",
  211. endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
  212. endpoints: &corev1.EndpointsList{
  213. Items: []corev1.Endpoints{{
  214. ObjectMeta: om("bar"),
  215. Subsets: []corev1.EndpointSubset{{
  216. Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
  217. Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
  218. }},
  219. }},
  220. },
  221. expectCreate: &corev1.Endpoints{
  222. ObjectMeta: om("foo"),
  223. Subsets: []corev1.EndpointSubset{{
  224. Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
  225. Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
  226. }},
  227. },
  228. },
  229. {
  230. testName: "existing endpoints wrong IP",
  231. serviceName: "foo",
  232. ip: "1.2.3.4",
  233. endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
  234. endpoints: &corev1.EndpointsList{
  235. Items: []corev1.Endpoints{{
  236. ObjectMeta: om("foo"),
  237. Subsets: []corev1.EndpointSubset{{
  238. Addresses: []corev1.EndpointAddress{{IP: "4.3.2.1"}},
  239. Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
  240. }},
  241. }},
  242. },
  243. expectUpdate: &corev1.Endpoints{
  244. ObjectMeta: om("foo"),
  245. Subsets: []corev1.EndpointSubset{{
  246. Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
  247. Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
  248. }},
  249. },
  250. },
  251. {
  252. testName: "existing endpoints wrong port",
  253. serviceName: "foo",
  254. ip: "1.2.3.4",
  255. endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
  256. endpoints: &corev1.EndpointsList{
  257. Items: []corev1.Endpoints{{
  258. ObjectMeta: om("foo"),
  259. Subsets: []corev1.EndpointSubset{{
  260. Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
  261. Ports: []corev1.EndpointPort{{Name: "foo", Port: 9090, Protocol: "TCP"}},
  262. }},
  263. }},
  264. },
  265. expectUpdate: &corev1.Endpoints{
  266. ObjectMeta: om("foo"),
  267. Subsets: []corev1.EndpointSubset{{
  268. Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
  269. Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
  270. }},
  271. },
  272. },
  273. {
  274. testName: "existing endpoints wrong protocol",
  275. serviceName: "foo",
  276. ip: "1.2.3.4",
  277. endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
  278. endpoints: &corev1.EndpointsList{
  279. Items: []corev1.Endpoints{{
  280. ObjectMeta: om("foo"),
  281. Subsets: []corev1.EndpointSubset{{
  282. Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
  283. Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "UDP"}},
  284. }},
  285. }},
  286. },
  287. expectUpdate: &corev1.Endpoints{
  288. ObjectMeta: om("foo"),
  289. Subsets: []corev1.EndpointSubset{{
  290. Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
  291. Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
  292. }},
  293. },
  294. },
  295. {
  296. testName: "existing endpoints wrong port name",
  297. serviceName: "foo",
  298. ip: "1.2.3.4",
  299. endpointPorts: []corev1.EndpointPort{{Name: "baz", Port: 8080, Protocol: "TCP"}},
  300. endpoints: &corev1.EndpointsList{
  301. Items: []corev1.Endpoints{{
  302. ObjectMeta: om("foo"),
  303. Subsets: []corev1.EndpointSubset{{
  304. Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
  305. Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
  306. }},
  307. }},
  308. },
  309. expectUpdate: &corev1.Endpoints{
  310. ObjectMeta: om("foo"),
  311. Subsets: []corev1.EndpointSubset{{
  312. Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
  313. Ports: []corev1.EndpointPort{{Name: "baz", Port: 8080, Protocol: "TCP"}},
  314. }},
  315. },
  316. },
  317. {
  318. testName: "existing endpoints extra service ports satisfy",
  319. serviceName: "foo",
  320. ip: "1.2.3.4",
  321. endpointPorts: []corev1.EndpointPort{
  322. {Name: "foo", Port: 8080, Protocol: "TCP"},
  323. {Name: "bar", Port: 1000, Protocol: "TCP"},
  324. {Name: "baz", Port: 1010, Protocol: "TCP"},
  325. },
  326. endpoints: &corev1.EndpointsList{
  327. Items: []corev1.Endpoints{{
  328. ObjectMeta: om("foo"),
  329. Subsets: []corev1.EndpointSubset{{
  330. Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
  331. Ports: []corev1.EndpointPort{
  332. {Name: "foo", Port: 8080, Protocol: "TCP"},
  333. {Name: "bar", Port: 1000, Protocol: "TCP"},
  334. {Name: "baz", Port: 1010, Protocol: "TCP"},
  335. },
  336. }},
  337. }},
  338. },
  339. },
  340. {
  341. testName: "existing endpoints extra service ports missing port",
  342. serviceName: "foo",
  343. ip: "1.2.3.4",
  344. endpointPorts: []corev1.EndpointPort{
  345. {Name: "foo", Port: 8080, Protocol: "TCP"},
  346. {Name: "bar", Port: 1000, Protocol: "TCP"},
  347. },
  348. endpoints: &corev1.EndpointsList{
  349. Items: []corev1.Endpoints{{
  350. ObjectMeta: om("foo"),
  351. Subsets: []corev1.EndpointSubset{{
  352. Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
  353. Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
  354. }},
  355. }},
  356. },
  357. expectUpdate: &corev1.Endpoints{
  358. ObjectMeta: om("foo"),
  359. Subsets: []corev1.EndpointSubset{{
  360. Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
  361. Ports: []corev1.EndpointPort{
  362. {Name: "foo", Port: 8080, Protocol: "TCP"},
  363. {Name: "bar", Port: 1000, Protocol: "TCP"},
  364. },
  365. }},
  366. },
  367. },
  368. {
  369. testName: "no existing sctp endpoints",
  370. serviceName: "boo",
  371. ip: "1.2.3.4",
  372. endpointPorts: []corev1.EndpointPort{{Name: "boo", Port: 7777, Protocol: "SCTP"}},
  373. endpoints: nil,
  374. expectCreate: &corev1.Endpoints{
  375. ObjectMeta: om("boo"),
  376. Subsets: []corev1.EndpointSubset{{
  377. Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
  378. Ports: []corev1.EndpointPort{{Name: "boo", Port: 7777, Protocol: "SCTP"}},
  379. }},
  380. },
  381. },
  382. }
  383. for _, test := range reconcileTests {
  384. fakeClient := fake.NewSimpleClientset()
  385. if test.endpoints != nil {
  386. fakeClient = fake.NewSimpleClientset(test.endpoints)
  387. }
  388. epAdapter := reconcilers.NewEndpointsAdapter(fakeClient.CoreV1(), nil)
  389. reconciler := reconcilers.NewMasterCountEndpointReconciler(test.additionalMasters+1, epAdapter)
  390. err := reconciler.ReconcileEndpoints(test.serviceName, net.ParseIP(test.ip), test.endpointPorts, true)
  391. if err != nil {
  392. t.Errorf("case %q: unexpected error: %v", test.testName, err)
  393. }
  394. updates := []core.UpdateAction{}
  395. for _, action := range fakeClient.Actions() {
  396. if action.GetVerb() != "update" {
  397. continue
  398. }
  399. updates = append(updates, action.(core.UpdateAction))
  400. }
  401. if test.expectUpdate != nil {
  402. if len(updates) != 1 {
  403. t.Errorf("case %q: unexpected updates: %v", test.testName, updates)
  404. } else if e, a := test.expectUpdate, updates[0].GetObject(); !reflect.DeepEqual(e, a) {
  405. t.Errorf("case %q: expected update:\n%#v\ngot:\n%#v\n", test.testName, e, a)
  406. }
  407. }
  408. if test.expectUpdate == nil && len(updates) > 0 {
  409. t.Errorf("case %q: no update expected, yet saw: %v", test.testName, updates)
  410. }
  411. creates := []core.CreateAction{}
  412. for _, action := range fakeClient.Actions() {
  413. if action.GetVerb() != "create" {
  414. continue
  415. }
  416. creates = append(creates, action.(core.CreateAction))
  417. }
  418. if test.expectCreate != nil {
  419. if len(creates) != 1 {
  420. t.Errorf("case %q: unexpected creates: %v", test.testName, creates)
  421. } else if e, a := test.expectCreate, creates[0].GetObject(); !reflect.DeepEqual(e, a) {
  422. t.Errorf("case %q: expected create:\n%#v\ngot:\n%#v\n", test.testName, e, a)
  423. }
  424. }
  425. if test.expectCreate == nil && len(creates) > 0 {
  426. t.Errorf("case %q: no create expected, yet saw: %v", test.testName, creates)
  427. }
  428. }
  429. nonReconcileTests := []struct {
  430. testName string
  431. serviceName string
  432. ip string
  433. endpointPorts []corev1.EndpointPort
  434. additionalMasters int
  435. endpoints *corev1.EndpointsList
  436. expectUpdate *corev1.Endpoints // nil means none expected
  437. expectCreate *corev1.Endpoints // nil means none expected
  438. }{
  439. {
  440. testName: "existing endpoints extra service ports missing port no update",
  441. serviceName: "foo",
  442. ip: "1.2.3.4",
  443. endpointPorts: []corev1.EndpointPort{
  444. {Name: "foo", Port: 8080, Protocol: "TCP"},
  445. {Name: "bar", Port: 1000, Protocol: "TCP"},
  446. },
  447. endpoints: &corev1.EndpointsList{
  448. Items: []corev1.Endpoints{{
  449. ObjectMeta: om("foo"),
  450. Subsets: []corev1.EndpointSubset{{
  451. Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
  452. Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
  453. }},
  454. }},
  455. },
  456. expectUpdate: nil,
  457. },
  458. {
  459. testName: "existing endpoints extra service ports, wrong ports, wrong IP",
  460. serviceName: "foo",
  461. ip: "1.2.3.4",
  462. endpointPorts: []corev1.EndpointPort{
  463. {Name: "foo", Port: 8080, Protocol: "TCP"},
  464. {Name: "bar", Port: 1000, Protocol: "TCP"},
  465. },
  466. endpoints: &corev1.EndpointsList{
  467. Items: []corev1.Endpoints{{
  468. ObjectMeta: om("foo"),
  469. Subsets: []corev1.EndpointSubset{{
  470. Addresses: []corev1.EndpointAddress{{IP: "4.3.2.1"}},
  471. Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
  472. }},
  473. }},
  474. },
  475. expectUpdate: &corev1.Endpoints{
  476. ObjectMeta: om("foo"),
  477. Subsets: []corev1.EndpointSubset{{
  478. Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
  479. Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
  480. }},
  481. },
  482. },
  483. {
  484. testName: "no existing endpoints",
  485. serviceName: "foo",
  486. ip: "1.2.3.4",
  487. endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
  488. endpoints: nil,
  489. expectCreate: &corev1.Endpoints{
  490. ObjectMeta: om("foo"),
  491. Subsets: []corev1.EndpointSubset{{
  492. Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
  493. Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
  494. }},
  495. },
  496. },
  497. }
  498. for _, test := range nonReconcileTests {
  499. fakeClient := fake.NewSimpleClientset()
  500. if test.endpoints != nil {
  501. fakeClient = fake.NewSimpleClientset(test.endpoints)
  502. }
  503. epAdapter := reconcilers.NewEndpointsAdapter(fakeClient.CoreV1(), nil)
  504. reconciler := reconcilers.NewMasterCountEndpointReconciler(test.additionalMasters+1, epAdapter)
  505. err := reconciler.ReconcileEndpoints(test.serviceName, net.ParseIP(test.ip), test.endpointPorts, false)
  506. if err != nil {
  507. t.Errorf("case %q: unexpected error: %v", test.testName, err)
  508. }
  509. updates := []core.UpdateAction{}
  510. for _, action := range fakeClient.Actions() {
  511. if action.GetVerb() != "update" {
  512. continue
  513. }
  514. updates = append(updates, action.(core.UpdateAction))
  515. }
  516. if test.expectUpdate != nil {
  517. if len(updates) != 1 {
  518. t.Errorf("case %q: unexpected updates: %v", test.testName, updates)
  519. } else if e, a := test.expectUpdate, updates[0].GetObject(); !reflect.DeepEqual(e, a) {
  520. t.Errorf("case %q: expected update:\n%#v\ngot:\n%#v\n", test.testName, e, a)
  521. }
  522. }
  523. if test.expectUpdate == nil && len(updates) > 0 {
  524. t.Errorf("case %q: no update expected, yet saw: %v", test.testName, updates)
  525. }
  526. creates := []core.CreateAction{}
  527. for _, action := range fakeClient.Actions() {
  528. if action.GetVerb() != "create" {
  529. continue
  530. }
  531. creates = append(creates, action.(core.CreateAction))
  532. }
  533. if test.expectCreate != nil {
  534. if len(creates) != 1 {
  535. t.Errorf("case %q: unexpected creates: %v", test.testName, creates)
  536. } else if e, a := test.expectCreate, creates[0].GetObject(); !reflect.DeepEqual(e, a) {
  537. t.Errorf("case %q: expected create:\n%#v\ngot:\n%#v\n", test.testName, e, a)
  538. }
  539. }
  540. if test.expectCreate == nil && len(creates) > 0 {
  541. t.Errorf("case %q: no create expected, yet saw: %v", test.testName, creates)
  542. }
  543. }
  544. }
  545. func TestEmptySubsets(t *testing.T) {
  546. ns := metav1.NamespaceDefault
  547. om := func(name string) metav1.ObjectMeta {
  548. return metav1.ObjectMeta{Namespace: ns, Name: name}
  549. }
  550. endpoints := &corev1.EndpointsList{
  551. Items: []corev1.Endpoints{{
  552. ObjectMeta: om("foo"),
  553. Subsets: nil,
  554. }},
  555. }
  556. fakeClient := fake.NewSimpleClientset()
  557. if endpoints != nil {
  558. fakeClient = fake.NewSimpleClientset(endpoints)
  559. }
  560. epAdapter := reconcilers.NewEndpointsAdapter(fakeClient.CoreV1(), nil)
  561. reconciler := reconcilers.NewMasterCountEndpointReconciler(1, epAdapter)
  562. endpointPorts := []corev1.EndpointPort{
  563. {Name: "foo", Port: 8080, Protocol: "TCP"},
  564. }
  565. err := reconciler.RemoveEndpoints("foo", net.ParseIP("1.2.3.4"), endpointPorts)
  566. if err != nil {
  567. t.Errorf("unexpected error: %v", err)
  568. }
  569. }
  570. func TestCreateOrUpdateMasterService(t *testing.T) {
  571. ns := metav1.NamespaceDefault
  572. om := func(name string) metav1.ObjectMeta {
  573. return metav1.ObjectMeta{Namespace: ns, Name: name}
  574. }
  575. createTests := []struct {
  576. testName string
  577. serviceName string
  578. servicePorts []corev1.ServicePort
  579. serviceType corev1.ServiceType
  580. expectCreate *corev1.Service // nil means none expected
  581. }{
  582. {
  583. testName: "service does not exist",
  584. serviceName: "foo",
  585. servicePorts: []corev1.ServicePort{
  586. {Name: "foo", Port: 8080, Protocol: "TCP", TargetPort: intstr.FromInt(8080)},
  587. },
  588. serviceType: corev1.ServiceTypeClusterIP,
  589. expectCreate: &corev1.Service{
  590. ObjectMeta: om("foo"),
  591. Spec: corev1.ServiceSpec{
  592. Ports: []corev1.ServicePort{
  593. {Name: "foo", Port: 8080, Protocol: "TCP", TargetPort: intstr.FromInt(8080)},
  594. },
  595. Selector: nil,
  596. ClusterIP: "1.2.3.4",
  597. SessionAffinity: corev1.ServiceAffinityNone,
  598. Type: corev1.ServiceTypeClusterIP,
  599. },
  600. },
  601. },
  602. }
  603. for _, test := range createTests {
  604. master := Controller{}
  605. fakeClient := fake.NewSimpleClientset()
  606. master.ServiceClient = fakeClient.CoreV1()
  607. master.CreateOrUpdateMasterServiceIfNeeded(test.serviceName, net.ParseIP("1.2.3.4"), test.servicePorts, test.serviceType, false)
  608. creates := []core.CreateAction{}
  609. for _, action := range fakeClient.Actions() {
  610. if action.GetVerb() == "create" {
  611. creates = append(creates, action.(core.CreateAction))
  612. }
  613. }
  614. if test.expectCreate != nil {
  615. if len(creates) != 1 {
  616. t.Errorf("case %q: unexpected creations: %v", test.testName, creates)
  617. } else {
  618. obj := creates[0].GetObject()
  619. if e, a := test.expectCreate.Spec, obj.(*corev1.Service).Spec; !reflect.DeepEqual(e, a) {
  620. t.Errorf("case %q: expected create:\n%#v\ngot:\n%#v\n", test.testName, e, a)
  621. }
  622. }
  623. }
  624. if test.expectCreate == nil && len(creates) > 1 {
  625. t.Errorf("case %q: no create expected, yet saw: %v", test.testName, creates)
  626. }
  627. }
  628. reconcileTests := []struct {
  629. testName string
  630. serviceName string
  631. servicePorts []corev1.ServicePort
  632. serviceType corev1.ServiceType
  633. service *corev1.Service
  634. expectUpdate *corev1.Service // nil means none expected
  635. }{
  636. {
  637. testName: "service definition wrong port",
  638. serviceName: "foo",
  639. servicePorts: []corev1.ServicePort{
  640. {Name: "foo", Port: 8080, Protocol: "TCP", TargetPort: intstr.FromInt(8080)},
  641. },
  642. serviceType: corev1.ServiceTypeClusterIP,
  643. service: &corev1.Service{
  644. ObjectMeta: om("foo"),
  645. Spec: corev1.ServiceSpec{
  646. Ports: []corev1.ServicePort{
  647. {Name: "foo", Port: 8000, Protocol: "TCP", TargetPort: intstr.FromInt(8080)},
  648. },
  649. Selector: nil,
  650. ClusterIP: "1.2.3.4",
  651. SessionAffinity: corev1.ServiceAffinityNone,
  652. Type: corev1.ServiceTypeClusterIP,
  653. },
  654. },
  655. expectUpdate: &corev1.Service{
  656. ObjectMeta: om("foo"),
  657. Spec: corev1.ServiceSpec{
  658. Ports: []corev1.ServicePort{
  659. {Name: "foo", Port: 8080, Protocol: "TCP", TargetPort: intstr.FromInt(8080)},
  660. },
  661. Selector: nil,
  662. ClusterIP: "1.2.3.4",
  663. SessionAffinity: corev1.ServiceAffinityNone,
  664. Type: corev1.ServiceTypeClusterIP,
  665. },
  666. },
  667. },
  668. {
  669. testName: "service definition missing port",
  670. serviceName: "foo",
  671. servicePorts: []corev1.ServicePort{
  672. {Name: "foo", Port: 8080, Protocol: "TCP", TargetPort: intstr.FromInt(8080)},
  673. {Name: "baz", Port: 1000, Protocol: "TCP", TargetPort: intstr.FromInt(1000)},
  674. },
  675. serviceType: corev1.ServiceTypeClusterIP,
  676. service: &corev1.Service{
  677. ObjectMeta: om("foo"),
  678. Spec: corev1.ServiceSpec{
  679. Ports: []corev1.ServicePort{
  680. {Name: "foo", Port: 8080, Protocol: "TCP", TargetPort: intstr.FromInt(8080)},
  681. },
  682. Selector: nil,
  683. ClusterIP: "1.2.3.4",
  684. SessionAffinity: corev1.ServiceAffinityNone,
  685. Type: corev1.ServiceTypeClusterIP,
  686. },
  687. },
  688. expectUpdate: &corev1.Service{
  689. ObjectMeta: om("foo"),
  690. Spec: corev1.ServiceSpec{
  691. Ports: []corev1.ServicePort{
  692. {Name: "foo", Port: 8080, Protocol: "TCP", TargetPort: intstr.FromInt(8080)},
  693. {Name: "baz", Port: 1000, Protocol: "TCP", TargetPort: intstr.FromInt(1000)},
  694. },
  695. Selector: nil,
  696. ClusterIP: "1.2.3.4",
  697. SessionAffinity: corev1.ServiceAffinityNone,
  698. Type: corev1.ServiceTypeClusterIP,
  699. },
  700. },
  701. },
  702. {
  703. testName: "service definition incorrect port",
  704. serviceName: "foo",
  705. servicePorts: []corev1.ServicePort{
  706. {Name: "foo", Port: 8080, Protocol: "TCP", TargetPort: intstr.FromInt(8080)},
  707. },
  708. serviceType: corev1.ServiceTypeClusterIP,
  709. service: &corev1.Service{
  710. ObjectMeta: om("foo"),
  711. Spec: corev1.ServiceSpec{
  712. Ports: []corev1.ServicePort{
  713. {Name: "bar", Port: 1000, Protocol: "UDP", TargetPort: intstr.FromInt(1000)},
  714. },
  715. Selector: nil,
  716. ClusterIP: "1.2.3.4",
  717. SessionAffinity: corev1.ServiceAffinityNone,
  718. Type: corev1.ServiceTypeClusterIP,
  719. },
  720. },
  721. expectUpdate: &corev1.Service{
  722. ObjectMeta: om("foo"),
  723. Spec: corev1.ServiceSpec{
  724. Ports: []corev1.ServicePort{
  725. {Name: "foo", Port: 8080, Protocol: "TCP", TargetPort: intstr.FromInt(8080)},
  726. },
  727. Selector: nil,
  728. ClusterIP: "1.2.3.4",
  729. SessionAffinity: corev1.ServiceAffinityNone,
  730. Type: corev1.ServiceTypeClusterIP,
  731. },
  732. },
  733. },
  734. {
  735. testName: "service definition incorrect port name",
  736. serviceName: "foo",
  737. servicePorts: []corev1.ServicePort{
  738. {Name: "foo", Port: 8080, Protocol: "TCP", TargetPort: intstr.FromInt(8080)},
  739. },
  740. serviceType: corev1.ServiceTypeClusterIP,
  741. service: &corev1.Service{
  742. ObjectMeta: om("foo"),
  743. Spec: corev1.ServiceSpec{
  744. Ports: []corev1.ServicePort{
  745. {Name: "foo", Port: 1000, Protocol: "UDP", TargetPort: intstr.FromInt(1000)},
  746. },
  747. Selector: nil,
  748. ClusterIP: "1.2.3.4",
  749. SessionAffinity: corev1.ServiceAffinityNone,
  750. Type: corev1.ServiceTypeClusterIP,
  751. },
  752. },
  753. expectUpdate: &corev1.Service{
  754. ObjectMeta: om("foo"),
  755. Spec: corev1.ServiceSpec{
  756. Ports: []corev1.ServicePort{
  757. {Name: "foo", Port: 8080, Protocol: "TCP", TargetPort: intstr.FromInt(8080)},
  758. },
  759. Selector: nil,
  760. ClusterIP: "1.2.3.4",
  761. SessionAffinity: corev1.ServiceAffinityNone,
  762. Type: corev1.ServiceTypeClusterIP,
  763. },
  764. },
  765. },
  766. {
  767. testName: "service definition incorrect target port",
  768. serviceName: "foo",
  769. servicePorts: []corev1.ServicePort{
  770. {Name: "foo", Port: 8080, Protocol: "TCP", TargetPort: intstr.FromInt(8080)},
  771. },
  772. serviceType: corev1.ServiceTypeClusterIP,
  773. service: &corev1.Service{
  774. ObjectMeta: om("foo"),
  775. Spec: corev1.ServiceSpec{
  776. Ports: []corev1.ServicePort{
  777. {Name: "foo", Port: 8080, Protocol: "TCP", TargetPort: intstr.FromInt(1000)},
  778. },
  779. Selector: nil,
  780. ClusterIP: "1.2.3.4",
  781. SessionAffinity: corev1.ServiceAffinityNone,
  782. Type: corev1.ServiceTypeClusterIP,
  783. },
  784. },
  785. expectUpdate: &corev1.Service{
  786. ObjectMeta: om("foo"),
  787. Spec: corev1.ServiceSpec{
  788. Ports: []corev1.ServicePort{
  789. {Name: "foo", Port: 8080, Protocol: "TCP", TargetPort: intstr.FromInt(8080)},
  790. },
  791. Selector: nil,
  792. ClusterIP: "1.2.3.4",
  793. SessionAffinity: corev1.ServiceAffinityNone,
  794. Type: corev1.ServiceTypeClusterIP,
  795. },
  796. },
  797. },
  798. {
  799. testName: "service definition incorrect protocol",
  800. serviceName: "foo",
  801. servicePorts: []corev1.ServicePort{
  802. {Name: "foo", Port: 8080, Protocol: "TCP", TargetPort: intstr.FromInt(8080)},
  803. },
  804. serviceType: corev1.ServiceTypeClusterIP,
  805. service: &corev1.Service{
  806. ObjectMeta: om("foo"),
  807. Spec: corev1.ServiceSpec{
  808. Ports: []corev1.ServicePort{
  809. {Name: "foo", Port: 8080, Protocol: "UDP", TargetPort: intstr.FromInt(8080)},
  810. },
  811. Selector: nil,
  812. ClusterIP: "1.2.3.4",
  813. SessionAffinity: corev1.ServiceAffinityNone,
  814. Type: corev1.ServiceTypeClusterIP,
  815. },
  816. },
  817. expectUpdate: &corev1.Service{
  818. ObjectMeta: om("foo"),
  819. Spec: corev1.ServiceSpec{
  820. Ports: []corev1.ServicePort{
  821. {Name: "foo", Port: 8080, Protocol: "TCP", TargetPort: intstr.FromInt(8080)},
  822. },
  823. Selector: nil,
  824. ClusterIP: "1.2.3.4",
  825. SessionAffinity: corev1.ServiceAffinityNone,
  826. Type: corev1.ServiceTypeClusterIP,
  827. },
  828. },
  829. },
  830. {
  831. testName: "service definition has incorrect type",
  832. serviceName: "foo",
  833. servicePorts: []corev1.ServicePort{
  834. {Name: "foo", Port: 8080, Protocol: "TCP", TargetPort: intstr.FromInt(8080)},
  835. },
  836. serviceType: corev1.ServiceTypeClusterIP,
  837. service: &corev1.Service{
  838. ObjectMeta: om("foo"),
  839. Spec: corev1.ServiceSpec{
  840. Ports: []corev1.ServicePort{
  841. {Name: "foo", Port: 8080, Protocol: "TCP", TargetPort: intstr.FromInt(8080)},
  842. },
  843. Selector: nil,
  844. ClusterIP: "1.2.3.4",
  845. SessionAffinity: corev1.ServiceAffinityNone,
  846. Type: corev1.ServiceTypeNodePort,
  847. },
  848. },
  849. expectUpdate: &corev1.Service{
  850. ObjectMeta: om("foo"),
  851. Spec: corev1.ServiceSpec{
  852. Ports: []corev1.ServicePort{
  853. {Name: "foo", Port: 8080, Protocol: "TCP", TargetPort: intstr.FromInt(8080)},
  854. },
  855. Selector: nil,
  856. ClusterIP: "1.2.3.4",
  857. SessionAffinity: corev1.ServiceAffinityNone,
  858. Type: corev1.ServiceTypeClusterIP,
  859. },
  860. },
  861. },
  862. {
  863. testName: "service definition satisfies",
  864. serviceName: "foo",
  865. servicePorts: []corev1.ServicePort{
  866. {Name: "foo", Port: 8080, Protocol: "TCP", TargetPort: intstr.FromInt(8080)},
  867. },
  868. serviceType: corev1.ServiceTypeClusterIP,
  869. service: &corev1.Service{
  870. ObjectMeta: om("foo"),
  871. Spec: corev1.ServiceSpec{
  872. Ports: []corev1.ServicePort{
  873. {Name: "foo", Port: 8080, Protocol: "TCP", TargetPort: intstr.FromInt(8080)},
  874. },
  875. Selector: nil,
  876. ClusterIP: "1.2.3.4",
  877. SessionAffinity: corev1.ServiceAffinityNone,
  878. Type: corev1.ServiceTypeClusterIP,
  879. },
  880. },
  881. expectUpdate: nil,
  882. },
  883. }
  884. for _, test := range reconcileTests {
  885. master := Controller{}
  886. fakeClient := fake.NewSimpleClientset(test.service)
  887. master.ServiceClient = fakeClient.CoreV1()
  888. err := master.CreateOrUpdateMasterServiceIfNeeded(test.serviceName, net.ParseIP("1.2.3.4"), test.servicePorts, test.serviceType, true)
  889. if err != nil {
  890. t.Errorf("case %q: unexpected error: %v", test.testName, err)
  891. }
  892. updates := []core.UpdateAction{}
  893. for _, action := range fakeClient.Actions() {
  894. if action.GetVerb() == "update" {
  895. updates = append(updates, action.(core.UpdateAction))
  896. }
  897. }
  898. if test.expectUpdate != nil {
  899. if len(updates) != 1 {
  900. t.Errorf("case %q: unexpected updates: %v", test.testName, updates)
  901. } else {
  902. obj := updates[0].GetObject()
  903. if e, a := test.expectUpdate.Spec, obj.(*corev1.Service).Spec; !reflect.DeepEqual(e, a) {
  904. t.Errorf("case %q: expected update:\n%#v\ngot:\n%#v\n", test.testName, e, a)
  905. }
  906. }
  907. }
  908. if test.expectUpdate == nil && len(updates) > 0 {
  909. t.Errorf("case %q: no update expected, yet saw: %v", test.testName, updates)
  910. }
  911. }
  912. nonReconcileTests := []struct {
  913. testName string
  914. serviceName string
  915. servicePorts []corev1.ServicePort
  916. serviceType corev1.ServiceType
  917. service *corev1.Service
  918. expectUpdate *corev1.Service // nil means none expected
  919. }{
  920. {
  921. testName: "service definition wrong port, no expected update",
  922. serviceName: "foo",
  923. servicePorts: []corev1.ServicePort{
  924. {Name: "foo", Port: 8080, Protocol: "TCP", TargetPort: intstr.FromInt(8080)},
  925. },
  926. serviceType: corev1.ServiceTypeClusterIP,
  927. service: &corev1.Service{
  928. ObjectMeta: om("foo"),
  929. Spec: corev1.ServiceSpec{
  930. Ports: []corev1.ServicePort{
  931. {Name: "foo", Port: 1000, Protocol: "TCP", TargetPort: intstr.FromInt(1000)},
  932. },
  933. Selector: nil,
  934. ClusterIP: "1.2.3.4",
  935. SessionAffinity: corev1.ServiceAffinityNone,
  936. Type: corev1.ServiceTypeClusterIP,
  937. },
  938. },
  939. expectUpdate: nil,
  940. },
  941. }
  942. for _, test := range nonReconcileTests {
  943. master := Controller{}
  944. fakeClient := fake.NewSimpleClientset(test.service)
  945. master.ServiceClient = fakeClient.CoreV1()
  946. err := master.CreateOrUpdateMasterServiceIfNeeded(test.serviceName, net.ParseIP("1.2.3.4"), test.servicePorts, test.serviceType, false)
  947. if err != nil {
  948. t.Errorf("case %q: unexpected error: %v", test.testName, err)
  949. }
  950. updates := []core.UpdateAction{}
  951. for _, action := range fakeClient.Actions() {
  952. if action.GetVerb() == "update" {
  953. updates = append(updates, action.(core.UpdateAction))
  954. }
  955. }
  956. if test.expectUpdate != nil {
  957. if len(updates) != 1 {
  958. t.Errorf("case %q: unexpected updates: %v", test.testName, updates)
  959. } else {
  960. obj := updates[0].GetObject()
  961. if e, a := test.expectUpdate.Spec, obj.(*corev1.Service).Spec; !reflect.DeepEqual(e, a) {
  962. t.Errorf("case %q: expected update:\n%#v\ngot:\n%#v\n", test.testName, e, a)
  963. }
  964. }
  965. }
  966. if test.expectUpdate == nil && len(updates) > 0 {
  967. t.Errorf("case %q: no update expected, yet saw: %v", test.testName, updates)
  968. }
  969. }
  970. }