admission_test.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. Copyright 2016 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 antiaffinity
  14. import (
  15. "testing"
  16. v1 "k8s.io/api/core/v1"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. "k8s.io/apimachinery/pkg/runtime"
  19. "k8s.io/apiserver/pkg/admission"
  20. api "k8s.io/kubernetes/pkg/apis/core"
  21. )
  22. // ensures the hard PodAntiAffinity is denied if it defines TopologyKey other than kubernetes.io/hostname.
  23. // TODO: Add test case "invalid topologyKey in requiredDuringSchedulingRequiredDuringExecution then admission fails"
  24. // after RequiredDuringSchedulingRequiredDuringExecution is implemented.
  25. func TestInterPodAffinityAdmission(t *testing.T) {
  26. handler := NewInterPodAntiAffinity()
  27. pod := api.Pod{
  28. Spec: api.PodSpec{},
  29. }
  30. tests := []struct {
  31. affinity *api.Affinity
  32. errorExpected bool
  33. }{
  34. // empty affinity its success.
  35. {
  36. affinity: &api.Affinity{},
  37. errorExpected: false,
  38. },
  39. // what ever topologyKey in preferredDuringSchedulingIgnoredDuringExecution, the admission should success.
  40. {
  41. affinity: &api.Affinity{
  42. PodAntiAffinity: &api.PodAntiAffinity{
  43. PreferredDuringSchedulingIgnoredDuringExecution: []api.WeightedPodAffinityTerm{
  44. {
  45. Weight: 5,
  46. PodAffinityTerm: api.PodAffinityTerm{
  47. LabelSelector: &metav1.LabelSelector{
  48. MatchExpressions: []metav1.LabelSelectorRequirement{
  49. {
  50. Key: "security",
  51. Operator: metav1.LabelSelectorOpIn,
  52. Values: []string{"S2"},
  53. },
  54. },
  55. },
  56. TopologyKey: "az",
  57. },
  58. },
  59. },
  60. },
  61. },
  62. errorExpected: false,
  63. },
  64. // valid topologyKey in requiredDuringSchedulingIgnoredDuringExecution,
  65. // plus any topologyKey in preferredDuringSchedulingIgnoredDuringExecution, then admission success.
  66. {
  67. affinity: &api.Affinity{
  68. PodAntiAffinity: &api.PodAntiAffinity{
  69. PreferredDuringSchedulingIgnoredDuringExecution: []api.WeightedPodAffinityTerm{
  70. {
  71. Weight: 5,
  72. PodAffinityTerm: api.PodAffinityTerm{
  73. LabelSelector: &metav1.LabelSelector{
  74. MatchExpressions: []metav1.LabelSelectorRequirement{
  75. {
  76. Key: "security",
  77. Operator: metav1.LabelSelectorOpIn,
  78. Values: []string{"S2"},
  79. },
  80. },
  81. },
  82. TopologyKey: "az",
  83. },
  84. },
  85. },
  86. RequiredDuringSchedulingIgnoredDuringExecution: []api.PodAffinityTerm{
  87. {
  88. LabelSelector: &metav1.LabelSelector{
  89. MatchExpressions: []metav1.LabelSelectorRequirement{
  90. {
  91. Key: "security",
  92. Operator: metav1.LabelSelectorOpIn,
  93. Values: []string{"S2"},
  94. },
  95. },
  96. },
  97. TopologyKey: v1.LabelHostname,
  98. },
  99. },
  100. },
  101. },
  102. errorExpected: false,
  103. },
  104. // valid topologyKey in requiredDuringSchedulingIgnoredDuringExecution then admission success.
  105. {
  106. affinity: &api.Affinity{
  107. PodAntiAffinity: &api.PodAntiAffinity{
  108. RequiredDuringSchedulingIgnoredDuringExecution: []api.PodAffinityTerm{
  109. {
  110. LabelSelector: &metav1.LabelSelector{
  111. MatchExpressions: []metav1.LabelSelectorRequirement{
  112. {
  113. Key: "security",
  114. Operator: metav1.LabelSelectorOpIn,
  115. Values: []string{"S2"},
  116. },
  117. },
  118. },
  119. TopologyKey: v1.LabelHostname,
  120. },
  121. },
  122. },
  123. },
  124. errorExpected: false,
  125. },
  126. // invalid topologyKey in requiredDuringSchedulingIgnoredDuringExecution then admission fails.
  127. {
  128. affinity: &api.Affinity{
  129. PodAntiAffinity: &api.PodAntiAffinity{
  130. RequiredDuringSchedulingIgnoredDuringExecution: []api.PodAffinityTerm{
  131. {
  132. LabelSelector: &metav1.LabelSelector{
  133. MatchExpressions: []metav1.LabelSelectorRequirement{
  134. {
  135. Key: "security",
  136. Operator: metav1.LabelSelectorOpIn,
  137. Values: []string{"S2"},
  138. },
  139. },
  140. },
  141. TopologyKey: " zone ",
  142. },
  143. },
  144. },
  145. },
  146. errorExpected: true,
  147. },
  148. // list of requiredDuringSchedulingIgnoredDuringExecution middle element topologyKey is not valid.
  149. {
  150. affinity: &api.Affinity{
  151. PodAntiAffinity: &api.PodAntiAffinity{
  152. RequiredDuringSchedulingIgnoredDuringExecution: []api.PodAffinityTerm{
  153. {
  154. LabelSelector: &metav1.LabelSelector{
  155. MatchExpressions: []metav1.LabelSelectorRequirement{
  156. {
  157. Key: "security",
  158. Operator: metav1.LabelSelectorOpIn,
  159. Values: []string{"S2"},
  160. },
  161. },
  162. },
  163. TopologyKey: v1.LabelHostname,
  164. }, {
  165. LabelSelector: &metav1.LabelSelector{
  166. MatchExpressions: []metav1.LabelSelectorRequirement{
  167. {
  168. Key: "security",
  169. Operator: metav1.LabelSelectorOpIn,
  170. Values: []string{"S2"},
  171. },
  172. },
  173. },
  174. TopologyKey: " zone ",
  175. }, {
  176. LabelSelector: &metav1.LabelSelector{
  177. MatchExpressions: []metav1.LabelSelectorRequirement{
  178. {
  179. Key: "security",
  180. Operator: metav1.LabelSelectorOpIn,
  181. Values: []string{"S2"},
  182. },
  183. },
  184. },
  185. TopologyKey: v1.LabelHostname,
  186. },
  187. },
  188. },
  189. },
  190. errorExpected: true,
  191. },
  192. }
  193. for _, test := range tests {
  194. pod.Spec.Affinity = test.affinity
  195. err := handler.Validate(admission.NewAttributesRecord(&pod, nil, api.Kind("Pod").WithVersion("version"), "foo", "name", api.Resource("pods").WithVersion("version"), "", "ignored", nil, false, nil), nil)
  196. if test.errorExpected && err == nil {
  197. t.Errorf("Expected error for Anti Affinity %+v but did not get an error", test.affinity)
  198. }
  199. if !test.errorExpected && err != nil {
  200. t.Errorf("Unexpected error %v for AntiAffinity %+v", err, test.affinity)
  201. }
  202. }
  203. }
  204. func TestHandles(t *testing.T) {
  205. handler := NewInterPodAntiAffinity()
  206. tests := map[admission.Operation]bool{
  207. admission.Update: true,
  208. admission.Create: true,
  209. admission.Delete: false,
  210. admission.Connect: false,
  211. }
  212. for op, expected := range tests {
  213. result := handler.Handles(op)
  214. if result != expected {
  215. t.Errorf("Unexpected result for operation %s: %v\n", op, result)
  216. }
  217. }
  218. }
  219. // TestOtherResources ensures that this admission controller is a no-op for other resources,
  220. // subresources, and non-pods.
  221. func TestOtherResources(t *testing.T) {
  222. namespace := "testnamespace"
  223. name := "testname"
  224. pod := &api.Pod{
  225. ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: namespace},
  226. }
  227. tests := []struct {
  228. name string
  229. kind string
  230. resource string
  231. subresource string
  232. object runtime.Object
  233. expectError bool
  234. }{
  235. {
  236. name: "non-pod resource",
  237. kind: "Foo",
  238. resource: "foos",
  239. object: pod,
  240. },
  241. {
  242. name: "pod subresource",
  243. kind: "Pod",
  244. resource: "pods",
  245. subresource: "eviction",
  246. object: pod,
  247. },
  248. {
  249. name: "non-pod object",
  250. kind: "Pod",
  251. resource: "pods",
  252. object: &api.Service{},
  253. expectError: true,
  254. },
  255. }
  256. for _, tc := range tests {
  257. handler := &Plugin{}
  258. err := handler.Validate(admission.NewAttributesRecord(tc.object, nil, api.Kind(tc.kind).WithVersion("version"), namespace, name, api.Resource(tc.resource).WithVersion("version"), tc.subresource, admission.Create, &metav1.CreateOptions{}, false, nil), nil)
  259. if tc.expectError {
  260. if err == nil {
  261. t.Errorf("%s: unexpected nil error", tc.name)
  262. }
  263. continue
  264. }
  265. if err != nil {
  266. t.Errorf("%s: unexpected error: %v", tc.name, err)
  267. continue
  268. }
  269. }
  270. }