Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 136 additions & 0 deletions test/e2e/e2e_deschedulinginterval_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*
Copyright 2024 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package e2e

import (
"context"
"os"
"testing"
"time"

v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
componentbaseconfig "k8s.io/component-base/config"

apiv1alpha2 "sigs.k8s.io/descheduler/pkg/api/v1alpha2"
"sigs.k8s.io/descheduler/pkg/descheduler/client"
)

// TestDeschedulingInterval verifies that without `--descheduling-interval` set
// (or with the default of 0), the descheduler binary runs a single pass and
// then exits cleanly.
//
// The previous version of this test invoked descheduler.RunDeschedulerStrategies
// in-process; that no longer reflects how the descheduler is shipped (an image
// running as a workload), so we now run it as a Pod with restartPolicy=Never
// and assert it terminates with `Succeeded` within a generous timeout.
func TestDeschedulingInterval(t *testing.T) {
ctx := context.Background()

clientSet, err := client.CreateClient(componentbaseconfig.ClientConnectionConfiguration{Kubeconfig: os.Getenv("KUBECONFIG")}, "")
if err != nil {
t.Errorf("Error during kubernetes client creation with %v", err)
}

// Empty policy is fine — we are testing the run-once-and-exit lifecycle, not
// any specific descheduling decision.
deschedulerPolicyConfigMapObj, err := deschedulerPolicyConfigMap(&apiv1alpha2.DeschedulerPolicy{})
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deschedulerPolicyConfigMapObj's .Name, resp. .Namespace are not changed. newDeschedulerOneShotPod can be simplified to keep the original CM name.

if err != nil {
t.Fatalf("Error building descheduler policy configmap: %v", err)
}

t.Logf("Creating %q policy CM ...", deschedulerPolicyConfigMapObj.Name)
if _, err := clientSet.CoreV1().ConfigMaps(deschedulerPolicyConfigMapObj.Namespace).Create(ctx, deschedulerPolicyConfigMapObj, metav1.CreateOptions{}); err != nil {
t.Fatalf("Error creating %q CM: %v", deschedulerPolicyConfigMapObj.Name, err)
}
defer func() {
t.Logf("Deleting %q CM...", deschedulerPolicyConfigMapObj.Name)
if err := clientSet.CoreV1().ConfigMaps(deschedulerPolicyConfigMapObj.Namespace).Delete(ctx, deschedulerPolicyConfigMapObj.Name, metav1.DeleteOptions{}); err != nil {
t.Fatalf("Unable to delete %q CM: %v", deschedulerPolicyConfigMapObj.Name, err)
}
}()

// Build a single-run Pod from the standard descheduler deployment template,
// dropping the `--descheduling-interval` flag so the descheduler exits after
// one iteration, and setting RestartPolicy=Never so the Pod doesn't get
// restarted by the kubelet on completion.
pod := newDeschedulerOneShotPod("descheduling-interval")

t.Logf("Creating one-shot descheduler pod %v", pod.Name)
if _, err := clientSet.CoreV1().Pods(pod.Namespace).Create(ctx, pod, metav1.CreateOptions{}); err != nil {
t.Fatalf("Error creating descheduler pod %q: %v", pod.Name, err)
}
defer func() {
printPodLogs(ctx, t, clientSet, pod.Name)
t.Logf("Deleting descheduler pod %q...", pod.Name)
if err := clientSet.CoreV1().Pods(pod.Namespace).Delete(ctx, pod.Name, metav1.DeleteOptions{}); err != nil {
t.Fatalf("Unable to delete descheduler pod %q: %v", pod.Name, err)
}
}()

t.Logf("Waiting for descheduler pod %q to reach Succeeded phase", pod.Name)
if err := wait.PollUntilContextTimeout(ctx, 5*time.Second, 3*time.Minute, true, func(ctx context.Context) (bool, error) {
current, err := clientSet.CoreV1().Pods(pod.Namespace).Get(ctx, pod.Name, metav1.GetOptions{})
if err != nil {
return false, err
}
switch current.Status.Phase {
case v1.PodSucceeded:
return true, nil
case v1.PodFailed:
return false, nil
default:
t.Logf("Pod %q is in phase %q, waiting...", pod.Name, current.Status.Phase)
return false, nil
}
}); err != nil {
t.Errorf("Descheduler pod did not run-once-and-exit within timeout: %v", err)
}
}

// newDeschedulerOneShotPod builds a Pod that runs the descheduler image once
// and exits, mirroring the container/security/volume configuration produced
// by deschedulerDeployment. Differences from the Deployment helper:
//
// - RestartPolicy is Never (Pods default to Always otherwise).
// - The `--descheduling-interval=100m` argument is dropped so the descheduler
// follows the run-once-and-exit code path.
// - No liveness probe — a one-shot run isn't long enough to make probing
// meaningful, and a slow first probe could race the clean exit.
func newDeschedulerOneShotPod(testName string) *v1.Pod {
deployment := deschedulerDeployment(testName)
podSpec := deployment.Spec.Template.Spec
podSpec.RestartPolicy = v1.RestartPolicyNever

container := podSpec.Containers[0]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same here

container.Args = []string{
"--policy-config-file", "/policy-dir/policy.yaml",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of resetting the args you could have deschedulerPodSpec take the descheduling interval value (set to 0 in this case). To avoid exposing the actual arguments and keeping only a single place where the binary args are set.

There's already a test under e2e_evictioninbackground_test.go that resets the args. So you are not breaking any convention here. Yet, if there's a way to avoid exposing the arguments/implementation details it's still preferable.

"--v", "4",
}
container.LivenessProbe = nil
podSpec.Containers[0] = container

return &v1.Pod{
Copy link
Copy Markdown
Contributor

@ingvagabund ingvagabund May 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you considered running it as a Job? To avoid the cases where a pod gets evicted in-flight before it finishes? E.g. due to resource constraints. Creating a job can help with re-creating the pod in these cases. To avoid some of the potential flakes.

ObjectMeta: metav1.ObjectMeta{
Name: "descheduler-oneshot-" + testName,
Namespace: deployment.Namespace,
Labels: deployment.Spec.Template.Labels,
},
Spec: podSpec,
}
}
39 changes: 0 additions & 39 deletions test/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,8 @@ import (
utilptr "k8s.io/utils/ptr"
"sigs.k8s.io/yaml"

"sigs.k8s.io/descheduler/cmd/descheduler/app/options"
deschedulerapi "sigs.k8s.io/descheduler/pkg/api"
deschedulerapiv1alpha2 "sigs.k8s.io/descheduler/pkg/api/v1alpha2"
"sigs.k8s.io/descheduler/pkg/descheduler"
"sigs.k8s.io/descheduler/pkg/descheduler/client"
"sigs.k8s.io/descheduler/pkg/descheduler/evictions"
eutils "sigs.k8s.io/descheduler/pkg/descheduler/evictions/utils"
Expand Down Expand Up @@ -1392,43 +1390,6 @@ func TestPodLifeTimeOldestEvicted(t *testing.T) {
}
}

func TestDeschedulingInterval(t *testing.T) {
ctx := context.Background()
clientSet, err := client.CreateClient(componentbaseconfig.ClientConnectionConfiguration{Kubeconfig: os.Getenv("KUBECONFIG")}, "")
if err != nil {
t.Errorf("Error during client creation with %v", err)
}

// By default, the DeschedulingInterval param should be set to 0, meaning Descheduler only runs once then exits
s, err := options.NewDeschedulerServer()
if err != nil {
t.Fatalf("Unable to initialize server: %v", err)
}
s.Client = clientSet
s.DefaultFeatureGates = initFeatureGates()

deschedulerPolicy := &deschedulerapi.DeschedulerPolicy{}

c := make(chan bool, 1)
go func() {
evictionPolicyGroupVersion, err := eutils.SupportEviction(s.Client)
if err != nil || len(evictionPolicyGroupVersion) == 0 {
t.Errorf("Error when checking support for eviction: %v", err)
}
if err := descheduler.RunDeschedulerStrategies(ctx, s, deschedulerPolicy, evictionPolicyGroupVersion); err != nil {
t.Errorf("Error running descheduler strategies: %+v", err)
}
c <- true
}()

select {
case <-c:
// successfully returned
case <-time.After(3 * time.Minute):
t.Errorf("descheduler.Run timed out even without descheduling-interval set")
}
}

func waitForRCPodsRunning(ctx context.Context, t *testing.T, clientSet clientset.Interface, rc *v1.ReplicationController) {
if err := wait.PollUntilContextTimeout(ctx, 5*time.Second, 30*time.Second, true, func(ctx context.Context) (bool, error) {
podList, err := clientSet.CoreV1().Pods(rc.Namespace).List(ctx, metav1.ListOptions{
Expand Down