graceful_termination_test.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 ipvs
  14. import (
  15. "net"
  16. "reflect"
  17. "testing"
  18. utilipvs "k8s.io/kubernetes/pkg/util/ipvs"
  19. utilipvstest "k8s.io/kubernetes/pkg/util/ipvs/testing"
  20. )
  21. func Test_GracefulDeleteRS(t *testing.T) {
  22. tests := []struct {
  23. name string
  24. vs *utilipvs.VirtualServer
  25. rs *utilipvs.RealServer
  26. existingIPVS *utilipvstest.FakeIPVS
  27. expectedIPVS *utilipvstest.FakeIPVS
  28. err error
  29. }{
  30. {
  31. name: "graceful delete, no connections results in deleting the real server immediatetly",
  32. vs: &utilipvs.VirtualServer{
  33. Address: net.ParseIP("1.1.1.1"),
  34. Protocol: "tcp",
  35. Port: uint16(80),
  36. },
  37. rs: &utilipvs.RealServer{
  38. Address: net.ParseIP("10.0.0.1"),
  39. Port: uint16(80),
  40. Weight: 100,
  41. ActiveConn: 0,
  42. InactiveConn: 0,
  43. },
  44. existingIPVS: &utilipvstest.FakeIPVS{
  45. Services: map[utilipvstest.ServiceKey]*utilipvs.VirtualServer{
  46. {
  47. IP: "1.1.1.1",
  48. Port: 80,
  49. Protocol: "tcp",
  50. }: {
  51. Address: net.ParseIP("1.1.1.1"),
  52. Protocol: "tcp",
  53. Port: uint16(80),
  54. },
  55. },
  56. Destinations: map[utilipvstest.ServiceKey][]*utilipvs.RealServer{
  57. {
  58. IP: "1.1.1.1",
  59. Port: 80,
  60. Protocol: "tcp",
  61. }: {
  62. {
  63. Address: net.ParseIP("10.0.0.1"),
  64. Port: uint16(80),
  65. Weight: 100,
  66. ActiveConn: 0,
  67. InactiveConn: 0,
  68. },
  69. },
  70. },
  71. },
  72. expectedIPVS: &utilipvstest.FakeIPVS{
  73. Services: map[utilipvstest.ServiceKey]*utilipvs.VirtualServer{
  74. {
  75. IP: "1.1.1.1",
  76. Port: 80,
  77. Protocol: "tcp",
  78. }: {
  79. Address: net.ParseIP("1.1.1.1"),
  80. Protocol: "tcp",
  81. Port: uint16(80),
  82. },
  83. },
  84. Destinations: map[utilipvstest.ServiceKey][]*utilipvs.RealServer{
  85. {
  86. IP: "1.1.1.1",
  87. Port: 80,
  88. Protocol: "tcp",
  89. }: {},
  90. },
  91. },
  92. err: nil,
  93. },
  94. {
  95. name: "graceful delete, real server has active connections, weight should be 0 but don't delete",
  96. vs: &utilipvs.VirtualServer{
  97. Address: net.ParseIP("1.1.1.1"),
  98. Protocol: "tcp",
  99. Port: uint16(80),
  100. },
  101. rs: &utilipvs.RealServer{
  102. Address: net.ParseIP("10.0.0.1"),
  103. Port: uint16(80),
  104. Weight: 100,
  105. ActiveConn: 10,
  106. InactiveConn: 0,
  107. },
  108. existingIPVS: &utilipvstest.FakeIPVS{
  109. Services: map[utilipvstest.ServiceKey]*utilipvs.VirtualServer{
  110. {
  111. IP: "1.1.1.1",
  112. Port: 80,
  113. Protocol: "tcp",
  114. }: {
  115. Address: net.ParseIP("1.1.1.1"),
  116. Protocol: "tcp",
  117. Port: uint16(80),
  118. },
  119. },
  120. Destinations: map[utilipvstest.ServiceKey][]*utilipvs.RealServer{
  121. {
  122. IP: "1.1.1.1",
  123. Port: 80,
  124. Protocol: "tcp",
  125. }: {
  126. {
  127. Address: net.ParseIP("10.0.0.1"),
  128. Port: uint16(80),
  129. Weight: 100,
  130. ActiveConn: 10,
  131. InactiveConn: 0,
  132. },
  133. },
  134. },
  135. },
  136. expectedIPVS: &utilipvstest.FakeIPVS{
  137. Services: map[utilipvstest.ServiceKey]*utilipvs.VirtualServer{
  138. {
  139. IP: "1.1.1.1",
  140. Port: 80,
  141. Protocol: "tcp",
  142. }: {
  143. Address: net.ParseIP("1.1.1.1"),
  144. Protocol: "tcp",
  145. Port: uint16(80),
  146. },
  147. },
  148. Destinations: map[utilipvstest.ServiceKey][]*utilipvs.RealServer{
  149. {
  150. IP: "1.1.1.1",
  151. Port: 80,
  152. Protocol: "tcp",
  153. }: {
  154. {
  155. Address: net.ParseIP("10.0.0.1"),
  156. Port: uint16(80),
  157. Weight: 0,
  158. ActiveConn: 10,
  159. InactiveConn: 0,
  160. },
  161. },
  162. },
  163. },
  164. err: nil,
  165. },
  166. {
  167. name: "graceful delete, real server has in-active connections, weight should be 0 but don't delete",
  168. vs: &utilipvs.VirtualServer{
  169. Address: net.ParseIP("1.1.1.1"),
  170. Protocol: "tcp",
  171. Port: uint16(80),
  172. },
  173. rs: &utilipvs.RealServer{
  174. Address: net.ParseIP("10.0.0.1"),
  175. Port: uint16(80),
  176. Weight: 100,
  177. ActiveConn: 0,
  178. InactiveConn: 10,
  179. },
  180. existingIPVS: &utilipvstest.FakeIPVS{
  181. Services: map[utilipvstest.ServiceKey]*utilipvs.VirtualServer{
  182. {
  183. IP: "1.1.1.1",
  184. Port: 80,
  185. Protocol: "tcp",
  186. }: {
  187. Address: net.ParseIP("1.1.1.1"),
  188. Protocol: "tcp",
  189. Port: uint16(80),
  190. },
  191. },
  192. Destinations: map[utilipvstest.ServiceKey][]*utilipvs.RealServer{
  193. {
  194. IP: "1.1.1.1",
  195. Port: 80,
  196. Protocol: "tcp",
  197. }: {
  198. {
  199. Address: net.ParseIP("10.0.0.1"),
  200. Port: uint16(80),
  201. Weight: 100,
  202. ActiveConn: 0,
  203. InactiveConn: 10,
  204. },
  205. },
  206. },
  207. },
  208. expectedIPVS: &utilipvstest.FakeIPVS{
  209. Services: map[utilipvstest.ServiceKey]*utilipvs.VirtualServer{
  210. {
  211. IP: "1.1.1.1",
  212. Port: 80,
  213. Protocol: "tcp",
  214. }: {
  215. Address: net.ParseIP("1.1.1.1"),
  216. Protocol: "tcp",
  217. Port: uint16(80),
  218. },
  219. },
  220. Destinations: map[utilipvstest.ServiceKey][]*utilipvs.RealServer{
  221. {
  222. IP: "1.1.1.1",
  223. Port: 80,
  224. Protocol: "tcp",
  225. }: {
  226. {
  227. Address: net.ParseIP("10.0.0.1"),
  228. Port: uint16(80),
  229. Weight: 0,
  230. ActiveConn: 0,
  231. InactiveConn: 10,
  232. },
  233. },
  234. },
  235. },
  236. err: nil,
  237. },
  238. {
  239. name: "graceful delete, real server mismatch should be no-op",
  240. vs: &utilipvs.VirtualServer{
  241. Address: net.ParseIP("1.1.1.1"),
  242. Protocol: "tcp",
  243. Port: uint16(80),
  244. },
  245. rs: &utilipvs.RealServer{
  246. Address: net.ParseIP("10.0.0.1"),
  247. Port: uint16(81), // port mismatched
  248. Weight: 100,
  249. ActiveConn: 0,
  250. InactiveConn: 10,
  251. },
  252. existingIPVS: &utilipvstest.FakeIPVS{
  253. Services: map[utilipvstest.ServiceKey]*utilipvs.VirtualServer{
  254. {
  255. IP: "1.1.1.1",
  256. Port: 80,
  257. Protocol: "tcp",
  258. }: {
  259. Address: net.ParseIP("1.1.1.1"),
  260. Protocol: "tcp",
  261. Port: uint16(80),
  262. },
  263. },
  264. Destinations: map[utilipvstest.ServiceKey][]*utilipvs.RealServer{
  265. {
  266. IP: "1.1.1.1",
  267. Port: 80,
  268. Protocol: "tcp",
  269. }: {
  270. {
  271. Address: net.ParseIP("10.0.0.1"),
  272. Port: uint16(80),
  273. Weight: 100,
  274. ActiveConn: 0,
  275. InactiveConn: 10,
  276. },
  277. },
  278. },
  279. },
  280. expectedIPVS: &utilipvstest.FakeIPVS{
  281. Services: map[utilipvstest.ServiceKey]*utilipvs.VirtualServer{
  282. {
  283. IP: "1.1.1.1",
  284. Port: 80,
  285. Protocol: "tcp",
  286. }: {
  287. Address: net.ParseIP("1.1.1.1"),
  288. Protocol: "tcp",
  289. Port: uint16(80),
  290. },
  291. },
  292. Destinations: map[utilipvstest.ServiceKey][]*utilipvs.RealServer{
  293. {
  294. IP: "1.1.1.1",
  295. Port: 80,
  296. Protocol: "tcp",
  297. }: {
  298. {
  299. Address: net.ParseIP("10.0.0.1"),
  300. Port: uint16(80),
  301. Weight: 100,
  302. ActiveConn: 0,
  303. InactiveConn: 10,
  304. },
  305. },
  306. },
  307. },
  308. err: nil,
  309. },
  310. }
  311. for _, test := range tests {
  312. t.Run(test.name, func(t *testing.T) {
  313. ipvs := test.existingIPVS
  314. gracefulTerminationManager := NewGracefulTerminationManager(ipvs)
  315. err := gracefulTerminationManager.GracefulDeleteRS(test.vs, test.rs)
  316. if err != test.err {
  317. t.Logf("actual err: %v", err)
  318. t.Logf("expected err: %v", test.err)
  319. t.Errorf("unexpected error")
  320. }
  321. if !reflect.DeepEqual(ipvs, test.expectedIPVS) {
  322. t.Logf("actual: %+v", ipvs)
  323. t.Logf("expected : %+v", test.expectedIPVS)
  324. t.Errorf("unexpected IPVS servers")
  325. }
  326. })
  327. }
  328. }