Skip to content

Commit 69d0bfc

Browse files
committed
e2e: TestTooManyRestarts: run descheduler as a whole instead of a single plugin
1 parent 0c9750c commit 69d0bfc

File tree

2 files changed

+90
-68
lines changed

2 files changed

+90
-68
lines changed

test/e2e/e2e_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,23 @@ import (
5050
nodeutil "sigs.k8s.io/descheduler/pkg/descheduler/node"
5151
podutil "sigs.k8s.io/descheduler/pkg/descheduler/pod"
5252
frameworkfake "sigs.k8s.io/descheduler/pkg/framework/fake"
53+
"sigs.k8s.io/descheduler/pkg/framework/pluginregistry"
5354
"sigs.k8s.io/descheduler/pkg/framework/plugins/defaultevictor"
5455
"sigs.k8s.io/descheduler/pkg/framework/plugins/nodeutilization"
5556
"sigs.k8s.io/descheduler/pkg/framework/plugins/podlifetime"
57+
"sigs.k8s.io/descheduler/pkg/framework/plugins/removepodshavingtoomanyrestarts"
5658
frameworktypes "sigs.k8s.io/descheduler/pkg/framework/types"
5759
"sigs.k8s.io/descheduler/pkg/utils"
5860
"sigs.k8s.io/descheduler/test"
5961
)
6062

63+
func initPluginRegistry() {
64+
pluginregistry.PluginRegistry = pluginregistry.NewRegistry()
65+
pluginregistry.Register(defaultevictor.PluginName, defaultevictor.New, &defaultevictor.DefaultEvictor{}, &defaultevictor.DefaultEvictorArgs{}, defaultevictor.ValidateDefaultEvictorArgs, defaultevictor.SetDefaults_DefaultEvictorArgs, pluginregistry.PluginRegistry)
66+
pluginregistry.Register(podlifetime.PluginName, podlifetime.New, &podlifetime.PodLifeTime{}, &podlifetime.PodLifeTimeArgs{}, podlifetime.ValidatePodLifeTimeArgs, podlifetime.SetDefaults_PodLifeTimeArgs, pluginregistry.PluginRegistry)
67+
pluginregistry.Register(removepodshavingtoomanyrestarts.PluginName, removepodshavingtoomanyrestarts.New, &removepodshavingtoomanyrestarts.RemovePodsHavingTooManyRestarts{}, &removepodshavingtoomanyrestarts.RemovePodsHavingTooManyRestartsArgs{}, removepodshavingtoomanyrestarts.ValidateRemovePodsHavingTooManyRestartsArgs, removepodshavingtoomanyrestarts.SetDefaults_RemovePodsHavingTooManyRestartsArgs, pluginregistry.PluginRegistry)
68+
}
69+
6170
// RcByNameContainer returns a ReplicationController with specified name and container
6271
func RcByNameContainer(name, namespace string, replicas int32, labels map[string]string, gracePeriod *int64, priorityClassName string) *v1.ReplicationController {
6372
// Add "name": name to the labels, overwriting if it exists.
@@ -1580,3 +1589,17 @@ func initPodEvictorOrFail(t *testing.T, clientSet clientset.Interface, getPodsAs
15801589
evictions.NewOptions().WithPolicyGroupVersion(evictionPolicyGroupVersion),
15811590
)
15821591
}
1592+
1593+
func getCurrentPodNames(t *testing.T, ctx context.Context, kubeClient clientset.Interface, namespace string) []string {
1594+
podList, err := kubeClient.CoreV1().Pods(namespace).List(ctx, metav1.ListOptions{})
1595+
if err != nil {
1596+
t.Logf("Unable to list pods: %v", err)
1597+
return nil
1598+
}
1599+
1600+
names := []string{}
1601+
for _, item := range podList.Items {
1602+
names = append(names, item.Name)
1603+
}
1604+
return names
1605+
}

test/e2e/e2e_toomanyrestarts_test.go

Lines changed: 67 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package e2e
1919
import (
2020
"context"
2121
"fmt"
22+
"os"
2223
"strings"
2324
"testing"
2425
"time"
@@ -28,28 +29,66 @@ import (
2829
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2930
"k8s.io/apimachinery/pkg/labels"
3031
clientset "k8s.io/client-go/kubernetes"
31-
"k8s.io/client-go/tools/events"
32+
componentbaseconfig "k8s.io/component-base/config"
3233
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"
3639
"sigs.k8s.io/descheduler/pkg/framework/plugins/defaultevictor"
3740
"sigs.k8s.io/descheduler/pkg/framework/plugins/removepodshavingtoomanyrestarts"
38-
frameworktypes "sigs.k8s.io/descheduler/pkg/framework/types"
3941
)
4042

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+
4183
func TestTooManyRestarts(t *testing.T) {
4284
ctx := context.Background()
85+
initPluginRegistry()
4386

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")}, "")
4788
if err != nil {
48-
t.Errorf("Error listing node with %v", err)
89+
t.Errorf("Error during kubernetes client creation with %v", err)
4990
}
5091

51-
_, workerNodes := splitNodesAndWorkerNodes(nodeList.Items)
52-
5392
t.Logf("Creating testing namespace %v", t.Name())
5493
testNamespace := &v1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "e2e-" + strings.ToLower(t.Name())}}
5594
if _, err := clientSet.CoreV1().Namespaces().Create(ctx, testNamespace, metav1.CreateOptions{}); err != nil {
@@ -124,86 +163,46 @@ func TestTooManyRestarts(t *testing.T) {
124163
t.Fatal("Pod restart count not as expected")
125164
}
126165

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-
137166
tests := []struct {
138167
name string
139-
args removepodshavingtoomanyrestarts.RemovePodsHavingTooManyRestartsArgs
168+
policy *api.DeschedulerPolicy
140169
expectedEvictedPodCount uint
141170
}{
142171
{
143172
name: "test-no-evictions",
144-
args: createRemovePodsHavingTooManyRestartsAgrs(10000, true),
173+
policy: tooManyRestartsPolicy(testNamespace.Name, 10000, true),
145174
expectedEvictedPodCount: 0,
146175
},
147176
{
148177
name: "test-one-evictions",
149-
args: createRemovePodsHavingTooManyRestartsAgrs(4, true),
178+
policy: tooManyRestartsPolicy(testNamespace.Name, 4, true),
150179
expectedEvictedPodCount: 4,
151180
},
152181
}
153182
for _, tc := range tests {
154183
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()
183185
if err != nil {
184-
t.Fatalf("Unable to initialize the plugin: %v", err)
186+
t.Fatalf("Unable to initialize server: %v\n", err)
185187
}
188+
rs.Client = clientSet
189+
rs.EventClient = clientSet
186190

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")
196195
if err != nil {
197-
t.Fatalf("Unable to initialize the plugin: %v", err)
196+
t.Fatalf("Failed running a descheduling cycle: %v", err)
198197
}
199198

200-
// Run RemovePodsHavingTooManyRestarts strategy
201-
t.Log("Running RemovePodsHavingTooManyRestarts strategy")
202-
plugin.(frameworktypes.DeschedulePlugin).Deschedule(ctx, workerNodes)
203199
t.Logf("Finished RemoveFailedPods strategy for %s", tc.name)
204-
205200
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)
207206
if actualEvictedPodCount < tc.expectedEvictedPodCount {
208207
t.Errorf("Test error for description: %s. Unexpected number of pods have been evicted, got %v, expected %v", tc.name, actualEvictedPodCount, tc.expectedEvictedPodCount)
209208
}

0 commit comments

Comments
 (0)