csi_volume_predicate_test.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. Copyright 2018 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 predicates
  14. import (
  15. "fmt"
  16. "reflect"
  17. "testing"
  18. "k8s.io/api/core/v1"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. utilfeature "k8s.io/apiserver/pkg/util/feature"
  21. featuregatetesting "k8s.io/component-base/featuregate/testing"
  22. "k8s.io/kubernetes/pkg/features"
  23. )
  24. func TestCSIVolumeCountPredicate(t *testing.T) {
  25. // for pods with CSI pvcs
  26. oneVolPod := &v1.Pod{
  27. Spec: v1.PodSpec{
  28. Volumes: []v1.Volume{
  29. {
  30. VolumeSource: v1.VolumeSource{
  31. PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
  32. ClaimName: "csi-ebs-0",
  33. },
  34. },
  35. },
  36. },
  37. },
  38. }
  39. twoVolPod := &v1.Pod{
  40. Spec: v1.PodSpec{
  41. Volumes: []v1.Volume{
  42. {
  43. VolumeSource: v1.VolumeSource{
  44. PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
  45. ClaimName: "cs-ebs-1",
  46. },
  47. },
  48. },
  49. {
  50. VolumeSource: v1.VolumeSource{
  51. PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
  52. ClaimName: "csi-ebs-2",
  53. },
  54. },
  55. },
  56. },
  57. },
  58. }
  59. runningPod := &v1.Pod{
  60. Spec: v1.PodSpec{
  61. Volumes: []v1.Volume{
  62. {
  63. VolumeSource: v1.VolumeSource{
  64. PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
  65. ClaimName: "csi-ebs-3",
  66. },
  67. },
  68. },
  69. },
  70. },
  71. }
  72. pendingVolumePod := &v1.Pod{
  73. Spec: v1.PodSpec{
  74. Volumes: []v1.Volume{
  75. {
  76. VolumeSource: v1.VolumeSource{
  77. PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
  78. ClaimName: "csi-4",
  79. },
  80. },
  81. },
  82. },
  83. },
  84. }
  85. // Different pod than pendingVolumePod, but using the same unbound PVC
  86. unboundPVCPod2 := &v1.Pod{
  87. Spec: v1.PodSpec{
  88. Volumes: []v1.Volume{
  89. {
  90. VolumeSource: v1.VolumeSource{
  91. PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
  92. ClaimName: "csi-4",
  93. },
  94. },
  95. },
  96. },
  97. },
  98. }
  99. missingPVPod := &v1.Pod{
  100. Spec: v1.PodSpec{
  101. Volumes: []v1.Volume{
  102. {
  103. VolumeSource: v1.VolumeSource{
  104. PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
  105. ClaimName: "csi-6",
  106. },
  107. },
  108. },
  109. },
  110. },
  111. }
  112. noSCPVCPod := &v1.Pod{
  113. Spec: v1.PodSpec{
  114. Volumes: []v1.Volume{
  115. {
  116. VolumeSource: v1.VolumeSource{
  117. PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
  118. ClaimName: "csi-5",
  119. },
  120. },
  121. },
  122. },
  123. },
  124. }
  125. gceTwoVolPod := &v1.Pod{
  126. Spec: v1.PodSpec{
  127. Volumes: []v1.Volume{
  128. {
  129. VolumeSource: v1.VolumeSource{
  130. PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
  131. ClaimName: "cs-gce-1",
  132. },
  133. },
  134. },
  135. {
  136. VolumeSource: v1.VolumeSource{
  137. PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
  138. ClaimName: "csi-gce-2",
  139. },
  140. },
  141. },
  142. },
  143. },
  144. }
  145. tests := []struct {
  146. newPod *v1.Pod
  147. existingPods []*v1.Pod
  148. filterName string
  149. maxVols int
  150. driverNames []string
  151. fits bool
  152. test string
  153. }{
  154. {
  155. newPod: oneVolPod,
  156. existingPods: []*v1.Pod{runningPod, twoVolPod},
  157. filterName: "csi",
  158. maxVols: 4,
  159. driverNames: []string{"ebs"},
  160. fits: true,
  161. test: "fits when node capacity >= new pods CSI volume",
  162. },
  163. {
  164. newPod: oneVolPod,
  165. existingPods: []*v1.Pod{runningPod, twoVolPod},
  166. filterName: "csi",
  167. maxVols: 2,
  168. driverNames: []string{"ebs"},
  169. fits: false,
  170. test: "doesn't when node capacity <= pods CSI volume",
  171. },
  172. // should count pending PVCs
  173. {
  174. newPod: oneVolPod,
  175. existingPods: []*v1.Pod{pendingVolumePod, twoVolPod},
  176. filterName: "csi",
  177. maxVols: 2,
  178. driverNames: []string{"ebs"},
  179. fits: false,
  180. test: "count pending PVCs towards capacity <= pods CSI volume",
  181. },
  182. // two same pending PVCs should be counted as 1
  183. {
  184. newPod: oneVolPod,
  185. existingPods: []*v1.Pod{pendingVolumePod, unboundPVCPod2, twoVolPod},
  186. filterName: "csi",
  187. maxVols: 3,
  188. driverNames: []string{"ebs"},
  189. fits: true,
  190. test: "count multiple pending pvcs towards capacity >= pods CSI volume",
  191. },
  192. // should count PVCs with invalid PV name but valid SC
  193. {
  194. newPod: oneVolPod,
  195. existingPods: []*v1.Pod{missingPVPod, twoVolPod},
  196. filterName: "csi",
  197. maxVols: 2,
  198. driverNames: []string{"ebs"},
  199. fits: false,
  200. test: "should count PVCs with invalid PV name but valid SC",
  201. },
  202. // don't count a volume which has storageclass missing
  203. {
  204. newPod: oneVolPod,
  205. existingPods: []*v1.Pod{runningPod, noSCPVCPod},
  206. filterName: "csi",
  207. maxVols: 2,
  208. driverNames: []string{"ebs"},
  209. fits: true,
  210. test: "don't count pvcs with missing SC towards capacity",
  211. },
  212. // don't count multiple volume types
  213. {
  214. newPod: oneVolPod,
  215. existingPods: []*v1.Pod{gceTwoVolPod, twoVolPod},
  216. filterName: "csi",
  217. maxVols: 2,
  218. driverNames: []string{"ebs", "gce"},
  219. fits: true,
  220. test: "don't count pvcs with different type towards capacity",
  221. },
  222. {
  223. newPod: gceTwoVolPod,
  224. existingPods: []*v1.Pod{twoVolPod, runningPod},
  225. filterName: "csi",
  226. maxVols: 2,
  227. driverNames: []string{"ebs", "gce"},
  228. fits: true,
  229. test: "don't count pvcs with different type towards capacity",
  230. },
  231. }
  232. defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.AttachVolumeLimit, true)()
  233. expectedFailureReasons := []PredicateFailureReason{ErrMaxVolumeCountExceeded}
  234. // running attachable predicate tests with feature gate and limit present on nodes
  235. for _, test := range tests {
  236. node := getNodeWithPodAndVolumeLimits(test.existingPods, int64(test.maxVols), test.driverNames...)
  237. pred := NewCSIMaxVolumeLimitPredicate(getFakeCSIPVInfo(test.filterName, test.driverNames...),
  238. getFakeCSIPVCInfo(test.filterName, "csi-sc", test.driverNames...),
  239. getFakeCSIStorageClassInfo("csi-sc", test.driverNames[0]))
  240. fits, reasons, err := pred(test.newPod, GetPredicateMetadata(test.newPod, nil), node)
  241. if err != nil {
  242. t.Errorf("Using allocatable [%s]%s: unexpected error: %v", test.filterName, test.test, err)
  243. }
  244. if !fits && !reflect.DeepEqual(reasons, expectedFailureReasons) {
  245. t.Errorf("Using allocatable [%s]%s: unexpected failure reasons: %v, want: %v", test.filterName, test.test, reasons, expectedFailureReasons)
  246. }
  247. if fits != test.fits {
  248. t.Errorf("Using allocatable [%s]%s: expected %v, got %v", test.filterName, test.test, test.fits, fits)
  249. }
  250. }
  251. }
  252. func getFakeCSIPVInfo(volumeName string, driverNames ...string) FakePersistentVolumeInfo {
  253. pvInfos := FakePersistentVolumeInfo{}
  254. for _, driver := range driverNames {
  255. for j := 0; j < 4; j++ {
  256. volumeHandle := fmt.Sprintf("%s-%s-%d", volumeName, driver, j)
  257. pv := v1.PersistentVolume{
  258. ObjectMeta: metav1.ObjectMeta{Name: volumeHandle},
  259. Spec: v1.PersistentVolumeSpec{
  260. PersistentVolumeSource: v1.PersistentVolumeSource{
  261. CSI: &v1.CSIPersistentVolumeSource{
  262. Driver: driver,
  263. VolumeHandle: volumeHandle,
  264. },
  265. },
  266. },
  267. }
  268. pvInfos = append(pvInfos, pv)
  269. }
  270. }
  271. return pvInfos
  272. }
  273. func getFakeCSIPVCInfo(volumeName, scName string, driverNames ...string) FakePersistentVolumeClaimInfo {
  274. pvcInfos := FakePersistentVolumeClaimInfo{}
  275. for _, driver := range driverNames {
  276. for j := 0; j < 4; j++ {
  277. v := fmt.Sprintf("%s-%s-%d", volumeName, driver, j)
  278. pvc := v1.PersistentVolumeClaim{
  279. ObjectMeta: metav1.ObjectMeta{Name: v},
  280. Spec: v1.PersistentVolumeClaimSpec{VolumeName: v},
  281. }
  282. pvcInfos = append(pvcInfos, pvc)
  283. }
  284. }
  285. pvcInfos = append(pvcInfos, v1.PersistentVolumeClaim{
  286. ObjectMeta: metav1.ObjectMeta{Name: volumeName + "-4"},
  287. Spec: v1.PersistentVolumeClaimSpec{StorageClassName: &scName},
  288. })
  289. pvcInfos = append(pvcInfos, v1.PersistentVolumeClaim{
  290. ObjectMeta: metav1.ObjectMeta{Name: volumeName + "-5"},
  291. Spec: v1.PersistentVolumeClaimSpec{},
  292. })
  293. // a pvc with missing PV but available storageclass.
  294. pvcInfos = append(pvcInfos, v1.PersistentVolumeClaim{
  295. ObjectMeta: metav1.ObjectMeta{Name: volumeName + "-6"},
  296. Spec: v1.PersistentVolumeClaimSpec{StorageClassName: &scName, VolumeName: "missing-in-action"},
  297. })
  298. return pvcInfos
  299. }
  300. func getFakeCSIStorageClassInfo(scName, provisionerName string) FakeStorageClassInfo {
  301. return FakeStorageClassInfo{
  302. {
  303. ObjectMeta: metav1.ObjectMeta{Name: scName},
  304. Provisioner: provisionerName,
  305. },
  306. }
  307. }