1
0
mirror of https://github.com/kubernetes-sigs/descheduler.git synced 2026-01-28 14:41:10 +01:00

Merge pull request #913 from a7i/migrate-PodLifeTime-to-plugin

Migrate PodLifeTime to plugin
This commit is contained in:
Kubernetes Prow Robot
2022-08-15 07:14:14 -07:00
committed by GitHub
10 changed files with 436 additions and 327 deletions

View File

@@ -39,6 +39,10 @@ import (
"k8s.io/client-go/tools/events"
v1qos "k8s.io/kubectl/pkg/util/qos"
"k8s.io/utils/pointer"
"sigs.k8s.io/descheduler/pkg/apis/componentconfig"
"sigs.k8s.io/descheduler/pkg/framework"
frameworkfake "sigs.k8s.io/descheduler/pkg/framework/fake"
"sigs.k8s.io/descheduler/pkg/framework/plugins/podlifetime"
"sigs.k8s.io/descheduler/cmd/descheduler/app/options"
deschedulerapi "sigs.k8s.io/descheduler/pkg/api"
@@ -48,7 +52,6 @@ import (
eutils "sigs.k8s.io/descheduler/pkg/descheduler/evictions/utils"
nodeutil "sigs.k8s.io/descheduler/pkg/descheduler/node"
podutil "sigs.k8s.io/descheduler/pkg/descheduler/pod"
"sigs.k8s.io/descheduler/pkg/descheduler/strategies"
"sigs.k8s.io/descheduler/pkg/descheduler/strategies/nodeutilization"
"sigs.k8s.io/descheduler/pkg/utils"
)
@@ -153,7 +156,7 @@ func waitForNodesReady(ctx context.Context, t *testing.T, clientSet clientset.In
}
}
func runPodLifetimeStrategy(
func runPodLifetimePlugin(
ctx context.Context,
t *testing.T,
clientset clientset.Interface,
@@ -166,7 +169,6 @@ func runPodLifetimeStrategy(
labelSelector *metav1.LabelSelector,
getPodsAssignedToNode podutil.GetPodsAssignedToNodeFunc,
) {
// Run descheduler.
evictionPolicyGroupVersion, err := eutils.SupportEviction(clientset)
if err != nil || len(evictionPolicyGroupVersion) == 0 {
t.Fatalf("%v", err)
@@ -177,51 +179,54 @@ func runPodLifetimeStrategy(
t.Fatalf("%v", err)
}
maxPodLifeTimeSeconds := uint(1)
strategy := deschedulerapi.DeschedulerStrategy{
Enabled: true,
Params: &deschedulerapi.StrategyParameters{
PodLifeTime: &deschedulerapi.PodLifeTime{MaxPodLifeTimeSeconds: &maxPodLifeTimeSeconds},
Namespaces: namespaces,
ThresholdPriority: priority,
ThresholdPriorityClassName: priorityClass,
LabelSelector: labelSelector,
},
}
thresholdPriority, err := utils.GetPriorityFromStrategyParams(ctx, clientset, strategy.Params)
if err != nil {
t.Fatalf("Failed to get threshold priority from strategy's params")
}
eventRecorder := &events.FakeRecorder{}
strategies.PodLifeTime(
ctx,
podEvictor := evictions.NewPodEvictor(
clientset,
strategy,
evictionPolicyGroupVersion,
false,
nil,
maxPodsToEvictPerNamespace,
nodes,
evictions.NewPodEvictor(
clientset,
evictionPolicyGroupVersion,
false,
nil,
maxPodsToEvictPerNamespace,
nodes,
false,
eventRecorder,
),
evictions.NewEvictorFilter(
nodes,
getPodsAssignedToNode,
false,
evictCritical,
false,
false,
evictions.WithPriorityThreshold(thresholdPriority),
),
getPodsAssignedToNode,
false,
&events.FakeRecorder{},
)
var thresholdPriority int32
if priority != nil {
thresholdPriority = *priority
} else {
thresholdPriority, err = utils.GetPriorityFromPriorityClass(ctx, clientset, priorityClass)
if err != nil {
t.Fatalf("Failed to get threshold priority from plugin arg params")
}
}
evictorFilter := evictions.NewEvictorFilter(
nodes,
getPodsAssignedToNode,
false,
evictCritical,
false,
false,
evictions.WithPriorityThreshold(thresholdPriority),
)
maxPodLifeTimeSeconds := uint(1)
plugin, err := podlifetime.New(&componentconfig.PodLifeTimeArgs{
MaxPodLifeTimeSeconds: &maxPodLifeTimeSeconds,
LabelSelector: labelSelector,
Namespaces: namespaces,
}, &frameworkfake.HandleImpl{
ClientsetImpl: clientset,
PodEvictorImpl: podEvictor,
EvictorFilterImpl: evictorFilter,
GetPodsAssignedToNodeFuncImpl: getPodsAssignedToNode,
})
if err != nil {
t.Fatalf("Unable to initialize the plugin: %v", err)
}
t.Log("Running podlifetime plugin")
plugin.(framework.DeschedulePlugin).Deschedule(ctx, nodes)
}
func getPodNames(pods []v1.Pod) []string {
@@ -440,8 +445,8 @@ func TestNamespaceConstraintsInclude(t *testing.T) {
sort.Strings(initialPodNames)
t.Logf("Existing pods: %v", initialPodNames)
t.Logf("set the strategy to delete pods from %v namespace", rc.Namespace)
runPodLifetimeStrategy(ctx, t, clientSet, nodeInformer, &deschedulerapi.Namespaces{
t.Logf("run the plugin to delete pods from %v namespace", rc.Namespace)
runPodLifetimePlugin(ctx, t, clientSet, nodeInformer, &deschedulerapi.Namespaces{
Include: []string{rc.Namespace},
}, "", nil, false, nil, nil, getPodsAssignedToNode)
@@ -511,8 +516,8 @@ func TestNamespaceConstraintsExclude(t *testing.T) {
sort.Strings(initialPodNames)
t.Logf("Existing pods: %v", initialPodNames)
t.Logf("set the strategy to delete pods from namespaces except the %v namespace", rc.Namespace)
runPodLifetimeStrategy(ctx, t, clientSet, nodeInformer, &deschedulerapi.Namespaces{
t.Logf("run the plugin to delete pods from namespaces except the %v namespace", rc.Namespace)
runPodLifetimePlugin(ctx, t, clientSet, nodeInformer, &deschedulerapi.Namespaces{
Exclude: []string{rc.Namespace},
}, "", nil, false, nil, nil, getPodsAssignedToNode)
@@ -520,7 +525,7 @@ func TestNamespaceConstraintsExclude(t *testing.T) {
time.Sleep(10 * time.Second)
podList, err = clientSet.CoreV1().Pods(rc.Namespace).List(ctx, metav1.ListOptions{LabelSelector: labels.SelectorFromSet(rc.Spec.Template.Labels).String()})
if err != nil {
t.Fatalf("Unable to list pods after running strategy: %v", err)
t.Fatalf("Unable to list pods after running PodLifeTime plugin: %v", err)
}
excludePodNames := getPodNames(podList.Items)
@@ -627,9 +632,9 @@ func testEvictSystemCritical(t *testing.T, isPriorityClass bool) {
t.Logf("Existing pods: %v", initialPodNames)
if isPriorityClass {
runPodLifetimeStrategy(ctx, t, clientSet, nodeInformer, nil, highPriorityClass.Name, nil, true, nil, nil, getPodsAssignedToNode)
runPodLifetimePlugin(ctx, t, clientSet, nodeInformer, nil, highPriorityClass.Name, nil, true, nil, nil, getPodsAssignedToNode)
} else {
runPodLifetimeStrategy(ctx, t, clientSet, nodeInformer, nil, "", &highPriority, true, nil, nil, getPodsAssignedToNode)
runPodLifetimePlugin(ctx, t, clientSet, nodeInformer, nil, "", &highPriority, true, nil, nil, getPodsAssignedToNode)
}
// All pods are supposed to be deleted, wait until all pods in the test namespace are terminating
@@ -745,11 +750,11 @@ func testPriority(t *testing.T, isPriorityClass bool) {
t.Logf("Pods not expected to be evicted: %v, pods expected to be evicted: %v", expectReservePodNames, expectEvictPodNames)
if isPriorityClass {
t.Logf("set the strategy to delete pods with priority lower than priority class %s", highPriorityClass.Name)
runPodLifetimeStrategy(ctx, t, clientSet, nodeInformer, nil, highPriorityClass.Name, nil, false, nil, nil, getPodsAssignedToNode)
t.Logf("run the plugin to delete pods with priority lower than priority class %s", highPriorityClass.Name)
runPodLifetimePlugin(ctx, t, clientSet, nodeInformer, nil, highPriorityClass.Name, nil, false, nil, nil, getPodsAssignedToNode)
} else {
t.Logf("set the strategy to delete pods with priority lower than %d", highPriority)
runPodLifetimeStrategy(ctx, t, clientSet, nodeInformer, nil, "", &highPriority, false, nil, nil, getPodsAssignedToNode)
t.Logf("run the plugin to delete pods with priority lower than %d", highPriority)
runPodLifetimePlugin(ctx, t, clientSet, nodeInformer, nil, "", &highPriority, false, nil, nil, getPodsAssignedToNode)
}
t.Logf("Waiting 10s")
@@ -758,7 +763,7 @@ func testPriority(t *testing.T, isPriorityClass bool) {
podListHighPriority, err = clientSet.CoreV1().Pods(rcHighPriority.Namespace).List(
ctx, metav1.ListOptions{LabelSelector: labels.SelectorFromSet(rcHighPriority.Spec.Template.Labels).String()})
if err != nil {
t.Fatalf("Unable to list pods after running strategy: %v", err)
t.Fatalf("Unable to list pods after running plugin: %v", err)
}
excludePodNames := getPodNames(podListHighPriority.Items)
@@ -852,8 +857,8 @@ func TestPodLabelSelector(t *testing.T) {
sort.Strings(expectEvictPodNames)
t.Logf("Pods not expected to be evicted: %v, pods expected to be evicted: %v", expectReservePodNames, expectEvictPodNames)
t.Logf("set the strategy to delete pods with label test:podlifetime-evict")
runPodLifetimeStrategy(ctx, t, clientSet, nodeInformer, nil, "", nil, false, nil, &metav1.LabelSelector{MatchLabels: map[string]string{"test": "podlifetime-evict"}}, getPodsAssignedToNode)
t.Logf("run the plugin to delete pods with label test:podlifetime-evict")
runPodLifetimePlugin(ctx, t, clientSet, nodeInformer, nil, "", nil, false, nil, &metav1.LabelSelector{MatchLabels: map[string]string{"test": "podlifetime-evict"}}, getPodsAssignedToNode)
t.Logf("Waiting 10s")
time.Sleep(10 * time.Second)
@@ -861,7 +866,7 @@ func TestPodLabelSelector(t *testing.T) {
podListReserve, err = clientSet.CoreV1().Pods(rcReserve.Namespace).List(
ctx, metav1.ListOptions{LabelSelector: labels.SelectorFromSet(rcReserve.Spec.Template.Labels).String()})
if err != nil {
t.Fatalf("Unable to list pods after running strategy: %v", err)
t.Fatalf("Unable to list pods after running plugin: %v", err)
}
reservedPodNames := getPodNames(podListReserve.Items)
@@ -952,13 +957,13 @@ func TestEvictAnnotation(t *testing.T) {
sort.Strings(initialPodNames)
t.Logf("Existing pods: %v", initialPodNames)
t.Log("Running PodLifetime strategy")
runPodLifetimeStrategy(ctx, t, clientSet, nodeInformer, nil, "", nil, false, nil, nil, getPodsAssignedToNode)
t.Log("Running PodLifetime plugin")
runPodLifetimePlugin(ctx, t, clientSet, nodeInformer, nil, "", nil, false, nil, nil, getPodsAssignedToNode)
if err := wait.PollImmediate(5*time.Second, time.Minute, func() (bool, error) {
podList, err = clientSet.CoreV1().Pods(rc.Namespace).List(ctx, metav1.ListOptions{LabelSelector: labels.SelectorFromSet(rc.Spec.Template.Labels).String()})
if err != nil {
return false, fmt.Errorf("Unable to list pods after running strategy: %v", err)
return false, fmt.Errorf("unable to list pods after running plugin: %v", err)
}
excludePodNames := getPodNames(podList.Items)
@@ -1017,10 +1022,10 @@ func TestPodLifeTimeOldestEvicted(t *testing.T) {
t.Fatalf("Unable to list pods: %v", err)
}
t.Log("Running PodLifetime strategy with maxPodsToEvictPerNamespace=1 to ensure only the oldest pod is evicted")
t.Log("Running PodLifetime plugin with maxPodsToEvictPerNamespace=1 to ensure only the oldest pod is evicted")
var maxPodsToEvictPerNamespace uint = 1
runPodLifetimeStrategy(ctx, t, clientSet, nodeInformer, nil, "", nil, false, &maxPodsToEvictPerNamespace, nil, getPodsAssignedToNode)
t.Log("Finished PodLifetime strategy")
runPodLifetimePlugin(ctx, t, clientSet, nodeInformer, nil, "", nil, false, &maxPodsToEvictPerNamespace, nil, getPodsAssignedToNode)
t.Log("Finished PodLifetime plugin")
t.Logf("Wait for terminating pod to disappear")
waitForTerminatingPodsToDisappear(ctx, t, clientSet, rc.Namespace)