validation_test.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  1. /*
  2. Copyright 2017 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 validation
  14. import (
  15. "fmt"
  16. "runtime"
  17. "strings"
  18. "testing"
  19. "time"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/apimachinery/pkg/util/validation/field"
  22. componentbaseconfig "k8s.io/component-base/config"
  23. kubeproxyconfig "k8s.io/kubernetes/pkg/proxy/apis/config"
  24. "k8s.io/utils/pointer"
  25. )
  26. func TestValidateKubeProxyConfiguration(t *testing.T) {
  27. var proxyMode kubeproxyconfig.ProxyMode
  28. if runtime.GOOS == "windows" {
  29. proxyMode = kubeproxyconfig.ProxyModeKernelspace
  30. } else {
  31. proxyMode = kubeproxyconfig.ProxyModeIPVS
  32. }
  33. successCases := []kubeproxyconfig.KubeProxyConfiguration{
  34. {
  35. BindAddress: "192.168.59.103",
  36. HealthzBindAddress: "0.0.0.0:10256",
  37. MetricsBindAddress: "127.0.0.1:10249",
  38. ClusterCIDR: "192.168.59.0/24",
  39. UDPIdleTimeout: metav1.Duration{Duration: 1 * time.Second},
  40. ConfigSyncPeriod: metav1.Duration{Duration: 1 * time.Second},
  41. IPTables: kubeproxyconfig.KubeProxyIPTablesConfiguration{
  42. MasqueradeAll: true,
  43. SyncPeriod: metav1.Duration{Duration: 5 * time.Second},
  44. MinSyncPeriod: metav1.Duration{Duration: 2 * time.Second},
  45. },
  46. Mode: proxyMode,
  47. IPVS: kubeproxyconfig.KubeProxyIPVSConfiguration{
  48. SyncPeriod: metav1.Duration{Duration: 10 * time.Second},
  49. MinSyncPeriod: metav1.Duration{Duration: 5 * time.Second},
  50. },
  51. Conntrack: kubeproxyconfig.KubeProxyConntrackConfiguration{
  52. MaxPerCore: pointer.Int32Ptr(1),
  53. Min: pointer.Int32Ptr(1),
  54. TCPEstablishedTimeout: &metav1.Duration{Duration: 5 * time.Second},
  55. TCPCloseWaitTimeout: &metav1.Duration{Duration: 5 * time.Second},
  56. },
  57. },
  58. {
  59. BindAddress: "192.168.59.103",
  60. HealthzBindAddress: "0.0.0.0:10256",
  61. MetricsBindAddress: "127.0.0.1:10249",
  62. ClusterCIDR: "192.168.59.0/24",
  63. UDPIdleTimeout: metav1.Duration{Duration: 1 * time.Second},
  64. ConfigSyncPeriod: metav1.Duration{Duration: 1 * time.Second},
  65. IPTables: kubeproxyconfig.KubeProxyIPTablesConfiguration{
  66. MasqueradeAll: true,
  67. SyncPeriod: metav1.Duration{Duration: 5 * time.Second},
  68. MinSyncPeriod: metav1.Duration{Duration: 2 * time.Second},
  69. },
  70. Conntrack: kubeproxyconfig.KubeProxyConntrackConfiguration{
  71. MaxPerCore: pointer.Int32Ptr(1),
  72. Min: pointer.Int32Ptr(1),
  73. TCPEstablishedTimeout: &metav1.Duration{Duration: 5 * time.Second},
  74. TCPCloseWaitTimeout: &metav1.Duration{Duration: 5 * time.Second},
  75. },
  76. },
  77. {
  78. BindAddress: "192.168.59.103",
  79. HealthzBindAddress: "",
  80. MetricsBindAddress: "127.0.0.1:10249",
  81. ClusterCIDR: "192.168.59.0/24",
  82. UDPIdleTimeout: metav1.Duration{Duration: 1 * time.Second},
  83. ConfigSyncPeriod: metav1.Duration{Duration: 1 * time.Second},
  84. IPTables: kubeproxyconfig.KubeProxyIPTablesConfiguration{
  85. MasqueradeAll: true,
  86. SyncPeriod: metav1.Duration{Duration: 5 * time.Second},
  87. MinSyncPeriod: metav1.Duration{Duration: 2 * time.Second},
  88. },
  89. Conntrack: kubeproxyconfig.KubeProxyConntrackConfiguration{
  90. MaxPerCore: pointer.Int32Ptr(1),
  91. Min: pointer.Int32Ptr(1),
  92. TCPEstablishedTimeout: &metav1.Duration{Duration: 5 * time.Second},
  93. TCPCloseWaitTimeout: &metav1.Duration{Duration: 5 * time.Second},
  94. },
  95. },
  96. }
  97. for _, successCase := range successCases {
  98. if errs := Validate(&successCase); len(errs) != 0 {
  99. t.Errorf("expected success: %v", errs)
  100. }
  101. }
  102. errorCases := []struct {
  103. config kubeproxyconfig.KubeProxyConfiguration
  104. msg string
  105. }{
  106. {
  107. config: kubeproxyconfig.KubeProxyConfiguration{
  108. // only BindAddress is invalid
  109. BindAddress: "10.10.12.11:2000",
  110. HealthzBindAddress: "0.0.0.0:10256",
  111. MetricsBindAddress: "127.0.0.1:10249",
  112. ClusterCIDR: "192.168.59.0/24",
  113. UDPIdleTimeout: metav1.Duration{Duration: 1 * time.Second},
  114. ConfigSyncPeriod: metav1.Duration{Duration: 1 * time.Second},
  115. IPTables: kubeproxyconfig.KubeProxyIPTablesConfiguration{
  116. MasqueradeAll: true,
  117. SyncPeriod: metav1.Duration{Duration: 5 * time.Second},
  118. MinSyncPeriod: metav1.Duration{Duration: 2 * time.Second},
  119. },
  120. Conntrack: kubeproxyconfig.KubeProxyConntrackConfiguration{
  121. MaxPerCore: pointer.Int32Ptr(1),
  122. Min: pointer.Int32Ptr(1),
  123. TCPEstablishedTimeout: &metav1.Duration{Duration: 5 * time.Second},
  124. TCPCloseWaitTimeout: &metav1.Duration{Duration: 5 * time.Second},
  125. },
  126. },
  127. msg: "not a valid textual representation of an IP address",
  128. },
  129. {
  130. config: kubeproxyconfig.KubeProxyConfiguration{
  131. BindAddress: "10.10.12.11",
  132. // only HealthzBindAddress is invalid
  133. HealthzBindAddress: "0.0.0.0",
  134. MetricsBindAddress: "127.0.0.1:10249",
  135. ClusterCIDR: "192.168.59.0/24",
  136. UDPIdleTimeout: metav1.Duration{Duration: 1 * time.Second},
  137. ConfigSyncPeriod: metav1.Duration{Duration: 1 * time.Second},
  138. IPTables: kubeproxyconfig.KubeProxyIPTablesConfiguration{
  139. MasqueradeAll: true,
  140. SyncPeriod: metav1.Duration{Duration: 5 * time.Second},
  141. MinSyncPeriod: metav1.Duration{Duration: 2 * time.Second},
  142. },
  143. Conntrack: kubeproxyconfig.KubeProxyConntrackConfiguration{
  144. MaxPerCore: pointer.Int32Ptr(1),
  145. Min: pointer.Int32Ptr(1),
  146. TCPEstablishedTimeout: &metav1.Duration{Duration: 5 * time.Second},
  147. TCPCloseWaitTimeout: &metav1.Duration{Duration: 5 * time.Second},
  148. },
  149. },
  150. msg: "must be IP:port",
  151. },
  152. {
  153. config: kubeproxyconfig.KubeProxyConfiguration{
  154. BindAddress: "10.10.12.11",
  155. HealthzBindAddress: "0.0.0.0:12345",
  156. // only MetricsBindAddress is invalid
  157. MetricsBindAddress: "127.0.0.1",
  158. ClusterCIDR: "192.168.59.0/24",
  159. UDPIdleTimeout: metav1.Duration{Duration: 1 * time.Second},
  160. ConfigSyncPeriod: metav1.Duration{Duration: 1 * time.Second},
  161. IPTables: kubeproxyconfig.KubeProxyIPTablesConfiguration{
  162. MasqueradeAll: true,
  163. SyncPeriod: metav1.Duration{Duration: 5 * time.Second},
  164. MinSyncPeriod: metav1.Duration{Duration: 2 * time.Second},
  165. },
  166. Conntrack: kubeproxyconfig.KubeProxyConntrackConfiguration{
  167. MaxPerCore: pointer.Int32Ptr(1),
  168. Min: pointer.Int32Ptr(1),
  169. TCPEstablishedTimeout: &metav1.Duration{Duration: 5 * time.Second},
  170. TCPCloseWaitTimeout: &metav1.Duration{Duration: 5 * time.Second},
  171. },
  172. },
  173. msg: "must be IP:port",
  174. },
  175. {
  176. config: kubeproxyconfig.KubeProxyConfiguration{
  177. BindAddress: "10.10.12.11",
  178. HealthzBindAddress: "0.0.0.0:12345",
  179. MetricsBindAddress: "127.0.0.1:10249",
  180. // only ClusterCIDR is invalid
  181. ClusterCIDR: "192.168.59.0",
  182. UDPIdleTimeout: metav1.Duration{Duration: 1 * time.Second},
  183. ConfigSyncPeriod: metav1.Duration{Duration: 1 * time.Second},
  184. IPTables: kubeproxyconfig.KubeProxyIPTablesConfiguration{
  185. MasqueradeAll: true,
  186. SyncPeriod: metav1.Duration{Duration: 5 * time.Second},
  187. MinSyncPeriod: metav1.Duration{Duration: 2 * time.Second},
  188. },
  189. Conntrack: kubeproxyconfig.KubeProxyConntrackConfiguration{
  190. MaxPerCore: pointer.Int32Ptr(1),
  191. Min: pointer.Int32Ptr(1),
  192. TCPEstablishedTimeout: &metav1.Duration{Duration: 5 * time.Second},
  193. TCPCloseWaitTimeout: &metav1.Duration{Duration: 5 * time.Second},
  194. },
  195. },
  196. msg: "must be a valid CIDR block (e.g. 10.100.0.0/16)",
  197. },
  198. {
  199. config: kubeproxyconfig.KubeProxyConfiguration{
  200. BindAddress: "10.10.12.11",
  201. HealthzBindAddress: "0.0.0.0:12345",
  202. MetricsBindAddress: "127.0.0.1:10249",
  203. ClusterCIDR: "192.168.59.0/24",
  204. // only UDPIdleTimeout is invalid
  205. UDPIdleTimeout: metav1.Duration{Duration: -1 * time.Second},
  206. ConfigSyncPeriod: metav1.Duration{Duration: 1 * time.Second},
  207. IPTables: kubeproxyconfig.KubeProxyIPTablesConfiguration{
  208. MasqueradeAll: true,
  209. SyncPeriod: metav1.Duration{Duration: 5 * time.Second},
  210. MinSyncPeriod: metav1.Duration{Duration: 2 * time.Second},
  211. },
  212. Conntrack: kubeproxyconfig.KubeProxyConntrackConfiguration{
  213. MaxPerCore: pointer.Int32Ptr(1),
  214. Min: pointer.Int32Ptr(1),
  215. TCPEstablishedTimeout: &metav1.Duration{Duration: 5 * time.Second},
  216. TCPCloseWaitTimeout: &metav1.Duration{Duration: 5 * time.Second},
  217. },
  218. },
  219. msg: "must be greater than 0",
  220. },
  221. {
  222. config: kubeproxyconfig.KubeProxyConfiguration{
  223. BindAddress: "10.10.12.11",
  224. HealthzBindAddress: "0.0.0.0:12345",
  225. MetricsBindAddress: "127.0.0.1:10249",
  226. ClusterCIDR: "192.168.59.0/24",
  227. UDPIdleTimeout: metav1.Duration{Duration: 1 * time.Second},
  228. // only ConfigSyncPeriod is invalid
  229. ConfigSyncPeriod: metav1.Duration{Duration: -1 * time.Second},
  230. IPTables: kubeproxyconfig.KubeProxyIPTablesConfiguration{
  231. MasqueradeAll: true,
  232. SyncPeriod: metav1.Duration{Duration: 5 * time.Second},
  233. MinSyncPeriod: metav1.Duration{Duration: 2 * time.Second},
  234. },
  235. Conntrack: kubeproxyconfig.KubeProxyConntrackConfiguration{
  236. MaxPerCore: pointer.Int32Ptr(1),
  237. Min: pointer.Int32Ptr(1),
  238. TCPEstablishedTimeout: &metav1.Duration{Duration: 5 * time.Second},
  239. TCPCloseWaitTimeout: &metav1.Duration{Duration: 5 * time.Second},
  240. },
  241. },
  242. msg: "must be greater than 0",
  243. },
  244. {
  245. config: kubeproxyconfig.KubeProxyConfiguration{
  246. BindAddress: "192.168.59.103",
  247. HealthzBindAddress: "0.0.0.0:10256",
  248. MetricsBindAddress: "127.0.0.1:10249",
  249. ClusterCIDR: "192.168.59.0/24",
  250. UDPIdleTimeout: metav1.Duration{Duration: 1 * time.Second},
  251. ConfigSyncPeriod: metav1.Duration{Duration: 1 * time.Second},
  252. IPTables: kubeproxyconfig.KubeProxyIPTablesConfiguration{
  253. MasqueradeAll: true,
  254. SyncPeriod: metav1.Duration{Duration: 5 * time.Second},
  255. MinSyncPeriod: metav1.Duration{Duration: 2 * time.Second},
  256. },
  257. // not specifying valid period in IPVS mode.
  258. Mode: kubeproxyconfig.ProxyModeIPVS,
  259. Conntrack: kubeproxyconfig.KubeProxyConntrackConfiguration{
  260. MaxPerCore: pointer.Int32Ptr(1),
  261. Min: pointer.Int32Ptr(1),
  262. TCPEstablishedTimeout: &metav1.Duration{Duration: 5 * time.Second},
  263. TCPCloseWaitTimeout: &metav1.Duration{Duration: 5 * time.Second},
  264. },
  265. },
  266. msg: "must be greater than 0",
  267. },
  268. }
  269. for _, errorCase := range errorCases {
  270. if errs := Validate(&errorCase.config); len(errs) == 0 {
  271. t.Errorf("expected failure for %s", errorCase.msg)
  272. } else if !strings.Contains(errs[0].Error(), errorCase.msg) {
  273. t.Errorf("unexpected error: %v, expected: %s", errs[0], errorCase.msg)
  274. }
  275. }
  276. }
  277. func TestValidateKubeProxyIPTablesConfiguration(t *testing.T) {
  278. valid := int32(5)
  279. successCases := []kubeproxyconfig.KubeProxyIPTablesConfiguration{
  280. {
  281. MasqueradeAll: true,
  282. SyncPeriod: metav1.Duration{Duration: 5 * time.Second},
  283. MinSyncPeriod: metav1.Duration{Duration: 2 * time.Second},
  284. },
  285. {
  286. MasqueradeBit: &valid,
  287. MasqueradeAll: true,
  288. SyncPeriod: metav1.Duration{Duration: 5 * time.Second},
  289. MinSyncPeriod: metav1.Duration{Duration: 2 * time.Second},
  290. },
  291. }
  292. newPath := field.NewPath("KubeProxyConfiguration")
  293. for _, successCase := range successCases {
  294. if errs := validateKubeProxyIPTablesConfiguration(successCase, newPath.Child("KubeProxyIPTablesConfiguration")); len(errs) != 0 {
  295. t.Errorf("expected success: %v", errs)
  296. }
  297. }
  298. invalid := int32(-10)
  299. errorCases := []struct {
  300. config kubeproxyconfig.KubeProxyIPTablesConfiguration
  301. msg string
  302. }{
  303. {
  304. config: kubeproxyconfig.KubeProxyIPTablesConfiguration{
  305. MasqueradeAll: true,
  306. SyncPeriod: metav1.Duration{Duration: -5 * time.Second},
  307. MinSyncPeriod: metav1.Duration{Duration: 2 * time.Second},
  308. },
  309. msg: "must be greater than 0",
  310. },
  311. {
  312. config: kubeproxyconfig.KubeProxyIPTablesConfiguration{
  313. MasqueradeBit: &valid,
  314. MasqueradeAll: true,
  315. SyncPeriod: metav1.Duration{Duration: 5 * time.Second},
  316. MinSyncPeriod: metav1.Duration{Duration: -1 * time.Second},
  317. },
  318. msg: "must be greater than or equal to 0",
  319. },
  320. {
  321. config: kubeproxyconfig.KubeProxyIPTablesConfiguration{
  322. MasqueradeBit: &invalid,
  323. MasqueradeAll: true,
  324. SyncPeriod: metav1.Duration{Duration: 5 * time.Second},
  325. MinSyncPeriod: metav1.Duration{Duration: 2 * time.Second},
  326. },
  327. msg: "must be within the range [0, 31]",
  328. },
  329. // SyncPeriod must be >= MinSyncPeriod
  330. {
  331. config: kubeproxyconfig.KubeProxyIPTablesConfiguration{
  332. MasqueradeBit: &valid,
  333. MasqueradeAll: true,
  334. SyncPeriod: metav1.Duration{Duration: 1 * time.Second},
  335. MinSyncPeriod: metav1.Duration{Duration: 5 * time.Second},
  336. },
  337. msg: fmt.Sprintf("must be greater than or equal to %s", newPath.Child("KubeProxyIPTablesConfiguration").Child("MinSyncPeriod").String()),
  338. },
  339. }
  340. for _, errorCase := range errorCases {
  341. if errs := validateKubeProxyIPTablesConfiguration(errorCase.config, newPath.Child("KubeProxyIPTablesConfiguration")); len(errs) == 0 {
  342. t.Errorf("expected failure for %s", errorCase.msg)
  343. } else if !strings.Contains(errs[0].Error(), errorCase.msg) {
  344. t.Errorf("unexpected error: %v, expected: %s", errs[0], errorCase.msg)
  345. }
  346. }
  347. }
  348. func TestValidateKubeProxyIPVSConfiguration(t *testing.T) {
  349. newPath := field.NewPath("KubeProxyConfiguration")
  350. testCases := []struct {
  351. config kubeproxyconfig.KubeProxyIPVSConfiguration
  352. expectErr bool
  353. reason string
  354. }{
  355. {
  356. config: kubeproxyconfig.KubeProxyIPVSConfiguration{
  357. SyncPeriod: metav1.Duration{Duration: -5 * time.Second},
  358. MinSyncPeriod: metav1.Duration{Duration: 2 * time.Second},
  359. },
  360. expectErr: true,
  361. reason: "SyncPeriod must be greater than 0",
  362. },
  363. {
  364. config: kubeproxyconfig.KubeProxyIPVSConfiguration{
  365. SyncPeriod: metav1.Duration{Duration: 0 * time.Second},
  366. MinSyncPeriod: metav1.Duration{Duration: 10 * time.Second},
  367. },
  368. expectErr: true,
  369. reason: "SyncPeriod must be greater than 0",
  370. },
  371. {
  372. config: kubeproxyconfig.KubeProxyIPVSConfiguration{
  373. SyncPeriod: metav1.Duration{Duration: 5 * time.Second},
  374. MinSyncPeriod: metav1.Duration{Duration: -1 * time.Second},
  375. },
  376. expectErr: true,
  377. reason: "MinSyncPeriod must be greater than or equal to 0",
  378. },
  379. {
  380. config: kubeproxyconfig.KubeProxyIPVSConfiguration{
  381. SyncPeriod: metav1.Duration{Duration: 1 * time.Second},
  382. MinSyncPeriod: metav1.Duration{Duration: 5 * time.Second},
  383. },
  384. expectErr: true,
  385. reason: "SyncPeriod must be greater than or equal to MinSyncPeriod",
  386. },
  387. // SyncPeriod == MinSyncPeriod
  388. {
  389. config: kubeproxyconfig.KubeProxyIPVSConfiguration{
  390. SyncPeriod: metav1.Duration{Duration: 10 * time.Second},
  391. MinSyncPeriod: metav1.Duration{Duration: 10 * time.Second},
  392. },
  393. expectErr: false,
  394. },
  395. // SyncPeriod > MinSyncPeriod
  396. {
  397. config: kubeproxyconfig.KubeProxyIPVSConfiguration{
  398. SyncPeriod: metav1.Duration{Duration: 10 * time.Second},
  399. MinSyncPeriod: metav1.Duration{Duration: 5 * time.Second},
  400. },
  401. expectErr: false,
  402. },
  403. // SyncPeriod can be 0
  404. {
  405. config: kubeproxyconfig.KubeProxyIPVSConfiguration{
  406. SyncPeriod: metav1.Duration{Duration: 5 * time.Second},
  407. MinSyncPeriod: metav1.Duration{Duration: 0 * time.Second},
  408. },
  409. expectErr: false,
  410. },
  411. }
  412. for _, test := range testCases {
  413. errs := validateKubeProxyIPVSConfiguration(test.config, newPath.Child("KubeProxyIPVSConfiguration"))
  414. if len(errs) == 0 && test.expectErr {
  415. t.Errorf("Expect error, got nil, reason: %s", test.reason)
  416. }
  417. if len(errs) > 0 && !test.expectErr {
  418. t.Errorf("Unexpected error: %v", errs)
  419. }
  420. }
  421. }
  422. func TestValidateKubeProxyConntrackConfiguration(t *testing.T) {
  423. successCases := []kubeproxyconfig.KubeProxyConntrackConfiguration{
  424. {
  425. MaxPerCore: pointer.Int32Ptr(1),
  426. Min: pointer.Int32Ptr(1),
  427. TCPEstablishedTimeout: &metav1.Duration{Duration: 5 * time.Second},
  428. TCPCloseWaitTimeout: &metav1.Duration{Duration: 5 * time.Second},
  429. },
  430. {
  431. MaxPerCore: pointer.Int32Ptr(0),
  432. Min: pointer.Int32Ptr(0),
  433. TCPEstablishedTimeout: &metav1.Duration{Duration: 0 * time.Second},
  434. TCPCloseWaitTimeout: &metav1.Duration{Duration: 0 * time.Second},
  435. },
  436. }
  437. newPath := field.NewPath("KubeProxyConfiguration")
  438. for _, successCase := range successCases {
  439. if errs := validateKubeProxyConntrackConfiguration(successCase, newPath.Child("KubeProxyConntrackConfiguration")); len(errs) != 0 {
  440. t.Errorf("expected success: %v", errs)
  441. }
  442. }
  443. errorCases := []struct {
  444. config kubeproxyconfig.KubeProxyConntrackConfiguration
  445. msg string
  446. }{
  447. {
  448. config: kubeproxyconfig.KubeProxyConntrackConfiguration{
  449. MaxPerCore: pointer.Int32Ptr(-1),
  450. Min: pointer.Int32Ptr(1),
  451. TCPEstablishedTimeout: &metav1.Duration{Duration: 5 * time.Second},
  452. TCPCloseWaitTimeout: &metav1.Duration{Duration: 5 * time.Second},
  453. },
  454. msg: "must be greater than or equal to 0",
  455. },
  456. {
  457. config: kubeproxyconfig.KubeProxyConntrackConfiguration{
  458. MaxPerCore: pointer.Int32Ptr(1),
  459. Min: pointer.Int32Ptr(-1),
  460. TCPEstablishedTimeout: &metav1.Duration{Duration: 5 * time.Second},
  461. TCPCloseWaitTimeout: &metav1.Duration{Duration: 5 * time.Second},
  462. },
  463. msg: "must be greater than or equal to 0",
  464. },
  465. {
  466. config: kubeproxyconfig.KubeProxyConntrackConfiguration{
  467. MaxPerCore: pointer.Int32Ptr(1),
  468. Min: pointer.Int32Ptr(3),
  469. TCPEstablishedTimeout: &metav1.Duration{Duration: -5 * time.Second},
  470. TCPCloseWaitTimeout: &metav1.Duration{Duration: 5 * time.Second},
  471. },
  472. msg: "must be greater than or equal to 0",
  473. },
  474. {
  475. config: kubeproxyconfig.KubeProxyConntrackConfiguration{
  476. MaxPerCore: pointer.Int32Ptr(1),
  477. Min: pointer.Int32Ptr(3),
  478. TCPEstablishedTimeout: &metav1.Duration{Duration: 5 * time.Second},
  479. TCPCloseWaitTimeout: &metav1.Duration{Duration: -5 * time.Second},
  480. },
  481. msg: "must be greater than or equal to 0",
  482. },
  483. }
  484. for _, errorCase := range errorCases {
  485. if errs := validateKubeProxyConntrackConfiguration(errorCase.config, newPath.Child("KubeProxyConntrackConfiguration")); len(errs) == 0 {
  486. t.Errorf("expected failure for %s", errorCase.msg)
  487. } else if !strings.Contains(errs[0].Error(), errorCase.msg) {
  488. t.Errorf("unexpected error: %v, expected: %s", errs[0], errorCase.msg)
  489. }
  490. }
  491. }
  492. func TestValidateProxyMode(t *testing.T) {
  493. newPath := field.NewPath("KubeProxyConfiguration")
  494. successCases := []kubeproxyconfig.ProxyMode{
  495. kubeproxyconfig.ProxyModeUserspace,
  496. kubeproxyconfig.ProxyMode(""),
  497. }
  498. if runtime.GOOS == "windows" {
  499. successCases = append(successCases, kubeproxyconfig.ProxyModeKernelspace)
  500. } else {
  501. successCases = append(successCases, kubeproxyconfig.ProxyModeIPTables, kubeproxyconfig.ProxyModeIPVS)
  502. }
  503. for _, successCase := range successCases {
  504. if errs := validateProxyMode(successCase, newPath.Child("ProxyMode")); len(errs) != 0 {
  505. t.Errorf("expected success: %v", errs)
  506. }
  507. }
  508. errorCases := []struct {
  509. mode kubeproxyconfig.ProxyMode
  510. msg string
  511. }{
  512. {
  513. mode: kubeproxyconfig.ProxyMode("non-existing"),
  514. msg: "or blank (blank means the",
  515. },
  516. }
  517. for _, errorCase := range errorCases {
  518. if errs := validateProxyMode(errorCase.mode, newPath.Child("ProxyMode")); len(errs) == 0 {
  519. t.Errorf("expected failure %s for %v", errorCase.msg, errorCase.mode)
  520. } else if !strings.Contains(errs[0].Error(), errorCase.msg) {
  521. t.Errorf("unexpected error: %v, expected: %s", errs[0], errorCase.msg)
  522. }
  523. }
  524. }
  525. func TestValidateClientConnectionConfiguration(t *testing.T) {
  526. newPath := field.NewPath("KubeProxyConfiguration")
  527. successCases := []componentbaseconfig.ClientConnectionConfiguration{
  528. {
  529. Burst: 0,
  530. },
  531. {
  532. Burst: 5,
  533. },
  534. }
  535. for _, successCase := range successCases {
  536. if errs := validateClientConnectionConfiguration(successCase, newPath.Child("Burst")); len(errs) != 0 {
  537. t.Errorf("expected success: %v", errs)
  538. }
  539. }
  540. errorCases := []struct {
  541. ccc componentbaseconfig.ClientConnectionConfiguration
  542. msg string
  543. }{
  544. {
  545. ccc: componentbaseconfig.ClientConnectionConfiguration{Burst: -5},
  546. msg: "must be greater than or equal to 0",
  547. },
  548. }
  549. for _, errorCase := range errorCases {
  550. if errs := validateClientConnectionConfiguration(errorCase.ccc, newPath.Child("Burst")); len(errs) == 0 {
  551. t.Errorf("expected failure for %s", errorCase.msg)
  552. } else if !strings.Contains(errs[0].Error(), errorCase.msg) {
  553. t.Errorf("unexpected error: %v, expected: %s", errs[0], errorCase.msg)
  554. }
  555. }
  556. }
  557. func TestValidateHostPort(t *testing.T) {
  558. newPath := field.NewPath("KubeProxyConfiguration")
  559. successCases := []string{
  560. "0.0.0.0:10256",
  561. "127.0.0.1:10256",
  562. "10.10.10.10:10256",
  563. }
  564. for _, successCase := range successCases {
  565. if errs := validateHostPort(successCase, newPath.Child("HealthzBindAddress")); len(errs) != 0 {
  566. t.Errorf("expected success: %v", errs)
  567. }
  568. }
  569. errorCases := []struct {
  570. ccc string
  571. msg string
  572. }{
  573. {
  574. ccc: "10.10.10.10",
  575. msg: "must be IP:port",
  576. },
  577. {
  578. ccc: "123.456.789.10:12345",
  579. msg: "must be a valid IP",
  580. },
  581. {
  582. ccc: "10.10.10.10:foo",
  583. msg: "must be a valid port",
  584. },
  585. {
  586. ccc: "10.10.10.10:0",
  587. msg: "must be a valid port",
  588. },
  589. {
  590. ccc: "10.10.10.10:65536",
  591. msg: "must be a valid port",
  592. },
  593. }
  594. for _, errorCase := range errorCases {
  595. if errs := validateHostPort(errorCase.ccc, newPath.Child("HealthzBindAddress")); len(errs) == 0 {
  596. t.Errorf("expected failure for %s", errorCase.msg)
  597. } else if !strings.Contains(errs[0].Error(), errorCase.msg) {
  598. t.Errorf("unexpected error: %v, expected: %s", errs[0], errorCase.msg)
  599. }
  600. }
  601. }
  602. func TestValidateIPVSSchedulerMethod(t *testing.T) {
  603. newPath := field.NewPath("KubeProxyConfiguration")
  604. successCases := []kubeproxyconfig.IPVSSchedulerMethod{
  605. kubeproxyconfig.RoundRobin,
  606. kubeproxyconfig.WeightedRoundRobin,
  607. kubeproxyconfig.LeastConnection,
  608. kubeproxyconfig.WeightedLeastConnection,
  609. kubeproxyconfig.LocalityBasedLeastConnection,
  610. kubeproxyconfig.LocalityBasedLeastConnectionWithReplication,
  611. kubeproxyconfig.SourceHashing,
  612. kubeproxyconfig.DestinationHashing,
  613. kubeproxyconfig.ShortestExpectedDelay,
  614. kubeproxyconfig.NeverQueue,
  615. "",
  616. }
  617. for _, successCase := range successCases {
  618. if errs := validateIPVSSchedulerMethod(successCase, newPath.Child("Scheduler")); len(errs) != 0 {
  619. t.Errorf("expected success: %v", errs)
  620. }
  621. }
  622. errorCases := []struct {
  623. mode kubeproxyconfig.IPVSSchedulerMethod
  624. msg string
  625. }{
  626. {
  627. mode: kubeproxyconfig.IPVSSchedulerMethod("non-existing"),
  628. msg: "blank means the default algorithm method (currently rr)",
  629. },
  630. }
  631. for _, errorCase := range errorCases {
  632. if errs := validateIPVSSchedulerMethod(errorCase.mode, newPath.Child("ProxyMode")); len(errs) == 0 {
  633. t.Errorf("expected failure for %s", errorCase.msg)
  634. } else if !strings.Contains(errs[0].Error(), errorCase.msg) {
  635. t.Errorf("unexpected error: %v, expected: %s", errs[0], errorCase.msg)
  636. }
  637. }
  638. }
  639. func TestValidateKubeProxyNodePortAddress(t *testing.T) {
  640. newPath := field.NewPath("KubeProxyConfiguration")
  641. successCases := []struct {
  642. addresses []string
  643. }{
  644. {[]string{}},
  645. {[]string{"127.0.0.0/8"}},
  646. {[]string{"0.0.0.0/0"}},
  647. {[]string{"::/0"}},
  648. {[]string{"127.0.0.1/32", "1.2.3.0/24"}},
  649. {[]string{"127.0.0.0/8"}},
  650. {[]string{"127.0.0.1/32"}},
  651. {[]string{"::1/128"}},
  652. {[]string{"1.2.3.4/32"}},
  653. {[]string{"10.20.30.0/24"}},
  654. {[]string{"10.20.0.0/16", "100.200.0.0/16"}},
  655. {[]string{"10.0.0.0/8"}},
  656. {[]string{"2001:db8::/32"}},
  657. }
  658. for _, successCase := range successCases {
  659. if errs := validateKubeProxyNodePortAddress(successCase.addresses, newPath.Child("NodePortAddresses")); len(errs) != 0 {
  660. t.Errorf("expected success: %v", errs)
  661. }
  662. }
  663. errorCases := []struct {
  664. addresses []string
  665. msg string
  666. }{
  667. {
  668. addresses: []string{"foo"},
  669. msg: "must be a valid IP block",
  670. },
  671. {
  672. addresses: []string{"1.2.3"},
  673. msg: "must be a valid IP block",
  674. },
  675. {
  676. addresses: []string{""},
  677. msg: "must be a valid IP block",
  678. },
  679. {
  680. addresses: []string{"10.20.30.40"},
  681. msg: "must be a valid IP block",
  682. },
  683. {
  684. addresses: []string{"::1"},
  685. msg: "must be a valid IP block",
  686. },
  687. {
  688. addresses: []string{"2001:db8:1"},
  689. msg: "must be a valid IP block",
  690. },
  691. {
  692. addresses: []string{"2001:db8:xyz/64"},
  693. msg: "must be a valid IP block",
  694. },
  695. }
  696. for _, errorCase := range errorCases {
  697. if errs := validateKubeProxyNodePortAddress(errorCase.addresses, newPath.Child("NodePortAddresses")); len(errs) == 0 {
  698. t.Errorf("expected failure for %s", errorCase.msg)
  699. } else if !strings.Contains(errs[0].Error(), errorCase.msg) {
  700. t.Errorf("unexpected error: %v, expected: %s", errs[0], errorCase.msg)
  701. }
  702. }
  703. }
  704. func TestValidateKubeProxyExcludeCIDRs(t *testing.T) {
  705. // TODO(rramkumar): This test is a copy of TestValidateKubeProxyNodePortAddress.
  706. // Maybe some code can be shared?
  707. newPath := field.NewPath("KubeProxyConfiguration")
  708. successCases := []struct {
  709. addresses []string
  710. }{
  711. {[]string{}},
  712. {[]string{"127.0.0.0/8"}},
  713. {[]string{"0.0.0.0/0"}},
  714. {[]string{"::/0"}},
  715. {[]string{"127.0.0.1/32", "1.2.3.0/24"}},
  716. {[]string{"127.0.0.0/8"}},
  717. {[]string{"127.0.0.1/32"}},
  718. {[]string{"::1/128"}},
  719. {[]string{"1.2.3.4/32"}},
  720. {[]string{"10.20.30.0/24"}},
  721. {[]string{"10.20.0.0/16", "100.200.0.0/16"}},
  722. {[]string{"10.0.0.0/8"}},
  723. {[]string{"2001:db8::/32"}},
  724. }
  725. for _, successCase := range successCases {
  726. if errs := validateIPVSExcludeCIDRs(successCase.addresses, newPath.Child("ExcludeCIDRs")); len(errs) != 0 {
  727. t.Errorf("expected success: %v", errs)
  728. }
  729. }
  730. errorCases := []struct {
  731. addresses []string
  732. msg string
  733. }{
  734. {
  735. addresses: []string{"foo"},
  736. msg: "must be a valid IP block",
  737. },
  738. {
  739. addresses: []string{"1.2.3"},
  740. msg: "must be a valid IP block",
  741. },
  742. {
  743. addresses: []string{""},
  744. msg: "must be a valid IP block",
  745. },
  746. {
  747. addresses: []string{"10.20.30.40"},
  748. msg: "must be a valid IP block",
  749. },
  750. {
  751. addresses: []string{"::1"},
  752. msg: "must be a valid IP block",
  753. },
  754. {
  755. addresses: []string{"2001:db8:1"},
  756. msg: "must be a valid IP block",
  757. },
  758. {
  759. addresses: []string{"2001:db8:xyz/64"},
  760. msg: "must be a valid IP block",
  761. },
  762. }
  763. for _, errorCase := range errorCases {
  764. if errs := validateIPVSExcludeCIDRs(errorCase.addresses, newPath.Child("ExcludeCIDRs")); len(errs) == 0 {
  765. t.Errorf("expected failure for %s", errorCase.msg)
  766. } else if !strings.Contains(errs[0].Error(), errorCase.msg) {
  767. t.Errorf("unexpected error: %v, expected: %s", errs[0], errorCase.msg)
  768. }
  769. }
  770. }