Skip to content

Commit 47c6ad8

Browse files
committed
fixup! feat: Add preflight checks framework
Each check returns list of causes
1 parent 690a89e commit 47c6ad8

File tree

2 files changed

+40
-25
lines changed

2 files changed

+40
-25
lines changed

pkg/webhook/preflight/preflight.go

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ import (
1616

1717
type CheckResult struct {
1818
Allowed bool
19-
Field string
20-
Message string
21-
Warning string
2219
Error bool
20+
21+
Causes []metav1.StatusCause
22+
Warnings []string
2323
}
2424

2525
type Check = func(ctx context.Context) CheckResult
@@ -121,28 +121,15 @@ func (h *WebhookHandler) Handle(ctx context.Context, req admission.Request) admi
121121
internalError := false
122122
for result := range resultCh {
123123
if result.Error {
124-
resp.Allowed = false
125-
resp.Result.Details.Causes = append(resp.Result.Details.Causes, metav1.StatusCause{
126-
Type: metav1.CauseTypeInternal,
127-
Field: result.Field,
128-
Message: result.Message,
129-
})
130124
internalError = true
131-
continue
132125
}
133126

134127
if !result.Allowed {
135128
resp.Allowed = false
136-
resp.Result.Details.Causes = append(resp.Result.Details.Causes, metav1.StatusCause{
137-
Type: metav1.CauseTypeFieldValueInvalid,
138-
Field: result.Field,
139-
Message: result.Message,
140-
})
141129
}
142130

143-
if result.Warning != "" {
144-
resp.Warnings = append(resp.Warnings, result.Warning)
145-
}
131+
resp.Result.Details.Causes = append(resp.Result.Details.Causes, result.Causes...)
132+
resp.Warnings = append(resp.Warnings, result.Warnings...)
146133
}
147134

148135
if len(resp.Result.Details.Causes) == 0 {

pkg/webhook/preflight/preflight_test.go

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,13 @@ func TestHandle(t *testing.T) {
194194
func(ctx context.Context) CheckResult {
195195
return CheckResult{
196196
Allowed: false,
197-
Field: "spec.test",
198-
Message: "test failed",
197+
Causes: []metav1.StatusCause{
198+
{
199+
Type: metav1.CauseTypeFieldValueInvalid,
200+
Field: "spec.test",
201+
Message: "test failed",
202+
},
203+
},
199204
}
200205
},
201206
},
@@ -239,7 +244,9 @@ func TestHandle(t *testing.T) {
239244
func(ctx context.Context) CheckResult {
240245
return CheckResult{
241246
Allowed: true,
242-
Warning: "test warning",
247+
Warnings: []string{
248+
"test warning",
249+
},
243250
}
244251
},
245252
},
@@ -275,7 +282,13 @@ func TestHandle(t *testing.T) {
275282
return CheckResult{
276283
Allowed: false,
277284
Error: true,
278-
Message: "internal error",
285+
286+
Causes: []metav1.StatusCause{
287+
{
288+
Type: metav1.CauseTypeInternal,
289+
Message: "internal error",
290+
},
291+
},
279292
}
280293
},
281294
},
@@ -285,7 +298,12 @@ func TestHandle(t *testing.T) {
285298
func(ctx context.Context) CheckResult {
286299
return CheckResult{
287300
Allowed: false,
288-
Message: "check failed",
301+
Causes: []metav1.StatusCause{
302+
{
303+
Type: metav1.CauseTypeFieldValueInvalid,
304+
Message: "check failed",
305+
},
306+
},
289307
}
290308
},
291309
},
@@ -401,7 +419,12 @@ func TestHandleCancelledContext(t *testing.T) {
401419
return CheckResult{
402420
Allowed: false,
403421
Error: true,
404-
Message: "context cancelled",
422+
Causes: []metav1.StatusCause{
423+
{
424+
Type: metav1.CauseTypeInternal,
425+
Message: "context cancelled",
426+
},
427+
},
405428
}
406429
case <-time.After(100 * time.Millisecond):
407430
return CheckResult{Allowed: true}
@@ -413,7 +436,12 @@ func TestHandleCancelledContext(t *testing.T) {
413436
return CheckResult{
414437
Allowed: false,
415438
Error: true,
416-
Message: "context cancelled",
439+
Causes: []metav1.StatusCause{
440+
{
441+
Type: metav1.CauseTypeInternal,
442+
Message: "context cancelled",
443+
},
444+
},
417445
}
418446
case <-time.After(100 * time.Millisecond):
419447
return CheckResult{Allowed: true}

0 commit comments

Comments
 (0)