Skip to content

Commit ff7f786

Browse files
authored
feat: finalization helper for ValidationRuleResults (#415)
## 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 <[email protected]>
1 parent 288194c commit ff7f786

File tree

7 files changed

+25
-15
lines changed

7 files changed

+25
-15
lines changed

.golangci.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ linters:
1616
enable:
1717
- dupl
1818
- errcheck
19-
- exportloopref
2019
- ginkgolinter
2120
- goconst
2221
- gocyclo

pkg/helm/helm.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func (c *CLIClient) run(name, namespace string, options Options, command string,
162162
}
163163

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

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

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

184184
setString := ""

pkg/util/test.go renamed to pkg/test/util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package util
1+
package test
22

33
import (
44
"reflect"

pkg/types/types.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,32 @@
11
// Package types contains structs used by to construct ValidationResults.
22
package types
33

4-
import "github.com/validator-labs/validator/api/v1alpha1"
4+
import (
5+
corev1 "k8s.io/api/core/v1"
6+
7+
"github.com/validator-labs/validator/api/v1alpha1"
8+
"github.com/validator-labs/validator/pkg/util"
9+
)
10+
11+
// ErrValidationFailed is the error message returned when a validation rule fails.
12+
const ErrValidationFailed = "Validation failed with an unexpected error"
513

614
// ValidationRuleResult is the result of the execution of a validation rule by a validator.
715
type ValidationRuleResult struct {
816
Condition *v1alpha1.ValidationCondition
917
State *v1alpha1.ValidationState
1018
}
1119

20+
// Finalize sets the ValidationRuleResult state to ValidationFailed if an non-nil error is provided.
21+
func (vrr *ValidationRuleResult) Finalize(err error) {
22+
if err != nil {
23+
vrr.State = util.Ptr(v1alpha1.ValidationFailed)
24+
vrr.Condition.Status = corev1.ConditionFalse
25+
vrr.Condition.Message = ErrValidationFailed
26+
vrr.Condition.Failures = append(vrr.Condition.Failures, err.Error())
27+
}
28+
}
29+
1230
// ValidationResponse is the reconciliation output of one or more validation rules by a validator.
1331
type ValidationResponse struct {
1432
ValidationRuleResults []*ValidationRuleResult

pkg/validationresult/validation_result.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ import (
1919
"github.com/validator-labs/validator/pkg/util"
2020
)
2121

22-
const validationErrorMsg = "Validation failed with an unexpected error"
23-
2422
// Patcher is an interface for patching objects.
2523
type Patcher interface {
2624
Patch(ctx context.Context, obj client.Object, opts ...patch.Option) error
@@ -156,12 +154,7 @@ func Finalize(vr *v1alpha1.ValidationResult, vrr types.ValidationResponse, l log
156154
func updateStatus(vr *v1alpha1.ValidationResult, vrr *types.ValidationRuleResult, vrrErr error, l logr.Logger) {
157155

158156
// Finalize result State and Condition in the event of an unexpected error
159-
if vrrErr != nil {
160-
vrr.State = util.Ptr(v1alpha1.ValidationFailed)
161-
vrr.Condition.Status = corev1.ConditionFalse
162-
vrr.Condition.Message = validationErrorMsg
163-
vrr.Condition.Failures = append(vrr.Condition.Failures, vrrErr.Error())
164-
}
157+
vrr.Finalize(vrrErr)
165158

166159
// Update and/or insert the ValidationResult's Conditions with the latest Condition
167160
idx := getConditionIndexByValidationRule(vr.Status.ValidationConditions, vrr.Condition.ValidationRule)

pkg/validationresult/validation_result_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func vr(cs []corev1.ConditionStatus, state v1alpha1.ValidationState, err error)
4141
ValidationRule: constants.ValidationRulePrefix,
4242
}
4343
if err != nil {
44-
condition.Message = validationErrorMsg
44+
condition.Message = types.ErrValidationFailed
4545
condition.Failures = append(condition.Failures, err.Error())
4646
}
4747
vr.Status.ValidationConditions = append(vr.Status.ValidationConditions, condition)

0 commit comments

Comments
 (0)