@@ -19,6 +19,7 @@ package e2e
19
19
import (
20
20
"context"
21
21
"fmt"
22
+ "os"
22
23
"strings"
23
24
"testing"
24
25
"time"
@@ -28,28 +29,66 @@ import (
28
29
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29
30
"k8s.io/apimachinery/pkg/labels"
30
31
clientset "k8s.io/client-go/kubernetes"
31
- "k8s.io/client-go/tools/events "
32
+ componentbaseconfig "k8s.io/component-base/config "
32
33
utilptr "k8s.io/utils/ptr"
33
- "sigs.k8s.io/descheduler/pkg/descheduler/evictions"
34
- eutils "sigs.k8s.io/descheduler/pkg/descheduler/evictions/utils"
35
- frameworkfake "sigs.k8s.io/descheduler/pkg/framework/fake"
34
+
35
+ "sigs.k8s.io/descheduler/cmd/descheduler/app/options"
36
+ "sigs.k8s.io/descheduler/pkg/api"
37
+ "sigs.k8s.io/descheduler/pkg/descheduler"
38
+ "sigs.k8s.io/descheduler/pkg/descheduler/client"
36
39
"sigs.k8s.io/descheduler/pkg/framework/plugins/defaultevictor"
37
40
"sigs.k8s.io/descheduler/pkg/framework/plugins/removepodshavingtoomanyrestarts"
38
- frameworktypes "sigs.k8s.io/descheduler/pkg/framework/types"
39
41
)
40
42
43
+ func tooManyRestartsPolicy (targetNamespace string , podRestartThresholds int32 , includingInitContainers bool ) * api.DeschedulerPolicy {
44
+ return & api.DeschedulerPolicy {
45
+ Profiles : []api.DeschedulerProfile {
46
+ {
47
+ Name : "TooManyRestartsProfile" ,
48
+ PluginConfigs : []api.PluginConfig {
49
+ {
50
+ Name : removepodshavingtoomanyrestarts .PluginName ,
51
+ Args : & removepodshavingtoomanyrestarts.RemovePodsHavingTooManyRestartsArgs {
52
+ PodRestartThreshold : podRestartThresholds ,
53
+ IncludingInitContainers : includingInitContainers ,
54
+ Namespaces : & api.Namespaces {
55
+ Include : []string {targetNamespace },
56
+ },
57
+ },
58
+ },
59
+ {
60
+ Name : defaultevictor .PluginName ,
61
+ Args : & defaultevictor.DefaultEvictorArgs {
62
+ EvictLocalStoragePods : true ,
63
+ },
64
+ },
65
+ },
66
+ Plugins : api.Plugins {
67
+ Filter : api.PluginSet {
68
+ Enabled : []string {
69
+ defaultevictor .PluginName ,
70
+ },
71
+ },
72
+ Deschedule : api.PluginSet {
73
+ Enabled : []string {
74
+ removepodshavingtoomanyrestarts .PluginName ,
75
+ },
76
+ },
77
+ },
78
+ },
79
+ },
80
+ }
81
+ }
82
+
41
83
func TestTooManyRestarts (t * testing.T ) {
42
84
ctx := context .Background ()
85
+ initPluginRegistry ()
43
86
44
- clientSet , sharedInformerFactory , _ , getPodsAssignedToNode := initializeClient (ctx , t )
45
-
46
- nodeList , err := clientSet .CoreV1 ().Nodes ().List (ctx , metav1.ListOptions {})
87
+ clientSet , err := client .CreateClient (componentbaseconfig.ClientConnectionConfiguration {Kubeconfig : os .Getenv ("KUBECONFIG" )}, "" )
47
88
if err != nil {
48
- t .Errorf ("Error listing node with %v" , err )
89
+ t .Errorf ("Error during kubernetes client creation with %v" , err )
49
90
}
50
91
51
- _ , workerNodes := splitNodesAndWorkerNodes (nodeList .Items )
52
-
53
92
t .Logf ("Creating testing namespace %v" , t .Name ())
54
93
testNamespace := & v1.Namespace {ObjectMeta : metav1.ObjectMeta {Name : "e2e-" + strings .ToLower (t .Name ())}}
55
94
if _ , err := clientSet .CoreV1 ().Namespaces ().Create (ctx , testNamespace , metav1.CreateOptions {}); err != nil {
@@ -124,86 +163,46 @@ func TestTooManyRestarts(t *testing.T) {
124
163
t .Fatal ("Pod restart count not as expected" )
125
164
}
126
165
127
- createRemovePodsHavingTooManyRestartsAgrs := func (
128
- podRestartThresholds int32 ,
129
- includingInitContainers bool ,
130
- ) removepodshavingtoomanyrestarts.RemovePodsHavingTooManyRestartsArgs {
131
- return removepodshavingtoomanyrestarts.RemovePodsHavingTooManyRestartsArgs {
132
- PodRestartThreshold : podRestartThresholds ,
133
- IncludingInitContainers : includingInitContainers ,
134
- }
135
- }
136
-
137
166
tests := []struct {
138
167
name string
139
- args removepodshavingtoomanyrestarts. RemovePodsHavingTooManyRestartsArgs
168
+ policy * api. DeschedulerPolicy
140
169
expectedEvictedPodCount uint
141
170
}{
142
171
{
143
172
name : "test-no-evictions" ,
144
- args : createRemovePodsHavingTooManyRestartsAgrs ( 10000 , true ),
173
+ policy : tooManyRestartsPolicy ( testNamespace . Name , 10000 , true ),
145
174
expectedEvictedPodCount : 0 ,
146
175
},
147
176
{
148
177
name : "test-one-evictions" ,
149
- args : createRemovePodsHavingTooManyRestartsAgrs ( 4 , true ),
178
+ policy : tooManyRestartsPolicy ( testNamespace . Name , 4 , true ),
150
179
expectedEvictedPodCount : 4 ,
151
180
},
152
181
}
153
182
for _ , tc := range tests {
154
183
t .Run (tc .name , func (t * testing.T ) {
155
- evictionPolicyGroupVersion , err := eutils .SupportEviction (clientSet )
156
- if err != nil || len (evictionPolicyGroupVersion ) == 0 {
157
- t .Fatalf ("Error creating eviction policy group: %v" , err )
158
- }
159
-
160
- eventRecorder := & events.FakeRecorder {}
161
-
162
- podEvictor := evictions .NewPodEvictor (
163
- clientSet ,
164
- eventRecorder ,
165
- evictions .NewOptions ().WithPolicyGroupVersion (evictionPolicyGroupVersion ),
166
- )
167
-
168
- defaultevictorArgs := & defaultevictor.DefaultEvictorArgs {
169
- EvictLocalStoragePods : true ,
170
- EvictSystemCriticalPods : false ,
171
- IgnorePvcPods : false ,
172
- EvictFailedBarePods : false ,
173
- }
174
-
175
- evictorFilter , err := defaultevictor .New (
176
- defaultevictorArgs ,
177
- & frameworkfake.HandleImpl {
178
- ClientsetImpl : clientSet ,
179
- GetPodsAssignedToNodeFuncImpl : getPodsAssignedToNode ,
180
- SharedInformerFactoryImpl : sharedInformerFactory ,
181
- },
182
- )
184
+ rs , err := options .NewDeschedulerServer ()
183
185
if err != nil {
184
- t .Fatalf ("Unable to initialize the plugin : %v" , err )
186
+ t .Fatalf ("Unable to initialize server : %v\n " , err )
185
187
}
188
+ rs .Client = clientSet
189
+ rs .EventClient = clientSet
186
190
187
- plugin , err := removepodshavingtoomanyrestarts .New (
188
- & tc .args ,
189
- & frameworkfake.HandleImpl {
190
- ClientsetImpl : clientSet ,
191
- PodEvictorImpl : podEvictor ,
192
- EvictorFilterImpl : evictorFilter .(frameworktypes.EvictorPlugin ),
193
- SharedInformerFactoryImpl : sharedInformerFactory ,
194
- GetPodsAssignedToNodeFuncImpl : getPodsAssignedToNode ,
195
- })
191
+ preRunNames := getCurrentPodNames (t , ctx , clientSet , testNamespace .Name )
192
+ // Run RemovePodsHavingTooManyRestarts strategy
193
+ t .Log ("Running RemovePodsHavingTooManyRestarts strategy" )
194
+ err = descheduler .RunDeschedulerStrategies (ctx , rs , tc .policy , "v1" )
196
195
if err != nil {
197
- t .Fatalf ("Unable to initialize the plugin : %v" , err )
196
+ t .Fatalf ("Failed running a descheduling cycle : %v" , err )
198
197
}
199
198
200
- // Run RemovePodsHavingTooManyRestarts strategy
201
- t .Log ("Running RemovePodsHavingTooManyRestarts strategy" )
202
- plugin .(frameworktypes.DeschedulePlugin ).Deschedule (ctx , workerNodes )
203
199
t .Logf ("Finished RemoveFailedPods strategy for %s" , tc .name )
204
-
205
200
waitForTerminatingPodsToDisappear (ctx , t , clientSet , testNamespace .Name )
206
- actualEvictedPodCount := podEvictor .TotalEvicted ()
201
+ afterRunNames := getCurrentPodNames (t , ctx , clientSet , testNamespace .Name )
202
+ namesInCommon := len (intersectStrings (preRunNames , afterRunNames ))
203
+
204
+ t .Logf ("preRunNames: %v, afterRunNames: %v, namesInCommonLen: %v\n " , preRunNames , afterRunNames , namesInCommon )
205
+ actualEvictedPodCount := uint (len (afterRunNames ) - namesInCommon )
207
206
if actualEvictedPodCount < tc .expectedEvictedPodCount {
208
207
t .Errorf ("Test error for description: %s. Unexpected number of pods have been evicted, got %v, expected %v" , tc .name , actualEvictedPodCount , tc .expectedEvictedPodCount )
209
208
}
0 commit comments