generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 31
Add support for shutdownTime in sandbox.spec #51
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ import ( | |
| "fmt" | ||
| "hash/fnv" | ||
| "reflect" | ||
| "time" | ||
|
|
||
| corev1 "k8s.io/api/core/v1" | ||
| k8serrors "k8s.io/apimachinery/pkg/api/errors" | ||
|
|
@@ -93,7 +94,26 @@ func (r *SandboxReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct | |
| } | ||
|
|
||
| oldStatus := sandbox.Status.DeepCopy() | ||
| var err error | ||
|
|
||
| expired, requeueAfter := r.processSandboxExpiry(sandbox) | ||
|
|
||
| // Check if sandbox has expired | ||
| if expired { | ||
| log.Info("Sandbox has expired, deleting pod and service") | ||
barney-s marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| err = r.deleteChildResources(ctx, sandbox) | ||
| } else { | ||
| err = r.reconcileChildResources(ctx, sandbox) | ||
| } | ||
|
|
||
| // Update status | ||
| err = errors.Join(err, r.updateStatus(ctx, oldStatus, sandbox)) | ||
|
|
||
| // return errors seen | ||
| return ctrl.Result{RequeueAfter: requeueAfter}, err | ||
| } | ||
|
|
||
| func (r *SandboxReconciler) reconcileChildResources(ctx context.Context, sandbox *sandboxv1alpha1.Sandbox) error { | ||
| // Create a hash from the sandbox.Name and use it as label value | ||
| nameHash := NameHash(sandbox.Name) | ||
|
|
||
|
|
@@ -115,12 +135,7 @@ func (r *SandboxReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct | |
| readyCondition := r.computeReadyCondition(sandbox.Generation, allErrors, svc, pod) | ||
| meta.SetStatusCondition(&sandbox.Status.Conditions, readyCondition) | ||
|
|
||
| // Update status | ||
| err = r.updateStatus(ctx, oldStatus, sandbox) | ||
| allErrors = errors.Join(allErrors, err) | ||
|
|
||
| // return errors seen | ||
| return ctrl.Result{}, allErrors | ||
| return allErrors | ||
| } | ||
|
|
||
| func (r *SandboxReconciler) computeReadyCondition(generation int64, err error, svc *corev1.Service, pod *corev1.Pod) metav1.Condition { | ||
|
|
@@ -342,6 +357,64 @@ func (r *SandboxReconciler) reconcilePVCs(ctx context.Context, sandbox *sandboxv | |
| return nil | ||
| } | ||
|
|
||
| func (r *SandboxReconciler) deleteChildResources(ctx context.Context, sandbox *sandboxv1alpha1.Sandbox) error { | ||
| var allErrors error | ||
| pod := &corev1.Pod{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: sandbox.Name, | ||
| Namespace: sandbox.Namespace, | ||
| }, | ||
| } | ||
| if err := r.Delete(ctx, pod); err != nil && !k8serrors.IsNotFound(err) { | ||
| allErrors = errors.Join(allErrors, fmt.Errorf("failed to delete pod: %w", err)) | ||
| } | ||
|
|
||
| service := &corev1.Service{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: sandbox.Name, | ||
| Namespace: sandbox.Namespace, | ||
| }, | ||
| } | ||
| if err := r.Delete(ctx, service); err != nil && !k8serrors.IsNotFound(err) { | ||
| allErrors = errors.Join(allErrors, fmt.Errorf("failed to delete service: %w", err)) | ||
| } | ||
|
|
||
| // Update status to remove Ready condition | ||
| meta.SetStatusCondition(&sandbox.Status.Conditions, metav1.Condition{ | ||
| Type: string(sandboxv1alpha1.SandboxConditionReady), | ||
| Status: metav1.ConditionFalse, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure this is not ready... The object has been reconciled and is not expected to change state. But let's see what feels natural in use |
||
| ObservedGeneration: sandbox.Generation, | ||
| Reason: "SandboxExpired", | ||
| Message: "Sandbox has expired", | ||
| }) | ||
|
|
||
| return allErrors | ||
| } | ||
|
|
||
| // checks if the sandbox has expired | ||
| // returns true if expired, false otherwise | ||
| // if not expired, also returns the duration to requeue after | ||
| func (r *SandboxReconciler) processSandboxExpiry(sandbox *sandboxv1alpha1.Sandbox) (bool, time.Duration) { | ||
| if sandbox.Spec.ShutdownTime == nil { | ||
| return false, 0 | ||
| } | ||
|
|
||
| expiryTime := sandbox.Spec.ShutdownTime.Time | ||
| if time.Now().After(expiryTime) { | ||
| return true, 0 | ||
| } | ||
|
|
||
| // Calculate remaining time | ||
| remainingTime := time.Until(expiryTime) | ||
|
|
||
| // TODO(barney-s): Do we need a inverse exponential backoff here ? | ||
| //requeueAfter := max(remainingTime/2, 2*time.Second) | ||
|
|
||
| // Requeue at expiry time or in 2 seconds whichever is later | ||
| requeueAfter := max(remainingTime, 2*time.Second) | ||
| return false, requeueAfter | ||
| } | ||
|
|
||
| // SetupWithManager sets up the controller with the Manager. | ||
| func (r *SandboxReconciler) SetupWithManager(mgr ctrl.Manager) error { | ||
| labelSelectorPredicate, err := predicate.LabelSelectorPredicate(metav1.LabelSelector{ | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.