Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
b4d8577
Add `FailureRecoveryPolicy` to `Configuration`
kshalot Nov 5, 2025
c8e14ce
Implement the `PodTermination` failure recovery action
kshalot Nov 13, 2025
e5e01fd
Add `failurerecovery` integration test suite
kshalot Nov 5, 2025
c1c3739
Use patch instead of update to terminate stuck pods
kshalot Nov 13, 2025
d18e6ba
Fix indendation in example failure recovery policy
kshalot Nov 14, 2025
3c8a8ae
Realign the configuration with KEP changes
kshalot Nov 18, 2025
4576252
Validate that `terminatePod` is set on failure policy
kshalot Nov 18, 2025
9aa810a
Fix invalid label selector in config test
kshalot Nov 18, 2025
ec481a2
Remove the `FailureRecoveryPolicy` API
kshalot Nov 18, 2025
d379ac4
Add `FailureRecoveryPolicy` feature gate
kshalot Nov 18, 2025
92c4e3a
Configure shorter termination grace period in test
kshalot Nov 19, 2025
178119f
Rename `gracePeriodLeft` to `remainingTime`
kshalot Nov 19, 2025
21ead2e
Fix option not being set on struct
kshalot Nov 19, 2025
86e4bc8
Remove `util.node` in favor of `util.taints`
kshalot Nov 19, 2025
b2c6e58
Add missing copyright headers
kshalot Nov 19, 2025
00254ab
Fix feature gate being true by default
kshalot Nov 19, 2025
a1a9346
Adjust timeouts in integration test
kshalot Nov 19, 2025
b494149
Consolidate unhappy path test cases into a single test
kshalot Nov 20, 2025
68e1f58
Move annotation costants to the `constants` package
kshalot Nov 20, 2025
b981205
Remove helper methods from tests
kshalot Nov 20, 2025
1044989
Fix counting deletion grace period twice
kshalot Nov 20, 2025
3f11131
Use event filter to unburden the reconciler
kshalot Nov 20, 2025
606dcc6
Adjust unit test
kshalot Nov 20, 2025
f5f1d2d
Move termination threshold check above node taint check
kshalot Nov 20, 2025
e0024de
Ignore node not found errors
kshalot Nov 20, 2025
2b70919
Emit an event upon forceful pod termination
kshalot Nov 20, 2025
e266319
Add `KueueFailureRecovery` condition after forceful pod termination
kshalot Nov 20, 2025
bee8767
Add missing event filter unit tests
kshalot Nov 20, 2025
20d4563
Simplify deletion update filter condition
kshalot Nov 20, 2025
c3a5c37
Rename integration test timeout variable
kshalot Nov 20, 2025
d15bb80
Fix extra lines at the start of block
kshalot Nov 20, 2025
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
11 changes: 6 additions & 5 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ import (
)

const (
KueueName = "kueue"
JobControllerName = KueueName + "-job-controller"
WorkloadControllerName = KueueName + "-workload-controller"
AdmissionName = KueueName + "-admission"
ReclaimablePodsMgr = KueueName + "-reclaimable-pods"
KueueName = "kueue"
JobControllerName = KueueName + "-job-controller"
WorkloadControllerName = KueueName + "-workload-controller"
PodTerminationControllerName = KueueName + "-pod-termination-controller"
AdmissionName = KueueName + "-admission"
ReclaimablePodsMgr = KueueName + "-reclaimable-pods"

// UpdatesBatchPeriod is the batch period to hold workload updates
// before syncing a Queue and ClusterQueue objects.
Expand Down
5 changes: 5 additions & 0 deletions pkg/controller/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,9 @@ const (

// MaxExecTimeSecondsLabel is the label key in the job that holds the maximum execution time.
MaxExecTimeSecondsLabel = `kueue.x-k8s.io/max-exec-time-seconds`

// SafeToForcefullyTerminateAnnotationKey is the annotation key that controls whether a pod opted in to FailureRecoveryPolicy.
SafeToForcefullyTerminateAnnotationKey = "kueue.x-k8s.io/safe-to-forcefully-terminate"
// SafeToForcefullyTerminateAnnotationValue is the value of that annotation that enables FailureRecoveryPolicy for that pod.
SafeToForcefullyTerminateAnnotationValue = "true"
)
15 changes: 15 additions & 0 deletions pkg/controller/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
qcache "sigs.k8s.io/kueue/pkg/cache/queue"
schdcache "sigs.k8s.io/kueue/pkg/cache/scheduler"
"sigs.k8s.io/kueue/pkg/constants"
"sigs.k8s.io/kueue/pkg/controller/failurerecovery"
"sigs.k8s.io/kueue/pkg/features"
"sigs.k8s.io/kueue/pkg/scheduler/preemption/fairsharing"
"sigs.k8s.io/kueue/pkg/util/waitforpodsready"
Expand Down Expand Up @@ -62,6 +63,20 @@ func SetupControllers(mgr ctrl.Manager, qManager *qcache.Manager, cc *schdcache.
watchers = append(watchers, cohortRec)
}

if features.Enabled(features.FailureRecoveryPolicy) {
tpRec, err := failurerecovery.NewTerminatingPodReconciler(
mgr.GetClient(),
mgr.GetEventRecorderFor(constants.PodTerminationControllerName),
)
if err != nil {
return "FailureRecoveryPolicy", err
}

if err := tpRec.SetupWithManager(mgr); err != nil {
return "FailureRecoveryPolicy", err
}
}

cqRec := NewClusterQueueReconciler(
mgr.GetClient(),
qManager,
Expand Down
199 changes: 199 additions & 0 deletions pkg/controller/failurerecovery/pod_termination_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
/*
Copyright 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 failurerecovery

import (
"context"
"fmt"
"time"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
"k8s.io/utils/clock"
"k8s.io/utils/ptr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/event"

"sigs.k8s.io/kueue/pkg/controller/constants"
utilpod "sigs.k8s.io/kueue/pkg/util/pod"
utiltaints "sigs.k8s.io/kueue/pkg/util/taints"
)

// +kubebuilder:rbac:groups="",resources=pods,verbs=get;list;watch
// +kubebuilder:rbac:groups="",resources=pods/status,verbs=get;patch
// +kubebuilder:rbac:groups="",resources=nodes,verbs=get;list;watch

var realClock = clock.RealClock{}

const (
KueueFailureRecoveryConditionType = "KueueFailureRecovery"
KueueForcefulTerminationReason = "KueueForcefullyTerminated"
)

type TerminatingPodReconciler struct {
client client.Client
clock clock.Clock
forcefulTerminationGracePeriod time.Duration
recorder record.EventRecorder
}

type TerminatingPodReconcilerOptions struct {
clock clock.Clock
forcefulTerminationGracePeriod time.Duration
}

type TerminatingPodReconcilerOption func(*TerminatingPodReconcilerOptions)

func WithClock(c clock.Clock) TerminatingPodReconcilerOption {
return func(o *TerminatingPodReconcilerOptions) {
o.clock = c
}
}

func WithForcefulTerminationGracePeriod(t time.Duration) TerminatingPodReconcilerOption {
return func(o *TerminatingPodReconcilerOptions) {
o.forcefulTerminationGracePeriod = t
}
}

var defaultOptions = TerminatingPodReconcilerOptions{
clock: realClock,
forcefulTerminationGracePeriod: time.Minute,
}

func NewTerminatingPodReconciler(
client client.Client,
recorder record.EventRecorder,
opts ...TerminatingPodReconcilerOption,
) (*TerminatingPodReconciler, error) {
options := defaultOptions
for _, opt := range opts {
opt(&options)
}

return &TerminatingPodReconciler{
client: client,
clock: options.clock,
forcefulTerminationGracePeriod: options.forcefulTerminationGracePeriod,
recorder: recorder,
}, nil
}

func (r *TerminatingPodReconciler) Generic(event.GenericEvent) bool {
return false
}

func (r *TerminatingPodReconciler) Create(e event.CreateEvent) bool {
pod := e.Object.(*corev1.Pod)

if !podOptedInToFailurePolicy(pod) {
return false
}

if pod.DeletionTimestamp.IsZero() {
return false
}

return true
}

func (r *TerminatingPodReconciler) Update(u event.UpdateEvent) bool {
oldPod := u.ObjectOld.(*corev1.Pod)
newPod := u.ObjectNew.(*corev1.Pod)

if !podOptedInToFailurePolicy(newPod) {
return false
}

// Pod was not marked for deletion in the update
if !oldPod.DeletionTimestamp.IsZero() || newPod.DeletionTimestamp.IsZero() {
return false
}

return true
}

func (r *TerminatingPodReconciler) Delete(event.DeleteEvent) bool {
return false
}

func podOptedInToFailurePolicy(p *corev1.Pod) bool {
annotationValue, hasAnnotation := p.Annotations[constants.SafeToForcefullyTerminateAnnotationKey]
return hasAnnotation && annotationValue == constants.SafeToForcefullyTerminateAnnotationValue
}

func (r *TerminatingPodReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
pod := &corev1.Pod{}
if err := r.client.Get(ctx, req.NamespacedName, pod); err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
}

// Pod was already terminated
if utilpod.IsTerminated(pod) {
return ctrl.Result{}, nil
}

// Forceful termination threshold not reached
now := r.clock.Now()
forcefulTerminationThreshold := pod.DeletionTimestamp.Add(r.forcefulTerminationGracePeriod)
if now.Before(forcefulTerminationThreshold) {
remainingTime := forcefulTerminationThreshold.Sub(now)
return ctrl.Result{RequeueAfter: remainingTime}, nil
}

node := &corev1.Node{}
nodeKey := types.NamespacedName{Name: pod.Spec.NodeName}
if err := r.client.Get(ctx, nodeKey, node); err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
}
// Pod is not scheduled on an unreachable node
if !utiltaints.TaintKeyExists(node.Spec.Taints, corev1.TaintNodeUnreachable) {
return ctrl.Result{}, nil
}

totalDeletionGracePeriod := time.Duration(ptr.Deref(pod.DeletionGracePeriodSeconds, 0)) + r.forcefulTerminationGracePeriod
eventMessage := fmt.Sprintf(
"Pod forcefully terminated after %s grace period due to unreachable node `%s` (triggered by `%s` annotation)",
totalDeletionGracePeriod,
node.Name,
constants.SafeToForcefullyTerminateAnnotationKey,
)

podPatch := pod.DeepCopy()
podPatch.Status.Phase = corev1.PodFailed
podPatch.Status.Conditions = append(podPatch.Status.Conditions, corev1.PodCondition{
Type: KueueFailureRecoveryConditionType,
Status: corev1.ConditionTrue,
Reason: KueueForcefulTerminationReason,
Message: eventMessage,
})
if err := r.client.Status().Patch(ctx, podPatch, client.MergeFrom(pod)); err != nil {
return ctrl.Result{}, err
}
r.recorder.Event(pod, corev1.EventTypeWarning, KueueForcefulTerminationReason, eventMessage)

return ctrl.Result{}, nil
}

func (r *TerminatingPodReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&corev1.Pod{}).
WithEventFilter(r).
Complete(r)
}
Loading