Skip to content

Commit 4522141

Browse files
deads2kmichaelgugino
authored andcommitted
reduce complexity in pdb refactor
1 parent 9f80e7a commit 4522141

File tree

1 file changed

+47
-57
lines changed

1 file changed

+47
-57
lines changed

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

Lines changed: 47 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -111,75 +111,65 @@ func (r *EvictionREST) Create(ctx context.Context, name string, obj runtime.Obje
111111
return nil, errors.NewBadRequest("name in URL does not match name in Eviction object")
112112
}
113113

114-
deletionOptions, err := propagateDryRun(eviction, options)
114+
originalDeleteOptions, err := propagateDryRun(eviction, options)
115115
if err != nil {
116116
return nil, err
117117
}
118118

119-
obj, err = r.store.Get(ctx, eviction.Name, &metav1.GetOptions{})
120-
if err != nil {
121-
return nil, err
122-
}
123-
pod := obj.(*api.Pod)
124-
125119
if createValidation != nil {
126120
if err := createValidation(ctx, eviction.DeepCopyObject()); err != nil {
127121
return nil, err
128122
}
129123
}
130124

131-
// Evicting a terminal pod should result in direct deletion of pod as it already caused disruption by the time we are evicting.
132-
// There is no need to check for pdb.
133-
if canIgnorePDB(pod) {
134-
deleteRefresh := false
135-
continueToPDBs := false
136-
// Preserve current deletionOptions if we need to fall through to checking PDBs later.
137-
preservedDeletionOptions := deletionOptions.DeepCopy()
138-
err := retry.RetryOnConflict(EvictionsRetry, func() error {
139-
if deleteRefresh {
140-
// If we hit a conflict error, get the latest pod
141-
obj, err = r.store.Get(ctx, eviction.Name, &metav1.GetOptions{})
142-
if err != nil {
143-
return err
144-
}
145-
pod = obj.(*api.Pod)
146-
if !canIgnorePDB(pod) {
147-
// Pod is no longer in a state where we can skip checking
148-
// PDBs, continue to PDB checks.
149-
continueToPDBs = true
150-
// restore original deletion options because we may have
151-
// modified them.
152-
deletionOptions = preservedDeletionOptions
153-
return nil
154-
}
155-
}
156-
if shouldEnforceResourceVersion(pod) && resourceVersionIsUnset(preservedDeletionOptions) {
157-
// Set deletionOptions.Preconditions.ResourceVersion to ensure we're not
158-
// racing with another PDB-impacting process elsewhere.
159-
if deletionOptions.Preconditions == nil {
160-
deletionOptions.Preconditions = &metav1.Preconditions{}
161-
}
162-
deletionOptions.Preconditions.ResourceVersion = &pod.ResourceVersion
163-
} else {
164-
// restore original deletion options because we may have
165-
// modified them.
166-
deletionOptions = preservedDeletionOptions
167-
}
168-
_, _, err = r.store.Delete(ctx, eviction.Name, rest.ValidateAllObjectFunc, deletionOptions)
169-
if err != nil {
170-
deleteRefresh = true
171-
return err
172-
}
125+
var pod *api.Pod
126+
deletedPod := false
127+
err = retry.RetryOnConflict(EvictionsRetry, func() error {
128+
obj, err = r.store.Get(ctx, eviction.Name, &metav1.GetOptions{})
129+
if err != nil {
130+
return err
131+
}
132+
pod = obj.(*api.Pod)
133+
134+
// Evicting a terminal pod should result in direct deletion of pod as it already caused disruption by the time we are evicting.
135+
// There is no need to check for pdb.
136+
if !canIgnorePDB(pod) {
137+
// Pod is not in a state where we can skip checking PDBs, exit the loop, and continue to PDB checks.
173138
return nil
174-
})
175-
if !continueToPDBs {
176-
if err != nil {
177-
return nil, err
139+
}
140+
141+
// the PDB can be ignored, so delete the pod
142+
deletionOptions := originalDeleteOptions.DeepCopy()
143+
if shouldEnforceResourceVersion(pod) && resourceVersionIsUnset(originalDeleteOptions) {
144+
// Set deletionOptions.Preconditions.ResourceVersion to ensure we're not
145+
// racing with another PDB-impacting process elsewhere.
146+
if deletionOptions.Preconditions == nil {
147+
deletionOptions.Preconditions = &metav1.Preconditions{}
178148
}
179-
return &metav1.Status{
180-
Status: metav1.StatusSuccess}, nil
149+
deletionOptions.Preconditions.ResourceVersion = &pod.ResourceVersion
150+
}
151+
_, _, err = r.store.Delete(ctx, eviction.Name, rest.ValidateAllObjectFunc, deletionOptions)
152+
if err != nil {
153+
return err
181154
}
155+
deletedPod = true
156+
return nil
157+
})
158+
switch {
159+
case err != nil:
160+
// this can happen in cases where the PDB can be ignored, but there was a problem issuing the pod delete:
161+
// maybe we conflicted two many times or we didn't have permission or something else weird.
162+
return nil, err
163+
164+
case deletedPod:
165+
// this happens when we successfully deleted the pod. In this case, we're done executing because we've evicted/deleted the pod
166+
return &metav1.Status{Status: metav1.StatusSuccess}, nil
167+
168+
default:
169+
// this happens when we didn't have an error and we didn't delete the pod. The only branch that happens on is when
170+
// we cannot ignored the PDB for this pod, so this is the fall through case.
182171
}
172+
183173
var rtStatus *metav1.Status
184174
var pdbName string
185175
err = func() error {
@@ -214,7 +204,7 @@ func (r *EvictionREST) Create(ctx context.Context, name string, obj runtime.Obje
214204

215205
// If it was false already, or if it becomes false during the course of our retries,
216206
// raise an error marked as a 429.
217-
if err = r.checkAndDecrement(pod.Namespace, pod.Name, *pdb, dryrun.IsDryRun(deletionOptions.DryRun)); err != nil {
207+
if err = r.checkAndDecrement(pod.Namespace, pod.Name, *pdb, dryrun.IsDryRun(originalDeleteOptions.DryRun)); err != nil {
218208
refresh = true
219209
return err
220210
}
@@ -236,7 +226,7 @@ func (r *EvictionREST) Create(ctx context.Context, name string, obj runtime.Obje
236226
// At this point there was either no PDB or we succeeded in decrementing
237227

238228
// Try the delete
239-
_, _, err = r.store.Delete(ctx, eviction.Name, rest.ValidateAllObjectFunc, deletionOptions)
229+
_, _, err = r.store.Delete(ctx, eviction.Name, rest.ValidateAllObjectFunc, originalDeleteOptions.DeepCopy())
240230
if err != nil {
241231
return nil, err
242232
}

0 commit comments

Comments
 (0)