Skip to content

Commit 62ea6cf

Browse files
committed
fixup! feat: Preflight check opt-out
Use 'skip' instead of 'opt-out' to be consistent with similar usage in core Cluster API
1 parent 0e36260 commit 62ea6cf

File tree

4 files changed

+58
-58
lines changed

4 files changed

+58
-58
lines changed

pkg/webhook/preflight/preflight.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
1717
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
1818

19-
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/webhook/preflight/optout"
19+
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/webhook/preflight/skip"
2020
)
2121

2222
type (
@@ -95,16 +95,16 @@ func (h *WebhookHandler) Handle(ctx context.Context, req admission.Request) admi
9595
return admission.Allowed("")
9696
}
9797

98-
optoutEvaluator := optout.New(cluster)
98+
skipEvaluator := skip.New(cluster)
9999

100-
if optoutEvaluator.ForAll() {
101-
// If the cluster has opted out of all checks, return allowed.
100+
if skipEvaluator.ForAll() {
101+
// If the cluster has skipped all checks, return allowed.
102102
return admission.Allowed("").WithWarnings(
103-
"Cluster has opted out of all preflight checks",
103+
"Cluster has skipped all preflight checks",
104104
)
105105
}
106106

107-
resultsOrderedByCheckerAndCheck := run(ctx, h.client, cluster, optoutEvaluator, h.checkers)
107+
resultsOrderedByCheckerAndCheck := run(ctx, h.client, cluster, skipEvaluator, h.checkers)
108108

109109
// Summarize the results.
110110
resp := admission.Response{
@@ -156,7 +156,7 @@ func (h *WebhookHandler) Handle(ctx context.Context, req admission.Request) admi
156156
func run(ctx context.Context,
157157
client ctrlclient.Client,
158158
cluster *clusterv1.Cluster,
159-
optoutEvaluator *optout.Evaluator,
159+
skipEvaluator *skip.Evaluator,
160160
checkers []Checker,
161161
) [][]namedResult {
162162
resultsOrderedByCheckerAndCheck := make([][]namedResult, len(checkers))
@@ -168,7 +168,7 @@ func run(ctx context.Context,
168168
ctx context.Context,
169169
client ctrlclient.Client,
170170
cluster *clusterv1.Cluster,
171-
optoutEvaluator *optout.Evaluator,
171+
skipEvaluator *skip.Evaluator,
172172
checker Checker,
173173
i int,
174174
) {
@@ -179,16 +179,15 @@ func run(ctx context.Context,
179179

180180
checksWG := sync.WaitGroup{}
181181
for j, check := range checks {
182-
if optoutEvaluator.For(check.Name()) {
183-
// If the cluster has opted out of this check, skip it.
182+
if skipEvaluator.For(check.Name()) {
184183
resultsOrderedByCheck[j] = namedResult{
185184
Name: check.Name(),
186185
CheckResult: CheckResult{
187186
Allowed: true,
188187
Error: false,
189188
Causes: nil,
190189
Warnings: []string{
191-
fmt.Sprintf("Cluster has opted out of preflight check %q", check.Name()),
190+
fmt.Sprintf("Cluster has skipped preflight check %q", check.Name()),
192191
},
193192
},
194193
}
@@ -238,7 +237,7 @@ func run(ctx context.Context,
238237
ctx,
239238
client,
240239
cluster,
241-
optoutEvaluator,
240+
skipEvaluator,
242241
checker,
243242
i,
244243
)

pkg/webhook/preflight/preflight_test.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
"sigs.k8s.io/controller-runtime/pkg/client/fake"
2525
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
2626

27-
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/webhook/preflight/optout"
27+
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/webhook/preflight/skip"
2828
)
2929

3030
type mockChecker struct {
@@ -63,12 +63,12 @@ func (m *mockDecoder) DecodeRaw(_ runtime.RawExtension, _ runtime.Object) error
6363
return m.err
6464
}
6565

66-
func topologyCluster(optoutCheckNames ...string) *clusterv1.Cluster {
66+
func topologyCluster(skippedCheckNames ...string) *clusterv1.Cluster {
6767
return &clusterv1.Cluster{
6868
ObjectMeta: metav1.ObjectMeta{
6969
Name: "test-cluster",
7070
Annotations: map[string]string{
71-
optout.AnnotationKey: strings.Join(optoutCheckNames, ","),
71+
skip.AnnotationKey: strings.Join(skippedCheckNames, ","),
7272
},
7373
},
7474
Spec: clusterv1.ClusterSpec{
@@ -146,8 +146,8 @@ func TestHandle(t *testing.T) {
146146
},
147147
},
148148
{
149-
name: "if cluster opts out of all checks, then allowed, with a warning",
150-
cluster: topologyCluster(optout.OptOutAllChecksAnnotationValue),
149+
name: "if cluster skips all checks, then allowed, with a warning",
150+
cluster: topologyCluster(skip.SkipAllChecksAnnotationValue),
151151
checkers: []Checker{
152152
&mockChecker{
153153
checks: []Check{},
@@ -157,19 +157,19 @@ func TestHandle(t *testing.T) {
157157
AdmissionResponse: admissionv1.AdmissionResponse{
158158
Allowed: true,
159159
Warnings: []string{
160-
"Cluster has opted out of all preflight checks",
160+
"Cluster has skipped all preflight checks",
161161
},
162162
},
163163
},
164164
},
165165
{
166-
name: "if cluster opts out of a check, then that check is not run",
167-
cluster: topologyCluster("OptOutCheck"),
166+
name: "if cluster skips a check, then that check is not run",
167+
cluster: topologyCluster("SkippedCheck"),
168168
checkers: []Checker{
169169
&mockChecker{
170170
checks: []Check{
171171
&mockCheck{
172-
name: "OptOutCheck",
172+
name: "SkippedCheck",
173173
result: CheckResult{},
174174
},
175175
&mockCheck{
@@ -185,7 +185,7 @@ func TestHandle(t *testing.T) {
185185
AdmissionResponse: admissionv1.AdmissionResponse{
186186
Allowed: true,
187187
Warnings: []string{
188-
"Cluster has opted out of preflight check \"OptOutCheck\"",
188+
"Cluster has skipped preflight check \"SkippedCheck\"",
189189
},
190190
},
191191
},
@@ -550,7 +550,7 @@ func TestRun_SingleCheckerSingleCheck(t *testing.T) {
550550
},
551551
},
552552
}
553-
resultsOrderedByCheckerAndCheck := run(ctx, nil, cluster, optout.New(cluster), []Checker{checker})
553+
resultsOrderedByCheckerAndCheck := run(ctx, nil, cluster, skip.New(cluster), []Checker{checker})
554554
if len(resultsOrderedByCheckerAndCheck) != 1 {
555555
t.Fatalf("expected results for 1 checker, got %d", len(resultsOrderedByCheckerAndCheck))
556556
}
@@ -590,7 +590,7 @@ func TestRun_MultipleCheckersMultipleChecks(t *testing.T) {
590590
},
591591
}
592592

593-
resultsOrderedByCheckerAndCheck := run(ctx, nil, cluster, optout.New(cluster), []Checker{checker1, checker2})
593+
resultsOrderedByCheckerAndCheck := run(ctx, nil, cluster, skip.New(cluster), []Checker{checker1, checker2})
594594
if len(resultsOrderedByCheckerAndCheck) != 2 {
595595
t.Fatalf("expected results for 2 checkers, got %d", len(resultsOrderedByCheckerAndCheck))
596596
}
@@ -654,7 +654,7 @@ func TestRun_ChecksRunInParallel(t *testing.T) {
654654
},
655655
},
656656
}
657-
resultsOrderedByCheckerAndCheck := run(ctx, nil, cluster, optout.New(cluster), []Checker{checker})
657+
resultsOrderedByCheckerAndCheck := run(ctx, nil, cluster, skip.New(cluster), []Checker{checker})
658658

659659
results := resultsOrderedByCheckerAndCheck[0]
660660
if len(results) != 2 {
@@ -693,7 +693,7 @@ func TestRun_CheckersRunInParallel(t *testing.T) {
693693
},
694694
}
695695

696-
results := run(ctx, nil, cluster, optout.New(cluster), []Checker{checker1, checker2})
696+
results := run(ctx, nil, cluster, skip.New(cluster), []Checker{checker1, checker2})
697697
if len(results) != 2 {
698698
t.Fatalf("expected 2 results, got %d", len(results))
699699
}
@@ -750,7 +750,7 @@ func TestRun_ContextCancellation(t *testing.T) {
750750
cancel()
751751
}()
752752

753-
resultsOrderedByCheckerAndCheck := run(ctx, nil, cluster, optout.New(cluster), []Checker{checker})
753+
resultsOrderedByCheckerAndCheck := run(ctx, nil, cluster, skip.New(cluster), []Checker{checker})
754754

755755
select {
756756
case <-completed:
@@ -798,7 +798,7 @@ func TestRun_OrderOfResults(t *testing.T) {
798798
},
799799
}
800800

801-
resultsOrderedByCheckerAndCheck := run(ctx, nil, cluster, optout.New(cluster), []Checker{checker1, checker2})
801+
resultsOrderedByCheckerAndCheck := run(ctx, nil, cluster, skip.New(cluster), []Checker{checker1, checker2})
802802
if len(resultsOrderedByCheckerAndCheck) != 2 {
803803
t.Fatalf("expected results for 2 checkers, got %d", len(resultsOrderedByCheckerAndCheck))
804804
}
@@ -843,7 +843,7 @@ func TestRun_LargeNumberOfCheckersAndChecks(t *testing.T) {
843843
}
844844

845845
start := time.Now()
846-
resultsOrderedByCheckerAndCheck := run(ctx, nil, cluster, optout.New(cluster), checkers)
846+
resultsOrderedByCheckerAndCheck := run(ctx, nil, cluster, skip.New(cluster), checkers)
847847
duration := time.Since(start)
848848

849849
resultTotal := 0
@@ -878,7 +878,7 @@ func TestRun_ErrorHandlingInChecks(t *testing.T) {
878878
}
879879

880880
// Run the checks
881-
resultsOrderedByCheckerAndCheck := run(ctx, nil, cluster, optout.New(cluster), []Checker{checker})
881+
resultsOrderedByCheckerAndCheck := run(ctx, nil, cluster, skip.New(cluster), []Checker{checker})
882882
assert.Len(t, resultsOrderedByCheckerAndCheck, 1, "expected results for 1 checker")
883883
assert.Len(t, resultsOrderedByCheckerAndCheck[0], 1, "expected 1 result from the checker")
884884

@@ -933,7 +933,7 @@ func TestRun_PanicHandlingInChecks(t *testing.T) {
933933
}
934934

935935
// Run the checks
936-
resultsOrderedByCheckerAndCheck := run(ctx, nil, cluster, optout.New(cluster), []Checker{checker})
936+
resultsOrderedByCheckerAndCheck := run(ctx, nil, cluster, skip.New(cluster), []Checker{checker})
937937
assert.Len(t, resultsOrderedByCheckerAndCheck, 1, "expected results for 1 checker")
938938
assert.Len(t, resultsOrderedByCheckerAndCheck[0], 2, "expected 2 results from the checker")
939939

@@ -980,7 +980,7 @@ func TestRun_ZeroChecksFromChecker(t *testing.T) {
980980
},
981981
}
982982

983-
resultsOrderedByCheckerAndCheck := run(ctx, nil, cluster, optout.New(cluster), []Checker{emptyChecker, normalChecker})
983+
resultsOrderedByCheckerAndCheck := run(ctx, nil, cluster, skip.New(cluster), []Checker{emptyChecker, normalChecker})
984984

985985
if len(resultsOrderedByCheckerAndCheck) != 2 {
986986
t.Fatalf("expected results for 2 checkers, got %d", len(resultsOrderedByCheckerAndCheck))

pkg/webhook/preflight/optout/optout.go renamed to pkg/webhook/preflight/skip/skip.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright 2025 Nutanix. All rights reserved.
22
// SPDX-License-Identifier: Apache-2.0
3-
package optout
3+
package skip
44

55
import (
66
"strings"
@@ -9,14 +9,15 @@ import (
99
)
1010

1111
const (
12-
// AnnotationKey is the key of the annotation on the Cluster used to opt out preflight checks.
13-
AnnotationKey = "preflight.cluster.caren.nutanix.com/opt-out"
12+
// AnnotationKey is the key of the annotation on the Cluster used to skip preflight checks.
13+
AnnotationKey = "preflight.cluster.caren.nutanix.com/skip"
1414

15-
// OptOutAllChecksAnnotationValue is the value used in the cluster's annotations to indicate
16-
// that all checks are opted out.
17-
OptOutAllChecksAnnotationValue = "all"
15+
// SkipAllChecksAnnotationValue is the value used in the cluster's annotations to indicate
16+
// that all checks are skipped.
17+
SkipAllChecksAnnotationValue = "all"
1818
)
1919

20+
// Evaluator is used to determine which checks should be skipped, based on the cluster's annotations.
2021
type Evaluator struct {
2122
normalizedCheckNames map[string]struct{}
2223
all bool
@@ -48,32 +49,32 @@ func New(cluster *clusterv1.Cluster) *Evaluator {
4849
normalizedCheckName := strings.TrimSpace(strings.ToLower(checkName))
4950
o.normalizedCheckNames[normalizedCheckName] = struct{}{}
5051
}
51-
if _, exists := o.normalizedCheckNames[OptOutAllChecksAnnotationValue]; exists && len(o.normalizedCheckNames) == 1 {
52+
if _, exists := o.normalizedCheckNames[SkipAllChecksAnnotationValue]; exists && len(o.normalizedCheckNames) == 1 {
5253
o.all = true
5354
}
5455
return o
5556
}
5657

57-
// For checks if the cluster has opted out of a specific check.
58-
// It returns true if the cluster has opted out of the check with the given name.
58+
// For checks if the specific check should be skipped.
59+
// It returns true if the cluster skip annotation contains the check name.
5960
// The check name is case-insensitive, so "CheckName1" and "checkname1" will both match.
60-
// If the cluster has opted out of all checks, For will return true for any check name.
61+
// If the cluster has skipped all checks, For will return true for any check name.
6162
//
62-
// For example, if the cluster has opted out of "CheckName1", then calling
63+
// For example, if the cluster has skipped "CheckName1", then calling
6364
// For("CheckName1") or For("checkname1") will return true, but For("CheckName2") will
6465
// return false.
6566
func (o *Evaluator) For(checkName string) bool {
6667
if o.all {
67-
// If the cluster has opted out of all checks, return true for any check name.
68+
// If the cluster has skipped all checks, return true for any check name.
6869
return true
6970
}
7071
normalizedCheckName := strings.TrimSpace(strings.ToLower(checkName))
7172
_, exists := o.normalizedCheckNames[normalizedCheckName]
7273
return exists
7374
}
7475

75-
// ForAll checks if the cluster has opted out of all checks.
76-
// It returns true if the cluster has a single prefix "all" in its opt-out annotations.
76+
// ForAll checks if all checks should be skipped.
77+
// It returns true if the cluster skip annotation contains "all".
7778
// The check is case-insensitive, so "all", "ALL", and "All" will all match.
7879
func (o *Evaluator) ForAll() bool {
7980
return o.all

0 commit comments

Comments
 (0)