Skip to content

Commit 22d9a13

Browse files
authored
fix devbox phase generate. (#5120)
1 parent d3ba17b commit 22d9a13

File tree

6 files changed

+45
-36
lines changed

6 files changed

+45
-36
lines changed

controllers/devbox/.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,6 @@ go.work
2525
*.swp
2626
*.swo
2727
*~
28+
29+
# ignore deploy.yaml
30+
deploy/manifests/deploy.yaml

controllers/devbox/api/v1alpha1/devbox_types.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -163,16 +163,16 @@ const (
163163
DevboxPhasePending DevboxPhase = "Pending"
164164
//DevboxPhaseStopped means Devbox is stop and stopped success
165165
DevboxPhaseStopped DevboxPhase = "Stopped"
166-
//DevboxPhaseStopping means Devbox is stop and not stopped success
166+
//DevboxPhaseStopping means Devbox is stopping
167167
DevboxPhaseStopping DevboxPhase = "Stopping"
168168
//DevboxPhaseError means Devbox is error
169169
DevboxPhaseError DevboxPhase = "Error"
170+
//DevboxPhaseUnknown means Devbox is unknown
171+
DevboxPhaseUnknown DevboxPhase = "Unknown"
170172
)
171173

172174
// DevboxStatus defines the observed state of Devbox
173175
type DevboxStatus struct {
174-
// +kubebuilder:validation:Optional
175-
DevboxPodPhase corev1.PodPhase `json:"podPhase"`
176176
// +kubebuilder:validation:Optional
177177
Network NetworkStatus `json:"network"`
178178
// +kubebuilder:validation:Optional

controllers/devbox/config/crd/bases/devbox.sealos.io_devboxes.yaml

-4
Original file line numberDiff line numberDiff line change
@@ -2848,10 +2848,6 @@ spec:
28482848
type: object
28492849
phase:
28502850
type: string
2851-
podPhase:
2852-
description: PodPhase is a label for the condition of a pod at the
2853-
current time.
2854-
type: string
28552851
state:
28562852
description: |-
28572853
ContainerState holds a possible state of container.

controllers/devbox/deploy/manifests/deploy.yaml.tmpl

+1-5
Original file line numberDiff line numberDiff line change
@@ -2856,10 +2856,6 @@ spec:
28562856
type: object
28572857
phase:
28582858
type: string
2859-
podPhase:
2860-
description: PodPhase is a label for the condition of a pod at the
2861-
current time.
2862-
type: string
28632859
state:
28642860
description: |-
28652861
ContainerState holds a possible state of container.
@@ -5639,7 +5635,7 @@ metadata:
56395635
name: devbox-controller-manager
56405636
namespace: devbox-system
56415637
spec:
5642-
replicas: 1
5638+
replicas: 2
56435639
selector:
56445640
matchLabels:
56455641
control-plane: controller-manager

controllers/devbox/internal/controller/devbox_controller.go

+12-23
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,17 @@ func (r *DevboxReconciler) syncSecret(ctx context.Context, devbox *devboxv1alpha
202202

203203
func (r *DevboxReconciler) syncPod(ctx context.Context, devbox *devboxv1alpha1.Devbox, recLabels map[string]string) error {
204204
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+
205216
// update devbox status after pod is created or updated
206217
defer func() {
207218
if err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
@@ -214,6 +225,7 @@ func (r *DevboxReconciler) syncPod(ctx context.Context, devbox *devboxv1alpha1.D
214225
// update devbox status with latestDevbox status
215226
logger.Info("updating devbox status")
216227
logger.Info("merge commit history", "devbox", devbox.Status.CommitHistory, "latestDevbox", latestDevbox.Status.CommitHistory)
228+
devbox.Status.Phase = helper.GenerateDevboxPhase(devbox, podList)
217229
helper.UpdateDevboxStatus(devbox, latestDevbox)
218230
return r.Status().Update(ctx, latestDevbox)
219231
}); err != nil {
@@ -225,17 +237,6 @@ func (r *DevboxReconciler) syncPod(ctx context.Context, devbox *devboxv1alpha1.D
225237
r.Recorder.Eventf(devbox, corev1.EventTypeNormal, "Sync pod success", "Sync pod success")
226238
}()
227239

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-
239240
switch devbox.Spec.State {
240241
case devboxv1alpha1.DevboxStateRunning:
241242
runtimecr, err := r.getRuntime(ctx, devbox)
@@ -249,14 +250,11 @@ func (r *DevboxReconciler) syncPod(ctx context.Context, devbox *devboxv1alpha1.D
249250
case 0:
250251
logger.Info("create pod")
251252
logger.Info("next commit history", "commit", nextCommitHistory)
252-
devbox.Status.Phase = devboxv1alpha1.DevboxPhasePending
253253
return r.createPod(ctx, devbox, expectPod, nextCommitHistory)
254254
case 1:
255255
pod := &podList.Items[0]
256-
devbox.Status.DevboxPodPhase = pod.Status.Phase
257256
// check pod container size, if it is 0, it means the pod is not running, return an error
258257
if len(pod.Status.ContainerStatuses) == 0 {
259-
devbox.Status.Phase = devboxv1alpha1.DevboxPhasePending
260258
return fmt.Errorf("pod container size is 0")
261259
}
262260
devbox.Status.State = pod.Status.ContainerStatuses[0].State
@@ -275,32 +273,26 @@ func (r *DevboxReconciler) syncPod(ctx context.Context, devbox *devboxv1alpha1.D
275273
case corev1.PodPending, corev1.PodRunning:
276274
// pod is running or pending, do nothing here
277275
logger.Info("pod is running or pending")
278-
devbox.Status.Phase = devboxv1alpha1.DevboxPhaseRunning
279276
// update commit history status by pod status
280277
helper.UpdateCommitHistory(devbox, pod, false)
281278
return nil
282279
case corev1.PodFailed, corev1.PodSucceeded:
283280
// pod failed or succeeded, we need delete pod and remove finalizer
284-
devbox.Status.Phase = devboxv1alpha1.DevboxPhaseStopped
285281
logger.Info("pod failed or succeeded, recreate pod")
286282
return r.deletePod(ctx, devbox, pod)
287283
}
288284
case false:
289285
// pod not match expectations, delete pod anyway
290286
logger.Info("pod not match expectations, recreate pod")
291-
devbox.Status.Phase = devboxv1alpha1.DevboxPhasePending
292287
return r.deletePod(ctx, devbox, pod)
293288
}
294289
}
295290
case devboxv1alpha1.DevboxStateStopped:
296291
switch len(podList.Items) {
297292
case 0:
298-
// update devbox status to stopped, no pod found, do nothing
299-
devbox.Status.Phase = devboxv1alpha1.DevboxPhaseStopped
300293
return nil
301294
case 1:
302295
pod := &podList.Items[0]
303-
devbox.Status.DevboxPodPhase = pod.Status.Phase
304296
// update state to empty since devbox is stopped
305297
devbox.Status.State = corev1.ContainerState{}
306298
// 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
309301
if !pod.DeletionTimestamp.IsZero() {
310302
return r.handlePodDeleted(ctx, devbox, pod)
311303
}
312-
devbox.Status.Phase = devboxv1alpha1.DevboxPhaseStopped
313304
// we need delete pod because devbox state is stopped
314305
// we don't care about the pod status, just delete it
315306
return r.deletePod(ctx, devbox, pod)
@@ -422,7 +413,6 @@ func (r *DevboxReconciler) createPod(ctx context.Context, devbox *devboxv1alpha1
422413
nextCommitHistory.PredicatedStatus = devboxv1alpha1.CommitStatusPending
423414
if err := r.Create(ctx, expectPod); err != nil {
424415
logger.Error(err, "create pod failed")
425-
devbox.Status.Phase = devboxv1alpha1.DevboxPhaseError
426416
return err
427417
}
428418
devbox.Status.CommitHistory = append(devbox.Status.CommitHistory, nextCommitHistory)
@@ -439,7 +429,6 @@ func (r *DevboxReconciler) deletePod(ctx context.Context, devbox *devboxv1alpha1
439429
}
440430
if err := r.Delete(ctx, pod); err != nil {
441431
logger.Error(err, "delete pod failed")
442-
devbox.Status.Phase = devboxv1alpha1.DevboxPhaseError
443432
return err
444433
}
445434
// update commit history status because pod has been deleted

controllers/devbox/internal/controller/helper/devbox.go

+26-1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,32 @@ func GeneratePodAnnotations(devbox *devboxv1alpha1.Devbox, runtime *devboxv1alph
7676
return annotations
7777
}
7878

79+
func GenerateDevboxPhase(devbox *devboxv1alpha1.Devbox, podList corev1.PodList) devboxv1alpha1.DevboxPhase {
80+
if len(podList.Items) > 1 {
81+
return devboxv1alpha1.DevboxPhaseError
82+
}
83+
switch devbox.Spec.State {
84+
case devboxv1alpha1.DevboxStateRunning:
85+
if len(podList.Items) == 0 {
86+
return devboxv1alpha1.DevboxPhasePending
87+
}
88+
switch podList.Items[0].Status.Phase {
89+
case corev1.PodFailed, corev1.PodSucceeded:
90+
return devboxv1alpha1.DevboxPhaseStopped
91+
case corev1.PodPending:
92+
return devboxv1alpha1.DevboxPhasePending
93+
case corev1.PodRunning:
94+
return devboxv1alpha1.DevboxPhaseRunning
95+
}
96+
case devboxv1alpha1.DevboxStateStopped:
97+
if len(podList.Items) == 0 {
98+
return devboxv1alpha1.DevboxPhaseStopped
99+
}
100+
return devboxv1alpha1.DevboxPhaseStopping
101+
}
102+
return devboxv1alpha1.DevboxPhaseUnknown
103+
}
104+
79105
func MergeCommitHistory(devbox *devboxv1alpha1.Devbox, latestDevbox *devboxv1alpha1.Devbox) []*devboxv1alpha1.CommitHistory {
80106
res := make([]*devboxv1alpha1.CommitHistory, 0)
81107
historyMap := make(map[string]*devboxv1alpha1.CommitHistory)
@@ -127,7 +153,6 @@ func UpdatePredicatedCommitStatus(devbox *devboxv1alpha1.Devbox, pod *corev1.Pod
127153
// TODO: move this function to devbox types.go
128154
func UpdateDevboxStatus(current, latest *devboxv1alpha1.Devbox) {
129155
latest.Status.Phase = current.Status.Phase
130-
latest.Status.DevboxPodPhase = current.Status.DevboxPodPhase
131156
latest.Status.State = current.Status.State
132157
latest.Status.LastTerminationState = current.Status.LastTerminationState
133158
latest.Status.CommitHistory = MergeCommitHistory(current, latest)

0 commit comments

Comments
 (0)