Skip to content

Commit 79b2e04

Browse files
authored
Merge pull request #1753 from tiraboschi/annotate_eviction_requests
feat(eviction): add annotations to eviction requests for observability
2 parents 07b1c4e + 3a608a5 commit 79b2e04

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

pkg/descheduler/evictions/evictions.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ import (
4242
"sigs.k8s.io/descheduler/pkg/tracing"
4343
)
4444

45+
const (
46+
deschedulerGlobalName = "sigs.k8s.io/descheduler"
47+
reasonAnnotationKey = "reason"
48+
requestedByAnnotationKey = "requested-by"
49+
)
50+
4551
var (
4652
assumedEvictionRequestTimeoutSeconds uint = 10 * 60 // 10 minutes
4753
evictionRequestsCacheResyncPeriod time.Duration = 10 * time.Minute
@@ -522,7 +528,7 @@ func (pe *PodEvictor) EvictPod(ctx context.Context, pod *v1.Pod, opts EvictOptio
522528
return err
523529
}
524530

525-
ignore, err := pe.evictPod(ctx, pod)
531+
ignore, err := pe.evictPod(ctx, pod, opts)
526532
if err != nil {
527533
// err is used only for logging purposes
528534
span.AddEvent("Eviction Failed", trace.WithAttributes(attribute.String("node", pod.Spec.NodeName), attribute.String("err", err.Error())))
@@ -569,7 +575,7 @@ func (pe *PodEvictor) EvictPod(ctx context.Context, pod *v1.Pod, opts EvictOptio
569575
}
570576

571577
// return (ignore, err)
572-
func (pe *PodEvictor) evictPod(ctx context.Context, pod *v1.Pod) (bool, error) {
578+
func (pe *PodEvictor) evictPod(ctx context.Context, pod *v1.Pod, opts EvictOptions) (bool, error) {
573579
deleteOptions := &metav1.DeleteOptions{
574580
GracePeriodSeconds: pe.gracePeriodSeconds,
575581
}
@@ -582,6 +588,10 @@ func (pe *PodEvictor) evictPod(ctx context.Context, pod *v1.Pod) (bool, error) {
582588
ObjectMeta: metav1.ObjectMeta{
583589
Name: pod.Name,
584590
Namespace: pod.Namespace,
591+
Annotations: map[string]string{
592+
"reason": fmt.Sprintf("triggered by %v/%v: %v", opts.ProfileName, opts.StrategyName, opts.Reason),
593+
"requested-by": deschedulerGlobalName,
594+
},
585595
},
586596
DeleteOptions: deleteOptions,
587597
}

pkg/descheduler/evictions/evictions_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"fmt"
2222
"reflect"
23+
"strings"
2324
"testing"
2425
"time"
2526

@@ -114,7 +115,7 @@ func TestEvictPod(t *testing.T) {
114115
t.Fatalf("Unexpected error when creating a pod evictor: %v", err)
115116
}
116117

117-
_, got := podEvictor.evictPod(ctx, test.evictedPod)
118+
_, got := podEvictor.evictPod(ctx, test.evictedPod, EvictOptions{})
118119
if got != test.wantErr {
119120
t.Errorf("Test error for Desc: %s. Expected %v pod eviction to be %v, got %v", test.description, test.evictedPod.Name, test.wantErr, got)
120121
}
@@ -418,7 +419,11 @@ func TestEvictionRequestsCacheCleanup(t *testing.T) {
418419
}
419420
if eviction, matched := createAct.Object.(*policy.Eviction); matched {
420421
podName := eviction.GetName()
421-
if podName == "p1" || podName == "p2" {
422+
annotations := eviction.GetAnnotations()
423+
if (podName == "p1" || podName == "p2") && annotations[requestedByAnnotationKey] == deschedulerGlobalName && strings.HasPrefix(
424+
annotations[reasonAnnotationKey],
425+
"triggered by",
426+
) {
422427
return true, nil, &apierrors.StatusError{
423428
ErrStatus: metav1.Status{
424429
Reason: metav1.StatusReasonTooManyRequests,

0 commit comments

Comments
 (0)