Skip to content

Commit ba947e3

Browse files
authored
refactor: do not return an error from Validate (#228)
## Issue N/A ## Description Refactor to align with behaviour in other plugins. Signed-off-by: Tyler Gillson <[email protected]>
1 parent f938caf commit ba947e3

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

internal/controller/azurevalidator_controller.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,7 @@ func (r *AzureValidatorReconciler) Reconcile(ctx context.Context, req ctrl.Reque
104104
vr.Spec.ExpectedResults = validator.Spec.ResultCount()
105105

106106
// Validate the rules
107-
resp, err := validate.Validate(ctx, validator.Spec, r.Log)
108-
if err != nil {
109-
return ctrl.Result{}, err
110-
}
107+
resp := validate.Validate(ctx, validator.Spec, r.Log)
111108

112109
// Patch the ValidationResult with the latest ValidationRuleResults
113110
if err := vres.SafeUpdate(ctx, p, vr, resp, r.Log); err != nil {

pkg/validate/validate.go

+18-3
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,28 @@ import (
88
"time"
99

1010
"github.com/go-logr/logr"
11+
vapi "github.com/validator-labs/validator/api/v1alpha1"
12+
vconstants "github.com/validator-labs/validator/pkg/constants"
1113
"github.com/validator-labs/validator/pkg/types"
1214

1315
"github.com/validator-labs/validator-plugin-azure/api/v1alpha1"
1416
"github.com/validator-labs/validator-plugin-azure/pkg/azure"
17+
"github.com/validator-labs/validator-plugin-azure/pkg/constants"
1518
utils "github.com/validator-labs/validator-plugin-azure/pkg/utils/azure"
1619
)
1720

1821
// Validate validates the AzureValidatorSpec and returns a ValidationResponse.
19-
func Validate(ctx context.Context, spec v1alpha1.AzureValidatorSpec, log logr.Logger) (types.ValidationResponse, error) {
22+
func Validate(ctx context.Context, spec v1alpha1.AzureValidatorSpec, log logr.Logger) types.ValidationResponse {
2023
resp := types.ValidationResponse{
2124
ValidationRuleResults: make([]*types.ValidationRuleResult, 0, spec.ResultCount()),
2225
ValidationRuleErrors: make([]error, 0, spec.ResultCount()),
2326
}
2427

2528
azureAPI, err := utils.NewAzureAPI()
2629
if err != nil {
27-
return resp, fmt.Errorf("failed to create Azure API object: %w", err)
30+
vrr := buildValidationResult()
31+
resp.AddResult(vrr, fmt.Errorf("failed to create Azure API object: %w", err))
32+
return resp
2833
}
2934

3035
ctx = context.WithoutCancel(ctx)
@@ -59,5 +64,15 @@ func Validate(ctx context.Context, spec v1alpha1.AzureValidatorSpec, log logr.Lo
5964
resp.AddResult(vrr, err)
6065
}
6166

62-
return resp, nil
67+
return resp
68+
}
69+
70+
func buildValidationResult() *types.ValidationRuleResult {
71+
state := vapi.ValidationSucceeded
72+
latestCondition := vapi.DefaultValidationCondition()
73+
latestCondition.Message = "Initialization succeeded"
74+
latestCondition.ValidationRule = fmt.Sprintf("%s-%s", vconstants.ValidationRulePrefix, constants.PluginCode)
75+
latestCondition.ValidationType = constants.PluginCode
76+
77+
return &types.ValidationRuleResult{Condition: &latestCondition, State: &state}
6378
}

0 commit comments

Comments
 (0)