Skip to content

Commit a1bbb2c

Browse files
authored
feat: Preflight check opt-out (#1156)
**What problem does this PR solve?**: Provides a way for the client to opt out of preflight checks for a specific Cluster by using an annotation: Examples Opt out of two specific checks: ``` preflight.cluster.caren.nutanix.com/opt-out: "CheckOne, checktwo" ``` (Note that both case and whitespace is ignored) Opt out of all checks: ``` preflight.cluster.caren.nutanix.com/opt-out: all ``` When a pre-flight check returns a false negative due to a bug, an issue in some external API, or some unforseen circumstance, the client needs a way to opt out. **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 da7dbfb commit a1bbb2c

File tree

4 files changed

+470
-106
lines changed

4 files changed

+470
-106
lines changed

pkg/webhook/preflight/preflight.go

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515
ctrl "sigs.k8s.io/controller-runtime"
1616
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
1717
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
18+
19+
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/webhook/preflight/skip"
1820
)
1921

2022
type (
@@ -93,7 +95,16 @@ func (h *WebhookHandler) Handle(ctx context.Context, req admission.Request) admi
9395
return admission.Allowed("")
9496
}
9597

96-
resultsOrderedByCheckerAndCheck := run(ctx, h.client, cluster, h.checkers)
98+
skipEvaluator := skip.New(cluster)
99+
100+
if skipEvaluator.ForAll() {
101+
// If the cluster has skipped all checks, return allowed.
102+
return admission.Allowed("").WithWarnings(
103+
"Cluster has skipped all preflight checks",
104+
)
105+
}
106+
107+
resultsOrderedByCheckerAndCheck := run(ctx, h.client, cluster, skipEvaluator, h.checkers)
97108

98109
// Summarize the results.
99110
resp := admission.Response{
@@ -145,23 +156,49 @@ func (h *WebhookHandler) Handle(ctx context.Context, req admission.Request) admi
145156
func run(ctx context.Context,
146157
client ctrlclient.Client,
147158
cluster *clusterv1.Cluster,
159+
skipEvaluator *skip.Evaluator,
148160
checkers []Checker,
149161
) [][]namedResult {
150162
resultsOrderedByCheckerAndCheck := make([][]namedResult, len(checkers))
151163

152164
checkersWG := sync.WaitGroup{}
153165
for i, checker := range checkers {
154166
checkersWG.Add(1)
155-
go func(ctx context.Context, client ctrlclient.Client, cluster *clusterv1.Cluster, checker Checker, i int) {
167+
go func(
168+
ctx context.Context,
169+
client ctrlclient.Client,
170+
cluster *clusterv1.Cluster,
171+
skipEvaluator *skip.Evaluator,
172+
checker Checker,
173+
i int,
174+
) {
156175
defer checkersWG.Done()
157176

158177
checks := checker.Init(ctx, client, cluster)
159178
resultsOrderedByCheck := make([]namedResult, len(checks))
160179

161180
checksWG := sync.WaitGroup{}
162181
for j, check := range checks {
182+
if skipEvaluator.For(check.Name()) {
183+
resultsOrderedByCheck[j] = namedResult{
184+
Name: check.Name(),
185+
CheckResult: CheckResult{
186+
Allowed: true,
187+
Error: false,
188+
Causes: nil,
189+
Warnings: []string{
190+
fmt.Sprintf("Cluster has skipped preflight check %q", check.Name()),
191+
},
192+
},
193+
}
194+
continue
195+
}
163196
checksWG.Add(1)
164-
go func(ctx context.Context, check Check, j int) {
197+
go func(
198+
ctx context.Context,
199+
check Check,
200+
j int,
201+
) {
165202
defer checksWG.Done()
166203
defer func() {
167204
if r := recover(); r != nil {
@@ -200,6 +237,7 @@ func run(ctx context.Context,
200237
ctx,
201238
client,
202239
cluster,
240+
skipEvaluator,
203241
checker,
204242
i,
205243
)

0 commit comments

Comments
 (0)