@@ -27,8 +27,6 @@ import (
27
27
"testing"
28
28
"time"
29
29
30
- "sigs.k8s.io/yaml"
31
-
32
30
appsv1 "k8s.io/api/apps/v1"
33
31
v1 "k8s.io/api/core/v1"
34
32
schedulingv1 "k8s.io/api/scheduling/v1"
@@ -44,6 +42,7 @@ import (
44
42
componentbaseconfig "k8s.io/component-base/config"
45
43
"k8s.io/klog/v2"
46
44
utilptr "k8s.io/utils/ptr"
45
+ "sigs.k8s.io/yaml"
47
46
48
47
"sigs.k8s.io/descheduler/cmd/descheduler/app/options"
49
48
"sigs.k8s.io/descheduler/pkg/api"
@@ -63,7 +62,6 @@ import (
63
62
frameworktesting "sigs.k8s.io/descheduler/pkg/framework/testing"
64
63
frameworktypes "sigs.k8s.io/descheduler/pkg/framework/types"
65
64
"sigs.k8s.io/descheduler/pkg/utils"
66
- "sigs.k8s.io/descheduler/test"
67
65
)
68
66
69
67
func isClientRateLimiterError (err error ) bool {
@@ -195,67 +193,6 @@ func printPodLogs(ctx context.Context, t *testing.T, kubeClient clientset.Interf
195
193
}
196
194
}
197
195
198
- func waitForDeschedulerPodRunning (t * testing.T , ctx context.Context , kubeClient clientset.Interface , testName string ) string {
199
- deschedulerPodName := ""
200
- if err := wait .PollUntilContextTimeout (ctx , 1 * time .Second , 60 * time .Second , true , func (ctx context.Context ) (bool , error ) {
201
- podList , err := kubeClient .CoreV1 ().Pods ("kube-system" ).List (ctx , metav1.ListOptions {
202
- LabelSelector : labels .SelectorFromSet (labels .Set (map [string ]string {"app" : "descheduler" , "test" : testName })).String (),
203
- })
204
- if err != nil {
205
- t .Logf ("Unable to list pods: %v" , err )
206
- if isClientRateLimiterError (err ) {
207
- return false , nil
208
- }
209
- return false , err
210
- }
211
-
212
- runningPods := []* v1.Pod {}
213
- for _ , item := range podList .Items {
214
- if item .Status .Phase != v1 .PodRunning {
215
- continue
216
- }
217
- pod := item
218
- runningPods = append (runningPods , & pod )
219
- }
220
-
221
- if len (runningPods ) != 1 {
222
- t .Logf ("Expected a single running pod, got %v instead" , len (runningPods ))
223
- return false , nil
224
- }
225
-
226
- deschedulerPodName = runningPods [0 ].Name
227
- t .Logf ("Found a descheduler pod running: %v" , deschedulerPodName )
228
- return true , nil
229
- }); err != nil {
230
- t .Fatalf ("Error waiting for a running descheduler: %v" , err )
231
- }
232
- return deschedulerPodName
233
- }
234
-
235
- func waitForDeschedulerPodAbsent (t * testing.T , ctx context.Context , kubeClient clientset.Interface , testName string ) {
236
- if err := wait .PollUntilContextTimeout (ctx , 1 * time .Second , 60 * time .Second , true , func (ctx context.Context ) (bool , error ) {
237
- podList , err := kubeClient .CoreV1 ().Pods ("kube-system" ).List (ctx , metav1.ListOptions {
238
- LabelSelector : labels .SelectorFromSet (labels .Set (map [string ]string {"app" : "descheduler" , "test" : testName })).String (),
239
- })
240
- if err != nil {
241
- t .Logf ("Unable to list pods: %v" , err )
242
- if isClientRateLimiterError (err ) {
243
- return false , nil
244
- }
245
- return false , err
246
- }
247
-
248
- if len (podList .Items ) > 0 {
249
- t .Logf ("Found a descheduler pod. Waiting until it gets deleted" )
250
- return false , nil
251
- }
252
-
253
- return true , nil
254
- }); err != nil {
255
- t .Fatalf ("Error waiting for a descheduler to disapear: %v" , err )
256
- }
257
- }
258
-
259
196
func TestMain (m * testing.M ) {
260
197
if os .Getenv ("DESCHEDULER_IMAGE" ) == "" {
261
198
klog .Errorf ("DESCHEDULER_IMAGE env is not set" )
@@ -297,7 +234,7 @@ func RcByNameContainer(name, namespace string, replicas int32, labels map[string
297
234
ObjectMeta : metav1.ObjectMeta {
298
235
Labels : labels ,
299
236
},
300
- Spec : test . MakePodSpec (priorityClassName , gracePeriod ),
237
+ Spec : makePodSpec (priorityClassName , gracePeriod ),
301
238
},
302
239
},
303
240
}
@@ -329,10 +266,81 @@ func DsByNameContainer(name, namespace string, labels map[string]string, gracePe
329
266
ObjectMeta : metav1.ObjectMeta {
330
267
Labels : labels ,
331
268
},
332
- Spec : test .MakePodSpec ("" , gracePeriod ),
269
+ Spec : makePodSpec ("" , gracePeriod ),
270
+ },
271
+ },
272
+ }
273
+ }
274
+
275
+ func buildTestDeployment (name , namespace string , replicas int32 , testLabel map [string ]string , apply func (deployment * appsv1.Deployment )) * appsv1.Deployment {
276
+ deployment := & appsv1.Deployment {
277
+ TypeMeta : metav1.TypeMeta {
278
+ Kind : "Deployment" ,
279
+ APIVersion : "apps/v1" ,
280
+ },
281
+ ObjectMeta : metav1.ObjectMeta {
282
+ Name : name ,
283
+ Namespace : namespace ,
284
+ Labels : testLabel ,
285
+ },
286
+ Spec : appsv1.DeploymentSpec {
287
+ Replicas : utilptr.To [int32 ](replicas ),
288
+ Selector : & metav1.LabelSelector {
289
+ MatchLabels : testLabel ,
290
+ },
291
+ Template : v1.PodTemplateSpec {
292
+ ObjectMeta : metav1.ObjectMeta {
293
+ Labels : testLabel ,
294
+ },
295
+ Spec : makePodSpec ("" , utilptr.To [int64 ](0 )),
333
296
},
334
297
},
335
298
}
299
+
300
+ if apply != nil {
301
+ apply (deployment )
302
+ }
303
+
304
+ return deployment
305
+ }
306
+
307
+ func makePodSpec (priorityClassName string , gracePeriod * int64 ) v1.PodSpec {
308
+ return v1.PodSpec {
309
+ SecurityContext : & v1.PodSecurityContext {
310
+ RunAsNonRoot : utilptr .To (true ),
311
+ RunAsUser : utilptr.To [int64 ](1000 ),
312
+ RunAsGroup : utilptr.To [int64 ](1000 ),
313
+ SeccompProfile : & v1.SeccompProfile {
314
+ Type : v1 .SeccompProfileTypeRuntimeDefault ,
315
+ },
316
+ },
317
+ Containers : []v1.Container {{
318
+ Name : "pause" ,
319
+ ImagePullPolicy : "IfNotPresent" ,
320
+ Image : "registry.k8s.io/pause" ,
321
+ Ports : []v1.ContainerPort {{ContainerPort : 80 }},
322
+ Resources : v1.ResourceRequirements {
323
+ Limits : v1.ResourceList {
324
+ v1 .ResourceCPU : resource .MustParse ("100m" ),
325
+ v1 .ResourceMemory : resource .MustParse ("200Mi" ),
326
+ },
327
+ Requests : v1.ResourceList {
328
+ v1 .ResourceCPU : resource .MustParse ("100m" ),
329
+ v1 .ResourceMemory : resource .MustParse ("100Mi" ),
330
+ },
331
+ },
332
+ SecurityContext : & v1.SecurityContext {
333
+ AllowPrivilegeEscalation : utilptr .To (false ),
334
+ Capabilities : & v1.Capabilities {
335
+ Drop : []v1.Capability {
336
+ "ALL" ,
337
+ },
338
+ },
339
+ },
340
+ }},
341
+ PriorityClassName : priorityClassName ,
342
+ TerminationGracePeriodSeconds : gracePeriod ,
343
+ }
336
344
}
337
345
338
346
func initializeClient (ctx context.Context , t * testing.T ) (clientset.Interface , informers.SharedInformerFactory , listersv1.NodeLister , podutil.GetPodsAssignedToNodeFunc ) {
@@ -1705,6 +1713,10 @@ func waitForPodRunning(ctx context.Context, t *testing.T, clientSet clientset.In
1705
1713
if err := wait .PollUntilContextTimeout (ctx , 5 * time .Second , 30 * time .Second , true , func (ctx context.Context ) (bool , error ) {
1706
1714
podItem , err := clientSet .CoreV1 ().Pods (pod .Namespace ).Get (ctx , pod .Name , metav1.GetOptions {})
1707
1715
if err != nil {
1716
+ t .Logf ("Unable to list pods: %v" , err )
1717
+ if isClientRateLimiterError (err ) {
1718
+ return false , nil
1719
+ }
1708
1720
return false , err
1709
1721
}
1710
1722
@@ -1719,27 +1731,65 @@ func waitForPodRunning(ctx context.Context, t *testing.T, clientSet clientset.In
1719
1731
}
1720
1732
}
1721
1733
1722
- func waitForPodsRunning (ctx context.Context , t * testing.T , clientSet clientset.Interface , labelMap map [string ]string , desireRunningPodNum int , namespace string ) {
1723
- if err := wait .PollUntilContextTimeout (ctx , 10 * time .Second , 60 * time .Second , true , func (ctx context.Context ) (bool , error ) {
1734
+ func waitForPodsRunning (ctx context.Context , t * testing.T , clientSet clientset.Interface , labelMap map [string ]string , desireRunningPodNum int , namespace string ) string {
1735
+ runningPodName := ""
1736
+ if err := wait .PollUntilContextTimeout (ctx , 5 * time .Second , 60 * time .Second , true , func (ctx context.Context ) (bool , error ) {
1724
1737
podList , err := clientSet .CoreV1 ().Pods (namespace ).List (ctx , metav1.ListOptions {
1725
1738
LabelSelector : labels .SelectorFromSet (labelMap ).String (),
1726
1739
})
1727
1740
if err != nil {
1741
+ t .Logf ("Unable to list pods: %v" , err )
1742
+ if isClientRateLimiterError (err ) {
1743
+ return false , nil
1744
+ }
1728
1745
return false , err
1729
1746
}
1730
- if len (podList .Items ) != desireRunningPodNum {
1731
- t .Logf ("Waiting for %v pods to be running, got %v instead" , desireRunningPodNum , len (podList .Items ))
1747
+
1748
+ runningPods := []* v1.Pod {}
1749
+ for _ , item := range podList .Items {
1750
+ if item .Status .Phase != v1 .PodRunning {
1751
+ continue
1752
+ }
1753
+ pod := item
1754
+ runningPods = append (runningPods , & pod )
1755
+ }
1756
+
1757
+ if len (runningPods ) != desireRunningPodNum {
1758
+ t .Logf ("Waiting for %v pods to be running, got %v instead" , desireRunningPodNum , len (runningPods ))
1732
1759
return false , nil
1733
1760
}
1734
- for _ , pod := range podList .Items {
1735
- if pod .Status .Phase != v1 .PodRunning {
1736
- t .Logf ("Pod %v not running yet, is %v instead" , pod .Name , pod .Status .Phase )
1761
+
1762
+ if desireRunningPodNum == 1 {
1763
+ runningPodName = runningPods [0 ].Name
1764
+ }
1765
+
1766
+ return true , nil
1767
+ }); err != nil {
1768
+ t .Fatalf ("Error waiting for pods running: %v" , err )
1769
+ }
1770
+ return runningPodName
1771
+ }
1772
+
1773
+ func waitForPodsToDisappear (ctx context.Context , t * testing.T , clientSet clientset.Interface , labelMap map [string ]string , namespace string ) {
1774
+ if err := wait .PollUntilContextTimeout (ctx , 5 * time .Second , 60 * time .Second , true , func (ctx context.Context ) (bool , error ) {
1775
+ podList , err := clientSet .CoreV1 ().Pods (namespace ).List (ctx , metav1.ListOptions {
1776
+ LabelSelector : labels .SelectorFromSet (labelMap ).String (),
1777
+ })
1778
+ if err != nil {
1779
+ t .Logf ("Unable to list pods: %v" , err )
1780
+ if isClientRateLimiterError (err ) {
1737
1781
return false , nil
1738
1782
}
1783
+ return false , err
1784
+ }
1785
+
1786
+ if len (podList .Items ) > 0 {
1787
+ t .Logf ("Found a existing pod. Waiting until it gets deleted" )
1788
+ return false , nil
1739
1789
}
1740
1790
return true , nil
1741
1791
}); err != nil {
1742
- t .Fatalf ("Error waiting for pods running : %v" , err )
1792
+ t .Fatalf ("Error waiting for pods to disappear : %v" , err )
1743
1793
}
1744
1794
}
1745
1795
@@ -1756,8 +1806,8 @@ func splitNodesAndWorkerNodes(nodes []v1.Node) ([]*v1.Node, []*v1.Node) {
1756
1806
return allNodes , workerNodes
1757
1807
}
1758
1808
1759
- func getCurrentPodNames (t * testing. T , ctx context.Context , kubeClient clientset.Interface , namespace string ) []string {
1760
- podList , err := kubeClient .CoreV1 ().Pods (namespace ).List (ctx , metav1.ListOptions {})
1809
+ func getCurrentPodNames (ctx context.Context , clientSet clientset.Interface , namespace string , t * testing. T ) []string {
1810
+ podList , err := clientSet .CoreV1 ().Pods (namespace ).List (ctx , metav1.ListOptions {})
1761
1811
if err != nil {
1762
1812
t .Logf ("Unable to list pods: %v" , err )
1763
1813
return nil
0 commit comments