Skip to content

Commit 29ff28c

Browse files
authored
Merge pull request kubernetes-sigs#1536 from googs1025/test
feature(eviction): add event when EvictPod failed
2 parents d653537 + bbffb83 commit 29ff28c

File tree

9 files changed

+319
-94
lines changed

9 files changed

+319
-94
lines changed

pkg/api/types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ type DeschedulerPolicy struct {
4242
// MaxNoOfPodsToTotal restricts maximum of pods to be evicted total.
4343
MaxNoOfPodsToEvictTotal *uint
4444

45+
// EvictionFailureEventNotification should be set to true to enable eviction failure event notification.
46+
// Default is false.
47+
EvictionFailureEventNotification *bool
48+
4549
// MetricsCollector configures collection of metrics about actual resource utilization
4650
MetricsCollector MetricsCollector
4751
}

pkg/api/v1alpha2/types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ type DeschedulerPolicy struct {
4141
// MaxNoOfPodsToTotal restricts maximum of pods to be evicted total.
4242
MaxNoOfPodsToEvictTotal *uint `json:"maxNoOfPodsToEvictTotal,omitempty"`
4343

44+
// EvictionFailureEventNotification should be set to true to enable eviction failure event notification.
45+
// Default is false.
46+
EvictionFailureEventNotification *bool
47+
4448
// MetricsCollector configures collection of metrics for actual resource utilization
4549
MetricsCollector MetricsCollector `json:"metricsCollector,omitempty"`
4650
}

pkg/api/v1alpha2/zz_generated.conversion.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/api/v1alpha2/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/api/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/descheduler/descheduler.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ func newDescheduler(ctx context.Context, rs *options.DeschedulerServer, deschedu
156156
WithMaxPodsToEvictPerNode(deschedulerPolicy.MaxNoOfPodsToEvictPerNode).
157157
WithMaxPodsToEvictPerNamespace(deschedulerPolicy.MaxNoOfPodsToEvictPerNamespace).
158158
WithMaxPodsToEvictTotal(deschedulerPolicy.MaxNoOfPodsToEvictTotal).
159+
WithEvictionFailureEventNotification(deschedulerPolicy.EvictionFailureEventNotification).
159160
WithDryRun(rs.DryRun).
160161
WithMetricsEnabled(!rs.DisableMetrics),
161162
)

pkg/descheduler/evictions/evictions.go

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -206,20 +206,21 @@ type (
206206
)
207207

208208
type PodEvictor struct {
209-
mu sync.RWMutex
210-
client clientset.Interface
211-
policyGroupVersion string
212-
dryRun bool
213-
maxPodsToEvictPerNode *uint
214-
maxPodsToEvictPerNamespace *uint
215-
maxPodsToEvictTotal *uint
216-
nodePodCount nodePodEvictedCount
217-
namespacePodCount namespacePodEvictCount
218-
totalPodCount uint
219-
metricsEnabled bool
220-
eventRecorder events.EventRecorder
221-
erCache *evictionRequestsCache
222-
featureGates featuregate.FeatureGate
209+
mu sync.RWMutex
210+
client clientset.Interface
211+
policyGroupVersion string
212+
dryRun bool
213+
evictionFailureEventNotification bool
214+
maxPodsToEvictPerNode *uint
215+
maxPodsToEvictPerNamespace *uint
216+
maxPodsToEvictTotal *uint
217+
nodePodCount nodePodEvictedCount
218+
namespacePodCount namespacePodEvictCount
219+
totalPodCount uint
220+
metricsEnabled bool
221+
eventRecorder events.EventRecorder
222+
erCache *evictionRequestsCache
223+
featureGates featuregate.FeatureGate
223224

224225
// registeredHandlers contains the registrations of all handlers. It's used to check if all handlers have finished syncing before the scheduling cycles start.
225226
registeredHandlers []cache.ResourceEventHandlerRegistration
@@ -238,17 +239,18 @@ func NewPodEvictor(
238239
}
239240

240241
podEvictor := &PodEvictor{
241-
client: client,
242-
eventRecorder: eventRecorder,
243-
policyGroupVersion: options.policyGroupVersion,
244-
dryRun: options.dryRun,
245-
maxPodsToEvictPerNode: options.maxPodsToEvictPerNode,
246-
maxPodsToEvictPerNamespace: options.maxPodsToEvictPerNamespace,
247-
maxPodsToEvictTotal: options.maxPodsToEvictTotal,
248-
metricsEnabled: options.metricsEnabled,
249-
nodePodCount: make(nodePodEvictedCount),
250-
namespacePodCount: make(namespacePodEvictCount),
251-
featureGates: featureGates,
242+
client: client,
243+
eventRecorder: eventRecorder,
244+
policyGroupVersion: options.policyGroupVersion,
245+
dryRun: options.dryRun,
246+
evictionFailureEventNotification: options.evictionFailureEventNotification,
247+
maxPodsToEvictPerNode: options.maxPodsToEvictPerNode,
248+
maxPodsToEvictPerNamespace: options.maxPodsToEvictPerNamespace,
249+
maxPodsToEvictTotal: options.maxPodsToEvictTotal,
250+
metricsEnabled: options.metricsEnabled,
251+
nodePodCount: make(nodePodEvictedCount),
252+
namespacePodCount: make(namespacePodEvictCount),
253+
featureGates: featureGates,
252254
}
253255

254256
if featureGates.Enabled(features.EvictionsInBackground) {
@@ -481,6 +483,9 @@ func (pe *PodEvictor) EvictPod(ctx context.Context, pod *v1.Pod, opts EvictOptio
481483
}
482484
span.AddEvent("Eviction Failed", trace.WithAttributes(attribute.String("node", pod.Spec.NodeName), attribute.String("err", err.Error())))
483485
klog.ErrorS(err, "Error evicting pod", "limit", *pe.maxPodsToEvictTotal)
486+
if pe.evictionFailureEventNotification {
487+
pe.eventRecorder.Eventf(pod, nil, v1.EventTypeWarning, "EvictionFailed", "Descheduled", "pod eviction from %v node by sigs.k8s.io/descheduler failed: total eviction limit exceeded (%v)", pod.Spec.NodeName, *pe.maxPodsToEvictTotal)
488+
}
484489
return err
485490
}
486491

@@ -492,6 +497,9 @@ func (pe *PodEvictor) EvictPod(ctx context.Context, pod *v1.Pod, opts EvictOptio
492497
}
493498
span.AddEvent("Eviction Failed", trace.WithAttributes(attribute.String("node", pod.Spec.NodeName), attribute.String("err", err.Error())))
494499
klog.ErrorS(err, "Error evicting pod", "limit", *pe.maxPodsToEvictPerNode, "node", pod.Spec.NodeName)
500+
if pe.evictionFailureEventNotification {
501+
pe.eventRecorder.Eventf(pod, nil, v1.EventTypeWarning, "EvictionFailed", "Descheduled", "pod eviction from %v node by sigs.k8s.io/descheduler failed: node eviction limit exceeded (%v)", pod.Spec.NodeName, *pe.maxPodsToEvictPerNode)
502+
}
495503
return err
496504
}
497505
}
@@ -503,6 +511,9 @@ func (pe *PodEvictor) EvictPod(ctx context.Context, pod *v1.Pod, opts EvictOptio
503511
}
504512
span.AddEvent("Eviction Failed", trace.WithAttributes(attribute.String("node", pod.Spec.NodeName), attribute.String("err", err.Error())))
505513
klog.ErrorS(err, "Error evicting pod", "limit", *pe.maxPodsToEvictPerNamespace, "namespace", pod.Namespace, "pod", klog.KObj(pod))
514+
if pe.evictionFailureEventNotification {
515+
pe.eventRecorder.Eventf(pod, nil, v1.EventTypeWarning, "EvictionFailed", "Descheduled", "pod eviction from %v node by sigs.k8s.io/descheduler failed: namespace eviction limit exceeded (%v)", pod.Spec.NodeName, *pe.maxPodsToEvictPerNamespace)
516+
}
506517
return err
507518
}
508519

@@ -514,6 +525,9 @@ func (pe *PodEvictor) EvictPod(ctx context.Context, pod *v1.Pod, opts EvictOptio
514525
if pe.metricsEnabled {
515526
metrics.PodsEvicted.With(map[string]string{"result": "error", "strategy": opts.StrategyName, "namespace": pod.Namespace, "node": pod.Spec.NodeName, "profile": opts.ProfileName}).Inc()
516527
}
528+
if pe.evictionFailureEventNotification {
529+
pe.eventRecorder.Eventf(pod, nil, v1.EventTypeWarning, "EvictionFailed", "Descheduled", "pod eviction from %v node by sigs.k8s.io/descheduler failed: %v", pod.Spec.NodeName, err.Error())
530+
}
517531
return err
518532
}
519533

@@ -542,7 +556,7 @@ func (pe *PodEvictor) EvictPod(ctx context.Context, pod *v1.Pod, opts EvictOptio
542556
reason = "NotSet"
543557
}
544558
}
545-
pe.eventRecorder.Eventf(pod, nil, v1.EventTypeNormal, reason, "Descheduled", "pod evicted from %v node by sigs.k8s.io/descheduler", pod.Spec.NodeName)
559+
pe.eventRecorder.Eventf(pod, nil, v1.EventTypeNormal, reason, "Descheduled", "pod eviction from %v node by sigs.k8s.io/descheduler", pod.Spec.NodeName)
546560
}
547561
return nil
548562
}

0 commit comments

Comments
 (0)