Skip to content

Commit 4a69f52

Browse files
authored
feat: Reserve time to return preflight results before webhook timeout (#1235)
**What problem does this PR solve?**: Assuming that preflight checks return within 2 seconds of context cancellation, the preflight checks webhook will return before the API server webhook timeout. Some preflight checks already meet this criteria. I'm updating others to meet it in #1234 **Which issue(s) this PR fixes**: Fixes # **How Has This Been Tested?**: <!-- Please describe the tests that you ran to verify your changes. Provide output from the tests and any manual steps needed to replicate the tests. --> **Special notes for your reviewer**: <!-- Use this to provide any additional information to the reviewers. This may include: - Best way to review the PR. - Where the author wants the most review attention on. - etc. -->
1 parent 631a323 commit 4a69f52

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

pkg/webhook/preflight/doc.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
package preflight
44

55
// +kubebuilder:webhook:path=/preflight-v1beta1-cluster,mutating=false,failurePolicy=fail,groups="cluster.x-k8s.io",resources=clusters,verbs=create,versions=*,name=preflight.cluster.caren.nutanix.com,admissionReviewVersions=v1,sideEffects=None,timeoutSeconds=30
6+
7+
// IMPORTANT Keep timeoutSeconds in sync with the `Timeout` constant defined in this package.

pkg/webhook/preflight/preflight.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"net/http"
99
"runtime/debug"
1010
"sync"
11+
"time"
1112

1213
admissionv1 "k8s.io/api/admission/v1"
1314
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -19,6 +20,12 @@ import (
1920
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/webhook/preflight/skip"
2021
)
2122

23+
const (
24+
// Timeout is the duration, in seconds, that the preflight checks handler has to respond.
25+
// IMPORTANT Keep in sync timeoutSeconds in the kubebuilder:webhook marker defined in this package.
26+
Timeout = 30 * time.Second
27+
)
28+
2229
type (
2330
// Checker returns a set of checks that have been initialized with common dependencies,
2431
// such as an infrastructure API client.
@@ -138,7 +145,12 @@ func (h *WebhookHandler) Handle(ctx context.Context, req admission.Request) admi
138145
)
139146
}
140147

141-
resultsOrderedByCheckerAndCheck := run(ctx, h.client, cluster, skipEvaluator, h.checkers)
148+
// Reserve time for checks to handle context cancellation, so
149+
// that we have time to summarize the results, and return a response.
150+
checkTimeout := Timeout - 2*time.Second
151+
checkCtx, checkCtxCancel := context.WithTimeout(ctx, checkTimeout)
152+
resultsOrderedByCheckerAndCheck := run(checkCtx, h.client, cluster, skipEvaluator, h.checkers)
153+
checkCtxCancel()
142154

143155
// Summarize the results.
144156
resp := admission.Response{

0 commit comments

Comments
 (0)