Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add devbox phase and controller #5042

Merged
merged 13 commits into from
Sep 5, 2024
17 changes: 17 additions & 0 deletions controllers/devbox/api/v1alpha1/devbox_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,21 @@ 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"
)

// DevboxStatus defines the observed state of Devbox
type DevboxStatus struct {
// +kubebuilder:validation:Optional
Expand All @@ -149,6 +164,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
Expand Down
20 changes: 20 additions & 0 deletions controllers/devbox/internal/controller/devbox_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
lingdie marked this conversation as resolved.
Show resolved Hide resolved
case corev1.PodRunning:
//if pod is running,check pod need restart
if !helper.CheckPodConsistency(expectPod, &podList.Items[0]) {
Expand All @@ -276,6 +277,12 @@ 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")
return err
}
return r.updateDevboxCommitHistory(ctx, devbox, &podList.Items[0])
case corev1.PodSucceeded:
if controllerutil.RemoveFinalizer(&podList.Items[0], FinalizerName) {
Expand All @@ -290,16 +297,24 @@ 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")
return err
}
return r.updateDevboxCommitHistory(ctx, devbox, &podList.Items[0])
}
}
case devboxv1alpha1.DevboxStateStopped:
// check pod status, if no pod found, do nothing
if len(podList.Items) == 0 {
devbox.Spec.State = devboxv1alpha1.DevboxStateStopped
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix this, no need to chanege spec.

return nil
}
// if pod found, remove finalizer and delete pod
if len(podList.Items) == 1 {
devbox.Status.Phase = devboxv1alpha1.DevboxPhaseStopping
lingdie marked this conversation as resolved.
Show resolved Hide resolved
// remove finalizer and delete pod
if controllerutil.RemoveFinalizer(&podList.Items[0], FinalizerName) {
if err := r.Update(ctx, &podList.Items[0]); err != nil {
Expand All @@ -308,9 +323,14 @@ 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])
}
}
err = r.Status().Update(ctx, devbox)
if err != nil {
logger.Error(err, "update devbox phase failed")
}
return nil
}

Expand Down
Loading