Skip to content

Commit dd649bb

Browse files
authored
Merge pull request kubernetes#91342 from mgugino-upstream-stage/evict-deleted-ok
Eviction: ignore PDBs if pods with DeletionTimestamp
2 parents 82baa26 + dd49915 commit dd649bb

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

pkg/registry/core/pod/storage/eviction.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,15 +247,16 @@ func (r *EvictionREST) Create(ctx context.Context, name string, obj runtime.Obje
247247
// canIgnorePDB returns true for pod conditions that allow the pod to be deleted
248248
// without checking PDBs.
249249
func canIgnorePDB(pod *api.Pod) bool {
250-
if pod.Status.Phase == api.PodSucceeded || pod.Status.Phase == api.PodFailed || pod.Status.Phase == api.PodPending {
250+
if pod.Status.Phase == api.PodSucceeded || pod.Status.Phase == api.PodFailed ||
251+
pod.Status.Phase == api.PodPending || !pod.ObjectMeta.DeletionTimestamp.IsZero() {
251252
return true
252253
}
253254
return false
254255
}
255256

256257
func shouldEnforceResourceVersion(pod *api.Pod) bool {
257258
// We don't need to enforce ResourceVersion for terminal pods
258-
if pod.Status.Phase == api.PodSucceeded || pod.Status.Phase == api.PodFailed {
259+
if pod.Status.Phase == api.PodSucceeded || pod.Status.Phase == api.PodFailed || !pod.ObjectMeta.DeletionTimestamp.IsZero() {
259260
return false
260261
}
261262
// Return true for all other pods to ensure we don't race against a pod becoming

pkg/registry/core/pod/storage/eviction_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ func TestEvictionIngorePDB(t *testing.T) {
218218
podPhase api.PodPhase
219219
podName string
220220
expectedDeleteCount int
221+
podTerminating bool
221222
}{
222223
{
223224
name: "pdbs No disruptions allowed, pod pending, first delete conflict, pod still pending, pod deleted successfully",
@@ -287,6 +288,19 @@ func TestEvictionIngorePDB(t *testing.T) {
287288
podName: "t5",
288289
expectedDeleteCount: 1,
289290
},
291+
{
292+
name: "matching pdbs with no disruptions allowed, pod terminating",
293+
pdbs: []runtime.Object{&policyv1beta1.PodDisruptionBudget{
294+
ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "default"},
295+
Spec: policyv1beta1.PodDisruptionBudgetSpec{Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"a": "true"}}},
296+
Status: policyv1beta1.PodDisruptionBudgetStatus{DisruptionsAllowed: 0},
297+
}},
298+
eviction: &policy.Eviction{ObjectMeta: metav1.ObjectMeta{Name: "t6", Namespace: "default"}, DeleteOptions: metav1.NewDeleteOptions(300)},
299+
expectError: false,
300+
podName: "t6",
301+
expectedDeleteCount: 1,
302+
podTerminating: true,
303+
},
290304
}
291305

292306
for _, tc := range testcases {
@@ -304,6 +318,11 @@ func TestEvictionIngorePDB(t *testing.T) {
304318
pod.Status.Phase = tc.podPhase
305319
}
306320

321+
if tc.podTerminating {
322+
currentTime := metav1.Now()
323+
pod.ObjectMeta.DeletionTimestamp = &currentTime
324+
}
325+
307326
client := fake.NewSimpleClientset(tc.pdbs...)
308327
evictionRest := newEvictionStorage(ms, client.PolicyV1beta1())
309328

@@ -397,6 +416,10 @@ func (ms *mockStore) mutatorDeleteFunc(count int, options *metav1.DeleteOptions)
397416
// Always return error for this pod
398417
return nil, false, apierrors.NewConflict(resource("tests"), "2", errors.New("message"))
399418
}
419+
if ms.pod.Name == "t6" {
420+
// This pod has a deletionTimestamp and should not raise conflict on delete
421+
return nil, true, nil
422+
}
400423
if count == 1 {
401424
// This is a hack to ensure that some test pods don't change phase
402425
// but do change resource version

0 commit comments

Comments
 (0)