Skip to content

Commit

Permalink
fix: ValidationError restructure and readme (#13)
Browse files Browse the repository at this point in the history
Signed-off-by: Alfredo Garo <[email protected]>
  • Loading branch information
garomonegro authored Jan 25, 2022
1 parent fd9c0f3 commit 5a472d2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
}
}
```
13 changes: 8 additions & 5 deletions pkg/client/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ func NewFieldValidationResult(path string) FieldValidationResult {
}

type ValidationSummary struct {
GVR schema.GroupVersionResource
FieldValidation []FieldValidationResult
ConditionValidation []ConditionValidationResult
}
Expand Down Expand Up @@ -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))
}
7 changes: 4 additions & 3 deletions pkg/client/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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")
}

Expand Down

0 comments on commit 5a472d2

Please sign in to comment.