@@ -202,6 +202,17 @@ func (r *DevboxReconciler) syncSecret(ctx context.Context, devbox *devboxv1alpha
202
202
203
203
func (r * DevboxReconciler ) syncPod (ctx context.Context , devbox * devboxv1alpha1.Devbox , recLabels map [string ]string ) error {
204
204
logger := log .FromContext (ctx )
205
+
206
+ var podList corev1.PodList
207
+ if err := r .List (ctx , & podList , client .InNamespace (devbox .Namespace ), client .MatchingLabels (recLabels )); err != nil {
208
+ return err
209
+ }
210
+ // only one pod is allowed, if more than one pod found, return error
211
+ if len (podList .Items ) > 1 {
212
+ return fmt .Errorf ("more than one pod found" )
213
+ }
214
+ logger .Info ("pod list" , "length" , len (podList .Items ))
215
+
205
216
// update devbox status after pod is created or updated
206
217
defer func () {
207
218
if err := retry .RetryOnConflict (retry .DefaultRetry , func () error {
@@ -214,6 +225,7 @@ func (r *DevboxReconciler) syncPod(ctx context.Context, devbox *devboxv1alpha1.D
214
225
// update devbox status with latestDevbox status
215
226
logger .Info ("updating devbox status" )
216
227
logger .Info ("merge commit history" , "devbox" , devbox .Status .CommitHistory , "latestDevbox" , latestDevbox .Status .CommitHistory )
228
+ devbox .Status .Phase = helper .GenerateDevboxPhase (devbox , podList )
217
229
helper .UpdateDevboxStatus (devbox , latestDevbox )
218
230
return r .Status ().Update (ctx , latestDevbox )
219
231
}); err != nil {
@@ -225,17 +237,6 @@ func (r *DevboxReconciler) syncPod(ctx context.Context, devbox *devboxv1alpha1.D
225
237
r .Recorder .Eventf (devbox , corev1 .EventTypeNormal , "Sync pod success" , "Sync pod success" )
226
238
}()
227
239
228
- var podList corev1.PodList
229
- if err := r .List (ctx , & podList , client .InNamespace (devbox .Namespace ), client .MatchingLabels (recLabels )); err != nil {
230
- return err
231
- }
232
- // only one pod is allowed, if more than one pod found, return error
233
- if len (podList .Items ) > 1 {
234
- devbox .Status .Phase = devboxv1alpha1 .DevboxPhaseError
235
- return fmt .Errorf ("more than one pod found" )
236
- }
237
- logger .Info ("pod list" , "length" , len (podList .Items ))
238
-
239
240
switch devbox .Spec .State {
240
241
case devboxv1alpha1 .DevboxStateRunning :
241
242
runtimecr , err := r .getRuntime (ctx , devbox )
@@ -249,14 +250,11 @@ func (r *DevboxReconciler) syncPod(ctx context.Context, devbox *devboxv1alpha1.D
249
250
case 0 :
250
251
logger .Info ("create pod" )
251
252
logger .Info ("next commit history" , "commit" , nextCommitHistory )
252
- devbox .Status .Phase = devboxv1alpha1 .DevboxPhasePending
253
253
return r .createPod (ctx , devbox , expectPod , nextCommitHistory )
254
254
case 1 :
255
255
pod := & podList .Items [0 ]
256
- devbox .Status .DevboxPodPhase = pod .Status .Phase
257
256
// check pod container size, if it is 0, it means the pod is not running, return an error
258
257
if len (pod .Status .ContainerStatuses ) == 0 {
259
- devbox .Status .Phase = devboxv1alpha1 .DevboxPhasePending
260
258
return fmt .Errorf ("pod container size is 0" )
261
259
}
262
260
devbox .Status .State = pod .Status .ContainerStatuses [0 ].State
@@ -275,32 +273,26 @@ func (r *DevboxReconciler) syncPod(ctx context.Context, devbox *devboxv1alpha1.D
275
273
case corev1 .PodPending , corev1 .PodRunning :
276
274
// pod is running or pending, do nothing here
277
275
logger .Info ("pod is running or pending" )
278
- devbox .Status .Phase = devboxv1alpha1 .DevboxPhaseRunning
279
276
// update commit history status by pod status
280
277
helper .UpdateCommitHistory (devbox , pod , false )
281
278
return nil
282
279
case corev1 .PodFailed , corev1 .PodSucceeded :
283
280
// pod failed or succeeded, we need delete pod and remove finalizer
284
- devbox .Status .Phase = devboxv1alpha1 .DevboxPhaseStopped
285
281
logger .Info ("pod failed or succeeded, recreate pod" )
286
282
return r .deletePod (ctx , devbox , pod )
287
283
}
288
284
case false :
289
285
// pod not match expectations, delete pod anyway
290
286
logger .Info ("pod not match expectations, recreate pod" )
291
- devbox .Status .Phase = devboxv1alpha1 .DevboxPhasePending
292
287
return r .deletePod (ctx , devbox , pod )
293
288
}
294
289
}
295
290
case devboxv1alpha1 .DevboxStateStopped :
296
291
switch len (podList .Items ) {
297
292
case 0 :
298
- // update devbox status to stopped, no pod found, do nothing
299
- devbox .Status .Phase = devboxv1alpha1 .DevboxPhaseStopped
300
293
return nil
301
294
case 1 :
302
295
pod := & podList .Items [0 ]
303
- devbox .Status .DevboxPodPhase = pod .Status .Phase
304
296
// update state to empty since devbox is stopped
305
297
devbox .Status .State = corev1.ContainerState {}
306
298
// update commit predicated status by pod status, this should be done once find a pod
@@ -309,7 +301,6 @@ func (r *DevboxReconciler) syncPod(ctx context.Context, devbox *devboxv1alpha1.D
309
301
if ! pod .DeletionTimestamp .IsZero () {
310
302
return r .handlePodDeleted (ctx , devbox , pod )
311
303
}
312
- devbox .Status .Phase = devboxv1alpha1 .DevboxPhaseStopped
313
304
// we need delete pod because devbox state is stopped
314
305
// we don't care about the pod status, just delete it
315
306
return r .deletePod (ctx , devbox , pod )
@@ -422,7 +413,6 @@ func (r *DevboxReconciler) createPod(ctx context.Context, devbox *devboxv1alpha1
422
413
nextCommitHistory .PredicatedStatus = devboxv1alpha1 .CommitStatusPending
423
414
if err := r .Create (ctx , expectPod ); err != nil {
424
415
logger .Error (err , "create pod failed" )
425
- devbox .Status .Phase = devboxv1alpha1 .DevboxPhaseError
426
416
return err
427
417
}
428
418
devbox .Status .CommitHistory = append (devbox .Status .CommitHistory , nextCommitHistory )
@@ -439,7 +429,6 @@ func (r *DevboxReconciler) deletePod(ctx context.Context, devbox *devboxv1alpha1
439
429
}
440
430
if err := r .Delete (ctx , pod ); err != nil {
441
431
logger .Error (err , "delete pod failed" )
442
- devbox .Status .Phase = devboxv1alpha1 .DevboxPhaseError
443
432
return err
444
433
}
445
434
// update commit history status because pod has been deleted
0 commit comments