From 3b0edea23831c81f3fb4c4c90c0d69d2abcec6a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=8A=92=E6=83=85=E7=86=8A?= <2669184984@qq.com> Date: Thu, 5 Sep 2024 11:21:23 +0800 Subject: [PATCH 01/11] fix ssh volume bug --- controllers/devbox/internal/controller/helper/devbox.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/controllers/devbox/internal/controller/helper/devbox.go b/controllers/devbox/internal/controller/helper/devbox.go index bf43eff0a1d..02c9bd22264 100644 --- a/controllers/devbox/internal/controller/helper/devbox.go +++ b/controllers/devbox/internal/controller/helper/devbox.go @@ -236,11 +236,11 @@ func GenerateSSHVolume(devbox *devboxv1alpha1.Devbox) corev1.Volume { Items: []corev1.KeyToPath{ { Key: "SEALOS_DEVBOX_PRIVATE_KEY", - Path: "id_rsa", + Path: "id", }, { Key: "SEALOS_DEVBOX_PUBLIC_KEY", - Path: "id_rsa.pub", + Path: "id.pub", }, }, }, From 7481545823cf86910026de95fe35a2876102507a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=8A=92=E6=83=85=E7=86=8A?= <2669184984@qq.com> Date: Thu, 5 Sep 2024 14:27:40 +0800 Subject: [PATCH 02/11] add devbox phase and controller --- .../devbox/api/v1alpha1/devbox_types.go | 21 ++++++++ .../internal/controller/devbox_controller.go | 50 +++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/controllers/devbox/api/v1alpha1/devbox_types.go b/controllers/devbox/api/v1alpha1/devbox_types.go index 062de496a3e..e3fb8302afa 100644 --- a/controllers/devbox/api/v1alpha1/devbox_types.go +++ b/controllers/devbox/api/v1alpha1/devbox_types.go @@ -141,6 +141,25 @@ type CommitHistory struct { Status CommitStatus `json:"status"` } +type DevboxPhase string + +const ( + // DevboxPhaseRunning means Devbox is run and run success + DevboxPhaseRunning DevboxPhase = "Running" + // DevboxPhasePending means Devbox is run but not run success + DevboxPhasePending DevboxPhase = "Pending" + //DevboxPhaseStopped means Devbox is stop and stopped success + DevboxPhaseStopped DevboxPhase = "Stopped" + //DevboxPhaseStopping means Devbox is stop and not stopped success + DevboxPhaseStopping DevboxPhase = "Stopping" + //DevboxPhaseError means Devbox is error + DevboxPhaseError DevboxPhase = "Error" + //DevboxPhaseDelete DevboxPhase = "Delete" + DevboxPhaseDelete DevboxPhase = "Delete" + //DevboxPhaseUnknow means Devbox not know the status + DevboxPhaseUnknown DevboxPhase = "Unknown" +) + // DevboxStatus defines the observed state of Devbox type DevboxStatus struct { // +kubebuilder:validation:Optional @@ -149,6 +168,8 @@ type DevboxStatus struct { Network NetworkStatus `json:"network"` // +kubebuilder:validation:Optional CommitHistory []*CommitHistory `json:"commitHistory"` + // +kubebuilder:validation:Optional + Phase DevboxPhase `json:"phase"` } // +kubebuilder:object:root=true diff --git a/controllers/devbox/internal/controller/devbox_controller.go b/controllers/devbox/internal/controller/devbox_controller.go index c79f2fa7cc6..f575684e1ef 100644 --- a/controllers/devbox/internal/controller/devbox_controller.go +++ b/controllers/devbox/internal/controller/devbox_controller.go @@ -227,6 +227,13 @@ func (r *DevboxReconciler) syncPod(ctx context.Context, devbox *devboxv1alpha1.D switch devbox.Spec.State { case devboxv1alpha1.DevboxStateRunning: + // change devbox status + devbox.Status.Phase = devboxv1alpha1.DevboxPhasePending + err = r.Status().Update(ctx, devbox) + if err != nil { + logger.Error(err, "update devbox phase failed") + return err + } // check pod status, if no pod found, create a new one, with finalizer and controller reference if len(podList.Items) == 0 { if err := r.Create(ctx, expectPod); err != nil { @@ -235,6 +242,7 @@ func (r *DevboxReconciler) syncPod(ctx context.Context, devbox *devboxv1alpha1.D } // add next commit history to status devbox.Status.CommitHistory = append(devbox.Status.CommitHistory, nextCommitHistory) + devbox.Status.Phase = devboxv1alpha1.DevboxPhaseRunning return r.Status().Update(ctx, devbox) } // else if pod found, check pod status @@ -267,6 +275,13 @@ func (r *DevboxReconciler) syncPod(ctx context.Context, devbox *devboxv1alpha1.D logger.Info("pod", "pod", podList.Items[0].Name, "pod spec", podList.Items[0].Spec) logger.Info("expect pod", "pod", expectPod.Name, "pod spec", expectPod.Spec) _ = r.Delete(ctx, &podList.Items[0]) + // change devbox status + devbox.Status.Phase = devboxv1alpha1.DevboxPhaseDelete + err = r.Status().Update(ctx, devbox) + if err != nil { + logger.Error(err, "update devbox phase failed") + return err + } } case corev1.PodRunning: //if pod is running,check pod need restart @@ -275,6 +290,15 @@ func (r *DevboxReconciler) syncPod(ctx context.Context, devbox *devboxv1alpha1.D logger.Info("pod", "pod", podList.Items[0].Name, "pod spec", podList.Items[0].Spec) logger.Info("expect pod", "pod", expectPod.Name, "pod spec", expectPod.Spec) _ = r.Delete(ctx, &podList.Items[0]) + // change devbox status + devbox.Status.Phase = devboxv1alpha1.DevboxPhaseDelete + } else { + devbox.Status.Phase = devboxv1alpha1.DevboxPhaseRunning + } + err = r.Status().Update(ctx, devbox) + if err != nil { + logger.Error(err, "update devbox phase failed") + return err } return r.updateDevboxCommitHistory(ctx, devbox, &podList.Items[0]) case corev1.PodSucceeded: @@ -285,17 +309,37 @@ func (r *DevboxReconciler) syncPod(ctx context.Context, devbox *devboxv1alpha1.D } } _ = r.Delete(ctx, &podList.Items[0]) + // update commit history status to success by pod name return r.updateDevboxCommitHistory(ctx, devbox, &podList.Items[0]) case corev1.PodFailed: // we can't find the reason of failure, we assume the commit status is failed // todo maybe use pod condition to get the reason of failure and update commit history status to failed by pod name + devbox.Status.Phase = devboxv1alpha1.DevboxPhaseError + err = r.Status().Update(ctx, devbox) + if err != nil { + logger.Error(err, "update devbox phase failed") + return err + } return r.updateDevboxCommitHistory(ctx, devbox, &podList.Items[0]) } } case devboxv1alpha1.DevboxStateStopped: + // change Devbox status + devbox.Status.Phase = devboxv1alpha1.DevboxPhaseStopping + err = r.Status().Update(ctx, devbox) + if err != nil { + logger.Error(err, "update devbox phase failed") + return err + } // check pod status, if no pod found, do nothing if len(podList.Items) == 0 { + devbox.Status.Phase = devboxv1alpha1.DevboxPhaseStopped + err = r.Status().Update(ctx, devbox) + if err != nil { + logger.Error(err, "update devbox phase failed") + return err + } return nil } // if pod found, remove finalizer and delete pod @@ -308,6 +352,12 @@ func (r *DevboxReconciler) syncPod(ctx context.Context, devbox *devboxv1alpha1.D } } _ = r.Delete(ctx, &podList.Items[0]) + devbox.Status.Phase = devboxv1alpha1.DevboxPhaseStopped + err = r.Status().Update(ctx, devbox) + if err != nil { + logger.Error(err, "update devbox phase failed") + return err + } return r.updateDevboxCommitHistory(ctx, devbox, &podList.Items[0]) } } From 54512da6f9056ea90a86e31d98d1a38f8c015aa8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=8A=92=E6=83=85=E7=86=8A?= <2669184984@qq.com> Date: Thu, 5 Sep 2024 15:03:27 +0800 Subject: [PATCH 03/11] add devbox phase and controller --- .../internal/controller/devbox_controller.go | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/controllers/devbox/internal/controller/devbox_controller.go b/controllers/devbox/internal/controller/devbox_controller.go index f575684e1ef..efc5a731b9d 100644 --- a/controllers/devbox/internal/controller/devbox_controller.go +++ b/controllers/devbox/internal/controller/devbox_controller.go @@ -227,13 +227,6 @@ func (r *DevboxReconciler) syncPod(ctx context.Context, devbox *devboxv1alpha1.D switch devbox.Spec.State { case devboxv1alpha1.DevboxStateRunning: - // change devbox status - devbox.Status.Phase = devboxv1alpha1.DevboxPhasePending - err = r.Status().Update(ctx, devbox) - if err != nil { - logger.Error(err, "update devbox phase failed") - return err - } // check pod status, if no pod found, create a new one, with finalizer and controller reference if len(podList.Items) == 0 { if err := r.Create(ctx, expectPod); err != nil { @@ -325,13 +318,6 @@ func (r *DevboxReconciler) syncPod(ctx context.Context, devbox *devboxv1alpha1.D } } case devboxv1alpha1.DevboxStateStopped: - // change Devbox status - devbox.Status.Phase = devboxv1alpha1.DevboxPhaseStopping - err = r.Status().Update(ctx, devbox) - if err != nil { - logger.Error(err, "update devbox phase failed") - return err - } // check pod status, if no pod found, do nothing if len(podList.Items) == 0 { devbox.Status.Phase = devboxv1alpha1.DevboxPhaseStopped From 5365e9e850fca1c77abf5fce3fbd23c2ce3c845d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=8A=92=E6=83=85=E7=86=8A?= <2669184984@qq.com> Date: Thu, 5 Sep 2024 15:17:02 +0800 Subject: [PATCH 04/11] add devbox phase and controller --- .../devbox/api/v1alpha1/devbox_types.go | 2 - .../internal/controller/devbox_controller.go | 51 +++++++++---------- 2 files changed, 25 insertions(+), 28 deletions(-) diff --git a/controllers/devbox/api/v1alpha1/devbox_types.go b/controllers/devbox/api/v1alpha1/devbox_types.go index e3fb8302afa..2d07fcd080f 100644 --- a/controllers/devbox/api/v1alpha1/devbox_types.go +++ b/controllers/devbox/api/v1alpha1/devbox_types.go @@ -154,8 +154,6 @@ const ( DevboxPhaseStopping DevboxPhase = "Stopping" //DevboxPhaseError means Devbox is error DevboxPhaseError DevboxPhase = "Error" - //DevboxPhaseDelete DevboxPhase = "Delete" - DevboxPhaseDelete DevboxPhase = "Delete" //DevboxPhaseUnknow means Devbox not know the status DevboxPhaseUnknown DevboxPhase = "Unknown" ) diff --git a/controllers/devbox/internal/controller/devbox_controller.go b/controllers/devbox/internal/controller/devbox_controller.go index efc5a731b9d..9c31a6bc6a5 100644 --- a/controllers/devbox/internal/controller/devbox_controller.go +++ b/controllers/devbox/internal/controller/devbox_controller.go @@ -235,7 +235,6 @@ func (r *DevboxReconciler) syncPod(ctx context.Context, devbox *devboxv1alpha1.D } // add next commit history to status devbox.Status.CommitHistory = append(devbox.Status.CommitHistory, nextCommitHistory) - devbox.Status.Phase = devboxv1alpha1.DevboxPhaseRunning return r.Status().Update(ctx, devbox) } // else if pod found, check pod status @@ -268,13 +267,6 @@ func (r *DevboxReconciler) syncPod(ctx context.Context, devbox *devboxv1alpha1.D logger.Info("pod", "pod", podList.Items[0].Name, "pod spec", podList.Items[0].Spec) logger.Info("expect pod", "pod", expectPod.Name, "pod spec", expectPod.Spec) _ = r.Delete(ctx, &podList.Items[0]) - // change devbox status - devbox.Status.Phase = devboxv1alpha1.DevboxPhaseDelete - err = r.Status().Update(ctx, devbox) - if err != nil { - logger.Error(err, "update devbox phase failed") - return err - } } case corev1.PodRunning: //if pod is running,check pod need restart @@ -283,10 +275,6 @@ func (r *DevboxReconciler) syncPod(ctx context.Context, devbox *devboxv1alpha1.D logger.Info("pod", "pod", podList.Items[0].Name, "pod spec", podList.Items[0].Spec) logger.Info("expect pod", "pod", expectPod.Name, "pod spec", expectPod.Spec) _ = r.Delete(ctx, &podList.Items[0]) - // change devbox status - devbox.Status.Phase = devboxv1alpha1.DevboxPhaseDelete - } else { - devbox.Status.Phase = devboxv1alpha1.DevboxPhaseRunning } err = r.Status().Update(ctx, devbox) if err != nil { @@ -302,13 +290,11 @@ func (r *DevboxReconciler) syncPod(ctx context.Context, devbox *devboxv1alpha1.D } } _ = r.Delete(ctx, &podList.Items[0]) - // update commit history status to success by pod name return r.updateDevboxCommitHistory(ctx, devbox, &podList.Items[0]) case corev1.PodFailed: // we can't find the reason of failure, we assume the commit status is failed // todo maybe use pod condition to get the reason of failure and update commit history status to failed by pod name - devbox.Status.Phase = devboxv1alpha1.DevboxPhaseError err = r.Status().Update(ctx, devbox) if err != nil { logger.Error(err, "update devbox phase failed") @@ -320,12 +306,6 @@ func (r *DevboxReconciler) syncPod(ctx context.Context, devbox *devboxv1alpha1.D case devboxv1alpha1.DevboxStateStopped: // check pod status, if no pod found, do nothing if len(podList.Items) == 0 { - devbox.Status.Phase = devboxv1alpha1.DevboxPhaseStopped - err = r.Status().Update(ctx, devbox) - if err != nil { - logger.Error(err, "update devbox phase failed") - return err - } return nil } // if pod found, remove finalizer and delete pod @@ -338,15 +318,34 @@ func (r *DevboxReconciler) syncPod(ctx context.Context, devbox *devboxv1alpha1.D } } _ = r.Delete(ctx, &podList.Items[0]) - devbox.Status.Phase = devboxv1alpha1.DevboxPhaseStopped - err = r.Status().Update(ctx, devbox) - if err != nil { - logger.Error(err, "update devbox phase failed") - return err - } return r.updateDevboxCommitHistory(ctx, devbox, &podList.Items[0]) } } + if devbox.Spec.State == devboxv1alpha1.DevboxStateRunning { + if devbox.Status.DevboxPodPhase == corev1.PodRunning { + devbox.Status.Phase = devboxv1alpha1.DevboxPhaseRunning + } else if devbox.Status.DevboxPodPhase == corev1.PodPending { + devbox.Status.Phase = devboxv1alpha1.DevboxPhasePending + } else if devbox.Status.DevboxPodPhase == corev1.PodFailed { + devbox.Status.Phase = devboxv1alpha1.DevboxPhaseError + } else { + devbox.Status.Phase = devboxv1alpha1.DevboxPhaseUnknown + } + } else if devbox.Spec.State == devboxv1alpha1.DevboxStateStopped { + if devbox.Status.DevboxPodPhase == corev1.PodRunning || devbox.Status.DevboxPodPhase == corev1.PodPending { + devbox.Status.Phase = devboxv1alpha1.DevboxPhaseStopping + } else if devbox.Status.DevboxPodPhase == corev1.PodFailed { + devbox.Status.Phase = devboxv1alpha1.DevboxPhaseError + } else { + devbox.Status.Phase = devboxv1alpha1.DevboxPhaseStopped + } + } else { + devbox.Status.Phase = devboxv1alpha1.DevboxPhaseUnknown + } + err = r.Status().Update(ctx, devbox) + if err != nil { + logger.Error(err, "update devbox phase failed") + } return nil } From 00dadf3e83a675186d5e8895950d740a0fcb1ea8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=8A=92=E6=83=85=E7=86=8A?= <2669184984@qq.com> Date: Thu, 5 Sep 2024 15:26:08 +0800 Subject: [PATCH 05/11] add devbox phase and controller --- .../internal/controller/devbox_controller.go | 26 ++++--------------- 1 file changed, 5 insertions(+), 21 deletions(-) diff --git a/controllers/devbox/internal/controller/devbox_controller.go b/controllers/devbox/internal/controller/devbox_controller.go index 9c31a6bc6a5..c456911c37f 100644 --- a/controllers/devbox/internal/controller/devbox_controller.go +++ b/controllers/devbox/internal/controller/devbox_controller.go @@ -268,6 +268,7 @@ func (r *DevboxReconciler) syncPod(ctx context.Context, devbox *devboxv1alpha1.D logger.Info("expect pod", "pod", expectPod.Name, "pod spec", expectPod.Spec) _ = r.Delete(ctx, &podList.Items[0]) } + devbox.Status.Phase = devboxv1alpha1.DevboxPhasePending case corev1.PodRunning: //if pod is running,check pod need restart if !helper.CheckPodConsistency(expectPod, &podList.Items[0]) { @@ -276,6 +277,7 @@ func (r *DevboxReconciler) syncPod(ctx context.Context, devbox *devboxv1alpha1.D logger.Info("expect pod", "pod", expectPod.Name, "pod spec", expectPod.Spec) _ = r.Delete(ctx, &podList.Items[0]) } + devbox.Status.Phase = devboxv1alpha1.DevboxPhaseRunning err = r.Status().Update(ctx, devbox) if err != nil { logger.Error(err, "update devbox phase failed") @@ -295,6 +297,7 @@ func (r *DevboxReconciler) syncPod(ctx context.Context, devbox *devboxv1alpha1.D case corev1.PodFailed: // we can't find the reason of failure, we assume the commit status is failed // todo maybe use pod condition to get the reason of failure and update commit history status to failed by pod name + devbox.Status.Phase = devboxv1alpha1.DevboxPhaseError err = r.Status().Update(ctx, devbox) if err != nil { logger.Error(err, "update devbox phase failed") @@ -306,10 +309,12 @@ func (r *DevboxReconciler) syncPod(ctx context.Context, devbox *devboxv1alpha1.D case devboxv1alpha1.DevboxStateStopped: // check pod status, if no pod found, do nothing if len(podList.Items) == 0 { + devbox.Spec.State = devboxv1alpha1.DevboxStateStopped return nil } // if pod found, remove finalizer and delete pod if len(podList.Items) == 1 { + devbox.Status.Phase = devboxv1alpha1.DevboxPhaseStopping // remove finalizer and delete pod if controllerutil.RemoveFinalizer(&podList.Items[0], FinalizerName) { if err := r.Update(ctx, &podList.Items[0]); err != nil { @@ -321,27 +326,6 @@ func (r *DevboxReconciler) syncPod(ctx context.Context, devbox *devboxv1alpha1.D return r.updateDevboxCommitHistory(ctx, devbox, &podList.Items[0]) } } - if devbox.Spec.State == devboxv1alpha1.DevboxStateRunning { - if devbox.Status.DevboxPodPhase == corev1.PodRunning { - devbox.Status.Phase = devboxv1alpha1.DevboxPhaseRunning - } else if devbox.Status.DevboxPodPhase == corev1.PodPending { - devbox.Status.Phase = devboxv1alpha1.DevboxPhasePending - } else if devbox.Status.DevboxPodPhase == corev1.PodFailed { - devbox.Status.Phase = devboxv1alpha1.DevboxPhaseError - } else { - devbox.Status.Phase = devboxv1alpha1.DevboxPhaseUnknown - } - } else if devbox.Spec.State == devboxv1alpha1.DevboxStateStopped { - if devbox.Status.DevboxPodPhase == corev1.PodRunning || devbox.Status.DevboxPodPhase == corev1.PodPending { - devbox.Status.Phase = devboxv1alpha1.DevboxPhaseStopping - } else if devbox.Status.DevboxPodPhase == corev1.PodFailed { - devbox.Status.Phase = devboxv1alpha1.DevboxPhaseError - } else { - devbox.Status.Phase = devboxv1alpha1.DevboxPhaseStopped - } - } else { - devbox.Status.Phase = devboxv1alpha1.DevboxPhaseUnknown - } err = r.Status().Update(ctx, devbox) if err != nil { logger.Error(err, "update devbox phase failed") From b6f8c6f137539ba0d94c11f3a1e2ec1f9daf6379 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=8A=92=E6=83=85=E7=86=8A?= <2669184984@qq.com> Date: Thu, 5 Sep 2024 15:29:36 +0800 Subject: [PATCH 06/11] add devbox phase and controller --- controllers/devbox/internal/controller/devbox_controller.go | 1 + 1 file changed, 1 insertion(+) diff --git a/controllers/devbox/internal/controller/devbox_controller.go b/controllers/devbox/internal/controller/devbox_controller.go index c456911c37f..dd0c3b94c71 100644 --- a/controllers/devbox/internal/controller/devbox_controller.go +++ b/controllers/devbox/internal/controller/devbox_controller.go @@ -323,6 +323,7 @@ func (r *DevboxReconciler) syncPod(ctx context.Context, devbox *devboxv1alpha1.D } } _ = r.Delete(ctx, &podList.Items[0]) + devbox.Status.Phase = devboxv1alpha1.DevboxPhaseStopped return r.updateDevboxCommitHistory(ctx, devbox, &podList.Items[0]) } } From 5ad008097b8fc1c779aa3f9f6b68025a280fc974 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=8A=92=E6=83=85=E7=86=8A?= <2669184984@qq.com> Date: Thu, 5 Sep 2024 15:32:46 +0800 Subject: [PATCH 07/11] add devbox phase and controller --- controllers/devbox/api/v1alpha1/devbox_types.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/controllers/devbox/api/v1alpha1/devbox_types.go b/controllers/devbox/api/v1alpha1/devbox_types.go index 2d07fcd080f..70d7ea5d5d0 100644 --- a/controllers/devbox/api/v1alpha1/devbox_types.go +++ b/controllers/devbox/api/v1alpha1/devbox_types.go @@ -154,8 +154,6 @@ const ( DevboxPhaseStopping DevboxPhase = "Stopping" //DevboxPhaseError means Devbox is error DevboxPhaseError DevboxPhase = "Error" - //DevboxPhaseUnknow means Devbox not know the status - DevboxPhaseUnknown DevboxPhase = "Unknown" ) // DevboxStatus defines the observed state of Devbox From 2e921d9c344e062c44616c1633e00dbad8eb97a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=8A=92=E6=83=85=E7=86=8A?= <2669184984@qq.com> Date: Thu, 5 Sep 2024 15:53:09 +0800 Subject: [PATCH 08/11] add devbox phase and controller --- .../internal/controller/devbox_controller.go | 33 ++++++++++++++++--- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/controllers/devbox/internal/controller/devbox_controller.go b/controllers/devbox/internal/controller/devbox_controller.go index dd0c3b94c71..2f0a3380f6a 100644 --- a/controllers/devbox/internal/controller/devbox_controller.go +++ b/controllers/devbox/internal/controller/devbox_controller.go @@ -190,11 +190,18 @@ func (r *DevboxReconciler) syncPod(ctx context.Context, devbox *devboxv1alpha1.D var podList corev1.PodList if err := r.List(ctx, &podList, client.InNamespace(devbox.Namespace), client.MatchingLabels(recLabels)); err != nil { logger.Error(err, "list devbox pod failed") + return err } // only one pod is allowed, if more than one pod found, return error if len(podList.Items) > 1 { logger.Error(fmt.Errorf("more than one pod found"), "more than one pod found") + devbox.Status.Phase = devboxv1alpha1.DevboxPhaseError + err := r.Status().Update(ctx, devbox) + if err != nil { + logger.Error(err, "update devbox phase failed") + return err + } return fmt.Errorf("more than one pod found") } @@ -231,6 +238,12 @@ func (r *DevboxReconciler) syncPod(ctx context.Context, devbox *devboxv1alpha1.D if len(podList.Items) == 0 { if err := r.Create(ctx, expectPod); err != nil { logger.Error(err, "create pod failed") + devbox.Status.Phase = devboxv1alpha1.DevboxPhaseError + err := r.Status().Update(ctx, devbox) + if err != nil { + logger.Error(err, "update devbox phase failed") + return err + } return err } // add next commit history to status @@ -269,6 +282,11 @@ func (r *DevboxReconciler) syncPod(ctx context.Context, devbox *devboxv1alpha1.D _ = r.Delete(ctx, &podList.Items[0]) } devbox.Status.Phase = devboxv1alpha1.DevboxPhasePending + err := r.Status().Update(ctx, devbox) + if err != nil { + logger.Error(err, "update devbox phase failed") + return err + } case corev1.PodRunning: //if pod is running,check pod need restart if !helper.CheckPodConsistency(expectPod, &podList.Items[0]) { @@ -309,12 +327,17 @@ func (r *DevboxReconciler) syncPod(ctx context.Context, devbox *devboxv1alpha1.D case devboxv1alpha1.DevboxStateStopped: // check pod status, if no pod found, do nothing if len(podList.Items) == 0 { - devbox.Spec.State = devboxv1alpha1.DevboxStateStopped + devbox.Status.Phase = devboxv1alpha1.DevboxPhaseStopped return nil } // if pod found, remove finalizer and delete pod if len(podList.Items) == 1 { devbox.Status.Phase = devboxv1alpha1.DevboxPhaseStopping + err := r.Status().Update(ctx, devbox) + if err != nil { + logger.Error(err, "update devbox phase failed") + return err + } // remove finalizer and delete pod if controllerutil.RemoveFinalizer(&podList.Items[0], FinalizerName) { if err := r.Update(ctx, &podList.Items[0]); err != nil { @@ -324,13 +347,13 @@ func (r *DevboxReconciler) syncPod(ctx context.Context, devbox *devboxv1alpha1.D } _ = r.Delete(ctx, &podList.Items[0]) devbox.Status.Phase = devboxv1alpha1.DevboxPhaseStopped + err = r.Status().Update(ctx, devbox) + if err != nil { + logger.Error(err, "update devbox phase failed") + } return r.updateDevboxCommitHistory(ctx, devbox, &podList.Items[0]) } } - err = r.Status().Update(ctx, devbox) - if err != nil { - logger.Error(err, "update devbox phase failed") - } return nil } From e5dd84a5dfdd1b9ddd946c3604595f53903c14de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=8A=92=E6=83=85=E7=86=8A?= <2669184984@qq.com> Date: Thu, 5 Sep 2024 15:55:44 +0800 Subject: [PATCH 09/11] add devbox phase and controller --- controllers/devbox/internal/controller/devbox_controller.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/controllers/devbox/internal/controller/devbox_controller.go b/controllers/devbox/internal/controller/devbox_controller.go index 2f0a3380f6a..d180b50f803 100644 --- a/controllers/devbox/internal/controller/devbox_controller.go +++ b/controllers/devbox/internal/controller/devbox_controller.go @@ -190,7 +190,6 @@ func (r *DevboxReconciler) syncPod(ctx context.Context, devbox *devboxv1alpha1.D var podList corev1.PodList if err := r.List(ctx, &podList, client.InNamespace(devbox.Namespace), client.MatchingLabels(recLabels)); err != nil { logger.Error(err, "list devbox pod failed") - return err } // only one pod is allowed, if more than one pod found, return error @@ -328,6 +327,10 @@ func (r *DevboxReconciler) syncPod(ctx context.Context, devbox *devboxv1alpha1.D // check pod status, if no pod found, do nothing if len(podList.Items) == 0 { devbox.Status.Phase = devboxv1alpha1.DevboxPhaseStopped + err = r.Status().Update(ctx, devbox) + if err != nil { + logger.Error(err, "update devbox phase failed") + } return nil } // if pod found, remove finalizer and delete pod From dd2ba33851414affcdb09408c2454f32e708a7d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=8A=92=E6=83=85=E7=86=8A?= <2669184984@qq.com> Date: Thu, 5 Sep 2024 16:01:29 +0800 Subject: [PATCH 10/11] add devbox phase and controller --- controllers/devbox/api/v1alpha1/devbox_types.go | 1 + 1 file changed, 1 insertion(+) diff --git a/controllers/devbox/api/v1alpha1/devbox_types.go b/controllers/devbox/api/v1alpha1/devbox_types.go index 70d7ea5d5d0..636a2974a19 100644 --- a/controllers/devbox/api/v1alpha1/devbox_types.go +++ b/controllers/devbox/api/v1alpha1/devbox_types.go @@ -175,6 +175,7 @@ type DevboxStatus struct { // +kubebuilder:printcolumn:name="PodPhase",type="string",JSONPath=".status.podPhase" // +kubebuilder:printcolumn:name="NetworkType",type="string",JSONPath=".status.network.type" // +kubebuilder:printcolumn:name="NodePort",type="integer",JSONPath=".status.network.nodePort" +// +kubebuilder:printcolumn:name="Phase",type="string",JSONPath=".status.phase" // Devbox is the Schema for the devboxes API type Devbox struct { From 484d5bc94e9c360da91ca24e237088680fe32af3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=8A=92=E6=83=85=E7=86=8A?= <2669184984@qq.com> Date: Thu, 5 Sep 2024 16:46:20 +0800 Subject: [PATCH 11/11] add devbox phase and controller --- .../devbox/config/crd/bases/devbox.sealos.io_devboxes.yaml | 5 +++++ controllers/devbox/deploy/manifests/deploy.yaml.tmpl | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/controllers/devbox/config/crd/bases/devbox.sealos.io_devboxes.yaml b/controllers/devbox/config/crd/bases/devbox.sealos.io_devboxes.yaml index 8244b5e8b8d..26ff39819b5 100644 --- a/controllers/devbox/config/crd/bases/devbox.sealos.io_devboxes.yaml +++ b/controllers/devbox/config/crd/bases/devbox.sealos.io_devboxes.yaml @@ -44,6 +44,9 @@ spec: - jsonPath: .status.network.nodePort name: NodePort type: integer + - jsonPath: .status.phase + name: Phase + type: string name: v1alpha1 schema: openAPIV3Schema: @@ -2762,6 +2765,8 @@ spec: required: - type type: object + phase: + type: string podPhase: description: PodPhase is a label for the condition of a pod at the current time. diff --git a/controllers/devbox/deploy/manifests/deploy.yaml.tmpl b/controllers/devbox/deploy/manifests/deploy.yaml.tmpl index 3bb02f4ae30..827483b0e65 100644 --- a/controllers/devbox/deploy/manifests/deploy.yaml.tmpl +++ b/controllers/devbox/deploy/manifests/deploy.yaml.tmpl @@ -52,6 +52,9 @@ spec: - jsonPath: .status.network.nodePort name: NodePort type: integer + - jsonPath: .status.phase + name: Phase + type: string name: v1alpha1 schema: openAPIV3Schema: @@ -2770,6 +2773,8 @@ spec: required: - type type: object + phase: + type: string podPhase: description: PodPhase is a label for the condition of a pod at the current time.