@@ -235,22 +235,37 @@ func (m *Manager) StartWorkspace(ctx context.Context, req *api.StartWorkspaceReq
235
235
retryErr = err
236
236
237
237
var tempPod corev1.Pod
238
- getErr := m .Clientset .Get (ctx , types.NamespacedName {Namespace : pod .Namespace , Name : pod .Name }, & tempPod )
239
- if getErr != nil {
240
- clog .WithError (getErr ).WithField ("pod.Namespace" , pod .Namespace ).WithField ("pod.Name" , pod .Name ).Error ("was unable to get pod" )
241
- // pod doesn't exist, so we are safe to proceed with retry
242
- return false , nil
238
+ finalizerRemoved := false
239
+ // We do below in loop as sometimes the pod might be already deleted or modified when we attempt to remove the finalizer
240
+ // so we first get the pod and then attempt to remove the finalizer
241
+ // in case the pod is not found in the first place, we return success
242
+ for i := 0 ; i < 3 && ! finalizerRemoved ; i ++ {
243
+ getErr := m .Clientset .Get (ctx , types.NamespacedName {Namespace : pod .Namespace , Name : pod .Name }, & tempPod )
244
+ if getErr != nil {
245
+ if ! k8serr .IsNotFound (getErr ) {
246
+ // pod doesn't exist, so we are safe to proceed with retry
247
+ return true , nil
248
+ }
249
+ clog .WithError (getErr ).WithField ("pod.Namespace" , pod .Namespace ).WithField ("pod.Name" , pod .Name ).Error ("was unable to get pod" )
250
+ // pod get call failed so we error out
251
+ return false , retryErr
252
+ }
253
+ // we successfully got the pod, now we attempt to remove finalizer
254
+ tempPod .Finalizers = []string {}
255
+ updateErr := m .Clientset .Update (ctx , & tempPod )
256
+ if updateErr != nil {
257
+ clog .WithError (updateErr ).WithField ("pod.Namespace" , pod .Namespace ).WithField ("pod.Name" , pod .Name ).Error ("was unable to remove finalizer" )
258
+ continue
259
+ }
260
+ finalizerRemoved = true
243
261
}
244
- tempPod .Finalizers = []string {}
245
- updateErr := m .Clientset .Update (ctx , & tempPod )
246
- if updateErr != nil {
247
- clog .WithError (updateErr ).WithField ("pod.Namespace" , pod .Namespace ).WithField ("pod.Name" , pod .Name ).Error ("was unable to remove finalizer" )
248
- // failed to remove finalizer, we not going to be able to create a new pod, so bail out with retry error
262
+ // if even after retries we could not remove finalizers, then error out
263
+ if ! finalizerRemoved {
249
264
return false , retryErr
250
265
}
251
266
252
267
deleteErr := m .Clientset .Delete (ctx , & tempPod )
253
- if deleteErr != nil {
268
+ if deleteErr != nil && ! k8serr . IsNotFound ( deleteErr ) {
254
269
clog .WithError (deleteErr ).WithField ("pod.Namespace" , pod .Namespace ).WithField ("pod.Name" , pod .Name ).Error ("was unable to delete pod" )
255
270
// failed to delete pod, so not going to be able to create a new pod, so bail out
256
271
return false , retryErr
0 commit comments