Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: finalization helper for ValidationRuleResults #415

Merged
merged 3 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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