From 5a472d2e189eb420f7d89a6ea724942c63999396 Mon Sep 17 00:00:00 2001 From: Alfredo Garo <44888596+garomonegro@users.noreply.github.com> Date: Tue, 25 Jan 2022 12:08:38 -0500 Subject: [PATCH] fix: ValidationError restructure and readme (#13) Signed-off-by: Alfredo Garo <44888596+garomonegro@users.noreply.github.com> --- README.md | 21 +++++++++++++++++++++ pkg/client/types.go | 13 ++++++++----- pkg/client/validator.go | 7 ++++--- 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 1625eb1..d33cdb4 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,7 @@ INFO[0007] ✅ resource 'nodes' validated successfully ```golang import ( validator "github.com/keikoproj/cluster-validator/pkg/client" + "k8s.io/client-go/dynamic" ) func validate(client dynamic.Interface) error { @@ -111,5 +112,25 @@ func validate(client dynamic.Interface) error { return err } } +``` + +The `error` returned by `Validate()` has structured data with information on the failed validation: + +``` golang +v := validator.NewValidator(client, spec) +err := v.Validate() +if vErr, ok := err.(*validator.ValidationError); ok { + + fmt.Printf("Validation failed for %s/%s/%s.\n", + vErr.GVR.Group, vErr.GVR.Version, vErr.GVR.Resource) + for _, cVal := range vErr.ConditionValidations { + + fmt.Printf("Failed Condition: %s\n", cVal.Condition) + + for msg, resources := range cVal.ResourceErrors { + fmt.Printf("Reason: %s.\nResources: %v", msg, resources) + } + } +} ``` \ No newline at end of file diff --git a/pkg/client/types.go b/pkg/client/types.go index 2771a10..63bd388 100644 --- a/pkg/client/types.go +++ b/pkg/client/types.go @@ -67,7 +67,6 @@ func NewFieldValidationResult(path string) FieldValidationResult { } type ValidationSummary struct { - GVR schema.GroupVersionResource FieldValidation []FieldValidationResult ConditionValidation []ConditionValidationResult } @@ -113,11 +112,15 @@ func NewValidator(c dynamic.Interface, m *v1alpha1.ClusterValidation) *Validator } type ValidationError struct { - Message error - Summary ValidationSummary + Message error + GVR schema.GroupVersionResource + FieldValidations []FieldValidationResult + ConditionValidations []ConditionValidationResult } func (e *ValidationError) Error() string { - prettySummary, _ := json.MarshalIndent(e.Summary, "", "\t") - return fmt.Sprintf("%v. \n%s", e.Message, string(prettySummary)) + fieldValidationResult, _ := json.MarshalIndent(e.FieldValidations, "", "\t") + conditionValidationResult, _ := json.MarshalIndent(e.ConditionValidations, "", "\t") + return fmt.Sprintf("%v.\nGVR: %s/%s/%s.\nField Validation Results: %s\nCondition Validation Results: %s", e.Message, + e.GVR.Group, e.GVR.Version, e.GVR.Resource, string(fieldValidationResult), string(conditionValidationResult)) } diff --git a/pkg/client/validator.go b/pkg/client/validator.go index 43fa0bd..d7dff41 100644 --- a/pkg/client/validator.go +++ b/pkg/client/validator.go @@ -84,8 +84,10 @@ func (v *Validator) Validate() error { } if r.Required { v.Waiter.errors <- &ValidationError{ - Message: errors.Errorf("failure threshold met for resource '%v'", resourceName), - Summary: summary, + Message: errors.Errorf("failure threshold met for resource '%v'", resourceName), + GVR: groupVersionResource(r.APIVersion, r.Name), + FieldValidations: summary.FieldValidation, + ConditionValidations: summary.ConditionValidation, } } log.Warnf("%v resource '%v' validation failed", failEmoji, resourceName) @@ -166,7 +168,6 @@ func (v *Validator) validateResources(r v1alpha1.ClusterResource, resources []un } if failed { - summary.GVR = groupVersionResource(r.APIVersion, r.Name) return summary, errors.New("failed to validate resources") }