Skip to content

Commit 48275e9

Browse files
committed
feat: Reserve time to return preflight results before webhook timeout
1 parent 17abac1 commit 48275e9

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

charts/cluster-api-runtime-extensions-nutanix/templates/webhooks.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,4 @@ webhooks:
9494
resources:
9595
- clusters
9696
sideEffects: None
97-
timeoutSeconds: 30
97+
timeoutSeconds: 30 # Keep in sync with Timeout constant defined in pkg/webhook/preflight/preflight.go

pkg/webhook/preflight/preflight.go

Lines changed: 15 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,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+
2231
type (
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

Comments
 (0)