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,14 @@ 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+ //
26+ // Keep in sync with the `timeoutSeconds` field of the "preflight.cluster.caren.nutanix.com"
27+ // ValidatingWebhookConfiguration defined in charts/cluster-api-runtime-extensions-nutanix/templates/webhooks.yaml
28+ Timeout = 30 * time .Second
29+ )
30+
2231type (
2332 // Checker returns a set of checks that have been initialized with common dependencies,
2433 // such as an infrastructure API client.
@@ -138,7 +147,12 @@ func (h *WebhookHandler) Handle(ctx context.Context, req admission.Request) admi
138147 )
139148 }
140149
141- resultsOrderedByCheckerAndCheck := run (ctx , h .client , cluster , skipEvaluator , h .checkers )
150+ // Reserve time for checks to handle context cancellation, so
151+ // that we have time to summarize the results, and return a response.
152+ checkTimeout := Timeout - 2 * time .Second
153+ checkCtx , checkCtxCancel := context .WithTimeout (ctx , checkTimeout )
154+ resultsOrderedByCheckerAndCheck := run (checkCtx , h .client , cluster , skipEvaluator , h .checkers )
155+ checkCtxCancel ()
142156
143157 // Summarize the results.
144158 resp := admission.Response {
0 commit comments