Skip to content

Commit 4bfce0c

Browse files
committed
fix: Move preflight skip annotation constants to api module
1 parent 255a03e commit 4bfce0c

File tree

4 files changed

+32
-30
lines changed

4 files changed

+32
-30
lines changed

api/v1alpha1/constants.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,11 @@ const (
4040
DNSVariableName = "dns"
4141

4242
ClusterUUIDAnnotationKey = APIGroup + "/cluster-uuid"
43+
44+
// PreflightChecksSkipAnnotationKey is the key of the annotation on the Cluster used to skip preflight checks.
45+
PreflightChecksSkipAnnotationKey = "preflight.cluster.caren.nutanix.com/skip"
46+
47+
// PreflightChecksSkipAllAnnotationValue is the value used in the cluster's annotations to indicate
48+
// that all checks are skipped.
49+
PreflightChecksSkipAllAnnotationValue = "all"
4350
)

pkg/webhook/preflight/preflight_test.go

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

27+
carenv1 "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
2728
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/webhook/preflight/skip"
2829
)
2930

@@ -68,7 +69,7 @@ func topologyCluster(skippedCheckNames ...string) *clusterv1.Cluster {
6869
ObjectMeta: metav1.ObjectMeta{
6970
Name: "test-cluster",
7071
Annotations: map[string]string{
71-
skip.AnnotationKey: strings.Join(skippedCheckNames, ","),
72+
carenv1.PreflightChecksSkipAnnotationKey: strings.Join(skippedCheckNames, ","),
7273
},
7374
},
7475
Spec: clusterv1.ClusterSpec{
@@ -147,7 +148,7 @@ func TestHandle(t *testing.T) {
147148
},
148149
{
149150
name: "if cluster skips all checks, then allowed, with a warning",
150-
cluster: topologyCluster(skip.SkipAllChecksAnnotationValue),
151+
cluster: topologyCluster(carenv1.PreflightChecksSkipAllAnnotationValue),
151152
checkers: []Checker{
152153
&mockChecker{
153154
checks: []Check{},

pkg/webhook/preflight/skip/skip.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,10 @@ package skip
55
import (
66
"strings"
77

8+
carenv1 "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
89
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
910
)
1011

11-
const (
12-
// AnnotationKey is the key of the annotation on the Cluster used to skip preflight checks.
13-
AnnotationKey = "preflight.cluster.caren.nutanix.com/skip"
14-
15-
// SkipAllChecksAnnotationValue is the value used in the cluster's annotations to indicate
16-
// that all checks are skipped.
17-
SkipAllChecksAnnotationValue = "all"
18-
)
19-
2012
// Evaluator is used to determine which checks should be skipped, based on the cluster's annotations.
2113
type Evaluator struct {
2214
normalizedCheckNames map[string]struct{}
@@ -35,7 +27,7 @@ func New(cluster *clusterv1.Cluster) *Evaluator {
3527
return o
3628
}
3729

38-
value, exists := annotations[AnnotationKey]
30+
value, exists := annotations[carenv1.PreflightChecksSkipAnnotationKey]
3931
if !exists {
4032
// If the annotation does not exist, return an Evaluator with no prefixes.
4133
return o
@@ -49,7 +41,8 @@ func New(cluster *clusterv1.Cluster) *Evaluator {
4941
normalizedCheckName := strings.TrimSpace(strings.ToLower(checkName))
5042
o.normalizedCheckNames[normalizedCheckName] = struct{}{}
5143
}
52-
if _, exists := o.normalizedCheckNames[SkipAllChecksAnnotationValue]; exists && len(o.normalizedCheckNames) == 1 {
44+
if _, exists := o.normalizedCheckNames[carenv1.PreflightChecksSkipAllAnnotationValue]; exists &&
45+
len(o.normalizedCheckNames) == 1 {
5346
o.all = true
5447
}
5548
return o

pkg/webhook/preflight/skip/skip_test.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package skip
66
import (
77
"testing"
88

9+
carenv1 "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
910
"github.com/stretchr/testify/assert"
1011
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1112
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
@@ -37,14 +38,14 @@ func TestNew(t *testing.T) {
3738
{
3839
name: "ignores empty skip annotation value",
3940
annotations: map[string]string{
40-
AnnotationKey: "",
41+
carenv1.PreflightChecksSkipAnnotationKey: "",
4142
},
4243
expectCheckNames: map[string]struct{}{},
4344
},
4445
{
4546
name: "ignores empty check names",
4647
annotations: map[string]string{
47-
AnnotationKey: "InfraVMImage,,InfraCredentials,",
48+
carenv1.PreflightChecksSkipAnnotationKey: "InfraVMImage,,InfraCredentials,",
4849
},
4950
expectCheckNames: map[string]struct{}{
5051
"infravmimage": {},
@@ -54,14 +55,14 @@ func TestNew(t *testing.T) {
5455
{
5556
name: "ignores value of only commas",
5657
annotations: map[string]string{
57-
AnnotationKey: ",,,",
58+
carenv1.PreflightChecksSkipAnnotationKey: ",,,",
5859
},
5960
expectCheckNames: map[string]struct{}{},
6061
},
6162
{
6263
name: "accepts single check name",
6364
annotations: map[string]string{
64-
AnnotationKey: "InfraVMImage",
65+
carenv1.PreflightChecksSkipAnnotationKey: "InfraVMImage",
6566
},
6667
expectCheckNames: map[string]struct{}{
6768
"infravmimage": {},
@@ -70,7 +71,7 @@ func TestNew(t *testing.T) {
7071
{
7172
name: "accepts multiple check names",
7273
annotations: map[string]string{
73-
AnnotationKey: "InfraVMImage,InfraCredentials",
74+
carenv1.PreflightChecksSkipAnnotationKey: "InfraVMImage,InfraCredentials",
7475
},
7576
expectCheckNames: map[string]struct{}{
7677
"infravmimage": {},
@@ -80,7 +81,7 @@ func TestNew(t *testing.T) {
8081
{
8182
name: "trims spaces from check names",
8283
annotations: map[string]string{
83-
AnnotationKey: " InfraVMImage , InfraCredentials ",
84+
carenv1.PreflightChecksSkipAnnotationKey: " InfraVMImage , InfraCredentials ",
8485
},
8586
expectCheckNames: map[string]struct{}{
8687
"infravmimage": {},
@@ -125,47 +126,47 @@ func TestEvaluator_For(t *testing.T) {
125126
{
126127
name: "empty annotation value",
127128
annotations: map[string]string{
128-
AnnotationKey: "",
129+
carenv1.PreflightChecksSkipAnnotationKey: "",
129130
},
130131
checkName: "InfraVMImage",
131132
expectMatch: false,
132133
},
133134
{
134135
name: "skip InfraVMImage check",
135136
annotations: map[string]string{
136-
AnnotationKey: "InfraVMImage,InfraCredentials",
137+
carenv1.PreflightChecksSkipAnnotationKey: "InfraVMImage,InfraCredentials",
137138
},
138139
checkName: "InfraVMImage",
139140
expectMatch: true,
140141
},
141142
{
142143
name: "skip credentials, but not image",
143144
annotations: map[string]string{
144-
AnnotationKey: "InfraCredentials",
145+
carenv1.PreflightChecksSkipAnnotationKey: "InfraCredentials",
145146
},
146147
checkName: "InfraVMImage",
147148
expectMatch: false,
148149
},
149150
{
150151
name: "skip with spaces and case mixing",
151152
annotations: map[string]string{
152-
AnnotationKey: " infraVMImage , InfraCredentials ",
153+
carenv1.PreflightChecksSkipAnnotationKey: " infraVMImage , InfraCredentials ",
153154
},
154155
checkName: "InfraVMImage",
155156
expectMatch: true,
156157
},
157158
{
158159
name: "extra commas do not affect matching",
159160
annotations: map[string]string{
160-
AnnotationKey: "InfraVMImage,,InfraCredentials,",
161+
carenv1.PreflightChecksSkipAnnotationKey: "InfraVMImage,,InfraCredentials,",
161162
},
162163
checkName: "InfraVMImage",
163164
expectMatch: true,
164165
},
165166
{
166167
name: "skip all checks",
167168
annotations: map[string]string{
168-
AnnotationKey: "all",
169+
carenv1.PreflightChecksSkipAnnotationKey: "all",
169170
},
170171
checkName: "InfraVMImage",
171172
expectMatch: true,
@@ -206,34 +207,34 @@ func TestEvaluator_ForAll(t *testing.T) {
206207
{
207208
name: "empty annotation value",
208209
annotations: map[string]string{
209-
AnnotationKey: "",
210+
carenv1.PreflightChecksSkipAnnotationKey: "",
210211
},
211212
},
212213
{
213214
name: "skip all checks with spaces and case mixing",
214215
annotations: map[string]string{
215-
AnnotationKey: " aLL ",
216+
carenv1.PreflightChecksSkipAnnotationKey: " aLL ",
216217
},
217218
expectMatch: true,
218219
},
219220
{
220221
name: "skip all checks with extra commas",
221222
annotations: map[string]string{
222-
AnnotationKey: ",all,,",
223+
carenv1.PreflightChecksSkipAnnotationKey: ",all,,",
223224
},
224225
expectMatch: true,
225226
},
226227
{
227228
name: "skip all checks",
228229
annotations: map[string]string{
229-
AnnotationKey: "all",
230+
carenv1.PreflightChecksSkipAnnotationKey: "all",
230231
},
231232
expectMatch: true,
232233
},
233234
{
234235
name: "skip some checks, but not all",
235236
annotations: map[string]string{
236-
AnnotationKey: "OneCheck,AnotherCheck",
237+
carenv1.PreflightChecksSkipAnnotationKey: "OneCheck,AnotherCheck",
237238
},
238239
expectMatch: false,
239240
},

0 commit comments

Comments
 (0)