Skip to content

Commit 5291538

Browse files
committed
Unify webhook error messages
This patch unifies the error messages returned by the test operator webhooks. Previously, some error messages were returned from the validateCreate as a plain errors. This made the error messages unreadable. This patch returns all the errors in field.ErrorList. Each error is properly categorized with Type and Detail err message.
1 parent 1d06a39 commit 5291538

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

api/v1beta1/common_webhook.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ const (
44
// ErrPrivilegedModeRequired
55
ErrPrivilegedModeRequired = "%s.Spec.Privileged is requied in order to successfully " +
66
"execute tests with the provided configuration."
7+
8+
// ErrDebug
9+
ErrDebug = "%s.Spec.Workflow parameter must be empty to run debug mode"
710
)
811

912
const (

api/v1beta1/tempest_webhook.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,11 @@ func (r *Tempest) ValidateCreate() (admission.Warnings, error) {
9292
var allWarnings admission.Warnings
9393

9494
if len(r.Spec.Workflow) > 0 && r.Spec.Debug {
95-
return nil, errors.New("workflow variable must be empty to run debug mode")
95+
allErrs = append(allErrs, &field.Error{
96+
Type: field.ErrorTypeForbidden,
97+
BadValue: r.Spec.Workflow,
98+
Detail: fmt.Sprintf(ErrDebug, "Tempest"),
99+
})
96100
}
97101

98102
if !r.Spec.Privileged && r.PrivilegedRequired() {

api/v1beta1/tobiko_webhook.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ package v1beta1
2525
import (
2626
"fmt"
2727

28-
"errors"
28+
apierrors "k8s.io/apimachinery/pkg/api/errors"
2929
"k8s.io/apimachinery/pkg/runtime"
30+
"k8s.io/apimachinery/pkg/runtime/schema"
31+
"k8s.io/apimachinery/pkg/util/validation/field"
3032
ctrl "sigs.k8s.io/controller-runtime"
3133
logf "sigs.k8s.io/controller-runtime/pkg/log"
3234
"sigs.k8s.io/controller-runtime/pkg/webhook"
@@ -62,16 +64,29 @@ var _ webhook.Validator = &Tobiko{}
6264
func (r *Tobiko) ValidateCreate() (admission.Warnings, error) {
6365
tobikolog.Info("validate create", "name", r.Name)
6466

67+
var allErrs field.ErrorList
6568
var allWarnings admission.Warnings
6669

70+
if len(r.Spec.Workflow) > 0 && r.Spec.Debug {
71+
allErrs = append(allErrs, &field.Error{
72+
Type: field.ErrorTypeForbidden,
73+
BadValue: r.Spec.Workflow,
74+
Detail: fmt.Sprintf(ErrDebug, "Tobiko"),
75+
})
76+
}
77+
6778
if r.Spec.Privileged {
6879
allWarnings = append(allWarnings, fmt.Sprintf(WarnPrivilegedModeOn, "Tobiko"))
6980
} else {
7081
allWarnings = append(allWarnings, fmt.Sprintf(WarnPrivilegedModeOff, "Tobiko"))
7182
}
7283

73-
if len(r.Spec.Workflow) > 0 && r.Spec.Debug {
74-
return allWarnings, errors.New("workflow variable must be empty to run debug mode")
84+
if len(allErrs) > 0 {
85+
return allWarnings, apierrors.NewInvalid(
86+
schema.GroupKind{
87+
Group: GroupVersion.WithKind("Tobiko").Group,
88+
Kind: GroupVersion.WithKind("Tobiko").Kind,
89+
}, r.GetName(), allErrs)
7590
}
7691

7792
return allWarnings, nil

0 commit comments

Comments
 (0)