Skip to content
Merged
Show file tree
Hide file tree
Changes from 21 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
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"
)
12 changes: 12 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,17 @@ 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())
if err != nil {
return "FailureRecoveryPolicy", err
}

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

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

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

"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{}
)

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

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,
opts ...TerminatingPodReconcilerOption,
) (*TerminatingPodReconciler, error) {
options := defaultOptions
for _, opt := range opts {
opt(&options)
}

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

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 did not opt-in to be forcefully terminated
annotationValue, hasAnnotation := pod.Annotations[constants.SafeToForcefullyTerminateAnnotationKey]
if !hasAnnotation || annotationValue != constants.SafeToForcefullyTerminateAnnotationValue {
return ctrl.Result{}, nil
}

// Pod was not marked for termination
if pod.DeletionTimestamp.IsZero() {
return ctrl.Result{}, nil
}

// Pod is not in a running phase
if utilpod.IsTerminated(pod) {
return ctrl.Result{}, nil
}

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

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

podPatch := pod.DeepCopy()
podPatch.Status.Phase = corev1.PodFailed
if err := r.client.Status().Patch(ctx, podPatch, client.MergeFrom(pod)); err != nil {
Copy link
Contributor

@mimowo mimowo Nov 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This patch may potentially override some conditions or status changes done concurrently by another controller. To avoid that use our helper in clientutil which supports "strict" mode which compares the ResrouceVersion.

return ctrl.Result{}, err
}

return ctrl.Result{}, nil
}

func (r *TerminatingPodReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&corev1.Pod{}).
Complete(r)
}
177 changes: 177 additions & 0 deletions pkg/controller/failurerecovery/pod_termination_controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
/*
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"
"testing"
"time"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
testingclock "k8s.io/utils/clock/testing"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

"sigs.k8s.io/kueue/pkg/controller/constants"
utiltesting "sigs.k8s.io/kueue/pkg/util/testing"
testingnode "sigs.k8s.io/kueue/pkg/util/testingjobs/node"
testingpod "sigs.k8s.io/kueue/pkg/util/testingjobs/pod"
)

var (
podCmpOpts = cmp.Options{
cmpopts.EquateEmpty(),
cmpopts.IgnoreFields(
corev1.Pod{}, "ObjectMeta.ResourceVersion", "ObjectMeta.DeletionTimestamp",
),
}
)

func TestReconciler(t *testing.T) {
now := time.Now()
nowSecondPrecision := metav1.NewTime(now).Rfc3339Copy()
beforeGracePeriod := now.Add(-time.Minute * 3)
fakeClock := testingclock.NewFakeClock(now)

unreachableNode := testingnode.MakeNode("unreachable-node").
Taints(corev1.Taint{Key: corev1.TaintNodeUnreachable}).Obj()
healthyNode := testingnode.MakeNode("healthy-node").Obj()
podToForcefullyTerminate := testingpod.MakePod("pod", "").
StatusPhase(corev1.PodRunning).
Annotation(constants.SafeToForcefullyTerminateAnnotationKey, constants.SafeToForcefullyTerminateAnnotationValue).
NodeName(unreachableNode.Name).
DeletionTimestamp(beforeGracePeriod).
KueueFinalizer()

cases := map[string]struct {
testPod *corev1.Pod
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's split this into pod and wantPod. Always supplied to avoid tricks in https://github.com/kubernetes-sigs/kueue/pull/7312/files#diff-547b122e7c9dbc55a906f70d85ca16010acd456bb20e02158debbd9cd1b23a46R168-R171

Yes, this is more lines of test code, but very declarative in nature

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wantPod was already in the struct, it was just optional because most cases expected wantPod == testPod. I made it explicit in 606dcc6.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

wantResult ctrl.Result
wantErr error
wantPod *corev1.Pod
}{
"pod is not found": {
testPod: testingpod.MakePod("pod2", "").Obj(),
wantResult: ctrl.Result{},
wantErr: nil,
},
"pod did not opt-in with annotation": {
testPod: podToForcefullyTerminate.
Clone().
Annotation(constants.SafeToForcefullyTerminateAnnotationKey, "false").
Obj(),
wantResult: ctrl.Result{},
wantErr: nil,
},
"pod is not marked for termination": {
testPod: podToForcefullyTerminate.
Clone().
DeletionTimestamp(time.Time{}).
Obj(),
wantResult: ctrl.Result{},
wantErr: nil,
},
"pod is in failed phase": {
testPod: podToForcefullyTerminate.
Clone().
StatusPhase(corev1.PodFailed).
Obj(),
wantResult: ctrl.Result{},
wantErr: nil,
},
"pod is in succeeded phase": {
testPod: podToForcefullyTerminate.
Clone().
StatusPhase(corev1.PodSucceeded).
Obj(),
wantResult: ctrl.Result{},
wantErr: nil,
},
"pod is not scheduled on an unreachable node": {
testPod: podToForcefullyTerminate.
Clone().
NodeName(healthyNode.Name).
Obj(),
wantResult: ctrl.Result{},
wantErr: nil,
},
"forceful termination grace period did not elapse for pod": {
testPod: podToForcefullyTerminate.
Clone().
DeletionTimestamp(now).
Obj(),
wantResult: ctrl.Result{RequeueAfter: nowSecondPrecision.Add(time.Minute).Sub(now)},
wantErr: nil,
},
"forceful termination grace period elapsed for pod": {
testPod: podToForcefullyTerminate.Clone().Obj(),
wantResult: ctrl.Result{},
wantErr: nil,
wantPod: podToForcefullyTerminate.Clone().StatusPhase(corev1.PodFailed).Obj(),
},
"pod is scheduled on a node that does not exist": {
testPod: podToForcefullyTerminate.Clone().NodeName("missing-node").Obj(),
wantResult: ctrl.Result{},
wantErr: apierrors.NewNotFound(schema.GroupResource{Group: corev1.GroupName, Resource: "nodes"}, "missing-node"),
},
}

for name, tc := range cases {
t.Run(name, func(t *testing.T) {
objs := []client.Object{tc.testPod, healthyNode, unreachableNode}
clientBuilder := utiltesting.NewClientBuilder().WithObjects(objs...)
cl := clientBuilder.Build()
reconciler, err := NewTerminatingPodReconciler(cl, WithClock(fakeClock))
if err != nil {
t.Fatalf("could not create reconciler: %v", err)
}

ctxWithLogger, _ := utiltesting.ContextWithLog(t)
ctx, ctxCancel := context.WithCancel(ctxWithLogger)
defer ctxCancel()

gotResult, gotError := reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: client.ObjectKeyFromObject(tc.testPod)})

if diff := cmp.Diff(tc.wantResult, gotResult); diff != "" {
fmt.Println(tc.testPod.DeletionTimestamp.Add(time.Minute).Sub(now))
t.Errorf("unexpected reconcile result (-want/+got):\n%s", diff)
}

if diff := cmp.Diff(tc.wantErr, gotError); diff != "" {
t.Errorf("unexpected reconcile error (-want/+got):\n%s", diff)
}

gotPod := podToForcefullyTerminate.Clone().Obj()
if err := cl.Get(ctx, client.ObjectKeyFromObject(tc.testPod), gotPod); err != nil {
t.Fatalf("could not get pod after reconcile")
}
wantPod := tc.wantPod
if wantPod == nil {
wantPod = tc.testPod
}
if diff := cmp.Diff(wantPod, gotPod, podCmpOpts...); diff != "" {
t.Errorf("Workloads after reconcile (-want,+got):\n%s", diff)
}
})
}
}
10 changes: 9 additions & 1 deletion pkg/features/kube_features.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,12 @@ const (
//
// Enables ClusterProfile integration for MultiKueue.
MultiKueueClusterProfile featuregate.Feature = "MultiKueueClusterProfile"

// owner: @kshalot
//
// issue: https://github.com/kubernetes-sigs/kueue/issues/6757
// Enabled failure recovery of pods stuck in terminating state.
FailureRecoveryPolicy featuregate.Feature = "FailureRecoveryPolicy"
)

func init() {
Expand Down Expand Up @@ -346,10 +352,12 @@ var defaultVersionedFeatureGates = map[featuregate.Feature]featuregate.Versioned
PropagateBatchJobLabelsToWorkload: {
{Version: version.MustParse("0.15"), Default: true, PreRelease: featuregate.Beta},
},

MultiKueueClusterProfile: {
{Version: version.MustParse("0.15"), Default: false, PreRelease: featuregate.Alpha},
},
FailureRecoveryPolicy: {
{Version: version.MustParse("0.15"), Default: false, PreRelease: featuregate.Alpha},
},
}

func SetFeatureGateDuringTest(tb testing.TB, f featuregate.Feature, value bool) {
Expand Down
Loading