admission_test.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 defaulttolerationseconds
  14. import (
  15. "testing"
  16. "k8s.io/apiserver/pkg/admission"
  17. admissiontesting "k8s.io/apiserver/pkg/admission/testing"
  18. api "k8s.io/kubernetes/pkg/apis/core"
  19. "k8s.io/kubernetes/pkg/apis/core/helper"
  20. schedulerapi "k8s.io/kubernetes/pkg/scheduler/api"
  21. )
  22. func TestForgivenessAdmission(t *testing.T) {
  23. var defaultTolerationSeconds int64 = 300
  24. genTolerationSeconds := func(s int64) *int64 {
  25. return &s
  26. }
  27. handler := admissiontesting.WithReinvocationTesting(t, NewDefaultTolerationSeconds())
  28. // NOTE: for anyone who want to modify this test, the order of tolerations matters!
  29. tests := []struct {
  30. description string
  31. requestedPod api.Pod
  32. expectedPod api.Pod
  33. }{
  34. {
  35. description: "pod has no tolerations, expect add tolerations for `not-ready:NoExecute` and `unreachable:NoExecute`",
  36. requestedPod: api.Pod{
  37. Spec: api.PodSpec{},
  38. },
  39. expectedPod: api.Pod{
  40. Spec: api.PodSpec{
  41. Tolerations: []api.Toleration{
  42. {
  43. Key: schedulerapi.TaintNodeNotReady,
  44. Operator: api.TolerationOpExists,
  45. Effect: api.TaintEffectNoExecute,
  46. TolerationSeconds: &defaultTolerationSeconds,
  47. },
  48. {
  49. Key: schedulerapi.TaintNodeUnreachable,
  50. Operator: api.TolerationOpExists,
  51. Effect: api.TaintEffectNoExecute,
  52. TolerationSeconds: &defaultTolerationSeconds,
  53. },
  54. },
  55. },
  56. },
  57. },
  58. {
  59. description: "pod has tolerations, but none is for taint `not-ready:NoExecute` or `unreachable:NoExecute`, expect add tolerations for `not-ready:NoExecute` and `unreachable:NoExecute`",
  60. requestedPod: api.Pod{
  61. Spec: api.PodSpec{
  62. Tolerations: []api.Toleration{
  63. {
  64. Key: "foo",
  65. Operator: api.TolerationOpEqual,
  66. Value: "bar",
  67. Effect: api.TaintEffectNoSchedule,
  68. TolerationSeconds: genTolerationSeconds(700),
  69. },
  70. },
  71. },
  72. },
  73. expectedPod: api.Pod{
  74. Spec: api.PodSpec{
  75. Tolerations: []api.Toleration{
  76. {
  77. Key: "foo",
  78. Operator: api.TolerationOpEqual,
  79. Value: "bar",
  80. Effect: api.TaintEffectNoSchedule,
  81. TolerationSeconds: genTolerationSeconds(700),
  82. },
  83. {
  84. Key: schedulerapi.TaintNodeNotReady,
  85. Operator: api.TolerationOpExists,
  86. Effect: api.TaintEffectNoExecute,
  87. TolerationSeconds: &defaultTolerationSeconds,
  88. },
  89. {
  90. Key: schedulerapi.TaintNodeUnreachable,
  91. Operator: api.TolerationOpExists,
  92. Effect: api.TaintEffectNoExecute,
  93. TolerationSeconds: &defaultTolerationSeconds,
  94. },
  95. },
  96. },
  97. },
  98. },
  99. {
  100. description: "pod specified a toleration for taint `not-ready:NoExecute`, expect add toleration for `unreachable:NoExecute`",
  101. requestedPod: api.Pod{
  102. Spec: api.PodSpec{
  103. Tolerations: []api.Toleration{
  104. {
  105. Key: schedulerapi.TaintNodeNotReady,
  106. Operator: api.TolerationOpExists,
  107. Effect: api.TaintEffectNoExecute,
  108. TolerationSeconds: genTolerationSeconds(700),
  109. },
  110. },
  111. },
  112. },
  113. expectedPod: api.Pod{
  114. Spec: api.PodSpec{
  115. Tolerations: []api.Toleration{
  116. {
  117. Key: schedulerapi.TaintNodeNotReady,
  118. Operator: api.TolerationOpExists,
  119. Effect: api.TaintEffectNoExecute,
  120. TolerationSeconds: genTolerationSeconds(700),
  121. },
  122. {
  123. Key: schedulerapi.TaintNodeUnreachable,
  124. Operator: api.TolerationOpExists,
  125. Effect: api.TaintEffectNoExecute,
  126. TolerationSeconds: &defaultTolerationSeconds,
  127. },
  128. },
  129. },
  130. },
  131. },
  132. {
  133. description: "pod specified a toleration for taint `unreachable:NoExecute`, expect add toleration for `not-ready:NoExecute`",
  134. requestedPod: api.Pod{
  135. Spec: api.PodSpec{
  136. Tolerations: []api.Toleration{
  137. {
  138. Key: schedulerapi.TaintNodeUnreachable,
  139. Operator: api.TolerationOpExists,
  140. Effect: api.TaintEffectNoExecute,
  141. TolerationSeconds: genTolerationSeconds(700),
  142. },
  143. },
  144. },
  145. },
  146. expectedPod: api.Pod{
  147. Spec: api.PodSpec{
  148. Tolerations: []api.Toleration{
  149. {
  150. Key: schedulerapi.TaintNodeUnreachable,
  151. Operator: api.TolerationOpExists,
  152. Effect: api.TaintEffectNoExecute,
  153. TolerationSeconds: genTolerationSeconds(700),
  154. },
  155. {
  156. Key: schedulerapi.TaintNodeNotReady,
  157. Operator: api.TolerationOpExists,
  158. Effect: api.TaintEffectNoExecute,
  159. TolerationSeconds: &defaultTolerationSeconds,
  160. },
  161. },
  162. },
  163. },
  164. },
  165. {
  166. description: "pod specified tolerations for both `not-ready:NoExecute` and `unreachable:NoExecute`, expect no change",
  167. requestedPod: api.Pod{
  168. Spec: api.PodSpec{
  169. Tolerations: []api.Toleration{
  170. {
  171. Key: schedulerapi.TaintNodeNotReady,
  172. Operator: api.TolerationOpExists,
  173. Effect: api.TaintEffectNoExecute,
  174. TolerationSeconds: genTolerationSeconds(700),
  175. },
  176. {
  177. Key: schedulerapi.TaintNodeUnreachable,
  178. Operator: api.TolerationOpExists,
  179. Effect: api.TaintEffectNoExecute,
  180. TolerationSeconds: genTolerationSeconds(700),
  181. },
  182. },
  183. },
  184. },
  185. expectedPod: api.Pod{
  186. Spec: api.PodSpec{
  187. Tolerations: []api.Toleration{
  188. {
  189. Key: schedulerapi.TaintNodeNotReady,
  190. Operator: api.TolerationOpExists,
  191. Effect: api.TaintEffectNoExecute,
  192. TolerationSeconds: genTolerationSeconds(700),
  193. },
  194. {
  195. Key: schedulerapi.TaintNodeUnreachable,
  196. Operator: api.TolerationOpExists,
  197. Effect: api.TaintEffectNoExecute,
  198. TolerationSeconds: genTolerationSeconds(700),
  199. },
  200. },
  201. },
  202. },
  203. },
  204. {
  205. description: "pod specified toleration for taint `unreachable`, expect add toleration for `not-ready:NoExecute`",
  206. requestedPod: api.Pod{
  207. Spec: api.PodSpec{
  208. Tolerations: []api.Toleration{
  209. {
  210. Key: schedulerapi.TaintNodeUnreachable,
  211. Operator: api.TolerationOpExists,
  212. TolerationSeconds: genTolerationSeconds(700),
  213. },
  214. },
  215. },
  216. },
  217. expectedPod: api.Pod{
  218. Spec: api.PodSpec{
  219. Tolerations: []api.Toleration{
  220. {
  221. Key: schedulerapi.TaintNodeUnreachable,
  222. Operator: api.TolerationOpExists,
  223. TolerationSeconds: genTolerationSeconds(700),
  224. },
  225. {
  226. Key: schedulerapi.TaintNodeNotReady,
  227. Operator: api.TolerationOpExists,
  228. Effect: api.TaintEffectNoExecute,
  229. TolerationSeconds: genTolerationSeconds(300),
  230. },
  231. },
  232. },
  233. },
  234. },
  235. {
  236. description: "pod has wildcard toleration for all kind of taints, expect no change",
  237. requestedPod: api.Pod{
  238. Spec: api.PodSpec{
  239. Tolerations: []api.Toleration{
  240. {Operator: api.TolerationOpExists, TolerationSeconds: genTolerationSeconds(700)},
  241. },
  242. },
  243. },
  244. expectedPod: api.Pod{
  245. Spec: api.PodSpec{
  246. Tolerations: []api.Toleration{
  247. {
  248. Operator: api.TolerationOpExists,
  249. TolerationSeconds: genTolerationSeconds(700),
  250. },
  251. },
  252. },
  253. },
  254. },
  255. }
  256. for _, test := range tests {
  257. err := handler.Admit(admission.NewAttributesRecord(&test.requestedPod, nil, api.Kind("Pod").WithVersion("version"), "foo", "name", api.Resource("pods").WithVersion("version"), "", "ignored", nil, false, nil), nil)
  258. if err != nil {
  259. t.Errorf("[%s]: unexpected error %v for pod %+v", test.description, err, test.requestedPod)
  260. }
  261. if !helper.Semantic.DeepEqual(test.expectedPod.Spec.Tolerations, test.requestedPod.Spec.Tolerations) {
  262. t.Errorf("[%s]: expected %#v got %#v", test.description, test.expectedPod.Spec.Tolerations, test.requestedPod.Spec.Tolerations)
  263. }
  264. }
  265. }
  266. func TestHandles(t *testing.T) {
  267. handler := NewDefaultTolerationSeconds()
  268. tests := map[admission.Operation]bool{
  269. admission.Update: true,
  270. admission.Create: true,
  271. admission.Delete: false,
  272. admission.Connect: false,
  273. }
  274. for op, expected := range tests {
  275. result := handler.Handles(op)
  276. if result != expected {
  277. t.Errorf("Unexpected result for operation %s: %v\n", op, result)
  278. }
  279. }
  280. }