Skip to content

Commit

Permalink
feat: finalization helper for ValidationRuleResults (#415)
Browse files Browse the repository at this point in the history
## Issue
N/A

## Description
Add finalization helper for ValidationRuleResults. It must be called in
`Validate` for all plugins to ensure proper error handling in direct
mode.

---------

Signed-off-by: Tyler Gillson <tyler.gillson@gmail.com>
  • Loading branch information
TylerGillson authored Sep 6, 2024
1 parent 288194c commit ff7f786
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 15 deletions.
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ linters:
enable:
- dupl
- errcheck
- exportloopref
- ginkgolinter
- goconst
- gocyclo
Expand Down
2 changes: 1 addition & 1 deletion build
4 changes: 2 additions & 2 deletions pkg/helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func (c *CLIClient) run(name, namespace string, options Options, command string,
}

// Set values
if options.SetValues != nil && len(options.SetValues) > 0 {
if len(options.SetValues) > 0 {
args = append(args, "--set")

setString := ""
Expand All @@ -178,7 +178,7 @@ func (c *CLIClient) run(name, namespace string, options Options, command string,
}

// Set string values
if options.SetStringValues != nil && len(options.SetStringValues) > 0 {
if len(options.SetStringValues) > 0 {
args = append(args, "--set-string")

setString := ""
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/test.go → pkg/test/util.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package util
package test

import (
"reflect"
Expand Down
20 changes: 19 additions & 1 deletion pkg/types/types.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
// Package types contains structs used by to construct ValidationResults.
package types

import "github.com/validator-labs/validator/api/v1alpha1"
import (
corev1 "k8s.io/api/core/v1"

"github.com/validator-labs/validator/api/v1alpha1"
"github.com/validator-labs/validator/pkg/util"
)

// ErrValidationFailed is the error message returned when a validation rule fails.
const ErrValidationFailed = "Validation failed with an unexpected error"

// ValidationRuleResult is the result of the execution of a validation rule by a validator.
type ValidationRuleResult struct {
Condition *v1alpha1.ValidationCondition
State *v1alpha1.ValidationState
}

// Finalize sets the ValidationRuleResult state to ValidationFailed if an non-nil error is provided.
func (vrr *ValidationRuleResult) Finalize(err error) {
if err != nil {
vrr.State = util.Ptr(v1alpha1.ValidationFailed)
vrr.Condition.Status = corev1.ConditionFalse
vrr.Condition.Message = ErrValidationFailed
vrr.Condition.Failures = append(vrr.Condition.Failures, err.Error())
}
}

// ValidationResponse is the reconciliation output of one or more validation rules by a validator.
type ValidationResponse struct {
ValidationRuleResults []*ValidationRuleResult
Expand Down
9 changes: 1 addition & 8 deletions pkg/validationresult/validation_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import (
"github.com/validator-labs/validator/pkg/util"
)

const validationErrorMsg = "Validation failed with an unexpected error"

// Patcher is an interface for patching objects.
type Patcher interface {
Patch(ctx context.Context, obj client.Object, opts ...patch.Option) error
Expand Down Expand Up @@ -156,12 +154,7 @@ func Finalize(vr *v1alpha1.ValidationResult, vrr types.ValidationResponse, l log
func updateStatus(vr *v1alpha1.ValidationResult, vrr *types.ValidationRuleResult, vrrErr error, l logr.Logger) {

// Finalize result State and Condition in the event of an unexpected error
if vrrErr != nil {
vrr.State = util.Ptr(v1alpha1.ValidationFailed)
vrr.Condition.Status = corev1.ConditionFalse
vrr.Condition.Message = validationErrorMsg
vrr.Condition.Failures = append(vrr.Condition.Failures, vrrErr.Error())
}
vrr.Finalize(vrrErr)

// Update and/or insert the ValidationResult's Conditions with the latest Condition
idx := getConditionIndexByValidationRule(vr.Status.ValidationConditions, vrr.Condition.ValidationRule)
Expand Down
2 changes: 1 addition & 1 deletion pkg/validationresult/validation_result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func vr(cs []corev1.ConditionStatus, state v1alpha1.ValidationState, err error)
ValidationRule: constants.ValidationRulePrefix,
}
if err != nil {
condition.Message = validationErrorMsg
condition.Message = types.ErrValidationFailed
condition.Failures = append(condition.Failures, err.Error())
}
vr.Status.ValidationConditions = append(vr.Status.ValidationConditions, condition)
Expand Down

0 comments on commit ff7f786

Please sign in to comment.