Skip to content

Commit

Permalink
Merge pull request kubernetes#91342 from mgugino-upstream-stage/evict…
Browse files Browse the repository at this point in the history
…-deleted-ok

Eviction: ignore PDBs if pods with DeletionTimestamp
  • Loading branch information
k8s-ci-robot authored Jul 9, 2020
2 parents 82baa26 + dd49915 commit dd649bb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
5 changes: 3 additions & 2 deletions pkg/registry/core/pod/storage/eviction.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,15 +247,16 @@ func (r *EvictionREST) Create(ctx context.Context, name string, obj runtime.Obje
// canIgnorePDB returns true for pod conditions that allow the pod to be deleted
// without checking PDBs.
func canIgnorePDB(pod *api.Pod) bool {
if pod.Status.Phase == api.PodSucceeded || pod.Status.Phase == api.PodFailed || pod.Status.Phase == api.PodPending {
if pod.Status.Phase == api.PodSucceeded || pod.Status.Phase == api.PodFailed ||
pod.Status.Phase == api.PodPending || !pod.ObjectMeta.DeletionTimestamp.IsZero() {
return true
}
return false
}

func shouldEnforceResourceVersion(pod *api.Pod) bool {
// We don't need to enforce ResourceVersion for terminal pods
if pod.Status.Phase == api.PodSucceeded || pod.Status.Phase == api.PodFailed {
if pod.Status.Phase == api.PodSucceeded || pod.Status.Phase == api.PodFailed || !pod.ObjectMeta.DeletionTimestamp.IsZero() {
return false
}
// Return true for all other pods to ensure we don't race against a pod becoming
Expand Down
23 changes: 23 additions & 0 deletions pkg/registry/core/pod/storage/eviction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ func TestEvictionIngorePDB(t *testing.T) {
podPhase api.PodPhase
podName string
expectedDeleteCount int
podTerminating bool
}{
{
name: "pdbs No disruptions allowed, pod pending, first delete conflict, pod still pending, pod deleted successfully",
Expand Down Expand Up @@ -287,6 +288,19 @@ func TestEvictionIngorePDB(t *testing.T) {
podName: "t5",
expectedDeleteCount: 1,
},
{
name: "matching pdbs with no disruptions allowed, pod terminating",
pdbs: []runtime.Object{&policyv1beta1.PodDisruptionBudget{
ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "default"},
Spec: policyv1beta1.PodDisruptionBudgetSpec{Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"a": "true"}}},
Status: policyv1beta1.PodDisruptionBudgetStatus{DisruptionsAllowed: 0},
}},
eviction: &policy.Eviction{ObjectMeta: metav1.ObjectMeta{Name: "t6", Namespace: "default"}, DeleteOptions: metav1.NewDeleteOptions(300)},
expectError: false,
podName: "t6",
expectedDeleteCount: 1,
podTerminating: true,
},
}

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

if tc.podTerminating {
currentTime := metav1.Now()
pod.ObjectMeta.DeletionTimestamp = &currentTime
}

client := fake.NewSimpleClientset(tc.pdbs...)
evictionRest := newEvictionStorage(ms, client.PolicyV1beta1())

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

0 comments on commit dd649bb

Please sign in to comment.