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

Fix emptiness reconciler when ttl not expired #1262

Merged
merged 3 commits into from
Feb 2, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion pkg/controllers/node/emptiness.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (r *Emptiness) Reconcile(ctx context.Context, provisioner *v1alpha5.Provisi
return reconcile.Result{}, fmt.Errorf("deleting node, %w", err)
}
}
return reconcile.Result{}, nil
return reconcile.Result{RequeueAfter: emptinessTime.Add(ttl).Sub(injectabletime.Now())}, nil
}

func (r *Emptiness) isEmpty(ctx context.Context, n *v1.Node) (bool, error) {
Expand Down
23 changes: 23 additions & 0 deletions pkg/controllers/node/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
. "knative.dev/pkg/logging/testing"
"knative.dev/pkg/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

var ctx context.Context
Expand Down Expand Up @@ -285,6 +286,28 @@ var _ = Describe("Controller", func() {
node = ExpectNodeExists(ctx, env.Client, node.Name)
Expect(node.DeletionTimestamp.IsZero()).To(BeFalse())
})
It("should requeue reconcile if node is empty, but not past emptiness TTL", func() {
provisioner.Spec.TTLSecondsAfterEmpty = ptr.Int64(30)
currentTime := time.Now()
suket22 marked this conversation as resolved.
Show resolved Hide resolved
injectabletime.Now = func() time.Time { return currentTime } // injectabletime.Now() is called multiple times in function being tested.
suket22 marked this conversation as resolved.
Show resolved Hide resolved
emptinessTime := injectabletime.Now().Add(-10 * time.Second)
// Emptiness timestamps are first formatted to a string friendly (time.RFC3339) (to put it in the node object)
// and then eventually parsed back into time.Time when comparing ttls. Repeating that logic in the test.
emptinessTimestamp, _ := time.Parse(time.RFC3339, emptinessTime.Format(time.RFC3339))
expectedrequeueTime := emptinessTimestamp.Add(time.Duration(30 * time.Second)).Sub(injectabletime.Now()) // we should requeue in ~20 seconds.
suket22 marked this conversation as resolved.
Show resolved Hide resolved
suket22 marked this conversation as resolved.
Show resolved Hide resolved
node := test.Node(test.NodeOptions{ObjectMeta: metav1.ObjectMeta{
Finalizers: []string{v1alpha5.TerminationFinalizer},
Labels: map[string]string{v1alpha5.ProvisionerNameLabelKey: provisioner.Name},
Annotations: map[string]string{
v1alpha5.EmptinessTimestampAnnotationKey: emptinessTime.Format(time.RFC3339),
}},
})
ExpectCreated(ctx, env.Client, provisioner, node)
reconcileResult := ExpectReconcileSucceeded(ctx, controller, client.ObjectKeyFromObject(node))
suket22 marked this conversation as resolved.
Show resolved Hide resolved
Expect(reconcileResult).To(Equal(reconcile.Result{Requeue: true, RequeueAfter: expectedrequeueTime}))
suket22 marked this conversation as resolved.
Show resolved Hide resolved
node = ExpectNodeExists(ctx, env.Client, node.Name)
Expect(node.DeletionTimestamp.IsZero()).To(BeTrue())
})
suket22 marked this conversation as resolved.
Show resolved Hide resolved
})
Context("Finalizer", func() {
It("should add the termination finalizer if missing", func() {
Expand Down
5 changes: 3 additions & 2 deletions pkg/test/expectations/expectations.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ func ExpectProvisioned(ctx context.Context, c client.Client, selectionController
return result
}

func ExpectReconcileSucceeded(ctx context.Context, reconciler reconcile.Reconciler, key client.ObjectKey) {
_, err := reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: key})
func ExpectReconcileSucceeded(ctx context.Context, reconciler reconcile.Reconciler, key client.ObjectKey) reconcile.Result {
result, err := reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: key})
Expect(err).ToNot(HaveOccurred())
return result
}