From 9d134da58269a8bf895b67e12ecd7729b2f7668b Mon Sep 17 00:00:00 2001 From: Howard Lau Date: Sun, 1 Nov 2020 08:21:01 +0000 Subject: [PATCH] Aggregate errors Signed-off-by: Howard Lau --- api/v1/seaweed_webhook.go | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/api/v1/seaweed_webhook.go b/api/v1/seaweed_webhook.go index 667dca0..9bd9130 100644 --- a/api/v1/seaweed_webhook.go +++ b/api/v1/seaweed_webhook.go @@ -19,9 +19,10 @@ package v1 import ( "fmt" - v1 "k8s.io/api/core/v1" + corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/util/errors" ctrl "sigs.k8s.io/controller-runtime" logf "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/webhook" @@ -57,21 +58,22 @@ var _ webhook.Validator = &Seaweed{} // ValidateCreate implements webhook.Validator so a webhook will be registered for the type func (r *Seaweed) ValidateCreate() error { seaweedlog.Info("validate create", "name", r.Name) + errs := []error{} // TODO(user): fill in your validation logic upon object creation. if r.Spec.Master == nil { - return fmt.Errorf("seaweed[%s/%s] must have master spec", r.Namespace, r.Name) + errs = append(errs, fmt.Errorf("seaweed[%s/%s] must have master spec", r.Namespace, r.Name)) } if r.Spec.Volume == nil { - return fmt.Errorf("seaweed[%s/%s] must have volume spec", r.Namespace, r.Name) + errs = append(errs, fmt.Errorf("seaweed[%s/%s] must have volume spec", r.Namespace, r.Name)) + } else { + if r.Spec.Volume.Requests[corev1.ResourceStorage].Equal(resource.MustParse("0")) { + errs = append(errs, fmt.Errorf("seaweed[%s/%s] volume storage request cannot be zero", r.Namespace, r.Name)) + } } - if r.Spec.Volume.Requests[v1.ResourceStorage].Equal(resource.MustParse("0")) { - return fmt.Errorf("seaweed[%s/%s] volume storage request cannot be zero", r.Namespace, r.Name) - } - - return nil + return errors.NewAggregate(errs) } // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type