endpointsadapter_test.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*
  2. Copyright 2019 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 reconcilers
  14. import (
  15. "context"
  16. "fmt"
  17. "testing"
  18. corev1 "k8s.io/api/core/v1"
  19. discovery "k8s.io/api/discovery/v1beta1"
  20. apiequality "k8s.io/apimachinery/pkg/api/equality"
  21. "k8s.io/apimachinery/pkg/api/errors"
  22. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  23. "k8s.io/apimachinery/pkg/runtime/schema"
  24. "k8s.io/client-go/kubernetes/fake"
  25. )
  26. func TestEndpointsAdapterGet(t *testing.T) {
  27. endpoints1, _ := generateEndpointsAndSlice("foo", "testing", []int{80, 443}, []string{"10.1.2.3", "10.1.2.4"})
  28. testCases := map[string]struct {
  29. endpointSlicesEnabled bool
  30. expectedError error
  31. expectedEndpoints *corev1.Endpoints
  32. endpoints []*corev1.Endpoints
  33. namespaceParam string
  34. nameParam string
  35. }{
  36. "single-existing-endpoints": {
  37. endpointSlicesEnabled: false,
  38. expectedError: nil,
  39. expectedEndpoints: endpoints1,
  40. endpoints: []*corev1.Endpoints{endpoints1},
  41. namespaceParam: "testing",
  42. nameParam: "foo",
  43. },
  44. "single-existing-endpoints-slices-enabled": {
  45. endpointSlicesEnabled: true,
  46. expectedError: nil,
  47. expectedEndpoints: endpoints1,
  48. endpoints: []*corev1.Endpoints{endpoints1},
  49. namespaceParam: "testing",
  50. nameParam: "foo",
  51. },
  52. "wrong-namespace": {
  53. endpointSlicesEnabled: false,
  54. expectedError: errors.NewNotFound(schema.GroupResource{Group: "", Resource: "endpoints"}, "foo"),
  55. expectedEndpoints: nil,
  56. endpoints: []*corev1.Endpoints{endpoints1},
  57. namespaceParam: "foo",
  58. nameParam: "foo",
  59. },
  60. "wrong-name": {
  61. endpointSlicesEnabled: false,
  62. expectedError: errors.NewNotFound(schema.GroupResource{Group: "", Resource: "endpoints"}, "bar"),
  63. expectedEndpoints: nil,
  64. endpoints: []*corev1.Endpoints{endpoints1},
  65. namespaceParam: "testing",
  66. nameParam: "bar",
  67. },
  68. }
  69. for name, testCase := range testCases {
  70. t.Run(name, func(t *testing.T) {
  71. client := fake.NewSimpleClientset()
  72. epAdapter := EndpointsAdapter{endpointClient: client.CoreV1()}
  73. if testCase.endpointSlicesEnabled {
  74. epAdapter.endpointSliceClient = client.DiscoveryV1beta1()
  75. }
  76. for _, endpoints := range testCase.endpoints {
  77. _, err := client.CoreV1().Endpoints(endpoints.Namespace).Create(context.TODO(), endpoints, metav1.CreateOptions{})
  78. if err != nil {
  79. t.Fatalf("Error creating Endpoints: %v", err)
  80. }
  81. }
  82. endpoints, err := epAdapter.Get(testCase.namespaceParam, testCase.nameParam, metav1.GetOptions{})
  83. if !apiequality.Semantic.DeepEqual(testCase.expectedError, err) {
  84. t.Errorf("Expected error: %v, got: %v", testCase.expectedError, err)
  85. }
  86. if !apiequality.Semantic.DeepEqual(endpoints, testCase.expectedEndpoints) {
  87. t.Errorf("Expected endpoints: %v, got: %v", testCase.expectedEndpoints, endpoints)
  88. }
  89. })
  90. }
  91. }
  92. func TestEndpointsAdapterCreate(t *testing.T) {
  93. endpoints1, epSlice1 := generateEndpointsAndSlice("foo", "testing", []int{80}, []string{"10.1.2.3", "10.1.2.4"})
  94. // even if an Endpoints resource includes an IPv6 address, it should not be
  95. // included in the corresponding EndpointSlice.
  96. endpoints2, _ := generateEndpointsAndSlice("foo", "testing", []int{80}, []string{"10.1.2.5", "10.1.2.6", "1234::5678:0000:0000:9abc:def0"})
  97. _, epSlice2 := generateEndpointsAndSlice("foo", "testing", []int{80}, []string{"10.1.2.5", "10.1.2.6"})
  98. // ensure that Endpoints with only IPv6 addresses result in EndpointSlice
  99. // with an IPv6 address type.
  100. endpoints3, epSlice3 := generateEndpointsAndSlice("foo", "testing", []int{80}, []string{"1234::5678:0000:0000:9abc:def0"})
  101. epSlice3.AddressType = discovery.AddressTypeIPv6
  102. testCases := map[string]struct {
  103. endpointSlicesEnabled bool
  104. expectedError error
  105. expectedEndpoints *corev1.Endpoints
  106. expectedEndpointSlice *discovery.EndpointSlice
  107. endpoints []*corev1.Endpoints
  108. endpointSlices []*discovery.EndpointSlice
  109. namespaceParam string
  110. endpointsParam *corev1.Endpoints
  111. }{
  112. "single-endpoint": {
  113. endpointSlicesEnabled: true,
  114. expectedError: nil,
  115. expectedEndpoints: endpoints1,
  116. expectedEndpointSlice: epSlice1,
  117. endpoints: []*corev1.Endpoints{},
  118. namespaceParam: endpoints1.Namespace,
  119. endpointsParam: endpoints1,
  120. },
  121. "single-endpoint-partial-ipv6": {
  122. endpointSlicesEnabled: true,
  123. expectedError: nil,
  124. expectedEndpoints: endpoints2,
  125. expectedEndpointSlice: epSlice2,
  126. endpoints: []*corev1.Endpoints{},
  127. namespaceParam: endpoints2.Namespace,
  128. endpointsParam: endpoints2,
  129. },
  130. "single-endpoint-full-ipv6": {
  131. endpointSlicesEnabled: true,
  132. expectedError: nil,
  133. expectedEndpoints: endpoints3,
  134. expectedEndpointSlice: epSlice3,
  135. endpoints: []*corev1.Endpoints{},
  136. namespaceParam: endpoints3.Namespace,
  137. endpointsParam: endpoints3,
  138. },
  139. "single-endpoint-no-slices": {
  140. endpointSlicesEnabled: false,
  141. expectedError: nil,
  142. expectedEndpoints: endpoints1,
  143. expectedEndpointSlice: nil,
  144. endpoints: []*corev1.Endpoints{},
  145. namespaceParam: endpoints1.Namespace,
  146. endpointsParam: endpoints1,
  147. },
  148. "existing-endpoint": {
  149. endpointSlicesEnabled: true,
  150. expectedError: errors.NewAlreadyExists(schema.GroupResource{Group: "", Resource: "endpoints"}, "foo"),
  151. expectedEndpoints: nil,
  152. expectedEndpointSlice: nil,
  153. endpoints: []*corev1.Endpoints{endpoints1},
  154. namespaceParam: endpoints1.Namespace,
  155. endpointsParam: endpoints1,
  156. },
  157. }
  158. for name, testCase := range testCases {
  159. t.Run(name, func(t *testing.T) {
  160. client := fake.NewSimpleClientset()
  161. epAdapter := EndpointsAdapter{endpointClient: client.CoreV1()}
  162. if testCase.endpointSlicesEnabled {
  163. epAdapter.endpointSliceClient = client.DiscoveryV1beta1()
  164. }
  165. for _, endpoints := range testCase.endpoints {
  166. _, err := client.CoreV1().Endpoints(endpoints.Namespace).Create(context.TODO(), endpoints, metav1.CreateOptions{})
  167. if err != nil {
  168. t.Fatalf("Error creating Endpoints: %v", err)
  169. }
  170. }
  171. endpoints, err := epAdapter.Create(testCase.namespaceParam, testCase.endpointsParam)
  172. if !apiequality.Semantic.DeepEqual(testCase.expectedError, err) {
  173. t.Errorf("Expected error: %v, got: %v", testCase.expectedError, err)
  174. }
  175. if !apiequality.Semantic.DeepEqual(endpoints, testCase.expectedEndpoints) {
  176. t.Errorf("Expected endpoints: %v, got: %v", testCase.expectedEndpoints, endpoints)
  177. }
  178. epSliceList, err := client.DiscoveryV1beta1().EndpointSlices(testCase.namespaceParam).List(context.TODO(), metav1.ListOptions{})
  179. if err != nil {
  180. t.Fatalf("Error listing Endpoint Slices: %v", err)
  181. }
  182. if testCase.expectedEndpointSlice == nil {
  183. if len(epSliceList.Items) != 0 {
  184. t.Fatalf("Expected no Endpoint Slices, got: %v", epSliceList.Items)
  185. }
  186. } else {
  187. if len(epSliceList.Items) == 0 {
  188. t.Fatalf("No Endpoint Slices found, expected: %v", testCase.expectedEndpointSlice)
  189. }
  190. if len(epSliceList.Items) > 1 {
  191. t.Errorf("Only 1 Endpoint Slice expected, got: %v", testCase.expectedEndpointSlice)
  192. }
  193. if !apiequality.Semantic.DeepEqual(*testCase.expectedEndpointSlice, epSliceList.Items[0]) {
  194. t.Errorf("Expected Endpoint Slice: %v, got: %v", testCase.expectedEndpointSlice, epSliceList.Items[0])
  195. }
  196. }
  197. })
  198. }
  199. }
  200. func TestEndpointsAdapterUpdate(t *testing.T) {
  201. endpoints1, _ := generateEndpointsAndSlice("foo", "testing", []int{80}, []string{"10.1.2.3", "10.1.2.4"})
  202. endpoints2, epSlice2 := generateEndpointsAndSlice("foo", "testing", []int{80, 443}, []string{"10.1.2.3", "10.1.2.4", "10.1.2.5"})
  203. endpoints3, _ := generateEndpointsAndSlice("bar", "testing", []int{80, 443}, []string{"10.1.2.3", "10.1.2.4", "10.1.2.5"})
  204. // ensure that EndpointSlice with deprecated IP address type is replaced
  205. // with one that has an IPv4 address type.
  206. endpoints4, _ := generateEndpointsAndSlice("foo", "testing", []int{80}, []string{"10.1.2.7", "10.1.2.8"})
  207. _, epSlice4IP := generateEndpointsAndSlice("foo", "testing", []int{80}, []string{"10.1.2.7", "10.1.2.8"})
  208. epSlice4IP.AddressType = discovery.AddressTypeIP
  209. _, epSlice4IPv4 := generateEndpointsAndSlice("foo", "testing", []int{80}, []string{"10.1.2.7", "10.1.2.8"})
  210. testCases := map[string]struct {
  211. endpointSlicesEnabled bool
  212. expectedError error
  213. expectedEndpoints *corev1.Endpoints
  214. expectedEndpointSlice *discovery.EndpointSlice
  215. endpoints []*corev1.Endpoints
  216. endpointSlices []*discovery.EndpointSlice
  217. namespaceParam string
  218. endpointsParam *corev1.Endpoints
  219. }{
  220. "single-existing-endpoints-no-change": {
  221. endpointSlicesEnabled: false,
  222. expectedError: nil,
  223. expectedEndpoints: endpoints1,
  224. expectedEndpointSlice: nil,
  225. endpoints: []*corev1.Endpoints{endpoints1},
  226. namespaceParam: "testing",
  227. endpointsParam: endpoints1,
  228. },
  229. "existing-endpointslice-replaced-with-updated-ipv4-address-type": {
  230. endpointSlicesEnabled: true,
  231. expectedError: nil,
  232. expectedEndpoints: endpoints4,
  233. expectedEndpointSlice: epSlice4IPv4,
  234. endpoints: []*corev1.Endpoints{endpoints4},
  235. endpointSlices: []*discovery.EndpointSlice{epSlice4IP},
  236. namespaceParam: "testing",
  237. endpointsParam: endpoints4,
  238. },
  239. "add-ports-and-ips": {
  240. endpointSlicesEnabled: true,
  241. expectedError: nil,
  242. expectedEndpoints: endpoints2,
  243. expectedEndpointSlice: epSlice2,
  244. endpoints: []*corev1.Endpoints{endpoints1},
  245. namespaceParam: "testing",
  246. endpointsParam: endpoints2,
  247. },
  248. "missing-endpoints": {
  249. endpointSlicesEnabled: true,
  250. expectedError: errors.NewNotFound(schema.GroupResource{Group: "", Resource: "endpoints"}, "bar"),
  251. expectedEndpoints: nil,
  252. expectedEndpointSlice: nil,
  253. endpoints: []*corev1.Endpoints{endpoints1},
  254. namespaceParam: "testing",
  255. endpointsParam: endpoints3,
  256. },
  257. }
  258. for name, testCase := range testCases {
  259. t.Run(name, func(t *testing.T) {
  260. client := fake.NewSimpleClientset()
  261. epAdapter := EndpointsAdapter{endpointClient: client.CoreV1()}
  262. if testCase.endpointSlicesEnabled {
  263. epAdapter.endpointSliceClient = client.DiscoveryV1beta1()
  264. }
  265. for _, endpoints := range testCase.endpoints {
  266. _, err := client.CoreV1().Endpoints(endpoints.Namespace).Create(context.TODO(), endpoints, metav1.CreateOptions{})
  267. if err != nil {
  268. t.Fatalf("Error creating Endpoints: %v", err)
  269. }
  270. }
  271. endpoints, err := epAdapter.Update(testCase.namespaceParam, testCase.endpointsParam)
  272. if !apiequality.Semantic.DeepEqual(testCase.expectedError, err) {
  273. t.Errorf("Expected error: %v, got: %v", testCase.expectedError, err)
  274. }
  275. if !apiequality.Semantic.DeepEqual(endpoints, testCase.expectedEndpoints) {
  276. t.Errorf("Expected endpoints: %v, got: %v", testCase.expectedEndpoints, endpoints)
  277. }
  278. epSliceList, err := client.DiscoveryV1beta1().EndpointSlices(testCase.namespaceParam).List(context.TODO(), metav1.ListOptions{})
  279. if err != nil {
  280. t.Fatalf("Error listing Endpoint Slices: %v", err)
  281. }
  282. if testCase.expectedEndpointSlice == nil {
  283. if len(epSliceList.Items) != 0 {
  284. t.Fatalf("Expected no Endpoint Slices, got: %v", epSliceList.Items)
  285. }
  286. } else {
  287. if len(epSliceList.Items) == 0 {
  288. t.Fatalf("No Endpoint Slices found, expected: %v", testCase.expectedEndpointSlice)
  289. }
  290. if len(epSliceList.Items) > 1 {
  291. t.Errorf("Only 1 Endpoint Slice expected, got: %v", testCase.expectedEndpointSlice)
  292. }
  293. if !apiequality.Semantic.DeepEqual(*testCase.expectedEndpointSlice, epSliceList.Items[0]) {
  294. t.Errorf("Expected Endpoint Slice: %v, got: %v", testCase.expectedEndpointSlice, epSliceList.Items[0])
  295. }
  296. }
  297. })
  298. }
  299. }
  300. func generateEndpointsAndSlice(name, namespace string, ports []int, addresses []string) (*corev1.Endpoints, *discovery.EndpointSlice) {
  301. objectMeta := metav1.ObjectMeta{Name: name, Namespace: namespace}
  302. trueBool := true
  303. epSlice := &discovery.EndpointSlice{ObjectMeta: objectMeta, AddressType: discovery.AddressTypeIPv4}
  304. epSlice.Labels = map[string]string{discovery.LabelServiceName: name}
  305. subset := corev1.EndpointSubset{}
  306. for i, port := range ports {
  307. endpointPort := corev1.EndpointPort{
  308. Name: fmt.Sprintf("port-%d", i),
  309. Port: int32(port),
  310. Protocol: corev1.ProtocolTCP,
  311. }
  312. subset.Ports = append(subset.Ports, endpointPort)
  313. epSlice.Ports = append(epSlice.Ports, discovery.EndpointPort{
  314. Name: &endpointPort.Name,
  315. Port: &endpointPort.Port,
  316. Protocol: &endpointPort.Protocol,
  317. })
  318. }
  319. for i, address := range addresses {
  320. endpointAddress := corev1.EndpointAddress{
  321. IP: address,
  322. TargetRef: &corev1.ObjectReference{
  323. Kind: "Pod",
  324. Name: fmt.Sprintf("pod-%d", i),
  325. },
  326. }
  327. subset.Addresses = append(subset.Addresses, endpointAddress)
  328. epSlice.Endpoints = append(epSlice.Endpoints, discovery.Endpoint{
  329. Addresses: []string{endpointAddress.IP},
  330. TargetRef: endpointAddress.TargetRef,
  331. Conditions: discovery.EndpointConditions{Ready: &trueBool},
  332. })
  333. }
  334. return &corev1.Endpoints{
  335. ObjectMeta: objectMeta,
  336. Subsets: []corev1.EndpointSubset{subset},
  337. }, epSlice
  338. }
  339. func TestEndpointsAdapterEnsureEndpointSliceFromEndpoints(t *testing.T) {
  340. endpoints1, epSlice1 := generateEndpointsAndSlice("foo", "testing", []int{80, 443}, []string{"10.1.2.3", "10.1.2.4"})
  341. endpoints2, epSlice2 := generateEndpointsAndSlice("foo", "testing", []int{80, 443}, []string{"10.1.2.3", "10.1.2.4", "10.1.2.5"})
  342. testCases := map[string]struct {
  343. endpointSlicesEnabled bool
  344. expectedError error
  345. expectedEndpointSlice *discovery.EndpointSlice
  346. endpointSlices []*discovery.EndpointSlice
  347. namespaceParam string
  348. endpointsParam *corev1.Endpoints
  349. }{
  350. "existing-endpointslice-no-change": {
  351. endpointSlicesEnabled: true,
  352. expectedError: nil,
  353. expectedEndpointSlice: epSlice1,
  354. endpointSlices: []*discovery.EndpointSlice{epSlice1},
  355. namespaceParam: "testing",
  356. endpointsParam: endpoints1,
  357. },
  358. "existing-endpointslice-change": {
  359. endpointSlicesEnabled: true,
  360. expectedError: nil,
  361. expectedEndpointSlice: epSlice2,
  362. endpointSlices: []*discovery.EndpointSlice{epSlice1},
  363. namespaceParam: "testing",
  364. endpointsParam: endpoints2,
  365. },
  366. "missing-endpointslice": {
  367. endpointSlicesEnabled: true,
  368. expectedError: nil,
  369. expectedEndpointSlice: epSlice1,
  370. endpointSlices: []*discovery.EndpointSlice{},
  371. namespaceParam: "testing",
  372. endpointsParam: endpoints1,
  373. },
  374. "endpointslices-disabled": {
  375. endpointSlicesEnabled: false,
  376. expectedError: nil,
  377. expectedEndpointSlice: nil,
  378. endpointSlices: []*discovery.EndpointSlice{},
  379. namespaceParam: "testing",
  380. endpointsParam: endpoints1,
  381. },
  382. }
  383. for name, testCase := range testCases {
  384. t.Run(name, func(t *testing.T) {
  385. client := fake.NewSimpleClientset()
  386. epAdapter := EndpointsAdapter{endpointClient: client.CoreV1()}
  387. if testCase.endpointSlicesEnabled {
  388. epAdapter.endpointSliceClient = client.DiscoveryV1beta1()
  389. }
  390. for _, endpointSlice := range testCase.endpointSlices {
  391. _, err := client.DiscoveryV1beta1().EndpointSlices(endpointSlice.Namespace).Create(context.TODO(), endpointSlice, metav1.CreateOptions{})
  392. if err != nil {
  393. t.Fatalf("Error creating EndpointSlice: %v", err)
  394. }
  395. }
  396. err := epAdapter.EnsureEndpointSliceFromEndpoints(testCase.namespaceParam, testCase.endpointsParam)
  397. if !apiequality.Semantic.DeepEqual(testCase.expectedError, err) {
  398. t.Errorf("Expected error: %v, got: %v", testCase.expectedError, err)
  399. }
  400. endpointSlice, err := client.DiscoveryV1beta1().EndpointSlices(testCase.namespaceParam).Get(context.TODO(), testCase.endpointsParam.Name, metav1.GetOptions{})
  401. if err != nil && !errors.IsNotFound(err) {
  402. t.Fatalf("Error getting Endpoint Slice: %v", err)
  403. }
  404. if !apiequality.Semantic.DeepEqual(endpointSlice, testCase.expectedEndpointSlice) {
  405. t.Errorf("Expected Endpoint Slice: %v, got: %v", testCase.expectedEndpointSlice, endpointSlice)
  406. }
  407. })
  408. }
  409. }