controller_test.go 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  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. reconcile_tests := []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 reconcile_tests {
  384. fakeClient := fake.NewSimpleClientset()
  385. if test.endpoints != nil {
  386. fakeClient = fake.NewSimpleClientset(test.endpoints)
  387. }
  388. reconciler := reconcilers.NewMasterCountEndpointReconciler(test.additionalMasters+1, fakeClient.CoreV1())
  389. err := reconciler.ReconcileEndpoints(test.serviceName, net.ParseIP(test.ip), test.endpointPorts, true)
  390. if err != nil {
  391. t.Errorf("case %q: unexpected error: %v", test.testName, err)
  392. }
  393. updates := []core.UpdateAction{}
  394. for _, action := range fakeClient.Actions() {
  395. if action.GetVerb() != "update" {
  396. continue
  397. }
  398. updates = append(updates, action.(core.UpdateAction))
  399. }
  400. if test.expectUpdate != nil {
  401. if len(updates) != 1 {
  402. t.Errorf("case %q: unexpected updates: %v", test.testName, updates)
  403. } else if e, a := test.expectUpdate, updates[0].GetObject(); !reflect.DeepEqual(e, a) {
  404. t.Errorf("case %q: expected update:\n%#v\ngot:\n%#v\n", test.testName, e, a)
  405. }
  406. }
  407. if test.expectUpdate == nil && len(updates) > 0 {
  408. t.Errorf("case %q: no update expected, yet saw: %v", test.testName, updates)
  409. }
  410. creates := []core.CreateAction{}
  411. for _, action := range fakeClient.Actions() {
  412. if action.GetVerb() != "create" {
  413. continue
  414. }
  415. creates = append(creates, action.(core.CreateAction))
  416. }
  417. if test.expectCreate != nil {
  418. if len(creates) != 1 {
  419. t.Errorf("case %q: unexpected creates: %v", test.testName, creates)
  420. } else if e, a := test.expectCreate, creates[0].GetObject(); !reflect.DeepEqual(e, a) {
  421. t.Errorf("case %q: expected create:\n%#v\ngot:\n%#v\n", test.testName, e, a)
  422. }
  423. }
  424. if test.expectCreate == nil && len(creates) > 0 {
  425. t.Errorf("case %q: no create expected, yet saw: %v", test.testName, creates)
  426. }
  427. }
  428. non_reconcile_tests := []struct {
  429. testName string
  430. serviceName string
  431. ip string
  432. endpointPorts []corev1.EndpointPort
  433. additionalMasters int
  434. endpoints *corev1.EndpointsList
  435. expectUpdate *corev1.Endpoints // nil means none expected
  436. expectCreate *corev1.Endpoints // nil means none expected
  437. }{
  438. {
  439. testName: "existing endpoints extra service ports missing port no update",
  440. serviceName: "foo",
  441. ip: "1.2.3.4",
  442. endpointPorts: []corev1.EndpointPort{
  443. {Name: "foo", Port: 8080, Protocol: "TCP"},
  444. {Name: "bar", Port: 1000, Protocol: "TCP"},
  445. },
  446. endpoints: &corev1.EndpointsList{
  447. Items: []corev1.Endpoints{{
  448. ObjectMeta: om("foo"),
  449. Subsets: []corev1.EndpointSubset{{
  450. Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
  451. Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
  452. }},
  453. }},
  454. },
  455. expectUpdate: nil,
  456. },
  457. {
  458. testName: "existing endpoints extra service ports, wrong ports, wrong IP",
  459. serviceName: "foo",
  460. ip: "1.2.3.4",
  461. endpointPorts: []corev1.EndpointPort{
  462. {Name: "foo", Port: 8080, Protocol: "TCP"},
  463. {Name: "bar", Port: 1000, Protocol: "TCP"},
  464. },
  465. endpoints: &corev1.EndpointsList{
  466. Items: []corev1.Endpoints{{
  467. ObjectMeta: om("foo"),
  468. Subsets: []corev1.EndpointSubset{{
  469. Addresses: []corev1.EndpointAddress{{IP: "4.3.2.1"}},
  470. Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
  471. }},
  472. }},
  473. },
  474. expectUpdate: &corev1.Endpoints{
  475. ObjectMeta: om("foo"),
  476. Subsets: []corev1.EndpointSubset{{
  477. Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
  478. Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
  479. }},
  480. },
  481. },
  482. {
  483. testName: "no existing endpoints",
  484. serviceName: "foo",
  485. ip: "1.2.3.4",
  486. endpointPorts: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
  487. endpoints: nil,
  488. expectCreate: &corev1.Endpoints{
  489. ObjectMeta: om("foo"),
  490. Subsets: []corev1.EndpointSubset{{
  491. Addresses: []corev1.EndpointAddress{{IP: "1.2.3.4"}},
  492. Ports: []corev1.EndpointPort{{Name: "foo", Port: 8080, Protocol: "TCP"}},
  493. }},
  494. },
  495. },
  496. }
  497. for _, test := range non_reconcile_tests {
  498. fakeClient := fake.NewSimpleClientset()
  499. if test.endpoints != nil {
  500. fakeClient = fake.NewSimpleClientset(test.endpoints)
  501. }
  502. reconciler := reconcilers.NewMasterCountEndpointReconciler(test.additionalMasters+1, fakeClient.CoreV1())
  503. err := reconciler.ReconcileEndpoints(test.serviceName, net.ParseIP(test.ip), test.endpointPorts, false)
  504. if err != nil {
  505. t.Errorf("case %q: unexpected error: %v", test.testName, err)
  506. }
  507. updates := []core.UpdateAction{}
  508. for _, action := range fakeClient.Actions() {
  509. if action.GetVerb() != "update" {
  510. continue
  511. }
  512. updates = append(updates, action.(core.UpdateAction))
  513. }
  514. if test.expectUpdate != nil {
  515. if len(updates) != 1 {
  516. t.Errorf("case %q: unexpected updates: %v", test.testName, updates)
  517. } else if e, a := test.expectUpdate, updates[0].GetObject(); !reflect.DeepEqual(e, a) {
  518. t.Errorf("case %q: expected update:\n%#v\ngot:\n%#v\n", test.testName, e, a)
  519. }
  520. }
  521. if test.expectUpdate == nil && len(updates) > 0 {
  522. t.Errorf("case %q: no update expected, yet saw: %v", test.testName, updates)
  523. }
  524. creates := []core.CreateAction{}
  525. for _, action := range fakeClient.Actions() {
  526. if action.GetVerb() != "create" {
  527. continue
  528. }
  529. creates = append(creates, action.(core.CreateAction))
  530. }
  531. if test.expectCreate != nil {
  532. if len(creates) != 1 {
  533. t.Errorf("case %q: unexpected creates: %v", test.testName, creates)
  534. } else if e, a := test.expectCreate, creates[0].GetObject(); !reflect.DeepEqual(e, a) {
  535. t.Errorf("case %q: expected create:\n%#v\ngot:\n%#v\n", test.testName, e, a)
  536. }
  537. }
  538. if test.expectCreate == nil && len(creates) > 0 {
  539. t.Errorf("case %q: no create expected, yet saw: %v", test.testName, creates)
  540. }
  541. }
  542. }
  543. func TestCreateOrUpdateMasterService(t *testing.T) {
  544. ns := metav1.NamespaceDefault
  545. om := func(name string) metav1.ObjectMeta {
  546. return metav1.ObjectMeta{Namespace: ns, Name: name}
  547. }
  548. create_tests := []struct {
  549. testName string
  550. serviceName string
  551. servicePorts []corev1.ServicePort
  552. serviceType corev1.ServiceType
  553. expectCreate *corev1.Service // nil means none expected
  554. }{
  555. {
  556. testName: "service does not exist",
  557. serviceName: "foo",
  558. servicePorts: []corev1.ServicePort{
  559. {Name: "foo", Port: 8080, Protocol: "TCP", TargetPort: intstr.FromInt(8080)},
  560. },
  561. serviceType: corev1.ServiceTypeClusterIP,
  562. expectCreate: &corev1.Service{
  563. ObjectMeta: om("foo"),
  564. Spec: corev1.ServiceSpec{
  565. Ports: []corev1.ServicePort{
  566. {Name: "foo", Port: 8080, Protocol: "TCP", TargetPort: intstr.FromInt(8080)},
  567. },
  568. Selector: nil,
  569. ClusterIP: "1.2.3.4",
  570. SessionAffinity: corev1.ServiceAffinityNone,
  571. Type: corev1.ServiceTypeClusterIP,
  572. },
  573. },
  574. },
  575. }
  576. for _, test := range create_tests {
  577. master := Controller{}
  578. fakeClient := fake.NewSimpleClientset()
  579. master.ServiceClient = fakeClient.CoreV1()
  580. master.CreateOrUpdateMasterServiceIfNeeded(test.serviceName, net.ParseIP("1.2.3.4"), test.servicePorts, test.serviceType, false)
  581. creates := []core.CreateAction{}
  582. for _, action := range fakeClient.Actions() {
  583. if action.GetVerb() == "create" {
  584. creates = append(creates, action.(core.CreateAction))
  585. }
  586. }
  587. if test.expectCreate != nil {
  588. if len(creates) != 1 {
  589. t.Errorf("case %q: unexpected creations: %v", test.testName, creates)
  590. } else {
  591. obj := creates[0].GetObject()
  592. if e, a := test.expectCreate.Spec, obj.(*corev1.Service).Spec; !reflect.DeepEqual(e, a) {
  593. t.Errorf("case %q: expected create:\n%#v\ngot:\n%#v\n", test.testName, e, a)
  594. }
  595. }
  596. }
  597. if test.expectCreate == nil && len(creates) > 1 {
  598. t.Errorf("case %q: no create expected, yet saw: %v", test.testName, creates)
  599. }
  600. }
  601. reconcile_tests := []struct {
  602. testName string
  603. serviceName string
  604. servicePorts []corev1.ServicePort
  605. serviceType corev1.ServiceType
  606. service *corev1.Service
  607. expectUpdate *corev1.Service // nil means none expected
  608. }{
  609. {
  610. testName: "service definition wrong port",
  611. serviceName: "foo",
  612. servicePorts: []corev1.ServicePort{
  613. {Name: "foo", Port: 8080, Protocol: "TCP", TargetPort: intstr.FromInt(8080)},
  614. },
  615. serviceType: corev1.ServiceTypeClusterIP,
  616. service: &corev1.Service{
  617. ObjectMeta: om("foo"),
  618. Spec: corev1.ServiceSpec{
  619. Ports: []corev1.ServicePort{
  620. {Name: "foo", Port: 8000, Protocol: "TCP", TargetPort: intstr.FromInt(8080)},
  621. },
  622. Selector: nil,
  623. ClusterIP: "1.2.3.4",
  624. SessionAffinity: corev1.ServiceAffinityNone,
  625. Type: corev1.ServiceTypeClusterIP,
  626. },
  627. },
  628. expectUpdate: &corev1.Service{
  629. ObjectMeta: om("foo"),
  630. Spec: corev1.ServiceSpec{
  631. Ports: []corev1.ServicePort{
  632. {Name: "foo", Port: 8080, Protocol: "TCP", TargetPort: intstr.FromInt(8080)},
  633. },
  634. Selector: nil,
  635. ClusterIP: "1.2.3.4",
  636. SessionAffinity: corev1.ServiceAffinityNone,
  637. Type: corev1.ServiceTypeClusterIP,
  638. },
  639. },
  640. },
  641. {
  642. testName: "service definition missing port",
  643. serviceName: "foo",
  644. servicePorts: []corev1.ServicePort{
  645. {Name: "foo", Port: 8080, Protocol: "TCP", TargetPort: intstr.FromInt(8080)},
  646. {Name: "baz", Port: 1000, Protocol: "TCP", TargetPort: intstr.FromInt(1000)},
  647. },
  648. serviceType: corev1.ServiceTypeClusterIP,
  649. service: &corev1.Service{
  650. ObjectMeta: om("foo"),
  651. Spec: corev1.ServiceSpec{
  652. Ports: []corev1.ServicePort{
  653. {Name: "foo", Port: 8080, Protocol: "TCP", TargetPort: intstr.FromInt(8080)},
  654. },
  655. Selector: nil,
  656. ClusterIP: "1.2.3.4",
  657. SessionAffinity: corev1.ServiceAffinityNone,
  658. Type: corev1.ServiceTypeClusterIP,
  659. },
  660. },
  661. expectUpdate: &corev1.Service{
  662. ObjectMeta: om("foo"),
  663. Spec: corev1.ServiceSpec{
  664. Ports: []corev1.ServicePort{
  665. {Name: "foo", Port: 8080, Protocol: "TCP", TargetPort: intstr.FromInt(8080)},
  666. {Name: "baz", Port: 1000, Protocol: "TCP", TargetPort: intstr.FromInt(1000)},
  667. },
  668. Selector: nil,
  669. ClusterIP: "1.2.3.4",
  670. SessionAffinity: corev1.ServiceAffinityNone,
  671. Type: corev1.ServiceTypeClusterIP,
  672. },
  673. },
  674. },
  675. {
  676. testName: "service definition incorrect port",
  677. serviceName: "foo",
  678. servicePorts: []corev1.ServicePort{
  679. {Name: "foo", Port: 8080, Protocol: "TCP", TargetPort: intstr.FromInt(8080)},
  680. },
  681. serviceType: corev1.ServiceTypeClusterIP,
  682. service: &corev1.Service{
  683. ObjectMeta: om("foo"),
  684. Spec: corev1.ServiceSpec{
  685. Ports: []corev1.ServicePort{
  686. {Name: "bar", Port: 1000, Protocol: "UDP", TargetPort: intstr.FromInt(1000)},
  687. },
  688. Selector: nil,
  689. ClusterIP: "1.2.3.4",
  690. SessionAffinity: corev1.ServiceAffinityNone,
  691. Type: corev1.ServiceTypeClusterIP,
  692. },
  693. },
  694. expectUpdate: &corev1.Service{
  695. ObjectMeta: om("foo"),
  696. Spec: corev1.ServiceSpec{
  697. Ports: []corev1.ServicePort{
  698. {Name: "foo", Port: 8080, Protocol: "TCP", TargetPort: intstr.FromInt(8080)},
  699. },
  700. Selector: nil,
  701. ClusterIP: "1.2.3.4",
  702. SessionAffinity: corev1.ServiceAffinityNone,
  703. Type: corev1.ServiceTypeClusterIP,
  704. },
  705. },
  706. },
  707. {
  708. testName: "service definition incorrect port name",
  709. serviceName: "foo",
  710. servicePorts: []corev1.ServicePort{
  711. {Name: "foo", Port: 8080, Protocol: "TCP", TargetPort: intstr.FromInt(8080)},
  712. },
  713. serviceType: corev1.ServiceTypeClusterIP,
  714. service: &corev1.Service{
  715. ObjectMeta: om("foo"),
  716. Spec: corev1.ServiceSpec{
  717. Ports: []corev1.ServicePort{
  718. {Name: "foo", Port: 1000, Protocol: "UDP", TargetPort: intstr.FromInt(1000)},
  719. },
  720. Selector: nil,
  721. ClusterIP: "1.2.3.4",
  722. SessionAffinity: corev1.ServiceAffinityNone,
  723. Type: corev1.ServiceTypeClusterIP,
  724. },
  725. },
  726. expectUpdate: &corev1.Service{
  727. ObjectMeta: om("foo"),
  728. Spec: corev1.ServiceSpec{
  729. Ports: []corev1.ServicePort{
  730. {Name: "foo", Port: 8080, Protocol: "TCP", TargetPort: intstr.FromInt(8080)},
  731. },
  732. Selector: nil,
  733. ClusterIP: "1.2.3.4",
  734. SessionAffinity: corev1.ServiceAffinityNone,
  735. Type: corev1.ServiceTypeClusterIP,
  736. },
  737. },
  738. },
  739. {
  740. testName: "service definition incorrect target port",
  741. serviceName: "foo",
  742. servicePorts: []corev1.ServicePort{
  743. {Name: "foo", Port: 8080, Protocol: "TCP", TargetPort: intstr.FromInt(8080)},
  744. },
  745. serviceType: corev1.ServiceTypeClusterIP,
  746. service: &corev1.Service{
  747. ObjectMeta: om("foo"),
  748. Spec: corev1.ServiceSpec{
  749. Ports: []corev1.ServicePort{
  750. {Name: "foo", Port: 8080, Protocol: "TCP", TargetPort: intstr.FromInt(1000)},
  751. },
  752. Selector: nil,
  753. ClusterIP: "1.2.3.4",
  754. SessionAffinity: corev1.ServiceAffinityNone,
  755. Type: corev1.ServiceTypeClusterIP,
  756. },
  757. },
  758. expectUpdate: &corev1.Service{
  759. ObjectMeta: om("foo"),
  760. Spec: corev1.ServiceSpec{
  761. Ports: []corev1.ServicePort{
  762. {Name: "foo", Port: 8080, Protocol: "TCP", TargetPort: intstr.FromInt(8080)},
  763. },
  764. Selector: nil,
  765. ClusterIP: "1.2.3.4",
  766. SessionAffinity: corev1.ServiceAffinityNone,
  767. Type: corev1.ServiceTypeClusterIP,
  768. },
  769. },
  770. },
  771. {
  772. testName: "service definition incorrect protocol",
  773. serviceName: "foo",
  774. servicePorts: []corev1.ServicePort{
  775. {Name: "foo", Port: 8080, Protocol: "TCP", TargetPort: intstr.FromInt(8080)},
  776. },
  777. serviceType: corev1.ServiceTypeClusterIP,
  778. service: &corev1.Service{
  779. ObjectMeta: om("foo"),
  780. Spec: corev1.ServiceSpec{
  781. Ports: []corev1.ServicePort{
  782. {Name: "foo", Port: 8080, Protocol: "UDP", TargetPort: intstr.FromInt(8080)},
  783. },
  784. Selector: nil,
  785. ClusterIP: "1.2.3.4",
  786. SessionAffinity: corev1.ServiceAffinityNone,
  787. Type: corev1.ServiceTypeClusterIP,
  788. },
  789. },
  790. expectUpdate: &corev1.Service{
  791. ObjectMeta: om("foo"),
  792. Spec: corev1.ServiceSpec{
  793. Ports: []corev1.ServicePort{
  794. {Name: "foo", Port: 8080, Protocol: "TCP", TargetPort: intstr.FromInt(8080)},
  795. },
  796. Selector: nil,
  797. ClusterIP: "1.2.3.4",
  798. SessionAffinity: corev1.ServiceAffinityNone,
  799. Type: corev1.ServiceTypeClusterIP,
  800. },
  801. },
  802. },
  803. {
  804. testName: "service definition has incorrect type",
  805. serviceName: "foo",
  806. servicePorts: []corev1.ServicePort{
  807. {Name: "foo", Port: 8080, Protocol: "TCP", TargetPort: intstr.FromInt(8080)},
  808. },
  809. serviceType: corev1.ServiceTypeClusterIP,
  810. service: &corev1.Service{
  811. ObjectMeta: om("foo"),
  812. Spec: corev1.ServiceSpec{
  813. Ports: []corev1.ServicePort{
  814. {Name: "foo", Port: 8080, Protocol: "TCP", TargetPort: intstr.FromInt(8080)},
  815. },
  816. Selector: nil,
  817. ClusterIP: "1.2.3.4",
  818. SessionAffinity: corev1.ServiceAffinityNone,
  819. Type: corev1.ServiceTypeNodePort,
  820. },
  821. },
  822. expectUpdate: &corev1.Service{
  823. ObjectMeta: om("foo"),
  824. Spec: corev1.ServiceSpec{
  825. Ports: []corev1.ServicePort{
  826. {Name: "foo", Port: 8080, Protocol: "TCP", TargetPort: intstr.FromInt(8080)},
  827. },
  828. Selector: nil,
  829. ClusterIP: "1.2.3.4",
  830. SessionAffinity: corev1.ServiceAffinityNone,
  831. Type: corev1.ServiceTypeClusterIP,
  832. },
  833. },
  834. },
  835. {
  836. testName: "service definition satisfies",
  837. serviceName: "foo",
  838. servicePorts: []corev1.ServicePort{
  839. {Name: "foo", Port: 8080, Protocol: "TCP", TargetPort: intstr.FromInt(8080)},
  840. },
  841. serviceType: corev1.ServiceTypeClusterIP,
  842. service: &corev1.Service{
  843. ObjectMeta: om("foo"),
  844. Spec: corev1.ServiceSpec{
  845. Ports: []corev1.ServicePort{
  846. {Name: "foo", Port: 8080, Protocol: "TCP", TargetPort: intstr.FromInt(8080)},
  847. },
  848. Selector: nil,
  849. ClusterIP: "1.2.3.4",
  850. SessionAffinity: corev1.ServiceAffinityNone,
  851. Type: corev1.ServiceTypeClusterIP,
  852. },
  853. },
  854. expectUpdate: nil,
  855. },
  856. }
  857. for _, test := range reconcile_tests {
  858. master := Controller{}
  859. fakeClient := fake.NewSimpleClientset(test.service)
  860. master.ServiceClient = fakeClient.CoreV1()
  861. err := master.CreateOrUpdateMasterServiceIfNeeded(test.serviceName, net.ParseIP("1.2.3.4"), test.servicePorts, test.serviceType, true)
  862. if err != nil {
  863. t.Errorf("case %q: unexpected error: %v", test.testName, err)
  864. }
  865. updates := []core.UpdateAction{}
  866. for _, action := range fakeClient.Actions() {
  867. if action.GetVerb() == "update" {
  868. updates = append(updates, action.(core.UpdateAction))
  869. }
  870. }
  871. if test.expectUpdate != nil {
  872. if len(updates) != 1 {
  873. t.Errorf("case %q: unexpected updates: %v", test.testName, updates)
  874. } else {
  875. obj := updates[0].GetObject()
  876. if e, a := test.expectUpdate.Spec, obj.(*corev1.Service).Spec; !reflect.DeepEqual(e, a) {
  877. t.Errorf("case %q: expected update:\n%#v\ngot:\n%#v\n", test.testName, e, a)
  878. }
  879. }
  880. }
  881. if test.expectUpdate == nil && len(updates) > 0 {
  882. t.Errorf("case %q: no update expected, yet saw: %v", test.testName, updates)
  883. }
  884. }
  885. non_reconcile_tests := []struct {
  886. testName string
  887. serviceName string
  888. servicePorts []corev1.ServicePort
  889. serviceType corev1.ServiceType
  890. service *corev1.Service
  891. expectUpdate *corev1.Service // nil means none expected
  892. }{
  893. {
  894. testName: "service definition wrong port, no expected update",
  895. serviceName: "foo",
  896. servicePorts: []corev1.ServicePort{
  897. {Name: "foo", Port: 8080, Protocol: "TCP", TargetPort: intstr.FromInt(8080)},
  898. },
  899. serviceType: corev1.ServiceTypeClusterIP,
  900. service: &corev1.Service{
  901. ObjectMeta: om("foo"),
  902. Spec: corev1.ServiceSpec{
  903. Ports: []corev1.ServicePort{
  904. {Name: "foo", Port: 1000, Protocol: "TCP", TargetPort: intstr.FromInt(1000)},
  905. },
  906. Selector: nil,
  907. ClusterIP: "1.2.3.4",
  908. SessionAffinity: corev1.ServiceAffinityNone,
  909. Type: corev1.ServiceTypeClusterIP,
  910. },
  911. },
  912. expectUpdate: nil,
  913. },
  914. }
  915. for _, test := range non_reconcile_tests {
  916. master := Controller{}
  917. fakeClient := fake.NewSimpleClientset(test.service)
  918. master.ServiceClient = fakeClient.CoreV1()
  919. err := master.CreateOrUpdateMasterServiceIfNeeded(test.serviceName, net.ParseIP("1.2.3.4"), test.servicePorts, test.serviceType, false)
  920. if err != nil {
  921. t.Errorf("case %q: unexpected error: %v", test.testName, err)
  922. }
  923. updates := []core.UpdateAction{}
  924. for _, action := range fakeClient.Actions() {
  925. if action.GetVerb() == "update" {
  926. updates = append(updates, action.(core.UpdateAction))
  927. }
  928. }
  929. if test.expectUpdate != nil {
  930. if len(updates) != 1 {
  931. t.Errorf("case %q: unexpected updates: %v", test.testName, updates)
  932. } else {
  933. obj := updates[0].GetObject()
  934. if e, a := test.expectUpdate.Spec, obj.(*corev1.Service).Spec; !reflect.DeepEqual(e, a) {
  935. t.Errorf("case %q: expected update:\n%#v\ngot:\n%#v\n", test.testName, e, a)
  936. }
  937. }
  938. }
  939. if test.expectUpdate == nil && len(updates) > 0 {
  940. t.Errorf("case %q: no update expected, yet saw: %v", test.testName, updates)
  941. }
  942. }
  943. }