-
Notifications
You must be signed in to change notification settings - Fork 3
fix: detect expired preauth keys in status #44
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -205,16 +205,7 @@ func (r *HeadscalePreAuthKeyReconciler) Reconcile(ctx context.Context, req ctrl. | |||||||||||||||||
|
|
||||||||||||||||||
| // Check if the preauth key already exists | ||||||||||||||||||
| if preAuthKey.Status.KeyID != "" { | ||||||||||||||||||
| // PreAuth key already created | ||||||||||||||||||
| if err := r.updateStatusCondition(ctx, preAuthKey, metav1.Condition{ | ||||||||||||||||||
| Type: "Ready", | ||||||||||||||||||
| Status: metav1.ConditionTrue, | ||||||||||||||||||
| Reason: "PreAuthKeyReady", | ||||||||||||||||||
| Message: "PreAuth key is ready", | ||||||||||||||||||
| }); err != nil { | ||||||||||||||||||
| return ctrl.Result{}, err | ||||||||||||||||||
| } | ||||||||||||||||||
| return ctrl.Result{}, nil | ||||||||||||||||||
| return r.reconcileExistingKey(ctx, preAuthKey) | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| // Create the preauth key in Headscale | ||||||||||||||||||
|
|
@@ -249,6 +240,43 @@ func (r *HeadscalePreAuthKeyReconciler) Reconcile(ctx context.Context, req ctrl. | |||||||||||||||||
| return ctrl.Result{}, nil | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| // reconcileExistingKey handles reconciliation when a preauth key already exists (KeyID is set). | ||||||||||||||||||
| // It checks expiration status and requeues before expiration for active keys. | ||||||||||||||||||
| func (r *HeadscalePreAuthKeyReconciler) reconcileExistingKey( | ||||||||||||||||||
| ctx context.Context, | ||||||||||||||||||
| preAuthKey *headscalev1beta1.HeadscalePreAuthKey, | ||||||||||||||||||
| ) (ctrl.Result, error) { | ||||||||||||||||||
| log := logf.FromContext(ctx) | ||||||||||||||||||
|
|
||||||||||||||||||
| if preAuthKey.Status.ExpiresAt != nil && time.Now().After(preAuthKey.Status.ExpiresAt.Time) { | ||||||||||||||||||
| log.Info("PreAuth key has expired", "keyID", preAuthKey.Status.KeyID, "expiresAt", preAuthKey.Status.ExpiresAt.Time) | ||||||||||||||||||
| if err := r.updateStatusCondition(ctx, preAuthKey, metav1.Condition{ | ||||||||||||||||||
| Type: "Ready", | ||||||||||||||||||
| Status: metav1.ConditionFalse, | ||||||||||||||||||
| Reason: "KeyExpired", | ||||||||||||||||||
| Message: fmt.Sprintf("PreAuth key expired at %s", preAuthKey.Status.ExpiresAt.Format(time.RFC3339)), | ||||||||||||||||||
| }); err != nil { | ||||||||||||||||||
| return ctrl.Result{}, err | ||||||||||||||||||
| } | ||||||||||||||||||
| return ctrl.Result{}, nil | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| // Key is ready, requeue before expiration if ExpiresAt is set | ||||||||||||||||||
| var requeueAfter time.Duration | ||||||||||||||||||
| if preAuthKey.Status.ExpiresAt != nil { | ||||||||||||||||||
| requeueAfter = time.Until(preAuthKey.Status.ExpiresAt.Time) | ||||||||||||||||||
| } | ||||||||||||||||||
|
Comment on lines
+264
to
+268
|
||||||||||||||||||
| if err := r.updateStatusCondition(ctx, preAuthKey, metav1.Condition{ | ||||||||||||||||||
| Type: "Ready", | ||||||||||||||||||
| Status: metav1.ConditionTrue, | ||||||||||||||||||
| Reason: "PreAuthKeyReady", | ||||||||||||||||||
| Message: "PreAuth key is ready", | ||||||||||||||||||
| }); err != nil { | ||||||||||||||||||
| return ctrl.Result{}, err | ||||||||||||||||||
| } | ||||||||||||||||||
| return ctrl.Result{RequeueAfter: requeueAfter}, nil | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| // handleDeletion handles the deletion of a HeadscalePreAuthKey instance | ||||||||||||||||||
| func (r *HeadscalePreAuthKeyReconciler) handleDeletion( | ||||||||||||||||||
| ctx context.Context, | ||||||||||||||||||
|
|
@@ -459,6 +487,8 @@ func (r *HeadscalePreAuthKeyReconciler) createPreAuthKey( | |||||||||||||||||
|
|
||||||||||||||||||
| // Update status with preauth key information | ||||||||||||||||||
| preAuthKey.Status.KeyID = strconv.FormatUint(key.GetId(), 10) | ||||||||||||||||||
| expiresAt := metav1.NewTime(time.Now().Add(expiration)) | ||||||||||||||||||
| preAuthKey.Status.ExpiresAt = &expiresAt | ||||||||||||||||||
|
Comment on lines
+490
to
+491
|
||||||||||||||||||
| expiresAt := metav1.NewTime(time.Now().Add(expiration)) | |
| preAuthKey.Status.ExpiresAt = &expiresAt | |
| if expiration > 0 { | |
| expiresAt := metav1.NewTime(time.Now().Add(expiration)) | |
| preAuthKey.Status.ExpiresAt = &expiresAt | |
| } else { | |
| preAuthKey.Status.ExpiresAt = nil | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Expiration check uses time.Now().After(expiresAt) and later calls time.Until(expiresAt) with a different time.Now(). If ExpiresAt is equal/very close to now, this can fall through the "active" path, compute RequeueAfter <= 0, and then never reconcile again—leaving Ready=True after the key has actually expired. Consider capturing now := time.Now() once and treating ExpiresAt <= now as expired (e.g., !expiresAt.After(now)).