volume_zone_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 volumezone
  14. import (
  15. "context"
  16. "reflect"
  17. "testing"
  18. v1 "k8s.io/api/core/v1"
  19. storagev1 "k8s.io/api/storage/v1"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. framework "k8s.io/kubernetes/pkg/scheduler/framework/v1alpha1"
  22. fakelisters "k8s.io/kubernetes/pkg/scheduler/listers/fake"
  23. schedulernodeinfo "k8s.io/kubernetes/pkg/scheduler/nodeinfo"
  24. )
  25. func createPodWithVolume(pod, pv, pvc string) *v1.Pod {
  26. return &v1.Pod{
  27. ObjectMeta: metav1.ObjectMeta{Name: pod, Namespace: "default"},
  28. Spec: v1.PodSpec{
  29. Volumes: []v1.Volume{
  30. {
  31. Name: pv,
  32. VolumeSource: v1.VolumeSource{
  33. PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
  34. ClaimName: pvc,
  35. },
  36. },
  37. },
  38. },
  39. },
  40. }
  41. }
  42. func TestSingleZone(t *testing.T) {
  43. pvLister := fakelisters.PersistentVolumeLister{
  44. {
  45. ObjectMeta: metav1.ObjectMeta{Name: "Vol_1", Labels: map[string]string{v1.LabelZoneFailureDomain: "us-west1-a"}},
  46. },
  47. {
  48. ObjectMeta: metav1.ObjectMeta{Name: "Vol_2", Labels: map[string]string{v1.LabelZoneRegion: "us-west1-b", "uselessLabel": "none"}},
  49. },
  50. {
  51. ObjectMeta: metav1.ObjectMeta{Name: "Vol_3", Labels: map[string]string{v1.LabelZoneRegion: "us-west1-c"}},
  52. },
  53. }
  54. pvcLister := fakelisters.PersistentVolumeClaimLister{
  55. {
  56. ObjectMeta: metav1.ObjectMeta{Name: "PVC_1", Namespace: "default"},
  57. Spec: v1.PersistentVolumeClaimSpec{VolumeName: "Vol_1"},
  58. },
  59. {
  60. ObjectMeta: metav1.ObjectMeta{Name: "PVC_2", Namespace: "default"},
  61. Spec: v1.PersistentVolumeClaimSpec{VolumeName: "Vol_2"},
  62. },
  63. {
  64. ObjectMeta: metav1.ObjectMeta{Name: "PVC_3", Namespace: "default"},
  65. Spec: v1.PersistentVolumeClaimSpec{VolumeName: "Vol_3"},
  66. },
  67. {
  68. ObjectMeta: metav1.ObjectMeta{Name: "PVC_4", Namespace: "default"},
  69. Spec: v1.PersistentVolumeClaimSpec{VolumeName: "Vol_not_exist"},
  70. },
  71. }
  72. tests := []struct {
  73. name string
  74. Pod *v1.Pod
  75. Node *v1.Node
  76. wantStatus *framework.Status
  77. }{
  78. {
  79. name: "pod without volume",
  80. Pod: &v1.Pod{
  81. ObjectMeta: metav1.ObjectMeta{Name: "pod_1", Namespace: "default"},
  82. },
  83. Node: &v1.Node{
  84. ObjectMeta: metav1.ObjectMeta{
  85. Name: "host1",
  86. Labels: map[string]string{v1.LabelZoneFailureDomain: "us-west1-a"},
  87. },
  88. },
  89. },
  90. {
  91. name: "node without labels",
  92. Pod: createPodWithVolume("pod_1", "vol_1", "PVC_1"),
  93. Node: &v1.Node{
  94. ObjectMeta: metav1.ObjectMeta{
  95. Name: "host1",
  96. },
  97. },
  98. },
  99. {
  100. name: "label zone failure domain matched",
  101. Pod: createPodWithVolume("pod_1", "vol_1", "PVC_1"),
  102. Node: &v1.Node{
  103. ObjectMeta: metav1.ObjectMeta{
  104. Name: "host1",
  105. Labels: map[string]string{v1.LabelZoneFailureDomain: "us-west1-a", "uselessLabel": "none"},
  106. },
  107. },
  108. },
  109. {
  110. name: "label zone region matched",
  111. Pod: createPodWithVolume("pod_1", "vol_1", "PVC_2"),
  112. Node: &v1.Node{
  113. ObjectMeta: metav1.ObjectMeta{
  114. Name: "host1",
  115. Labels: map[string]string{v1.LabelZoneRegion: "us-west1-b", "uselessLabel": "none"},
  116. },
  117. },
  118. },
  119. {
  120. name: "label zone region failed match",
  121. Pod: createPodWithVolume("pod_1", "vol_1", "PVC_2"),
  122. Node: &v1.Node{
  123. ObjectMeta: metav1.ObjectMeta{
  124. Name: "host1",
  125. Labels: map[string]string{v1.LabelZoneRegion: "no_us-west1-b", "uselessLabel": "none"},
  126. },
  127. },
  128. wantStatus: framework.NewStatus(framework.UnschedulableAndUnresolvable, ErrReasonConflict),
  129. },
  130. {
  131. name: "label zone failure domain failed match",
  132. Pod: createPodWithVolume("pod_1", "vol_1", "PVC_1"),
  133. Node: &v1.Node{
  134. ObjectMeta: metav1.ObjectMeta{
  135. Name: "host1",
  136. Labels: map[string]string{v1.LabelZoneFailureDomain: "no_us-west1-a", "uselessLabel": "none"},
  137. },
  138. },
  139. wantStatus: framework.NewStatus(framework.UnschedulableAndUnresolvable, ErrReasonConflict),
  140. },
  141. }
  142. for _, test := range tests {
  143. t.Run(test.name, func(t *testing.T) {
  144. node := &schedulernodeinfo.NodeInfo{}
  145. node.SetNode(test.Node)
  146. p := &VolumeZone{
  147. pvLister,
  148. pvcLister,
  149. nil,
  150. }
  151. gotStatus := p.Filter(context.Background(), nil, test.Pod, node)
  152. if !reflect.DeepEqual(gotStatus, test.wantStatus) {
  153. t.Errorf("status does not match: %v, want: %v", gotStatus, test.wantStatus)
  154. }
  155. })
  156. }
  157. }
  158. func TestMultiZone(t *testing.T) {
  159. pvLister := fakelisters.PersistentVolumeLister{
  160. {
  161. ObjectMeta: metav1.ObjectMeta{Name: "Vol_1", Labels: map[string]string{v1.LabelZoneFailureDomain: "us-west1-a"}},
  162. },
  163. {
  164. ObjectMeta: metav1.ObjectMeta{Name: "Vol_2", Labels: map[string]string{v1.LabelZoneFailureDomain: "us-west1-b", "uselessLabel": "none"}},
  165. },
  166. {
  167. ObjectMeta: metav1.ObjectMeta{Name: "Vol_3", Labels: map[string]string{v1.LabelZoneFailureDomain: "us-west1-c__us-west1-a"}},
  168. },
  169. }
  170. pvcLister := fakelisters.PersistentVolumeClaimLister{
  171. {
  172. ObjectMeta: metav1.ObjectMeta{Name: "PVC_1", Namespace: "default"},
  173. Spec: v1.PersistentVolumeClaimSpec{VolumeName: "Vol_1"},
  174. },
  175. {
  176. ObjectMeta: metav1.ObjectMeta{Name: "PVC_2", Namespace: "default"},
  177. Spec: v1.PersistentVolumeClaimSpec{VolumeName: "Vol_2"},
  178. },
  179. {
  180. ObjectMeta: metav1.ObjectMeta{Name: "PVC_3", Namespace: "default"},
  181. Spec: v1.PersistentVolumeClaimSpec{VolumeName: "Vol_3"},
  182. },
  183. {
  184. ObjectMeta: metav1.ObjectMeta{Name: "PVC_4", Namespace: "default"},
  185. Spec: v1.PersistentVolumeClaimSpec{VolumeName: "Vol_not_exist"},
  186. },
  187. }
  188. tests := []struct {
  189. name string
  190. Pod *v1.Pod
  191. Node *v1.Node
  192. wantStatus *framework.Status
  193. }{
  194. {
  195. name: "node without labels",
  196. Pod: createPodWithVolume("pod_1", "Vol_3", "PVC_3"),
  197. Node: &v1.Node{
  198. ObjectMeta: metav1.ObjectMeta{
  199. Name: "host1",
  200. },
  201. },
  202. },
  203. {
  204. name: "label zone failure domain matched",
  205. Pod: createPodWithVolume("pod_1", "Vol_3", "PVC_3"),
  206. Node: &v1.Node{
  207. ObjectMeta: metav1.ObjectMeta{
  208. Name: "host1",
  209. Labels: map[string]string{v1.LabelZoneFailureDomain: "us-west1-a", "uselessLabel": "none"},
  210. },
  211. },
  212. },
  213. {
  214. name: "label zone failure domain failed match",
  215. Pod: createPodWithVolume("pod_1", "vol_1", "PVC_1"),
  216. Node: &v1.Node{
  217. ObjectMeta: metav1.ObjectMeta{
  218. Name: "host1",
  219. Labels: map[string]string{v1.LabelZoneFailureDomain: "us-west1-b", "uselessLabel": "none"},
  220. },
  221. },
  222. wantStatus: framework.NewStatus(framework.UnschedulableAndUnresolvable, ErrReasonConflict),
  223. },
  224. }
  225. for _, test := range tests {
  226. t.Run(test.name, func(t *testing.T) {
  227. node := &schedulernodeinfo.NodeInfo{}
  228. node.SetNode(test.Node)
  229. p := &VolumeZone{
  230. pvLister,
  231. pvcLister,
  232. nil,
  233. }
  234. gotStatus := p.Filter(context.Background(), nil, test.Pod, node)
  235. if !reflect.DeepEqual(gotStatus, test.wantStatus) {
  236. t.Errorf("status does not match: %v, want: %v", gotStatus, test.wantStatus)
  237. }
  238. })
  239. }
  240. }
  241. func TestWithBinding(t *testing.T) {
  242. var (
  243. modeWait = storagev1.VolumeBindingWaitForFirstConsumer
  244. class0 = "Class_0"
  245. classWait = "Class_Wait"
  246. classImmediate = "Class_Immediate"
  247. )
  248. scLister := fakelisters.StorageClassLister{
  249. {
  250. ObjectMeta: metav1.ObjectMeta{Name: classImmediate},
  251. },
  252. {
  253. ObjectMeta: metav1.ObjectMeta{Name: classWait},
  254. VolumeBindingMode: &modeWait,
  255. },
  256. }
  257. pvLister := fakelisters.PersistentVolumeLister{
  258. {
  259. ObjectMeta: metav1.ObjectMeta{Name: "Vol_1", Labels: map[string]string{v1.LabelZoneFailureDomain: "us-west1-a"}},
  260. },
  261. }
  262. pvcLister := fakelisters.PersistentVolumeClaimLister{
  263. {
  264. ObjectMeta: metav1.ObjectMeta{Name: "PVC_1", Namespace: "default"},
  265. Spec: v1.PersistentVolumeClaimSpec{VolumeName: "Vol_1"},
  266. },
  267. {
  268. ObjectMeta: metav1.ObjectMeta{Name: "PVC_NoSC", Namespace: "default"},
  269. Spec: v1.PersistentVolumeClaimSpec{StorageClassName: &class0},
  270. },
  271. {
  272. ObjectMeta: metav1.ObjectMeta{Name: "PVC_EmptySC", Namespace: "default"},
  273. },
  274. {
  275. ObjectMeta: metav1.ObjectMeta{Name: "PVC_WaitSC", Namespace: "default"},
  276. Spec: v1.PersistentVolumeClaimSpec{StorageClassName: &classWait},
  277. },
  278. {
  279. ObjectMeta: metav1.ObjectMeta{Name: "PVC_ImmediateSC", Namespace: "default"},
  280. Spec: v1.PersistentVolumeClaimSpec{StorageClassName: &classImmediate},
  281. },
  282. }
  283. testNode := &v1.Node{
  284. ObjectMeta: metav1.ObjectMeta{
  285. Name: "host1",
  286. Labels: map[string]string{v1.LabelZoneFailureDomain: "us-west1-a", "uselessLabel": "none"},
  287. },
  288. }
  289. tests := []struct {
  290. name string
  291. Pod *v1.Pod
  292. Node *v1.Node
  293. wantStatus *framework.Status
  294. }{
  295. {
  296. name: "label zone failure domain matched",
  297. Pod: createPodWithVolume("pod_1", "vol_1", "PVC_1"),
  298. Node: testNode,
  299. },
  300. {
  301. name: "unbound volume empty storage class",
  302. Pod: createPodWithVolume("pod_1", "vol_1", "PVC_EmptySC"),
  303. Node: testNode,
  304. wantStatus: framework.NewStatus(framework.Error,
  305. "PersistentVolumeClaim had no pv name and storageClass name"),
  306. },
  307. {
  308. name: "unbound volume no storage class",
  309. Pod: createPodWithVolume("pod_1", "vol_1", "PVC_NoSC"),
  310. Node: testNode,
  311. wantStatus: framework.NewStatus(framework.Error,
  312. "StorageClass \"Class_0\" claimed by PersistentVolumeClaim \"PVC_NoSC\" not found"),
  313. },
  314. {
  315. name: "unbound volume immediate binding mode",
  316. Pod: createPodWithVolume("pod_1", "vol_1", "PVC_ImmediateSC"),
  317. Node: testNode,
  318. wantStatus: framework.NewStatus(framework.Error, "VolumeBindingMode not set for StorageClass \"Class_Immediate\""),
  319. },
  320. {
  321. name: "unbound volume wait binding mode",
  322. Pod: createPodWithVolume("pod_1", "vol_1", "PVC_WaitSC"),
  323. Node: testNode,
  324. },
  325. }
  326. for _, test := range tests {
  327. t.Run(test.name, func(t *testing.T) {
  328. node := &schedulernodeinfo.NodeInfo{}
  329. node.SetNode(test.Node)
  330. p := &VolumeZone{
  331. pvLister,
  332. pvcLister,
  333. scLister,
  334. }
  335. gotStatus := p.Filter(context.Background(), nil, test.Pod, node)
  336. if !reflect.DeepEqual(gotStatus, test.wantStatus) {
  337. t.Errorf("status does not match: %v, want: %v", gotStatus, test.wantStatus)
  338. }
  339. })
  340. }
  341. }