-
Notifications
You must be signed in to change notification settings - Fork 8
feat: Add preflight checks framework #1129
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
Merged
Merged
Changes from 18 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
7511f49
fix: Use helm variables for all webhook configurations
dlipovetsky a40fd92
feat: Add preflight checks framework
dlipovetsky 24c9d45
fixup! feat: Add preflight checks framework
dlipovetsky 858e6fd
fixup! feat: Add preflight checks framework
dlipovetsky 2c69dd0
fixup! feat: Add preflight checks framework
dlipovetsky 7e687a2
fixup! feat: Add preflight checks framework
dlipovetsky 306a852
fixup! feat: Add preflight checks framework
dlipovetsky b3da7d4
fixup! feat: Add preflight checks framework
dlipovetsky 690a89e
fixup! feat: Add preflight checks framework
dlipovetsky 47c6ad8
fixup! feat: Add preflight checks framework
dlipovetsky 3211e7a
fixup! feat: Add preflight checks framework
dlipovetsky 5eb6d26
fixup! feat: Add preflight checks framework
dlipovetsky 4a518b3
fixup! feat: Add preflight checks framework
dlipovetsky 438495b
fixup! feat: Add preflight checks framework
dlipovetsky b377301
fixup! feat: Add preflight checks framework
dlipovetsky ec20c1e
fixup! feat: Add preflight checks framework
dlipovetsky d766f3a
fixup! feat: Add preflight checks framework
dlipovetsky 14ea63d
fixup! feat: Add preflight checks framework
dlipovetsky a4078ad
fixup! feat: Add preflight checks framework
dlipovetsky 10f132a
fixup! feat: Add preflight checks framework
dlipovetsky a067941
fixup! feat: Add preflight checks framework
dlipovetsky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// Copyright 2025 Nutanix. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package preflight | ||
|
||
// +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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
// Copyright 2025 Nutanix. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package preflight | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"sync" | ||
|
||
admissionv1 "k8s.io/api/admission/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" | ||
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
) | ||
|
||
type ( | ||
// CheckerFactory returns a Checker for a given cluster. | ||
CheckerFactory func(client ctrlclient.Client, cluster *clusterv1.Cluster) Checker | ||
Checker interface { | ||
// Init returns the checks that should run for the cluster. | ||
Init(ctx context.Context) []Check | ||
} | ||
|
||
Check = func(ctx context.Context) CheckResult | ||
CheckResult struct { | ||
Name string | ||
|
||
Allowed bool | ||
Error bool | ||
|
||
Causes []Cause | ||
Warnings []string | ||
} | ||
Cause struct { | ||
Message string | ||
Field string | ||
} | ||
) | ||
|
||
type WebhookHandler struct { | ||
client ctrlclient.Client | ||
decoder admission.Decoder | ||
factories []CheckerFactory | ||
} | ||
|
||
func New(client ctrlclient.Client, decoder admission.Decoder, factories ...CheckerFactory) *WebhookHandler { | ||
h := &WebhookHandler{ | ||
client: client, | ||
decoder: decoder, | ||
factories: factories, | ||
} | ||
return h | ||
} | ||
|
||
func (h *WebhookHandler) Handle(ctx context.Context, req admission.Request) admission.Response { | ||
if req.Operation == admissionv1.Delete { | ||
return admission.Allowed("") | ||
} | ||
|
||
cluster := &clusterv1.Cluster{} | ||
err := h.decoder.Decode(req, cluster) | ||
if err != nil { | ||
return admission.Errored(http.StatusBadRequest, err) | ||
} | ||
|
||
// Checks run only for ClusterClass-based clusters. | ||
if cluster.Spec.Topology == nil { | ||
return admission.Allowed("") | ||
} | ||
|
||
resultsOrderedByCheckerAndCheck := run(ctx, h.client, cluster, h.factories) | ||
|
||
// Summarize the results. | ||
resp := admission.Response{ | ||
AdmissionResponse: admissionv1.AdmissionResponse{ | ||
Allowed: true, | ||
Result: &metav1.Status{ | ||
Details: &metav1.StatusDetails{}, | ||
}, | ||
}, | ||
} | ||
internalError := false | ||
for _, results := range resultsOrderedByCheckerAndCheck { | ||
for _, result := range results { | ||
if result.Error { | ||
internalError = true | ||
} | ||
if !result.Allowed { | ||
resp.Allowed = false | ||
} | ||
for _, cause := range result.Causes { | ||
resp.Result.Details.Causes = append(resp.Result.Details.Causes, metav1.StatusCause{ | ||
Type: metav1.CauseType(fmt.Sprintf("FailedPreflight%s", result.Name)), | ||
dlipovetsky marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Message: cause.Message, | ||
Field: cause.Field, | ||
}) | ||
} | ||
resp.Warnings = append(resp.Warnings, result.Warnings...) | ||
} | ||
} | ||
|
||
switch { | ||
case internalError: | ||
// Internal errors take precedence over check failures. | ||
resp.Result.Message = "preflight checks failed due to an internal error" | ||
resp.Result.Code = http.StatusInternalServerError | ||
resp.Result.Reason = metav1.StatusReasonInternalError | ||
case !resp.Allowed: | ||
// Because the response is not allowed, preflights must have failed. | ||
resp.Result.Message = "preflight checks failed" | ||
resp.Result.Code = http.StatusUnprocessableEntity | ||
resp.Result.Reason = metav1.StatusReasonInvalid | ||
} | ||
|
||
return resp | ||
} | ||
|
||
// run runs all checks for the cluster, concurrently, and returns the results ordered by checker and check. | ||
// Checker are initialized concurrently, and checks runs concurrently as well. | ||
func run(ctx context.Context, | ||
dlipovetsky marked this conversation as resolved.
Show resolved
Hide resolved
|
||
client ctrlclient.Client, | ||
cluster *clusterv1.Cluster, | ||
factories []CheckerFactory, | ||
) [][]CheckResult { | ||
resultsOrderedByCheckerAndCheck := make([][]CheckResult, len(factories)) | ||
|
||
checkersWG := sync.WaitGroup{} | ||
for i, factory := range factories { | ||
checkersWG.Add(1) | ||
go func(ctx context.Context, client ctrlclient.Client, cluster *clusterv1.Cluster, factory CheckerFactory, i int) { | ||
checker := factory(client, cluster) | ||
|
||
checks := checker.Init(ctx) | ||
resultsOrderedByCheck := make([]CheckResult, len(checks)) | ||
|
||
checksWG := sync.WaitGroup{} | ||
for j, check := range checks { | ||
checksWG.Add(1) | ||
go func(ctx context.Context, check Check, j int) { | ||
result := check(ctx) | ||
resultsOrderedByCheck[j] = result | ||
checksWG.Done() | ||
}(ctx, check, j) | ||
} | ||
checksWG.Wait() | ||
resultsOrderedByCheckerAndCheck[i] = resultsOrderedByCheck | ||
|
||
checkersWG.Done() | ||
}( | ||
ctx, | ||
client, | ||
cluster, | ||
factory, | ||
i, | ||
) | ||
} | ||
checkersWG.Wait() | ||
|
||
return resultsOrderedByCheckerAndCheck | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.