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

refactor: do not return an error from Validate #228

Merged
merged 1 commit into from
Aug 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
5 changes: 1 addition & 4 deletions internal/controller/azurevalidator_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,7 @@ func (r *AzureValidatorReconciler) Reconcile(ctx context.Context, req ctrl.Reque
vr.Spec.ExpectedResults = validator.Spec.ResultCount()

// Validate the rules
resp, err := validate.Validate(ctx, validator.Spec, r.Log)
if err != nil {
return ctrl.Result{}, err
}
resp := validate.Validate(ctx, validator.Spec, r.Log)

// Patch the ValidationResult with the latest ValidationRuleResults
if err := vres.SafeUpdate(ctx, p, vr, resp, r.Log); err != nil {
Expand Down
21 changes: 18 additions & 3 deletions pkg/validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,28 @@ import (
"time"

"github.com/go-logr/logr"
vapi "github.com/validator-labs/validator/api/v1alpha1"
vconstants "github.com/validator-labs/validator/pkg/constants"
"github.com/validator-labs/validator/pkg/types"

"github.com/validator-labs/validator-plugin-azure/api/v1alpha1"
"github.com/validator-labs/validator-plugin-azure/pkg/azure"
"github.com/validator-labs/validator-plugin-azure/pkg/constants"
utils "github.com/validator-labs/validator-plugin-azure/pkg/utils/azure"
)

// Validate validates the AzureValidatorSpec and returns a ValidationResponse.
func Validate(ctx context.Context, spec v1alpha1.AzureValidatorSpec, log logr.Logger) (types.ValidationResponse, error) {
func Validate(ctx context.Context, spec v1alpha1.AzureValidatorSpec, log logr.Logger) types.ValidationResponse {
resp := types.ValidationResponse{
ValidationRuleResults: make([]*types.ValidationRuleResult, 0, spec.ResultCount()),
ValidationRuleErrors: make([]error, 0, spec.ResultCount()),
}

azureAPI, err := utils.NewAzureAPI()
if err != nil {
return resp, fmt.Errorf("failed to create Azure API object: %w", err)
vrr := buildValidationResult()
resp.AddResult(vrr, fmt.Errorf("failed to create Azure API object: %w", err))
return resp
}

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

return resp, nil
return resp
}

func buildValidationResult() *types.ValidationRuleResult {
state := vapi.ValidationSucceeded
latestCondition := vapi.DefaultValidationCondition()
latestCondition.Message = "Initialization succeeded"
latestCondition.ValidationRule = fmt.Sprintf("%s-%s", vconstants.ValidationRulePrefix, constants.PluginCode)
latestCondition.ValidationType = constants.PluginCode

return &types.ValidationRuleResult{Condition: &latestCondition, State: &state}
}