Skip to content

Commit

Permalink
add state and rename last state
Browse files Browse the repository at this point in the history
  • Loading branch information
lingdie committed Sep 14, 2024
1 parent 1fd3cb2 commit ef59fb5
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 52 deletions.
5 changes: 4 additions & 1 deletion controllers/devbox/api/v1alpha1/devbox_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,11 @@ type DevboxStatus struct {
CommitHistory []*CommitHistory `json:"commitHistory"`
// +kubebuilder:validation:Optional
Phase DevboxPhase `json:"phase"`

// +kubebuilder:validation:Optional
State corev1.ContainerState `json:"state"`
// +kubebuilder:validation:Optional
LastTerminatedState *corev1.ContainerStateTerminated `json:"lastTerminatedState"`
LastTerminationState corev1.ContainerState `json:"lastState"`
}

// +kubebuilder:object:root=true
Expand Down
7 changes: 2 additions & 5 deletions controllers/devbox/api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

150 changes: 120 additions & 30 deletions controllers/devbox/config/crd/bases/devbox.sealos.io_devboxes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2768,37 +2768,66 @@ spec:
- time
type: object
type: array
lastTerminatedState:
description: ContainerStateTerminated is a terminated state of a container.
lastState:
description: |-
ContainerState holds a possible state of container.
Only one of its members may be specified.
If none of them is specified, the default one is ContainerStateWaiting.
properties:
containerID:
description: Container's ID in the format '<type>://<container_id>'
type: string
exitCode:
description: Exit status from the last termination of the container
format: int32
type: integer
finishedAt:
description: Time at which the container last terminated
format: date-time
type: string
message:
description: Message regarding the last termination of the container
type: string
reason:
description: (brief) reason from the last termination of the container
type: string
signal:
description: Signal from the last termination of the container
format: int32
type: integer
startedAt:
description: Time at which previous execution of the container
started
format: date-time
type: string
required:
- exitCode
running:
description: Details about a running container
properties:
startedAt:
description: Time at which the container was last (re-)started
format: date-time
type: string
type: object
terminated:
description: Details about a terminated container
properties:
containerID:
description: Container's ID in the format '<type>://<container_id>'
type: string
exitCode:
description: Exit status from the last termination of the
container
format: int32
type: integer
finishedAt:
description: Time at which the container last terminated
format: date-time
type: string
message:
description: Message regarding the last termination of the
container
type: string
reason:
description: (brief) reason from the last termination of the
container
type: string
signal:
description: Signal from the last termination of the container
format: int32
type: integer
startedAt:
description: Time at which previous execution of the container
started
format: date-time
type: string
required:
- exitCode
type: object
waiting:
description: Details about a waiting container
properties:
message:
description: Message regarding why the container is not yet
running.
type: string
reason:
description: (brief) reason the container is not yet running.
type: string
type: object
type: object
network:
properties:
Expand All @@ -2823,6 +2852,67 @@ spec:
description: PodPhase is a label for the condition of a pod at the
current time.
type: string
state:
description: |-
ContainerState holds a possible state of container.
Only one of its members may be specified.
If none of them is specified, the default one is ContainerStateWaiting.
properties:
running:
description: Details about a running container
properties:
startedAt:
description: Time at which the container was last (re-)started
format: date-time
type: string
type: object
terminated:
description: Details about a terminated container
properties:
containerID:
description: Container's ID in the format '<type>://<container_id>'
type: string
exitCode:
description: Exit status from the last termination of the
container
format: int32
type: integer
finishedAt:
description: Time at which the container last terminated
format: date-time
type: string
message:
description: Message regarding the last termination of the
container
type: string
reason:
description: (brief) reason from the last termination of the
container
type: string
signal:
description: Signal from the last termination of the container
format: int32
type: integer
startedAt:
description: Time at which previous execution of the container
started
format: date-time
type: string
required:
- exitCode
type: object
waiting:
description: Details about a waiting container
properties:
message:
description: Message regarding why the container is not yet
running.
type: string
reason:
description: (brief) reason the container is not yet running.
type: string
type: object
type: object
type: object
type: object
served: true
Expand Down
10 changes: 5 additions & 5 deletions controllers/devbox/internal/controller/devbox_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,6 @@ func (r *DevboxReconciler) syncPod(ctx context.Context, devbox *devboxv1alpha1.D
logger.Info("updating devbox status")
logger.Info("merge commit history", "devbox", devbox.Status.CommitHistory, "latestDevbox", latestDevbox.Status.CommitHistory)
helper.UpdateDevboxStatus(devbox, latestDevbox)
logger.Info("devbox latest terminated state", "state", devbox.Status.LastTerminatedState)
return r.Status().Update(ctx, latestDevbox)
}); err != nil {
logger.Error(err, "sync pod failed")
Expand Down Expand Up @@ -255,8 +254,7 @@ func (r *DevboxReconciler) syncPod(ctx context.Context, devbox *devboxv1alpha1.D
case 1:
pod := &podList.Items[0]
devbox.Status.DevboxPodPhase = pod.Status.Phase
devbox.Status.LastTerminatedState = helper.GetLastTerminatedState(devbox, pod)
logger.Info("last terminated state", "state", devbox.Status.LastTerminatedState)
devbox.Status.State = pod.Status.ContainerStatuses[0].State
// update commit predicated status by pod status, this should be done once find a pod
helper.UpdatePredicatedCommitStatus(devbox, pod)
// pod has been deleted, handle it, next reconcile will create a new pod, and we will update commit history status by predicated status
Expand Down Expand Up @@ -298,8 +296,8 @@ func (r *DevboxReconciler) syncPod(ctx context.Context, devbox *devboxv1alpha1.D
case 1:
pod := &podList.Items[0]
devbox.Status.DevboxPodPhase = pod.Status.Phase
devbox.Status.LastTerminatedState = helper.GetLastTerminatedState(devbox, pod)
logger.Info("last terminated state", "state", devbox.Status.LastTerminatedState)
// update state to empty since devbox is stopped
devbox.Status.State = corev1.ContainerState{}
// update commit predicated status by pod status, this should be done once find a pod
helper.UpdatePredicatedCommitStatus(devbox, pod)
// pod has been deleted, handle it, next reconcile will create a new pod, and we will update commit history status by predicated status
Expand Down Expand Up @@ -440,6 +438,7 @@ func (r *DevboxReconciler) deletePod(ctx context.Context, devbox *devboxv1alpha1
return err
}
// update commit history status because pod has been deleted
devbox.Status.LastTerminationState = pod.Status.ContainerStatuses[0].State
helper.UpdateCommitHistory(devbox, pod, true)
return nil
}
Expand All @@ -453,6 +452,7 @@ func (r *DevboxReconciler) handlePodDeleted(ctx context.Context, devbox *devboxv
}
// update commit history status because pod has been deleted
helper.UpdateCommitHistory(devbox, pod, true)
devbox.Status.LastTerminationState = pod.Status.ContainerStatuses[0].State
return nil
}

Expand Down
17 changes: 6 additions & 11 deletions controllers/devbox/internal/controller/helper/devbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ func MergeCommitHistory(devbox *devboxv1alpha1.Devbox, latestDevbox *devboxv1alp
for _, c := range historyMap {
res = append(res, c)
}
// sort commit history by time in descending order
sort.Slice(res, func(i, j int) bool {
return res[i].Time.After(res[j].Time.Time)
})
return res
}

Expand Down Expand Up @@ -119,22 +123,13 @@ func UpdatePredicatedCommitStatus(devbox *devboxv1alpha1.Devbox, pod *corev1.Pod
}
}

func GetLastTerminatedState(devbox *devboxv1alpha1.Devbox, pod *corev1.Pod) *corev1.ContainerStateTerminated {
if len(pod.Status.ContainerStatuses) == 0 {
return nil
}
if pod.Status.ContainerStatuses[0].State.Terminated != nil {
return pod.Status.ContainerStatuses[0].State.Terminated
}
return devbox.Status.LastTerminatedState
}

// UpdateDevboxStatus updates the devbox status, including phase, pod phase, last terminated state and commit history, maybe we need update more fields in the future
// TODO: move this function to devbox types.go
func UpdateDevboxStatus(current, latest *devboxv1alpha1.Devbox) {
latest.Status.Phase = current.Status.Phase
latest.Status.DevboxPodPhase = current.Status.DevboxPodPhase
latest.Status.LastTerminatedState = current.Status.LastTerminatedState
latest.Status.State = current.Status.State
latest.Status.LastTerminationState = current.Status.LastTerminationState
latest.Status.CommitHistory = MergeCommitHistory(current, latest)
}

Expand Down

0 comments on commit ef59fb5

Please sign in to comment.