Skip to content

Commit

Permalink
Error aggregation for Validation Webhooks
Browse files Browse the repository at this point in the history
  • Loading branch information
k-keiichi-rh committed Aug 28, 2024
1 parent a6346d9 commit 7f1e148
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions api/v1alpha1/fenceagentsremediation_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ package v1alpha1
import (
"fmt"

"github.com/pkg/errors"

"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/errors"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
Expand Down Expand Up @@ -71,30 +70,35 @@ func (far *FenceAgentsRemediation) ValidateDelete() (admission.Warnings, error)
}

func validateFAR(farSpec *FenceAgentsRemediationSpec) (admission.Warnings, error) {
if _, err := validateAgentName(farSpec.Agent); err != nil {
return nil, err
}
return validateStrategy(farSpec.RemediationStrategy)
aggregated := errors.NewAggregate([]error{
validateAgentName(farSpec.Agent),
validateStrategy(farSpec.RemediationStrategy),
})

return nil, aggregated
}

func InitOutOfServiceTaintSupportedFlag(outOfServiceTaintSupported bool) {
isOutOfServiceTaintSupported = outOfServiceTaintSupported
}

func validateAgentName(agent string) (admission.Warnings, error) {
func validateAgentName(agent string) error {
exists, err := agentValidator.ValidateAgentName(agent)
if err != nil {
return nil, errors.WithMessagef(err, "Failed to validate fence agent: %s. You might want to try again.", agent)
return errors.NewAggregate([]error{
fmt.Errorf("Failed to validate fence agent: %s. You might want to try again.", agent),
err,
})
}
if !exists {
return nil, fmt.Errorf("unsupported fence agent: %s", agent)
return fmt.Errorf("unsupported fence agent: %s", agent)
}
return nil, nil
return nil
}

func validateStrategy(farRemStrategy RemediationStrategyType) (admission.Warnings, error) {
func validateStrategy(farRemStrategy RemediationStrategyType) error {
if farRemStrategy == OutOfServiceTaintRemediationStrategy && !isOutOfServiceTaintSupported {
return nil, fmt.Errorf("%s remediation strategy is not supported at kubernetes version lower than 1.26, please use a different remediation strategy", OutOfServiceTaintRemediationStrategy)
return fmt.Errorf("%s remediation strategy is not supported at kubernetes version lower than 1.26, please use a different remediation strategy", OutOfServiceTaintRemediationStrategy)
}
return nil, nil
return nil
}

0 comments on commit 7f1e148

Please sign in to comment.