@@ -42,16 +42,17 @@ type (
42
42
)
43
43
44
44
type PodEvictor struct {
45
- client clientset.Interface
46
- nodes []* v1.Node
47
- policyGroupVersion string
48
- dryRun bool
49
- maxPodsToEvictPerNode * uint
50
- maxPodsToEvictPerNamespace * uint
51
- nodepodCount nodePodEvictedCount
52
- namespacePodCount namespacePodEvictCount
53
- metricsEnabled bool
54
- eventRecorder events.EventRecorder
45
+ client clientset.Interface
46
+ nodes []* v1.Node
47
+ policyGroupVersion string
48
+ dryRun bool
49
+ maxPodsToEvictPerNode * uint
50
+ maxPodsToEvictPerNamespace * uint
51
+ nodepodCount nodePodEvictedCount
52
+ namespacePodCount namespacePodEvictCount
53
+ metricsEnabled bool
54
+ eventRecorder events.EventRecorder
55
+ recordEventsForEvictionErrors bool
55
56
}
56
57
57
58
func NewPodEvictor (
@@ -63,6 +64,7 @@ func NewPodEvictor(
63
64
nodes []* v1.Node ,
64
65
metricsEnabled bool ,
65
66
eventRecorder events.EventRecorder ,
67
+ recordEventsForEvictionErrors bool ,
66
68
) * PodEvictor {
67
69
nodePodCount := make (nodePodEvictedCount )
68
70
namespacePodCount := make (namespacePodEvictCount )
@@ -72,16 +74,17 @@ func NewPodEvictor(
72
74
}
73
75
74
76
return & PodEvictor {
75
- client : client ,
76
- nodes : nodes ,
77
- policyGroupVersion : policyGroupVersion ,
78
- dryRun : dryRun ,
79
- maxPodsToEvictPerNode : maxPodsToEvictPerNode ,
80
- maxPodsToEvictPerNamespace : maxPodsToEvictPerNamespace ,
81
- nodepodCount : nodePodCount ,
82
- namespacePodCount : namespacePodCount ,
83
- metricsEnabled : metricsEnabled ,
84
- eventRecorder : eventRecorder ,
77
+ client : client ,
78
+ nodes : nodes ,
79
+ policyGroupVersion : policyGroupVersion ,
80
+ dryRun : dryRun ,
81
+ maxPodsToEvictPerNode : maxPodsToEvictPerNode ,
82
+ maxPodsToEvictPerNamespace : maxPodsToEvictPerNamespace ,
83
+ nodepodCount : nodePodCount ,
84
+ namespacePodCount : namespacePodCount ,
85
+ metricsEnabled : metricsEnabled ,
86
+ eventRecorder : eventRecorder ,
87
+ recordEventsForEvictionErrors : recordEventsForEvictionErrors ,
85
88
}
86
89
}
87
90
@@ -152,6 +155,10 @@ func (pe *PodEvictor) EvictPod(ctx context.Context, pod *v1.Pod, opts EvictOptio
152
155
if pe .metricsEnabled {
153
156
metrics .PodsEvicted .With (map [string ]string {"result" : "error" , "strategy" : opts .StrategyName , "namespace" : pod .Namespace , "node" : pod .Spec .NodeName , "profile" : opts .ProfileName }).Inc ()
154
157
}
158
+ if pe .recordEventsForEvictionErrors {
159
+ reason := extractReason (opts , opts .StrategyName )
160
+ pe .eventRecorder .Eventf (pod , nil , v1 .EventTypeNormal , reason , "Descheduled" , "pod cannot be evicted from %v node by sigs.k8s.io/descheduler: %v" , pod .Spec .NodeName , err )
161
+ }
155
162
return false
156
163
}
157
164
@@ -167,19 +174,24 @@ func (pe *PodEvictor) EvictPod(ctx context.Context, pod *v1.Pod, opts EvictOptio
167
174
if pe .dryRun {
168
175
klog .V (1 ).InfoS ("Evicted pod in dry run mode" , "pod" , klog .KObj (pod ), "reason" , opts .Reason , "strategy" , opts .StrategyName , "node" , pod .Spec .NodeName , "profile" , opts .ProfileName )
169
176
} else {
170
- klog .V (1 ).InfoS ("Evicted pod" , "pod" , klog .KObj (pod ), "reason" , opts .Reason , "strategy" , opts .StrategyName , "node" , pod .Spec .NodeName , "profile" , opts .ProfileName )
171
- reason := opts .Reason
172
- if len (reason ) == 0 {
173
- reason = opts .StrategyName
174
- if len (reason ) == 0 {
175
- reason = "NotSet"
176
- }
177
- }
177
+ klog .V (1 ).InfoS ("Evicted pod" , "pod" , klog .KObj (pod ), "reason" , opts .Reason , "strategy" , opts .StrategyName , "node" , pod .Spec .NodeName )
178
+ reason := extractReason (opts , opts .StrategyName )
178
179
pe .eventRecorder .Eventf (pod , nil , v1 .EventTypeNormal , reason , "Descheduled" , "pod evicted from %v node by sigs.k8s.io/descheduler" , pod .Spec .NodeName )
179
180
}
180
181
return true
181
182
}
182
183
184
+ func extractReason (opts EvictOptions , strategy string ) string {
185
+ reason := opts .Reason
186
+ if len (reason ) == 0 {
187
+ reason = strategy
188
+ if len (reason ) == 0 {
189
+ reason = "NotSet"
190
+ }
191
+ }
192
+ return reason
193
+ }
194
+
183
195
func evictPod (ctx context.Context , client clientset.Interface , pod * v1.Pod , policyGroupVersion string ) error {
184
196
deleteOptions := & metav1.DeleteOptions {}
185
197
// GracePeriodSeconds ?
0 commit comments