Skip to content

feat: Devbox Scheduled shutdown. #5559

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

Open
wants to merge 17 commits into
base: main
Choose a base branch
from

Conversation

bearslyricattack
Copy link
Member

No description provided.

Copy link

codecov bot commented Apr 25, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 61.97%. Comparing base (0aabb5f) to head (8bf24e0).
Report is 65 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #5559   +/-   ##
=======================================
  Coverage   61.97%   61.97%           
=======================================
  Files           8        8           
  Lines         647      647           
=======================================
  Hits          401      401           
  Misses        200      200           
  Partials       46       46           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

return ctrl.Result{}, err
}

if scheduledShutdown.Status.State == devboxv1alpha1.ShutdownStateCompleted {
Copy link
Collaborator

Choose a reason for hiding this comment

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

考虑删除掉完成的

Copy link
Member Author

Choose a reason for hiding this comment

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

already fix it.


const (
// ShutdownStatePending indicates the shutdown is scheduled but not yet executed
ShutdownStatePending ShutdownState = "Pending"
Copy link
Collaborator

Choose a reason for hiding this comment

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

ShutdownState 名字不合适,crd的名字也不合适 crd叫 DevBoxSchedue,这里的state叫ScheduleState吧

Copy link
Collaborator

Choose a reason for hiding this comment

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

因为devbox有stop/shutdown两种状态了

Copy link
Member Author

Choose a reason for hiding this comment

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

already fix it.


// Format: RFC3339 (e.g., "2006-01-02T15:04:05Z")
// +kubebuilder:validation:Required
ShutdownTime metav1.Time `json:"shutdownTime"`
Copy link
Collaborator

Choose a reason for hiding this comment

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

这里应该是 scheduledTime

Copy link
Member Author

Choose a reason for hiding this comment

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

already fix it.


// +kubebuilder:validation:Required
// +kubebuilder:default=Stopped
ShutdownType ShutdownType `json:"shutdownType"`
Copy link
Collaborator

Choose a reason for hiding this comment

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

类型预留shutdown,或者考虑支持更多,比如 定时、运行多久后停止的类型这种

Copy link
Member Author

Choose a reason for hiding this comment

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

already fix it.

// ScheduledShutdownSpec defines the desired state of ScheduledShutdown.
type ScheduledShutdownSpec struct {
// +kubebuilder:validation:Required
DevBoxName string `json:"devBoxName"`
Copy link
Collaborator

Choose a reason for hiding this comment

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

这里新增devbox uid

Copy link
Member Author

Choose a reason for hiding this comment

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

already fix it.

type ScheduledShutdownStatus struct {
// State represents the current state of the scheduled shutdown
// +kubebuilder:default=Unknown
State ShutdownState `json:"state,omitempty"`
Copy link
Collaborator

Choose a reason for hiding this comment

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

加上enum

Copy link
Member Author

Choose a reason for hiding this comment

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

already fix it.

}

var devbox devboxv1alpha1.Devbox
if err := r.Get(ctx, types.NamespacedName{Name: scheduledShutdown.Spec.DevBoxName, Namespace: scheduledShutdown.Namespace}, &devbox); err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

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

添加devbox uid后判断uid是否一致

if err := r.Get(ctx, types.NamespacedName{Name: scheduledShutdown.Spec.DevBoxName, Namespace: scheduledShutdown.Namespace}, &devbox); err != nil {
if errors.IsNotFound(err) {
logger.Error(err, "DevBox not found", "DevBoxName", scheduledShutdown.Spec.DevBoxName)
return r.updateStatus(ctx, &scheduledShutdown, devboxv1alpha1.ShutdownStateUnknown)
Copy link
Collaborator

Choose a reason for hiding this comment

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

这里增加状态,uuid不匹配/devbox没找到的状态,既然能找到原因就将原因记录下来

Copy link
Member Author

Choose a reason for hiding this comment

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

already fix it.

return ctrl.Result{}, nil
}

// calculateRequeueTime 计算下一次重新队列的时间间隔,使用递减策略
Copy link
Collaborator

Choose a reason for hiding this comment

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

使用英文注释

Copy link
Member Author

Choose a reason for hiding this comment

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

already fix it.


// calculateRequeueTime 计算下一次重新队列的时间间隔,使用递减策略
func calculateRequeueTime(now time.Time, shutdownTime time.Time) time.Duration {
totalWaitDuration := shutdownTime.Sub(now)
Copy link
Collaborator

Choose a reason for hiding this comment

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

如果是负的了怎么办?

Copy link
Collaborator

Choose a reason for hiding this comment

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

关机时间判断抽离出函数,然后多写些测试用例测试

Copy link
Member Author

Choose a reason for hiding this comment

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

already fix it.

Comment on lines 73 to 82
shutdownTime := scheduledShutdown.Spec.ShutdownTime.Time
if now.After(shutdownTime) {
logger.Info("Shutdown time reached, performing shutdown", "DevBoxName", scheduledShutdown.Spec.DevBoxName, "ShutdownType", scheduledShutdown.Spec.ShutdownType)
//shutdown
if err := r.performShutdown(ctx, &devbox, scheduledShutdown.Spec.ShutdownType); err != nil {
logger.Error(err, "Failed to perform shutdown", "DevBoxName", scheduledShutdown.Spec.DevBoxName)
return r.updateStatus(ctx, &scheduledShutdown, devboxv1alpha1.ShutdownStateUnknown)
}
return r.updateStatus(ctx, &scheduledShutdown, devboxv1alpha1.ShutdownStateCompleted)
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

添加注释在这一段

Copy link
Member Author

Choose a reason for hiding this comment

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

already fix it.

default:
logger.Error(errors.NewBadRequest("Unknown shutdown type"), "DevBoxName", devbox.Name, "ShutdownType", shutdownType)
}
return r.Update(ctx, devbox)
Copy link
Collaborator

Choose a reason for hiding this comment

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

这里 得考虑在reconcile过程中devbox变化,以及 default状态下就不需要更新devbox了

Copy link
Member Author

Choose a reason for hiding this comment

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

part fix.

Copy link
Member Author

Choose a reason for hiding this comment

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

already fix it.

var devboxSchedule devboxv1alpha1.DevBoxSchedule
if err := r.Get(ctx, req.NamespacedName, &devboxSchedule); err != nil {
if errors.IsNotFound(err) {
logger.Info("DevboxSchedule resource not found. Ignoring since object must be deleted")
Copy link
Collaborator

Choose a reason for hiding this comment

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

typo error.

Copy link
Member Author

Choose a reason for hiding this comment

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

already fix it.

if err := r.Get(ctx, types.NamespacedName{Name: devboxSchedule.Spec.DevBoxName, Namespace: devboxSchedule.Namespace}, &devbox); err != nil {
if errors.IsNotFound(err) {
logger.Error(err, "DevBox not found", "DevBoxName", devboxSchedule.Spec.DevBoxName)
return r.updateStatus(ctx, &devboxSchedule, devboxv1alpha1.ScheduleStateNotFound)
Copy link
Collaborator

Choose a reason for hiding this comment

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

check uuid here.

Copy link
Member Author

Choose a reason for hiding this comment

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

already fix it.

Comment on lines 64 to 72
if devboxSchedule.Status.State == devboxv1alpha1.ScheduleStateCompleted {
logger.Info("Schedule already completed, deleting the CR")
if err := r.Delete(ctx, &devboxSchedule); err != nil {
logger.Error(err, "Failed to delete completed DevboxSchedule")
return ctrl.Result{}, err
}
logger.Info("Successfully deleted completed DevboxSchedule")
return ctrl.Result{}, nil
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

对只有pending状态的才去get devbox, 这里使用switch判断下schedule状态做具体操作

Copy link
Member Author

Choose a reason for hiding this comment

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

already fix it.

Comment on lines 134 to 153
func calculateRequeueTime(now time.Time, shutdownTime time.Time) time.Duration {
totalWaitDuration := shutdownTime.Sub(now)
var requeueAfter time.Duration
switch {
case totalWaitDuration > 24*time.Hour:
requeueAfter = 24 * time.Hour
case totalWaitDuration > 6*time.Hour:
requeueAfter = 6 * time.Hour
case totalWaitDuration > 1*time.Hour:
requeueAfter = 1 * time.Hour
case totalWaitDuration > 10*time.Minute:
requeueAfter = 10 * time.Minute
default:
requeueAfter = totalWaitDuration
}
if now.Add(requeueAfter).After(shutdownTime) {
requeueAfter = totalWaitDuration
}
return requeueAfter
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

尽量别硬编码,或者换种方式的硬编码,阶梯式弄个数组出来判断

Copy link
Member Author

Choose a reason for hiding this comment

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

already fix it.

Copy link

stale bot commented Jul 11, 2025

This issue has been automatically closed because we haven't heard back for more than 60 days, please reopen this issue if necessary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants