Skip to content

Commit 2761a9c

Browse files
Refactor some detials of disruption reason condition
1 parent e24a934 commit 2761a9c

File tree

3 files changed

+10
-15
lines changed

3 files changed

+10
-15
lines changed

pkg/controllers/disruption/controller.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ func (c *Controller) MarkDisrupted(ctx context.Context, m Method, candidates ...
278278
return client.IgnoreNotFound(err)
279279
}
280280
stored := nodeClaim.DeepCopy()
281-
nodeClaim.StatusConditions().SetTrueWithReason(v1.ConditionTypeDisruptionReason, v1.ConditionTypeDisruptionReason, string(m.Reason()))
281+
nodeClaim.StatusConditions().SetTrueWithReason(v1.ConditionTypeDisruptionReason, string(m.Reason()), string(m.Reason()))
282282
return client.IgnoreNotFound(c.kubeClient.Status().Patch(ctx, nodeClaim, client.MergeFrom(stored)))
283283
})...)
284284
}

pkg/controllers/node/termination/terminator/events/events.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ import (
2626
"sigs.k8s.io/karpenter/pkg/events"
2727
)
2828

29-
func EvictPod(pod *corev1.Pod, message string) events.Event {
29+
func EvictPod(pod *corev1.Pod, reason string) events.Event {
3030
return events.Event{
3131
InvolvedObject: pod,
3232
Type: corev1.EventTypeNormal,
3333
Reason: events.Evicted,
34-
Message: "Evicted pod: " + message,
34+
Message: "Evicted pod: " + reason,
3535
DedupeValues: []string{pod.Name},
3636
}
3737
}

pkg/controllers/node/termination/terminator/eviction.go

+7-12
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,6 @@ func (q *Queue) Reconcile(ctx context.Context) (reconcile.Result, error) {
175175
// Evict returns true if successful eviction call, and false if there was an eviction-related error
176176
func (q *Queue) Evict(ctx context.Context, key QueueKey) bool {
177177
ctx = log.IntoContext(ctx, log.FromContext(ctx).WithValues("Pod", klog.KRef(key.Namespace, key.Name)))
178-
evictionMessage, err := evictionReason(ctx, key, q.kubeClient)
179-
if err != nil {
180-
// XXX(cmcavoy): this should be unreachable, but we log it if it happens
181-
log.FromContext(ctx).V(1).Error(err, "failed looking up pod eviction reason")
182-
}
183178
if err := q.kubeClient.SubResource("eviction").Create(ctx,
184179
&corev1.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: key.Namespace, Name: key.Name}},
185180
&policyv1.Eviction{
@@ -214,18 +209,18 @@ func (q *Queue) Evict(ctx context.Context, key QueueKey) bool {
214209
return false
215210
}
216211
NodesEvictionRequestsTotal.Inc(map[string]string{CodeLabel: "200"})
217-
q.recorder.Publish(terminatorevents.EvictPod(&corev1.Pod{ObjectMeta: metav1.ObjectMeta{Name: key.Name, Namespace: key.Namespace}}, evictionMessage))
212+
q.recorder.Publish(terminatorevents.EvictPod(&corev1.Pod{ObjectMeta: metav1.ObjectMeta{Name: key.Name, Namespace: key.Namespace}}, evictionReason(ctx, key, q.kubeClient)))
218213
return true
219214
}
220215

221-
func evictionReason(ctx context.Context, key QueueKey, kubeClient client.Client) (string, error) {
216+
func evictionReason(ctx context.Context, key QueueKey, kubeClient client.Client) string {
222217
nodeClaim, err := node.NodeClaimForNode(ctx, kubeClient, &corev1.Node{Spec: corev1.NodeSpec{ProviderID: key.providerID}})
223218
if err != nil {
224-
return "", err
219+
log.FromContext(ctx).V(1).Error(err, "failed looking up pod eviction reason")
220+
return ""
225221
}
226-
terminationCondition := nodeClaim.StatusConditions().Get(v1.ConditionTypeDisruptionReason)
227-
if terminationCondition.IsTrue() {
228-
return terminationCondition.Message, nil
222+
if cond := nodeClaim.StatusConditions().Get(v1.ConditionTypeDisruptionReason); cond.IsTrue() {
223+
return cond.Reason
229224
}
230-
return "Forceful Termination", nil
225+
return "Forceful Termination"
231226
}

0 commit comments

Comments
 (0)