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

feat: change devbox to stop if exceeded quota #5182

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions controllers/devbox/internal/controller/devbox_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,19 @@ func (r *DevboxReconciler) syncPod(ctx context.Context, devbox *devboxv1alpha1.D
case 0:
logger.Info("create pod")
logger.Info("next commit history", "commit", nextCommitHistory)
return r.createPod(ctx, devbox, expectPod, nextCommitHistory)
err := r.createPod(ctx, devbox, expectPod, nextCommitHistory)
if err != nil && helper.IsExceededQuotaError(err) {
logger.Info("devbox is exceeded quota, change devbox state to Stopped")
r.Recorder.Eventf(devbox, corev1.EventTypeWarning, "Devbox is exceeded quota", "Devbox is exceeded quota")
devbox.Spec.State = devboxv1alpha1.DevboxStateStopped
_ = r.Update(ctx, devbox)
return nil
}
if err != nil {
logger.Error(err, "create pod failed")
return err
}
return nil
case 1:
pod := &podList.Items[0]
// check pod container size, if it is 0, it means the pod is not running, return an error
Expand Down Expand Up @@ -419,11 +431,9 @@ func (r *DevboxReconciler) getRuntime(ctx context.Context, devbox *devboxv1alpha

// create a new pod, add predicated status to nextCommitHistory
func (r *DevboxReconciler) createPod(ctx context.Context, devbox *devboxv1alpha1.Devbox, expectPod *corev1.Pod, nextCommitHistory *devboxv1alpha1.CommitHistory) error {
logger := log.FromContext(ctx)
nextCommitHistory.Status = devboxv1alpha1.CommitStatusPending
nextCommitHistory.PredicatedStatus = devboxv1alpha1.CommitStatusPending
if err := r.Create(ctx, expectPod); err != nil {
logger.Error(err, "create pod failed")
return err
}
devbox.Status.CommitHistory = append(devbox.Status.CommitHistory, nextCommitHistory)
Expand Down
5 changes: 5 additions & 0 deletions controllers/devbox/internal/controller/helper/devbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"
"log/slog"
"sort"
"strings"

"crypto/ed25519"
"crypto/rand"
Expand Down Expand Up @@ -397,6 +398,10 @@ func GenerateResourceRequirements(devbox *devboxv1alpha1.Devbox, requestEphemera
}
}

func IsExceededQuotaError(err error) bool {
return strings.Contains(err.Error(), "exceeded quota")
}

func calculateResourceRequest(limit corev1.ResourceList) corev1.ResourceList {
if limit == nil {
return nil
Expand Down
Loading