Aggregate errors

Signed-off-by: Howard Lau <howardlau1999@hotmail.com>
This commit is contained in:
Howard Lau 2020-11-01 08:21:01 +00:00
parent f0d0622b15
commit 9d134da582
No known key found for this signature in database
GPG Key ID: 5CCC55849E3CF8E2
1 changed files with 10 additions and 8 deletions

View File

@ -19,9 +19,10 @@ package v1
import ( import (
"fmt" "fmt"
v1 "k8s.io/api/core/v1" corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource" "k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/errors"
ctrl "sigs.k8s.io/controller-runtime" ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log" logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook" "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 // ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (r *Seaweed) ValidateCreate() error { func (r *Seaweed) ValidateCreate() error {
seaweedlog.Info("validate create", "name", r.Name) seaweedlog.Info("validate create", "name", r.Name)
errs := []error{}
// TODO(user): fill in your validation logic upon object creation. // TODO(user): fill in your validation logic upon object creation.
if r.Spec.Master == nil { 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 { 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 errors.NewAggregate(errs)
return fmt.Errorf("seaweed[%s/%s] volume storage request cannot be zero", r.Namespace, r.Name)
}
return nil
} }
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type